<?php
require_once 'functions.php';

$offer_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if (!$offer_id) {
    header('Location: offers.php');
    exit;
}

$offer = getOfferWithItems($offer_id);
	
$protocol = getAllOffers($offer_id);
$client = getAllClients();
foreach ($client as $key => $value) {
    
};

if (!$offer) {
    header('Location: offers.php');
    exit;
}

$mol = getMol();

?>

<!DOCTYPE html>
<html lang="bg">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=0.8">
    <title>Оферта №<?php echo htmlspecialchars($offer['offer_number']); ?> - </title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 10px;
            background-color: #f4f4f4;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background-color: white;
            padding: 12px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 10px;
        }
    
        .offer-info {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 20px;
            margin-bottom: 10px;
        }
        .info-box {
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
            background-color: #f9f9f9;
        }
        .info-label {
            font-weight: bold;
            color: #555;
            margin-bottom: 5px;
        }
        .info-value {
            font-size: 16px;
            color: #333;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 0px;
			margin-right: 8pt;
			margin-left: 15px;
        }
        th, td {
            border: 0px solid #ddd;
            padding: 8px;
            text-align: left;
        }
        th {
            background-color: #f8f9fa;
            font-weight: bold;
        }
        tr:nth-child(even) {
            background-color: #f9f9f9;
        }
        .total-row {
            font-weight: bold;
            background-color: #e9ecef !important;
        }
        .btn {
            background-color: #007bff;
            color: white;
            padding: 10px 20px;
            text-decoration: none;
            border-radius: 4px;
            border: none;
            cursor: pointer;
            margin: 5px;
            display: inline-block;
        }
        .btn:hover {
            background-color: #0056b3;
        }
        .btn-success {
            background-color: #28a745;
        }
        .btn-success:hover {
            background-color: #218838;
        }
        .actions {
            text-align: center;
            margin-top: 0px;
        }
        @media print {
            .nav, .actions {
                display: none;
				align-items: center;
            }
            body {
                background-color: white;
				align-items: center;
            }
            .container {
                box-shadow: none;
                padding: 0;
				align-items: center;
            }
        }
        @media (max-width: 1000px) {
            .nav {
                flex-direction: column;
                align-items: center;
            }
            .offer-info {
                grid-template-columns: 1fr;
            }
            table {
                font-size: 12px;
            }
            .container {
                padding: 0px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <?php if (empty($offer['items'])): ?>
            <p>Няма добавени артикули към тази оферта.</p>
        <?php else: ?>
		<table colspan="1">
		<tr>
		<td><center>
			<img src="logo.jpg" style="width: 70%;  align-item: center;"></img>
</center>
</td>
</tr>
</table>			
			<table>
       			  <td style="width:230pt;">
                           	<p><strong>&nbsp;</strong></p>
                            <p><strong>&nbsp;</strong></p>
							<p><strong>&nbsp;</strong></p>
							<p style="text-align:right; font-size:20pt;"><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ОФЕРТА</strong></p>
                            
                        </td>
                  <td style="width:170pt;">
                            <p style="text-align:right; font-size:12pt;"><strong>На Вниманието &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</strong></p>
                            <p style="text-align:right; font-size:12pt;"><strong>на&nbsp;&nbsp;<?php print_r($offer['recipient']); ?>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</strong></p>
                        </td>
       </table>

  <p style="margin-right: 18pt; font-size:12pt;"><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Във връзка с Ваше запитване,  Ви представяме следната оферта:</strong></p>
     
        <table style="margin-left: 8pt; padding: 0pt; border-collapse: collapse; width: 94%;">
	 <thead>

                </thead>

		   <table style="margin-left: 10pt; padding: 0pt; border-collapse: collapse; width: 94%; border: 1px solid #ddd;">
                <thead>
                    <tr style="border: 1px solid #ddd;">
                        <th>№</th>
                        <th>Артикул</th>
						<th>Снимка</th>
                        <th>Мярка</th>
						<th>Кол.</th>
                        <th colspan="2" style="text-align: right;">Ед. цена</th>
                        <th colspan="3" style="text-align: right;">Общо сума</th>
                    </tr>
                </thead>
                <tbody>
                    <?php 
                    $total_amount = 0;
                    foreach ($offer['items'] as $index => $item): 
                        $total_amount += $item['total_price'];
                    ?>
                    <tr style="border: 1px solid #ddd;">
                        <td><?php echo $index + 1; ?></td>
                        <td><?php echo htmlspecialchars($item['article_name']); ?></td>
						<td><img src="<?php echo htmlspecialchars($item['image']); ?>" style="width: 40px;"></td>
                        <td><?php echo htmlspecialchars($item['unit']); ?></td>
						<td><?php echo number_format($item['quantity'], 2, '.', ''); ?></td>
                        <td colspan="3" style="text-align: right;"><?php echo number_format($item['price_per_unit'], 2, '.', ''); ?> лв.</td>
                        <td colspan="4" style="text-align: right; width:90pt; vertical-align:center;"><?php echo number_format($item['total_price'], 2, '.', ''); ?> лв.</td>
                    </tr>
                    <?php endforeach; ?>
                    <tr class="total-row">
                        <td colspan="2"><strong>Общо артикули: <?php echo count($offer['items']); ?></strong></td>
						 <td colspan="5" style="text-align: right;"><strong>Общо Сума: </strong></td></br>
						      <td colspan="2" style="text-align: right;"><strong><?php echo number_format($total_amount, 2, '.', ''); ?> лв.</strong></td>
					</tr>
					<tr style="border: 1px solid #ddd;">
						  <td colspan="2"><strong> <?php  ?></strong></td>
						  <?php
						  $dds = (number_format($total_amount, 2, '.', '') * 1.20 - number_format($total_amount, 2, '.', '')); ?>
						 <td colspan="5" style="text-align: right;"><strong>ДДС: 20%</strong></td></br>
						  <td colspan="2" style="text-align: right;"><strong><?php echo number_format($dds, 2, '.', ''); ?> лв.</strong></td>
						 </tr>
					<tr>	 
					<td colspan="2"><strong> <?php  ?></strong></td>
						 <td colspan="5" style="text-align: right;"><strong>Общо Сума </br> с ДДС: </strong></td>
						 <?php $tots = (number_format($total_amount, 2, '.', '') * 1.20); ?>
                        <td colspan="2" style="text-align: right;"><strong><?php echo number_format($tots, 2, '.', ''); ?> лв.</strong></td>
                    </tr>
                </tbody>
            </table>
        <?php endif; ?>

			<table>
		    <tr style="height:30pt;">
			<td style="width:400pt;">
			</td>
			<td style="width:400pt;">
			</td>
			</table>
		<table>
		    <tr style="height:30pt;">
			            <td style="width:400pt;">
                          
                            <p style="font-size:12pt;"><strong>дата&nbsp;&nbsp;<?php echo date('d.m.Y', strtotime($offer['offer_date'])); ?></strong></p>
                            <p style="font-size:12pt;"><strong>гр. Сливен</strong></p>
                        </td>
						
                        <td style="width:400pt;">
							<p style="text-align:left; font-size:12pt;"><strong>УПРАВИТЕЛ: _________________</strong></p>
							<p style="text-align:left; font-size:12pt;"><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $mol; ?></strong></p>
                            
                        </td>

                    </tr>
</table>
		
  </div>
        <div class="actions">
            <button onclick="window.print()" class="btn btn-success">Печат</button>
			<button type="button" class="btn btn-danger" onclick="window.open('', '_self', ''); window.close();">Отказ</button>
 		</div>
    </div>
</body>
</html>

