<?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;
}


?>

<!DOCTYPE html>
<html lang="bg">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Оферта №<?php echo htmlspecialchars($offer['offer_number']); ?> - </title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f4f4f4;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
        }
    
        .offer-info {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 20px;
            margin-bottom: 30px;
        }
        .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: 20px;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 12px;
            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: 30px;
        }
        @media print {
            .nav, .actions {
                display: none;
            }
            body {
                background-color: white;
            }
            .container {
                box-shadow: none;
                padding: 0;
            }
        }
        @media (max-width: 768px) {
            .nav {
                flex-direction: column;
                align-items: center;
            }
            .offer-info {
                grid-template-columns: 1fr;
            }
            table {
                font-size: 14px;
            }
            .container {
                padding: 10px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
     
        
        <?php if (empty($offer['items'])): ?>
            <p>Няма добавени артикули към тази оферта.</p>
        <?php else: ?>
		 <table style="width: 99%;">
                <tbody>
                    <tr>
					    <td style="width:201.8pt; vertical-align:middle;">
							<img src="logo.jpg" style="width: 90%; height: 100%;"></img>
						</td>
                        <td style="width:96.05pt; vertical-align:middle;">
                            <p style="text-align:center;">&nbsp;</p>
                        </td>
                    </tr>
                    <tr>
                        <td >
					
                            <p style="font-size:10pt;">Поръчка № 000- <?php echo $next; ?> / <?php echo date('d.m.Y', strtotime($protocol['protocol_date'])); ?></p>
                            <p style="font-size:12pt;">обект: <strong><?php echo htmlspecialchars($protocol['issuer']); ?></strong></p>
                            <p style="font-size:10pt;">aдрес: <strong><?php echo htmlspecialchars($protocol['issuer_address']); ?></strong></p>
                        </td>
                        <td>
                            <p style="text-align:center; font-size:18px;"><strong>ОФЕРТА</strong></p>
                            <p style="text-align:center; font-size:16px;"><strong>за изграждане на охрана</strong></p>
                            <p style="text-align:center; font-size:16px;"><strong>с технически средства</strong></p>
                        </td>
                        
                    </tr>
                    <tr style="height:19.7pt;">
                        <td style="width:216pt;vertical-align:middle;">
                            <p style="font-size:10pt;"><strong>Счетоводител :</strong></p>
                        </td>
                        <td style="width:201.8pt; vertical-align:middle;">
                            <p style="font-size:10pt;"><strong>№ 000- <?php echo $next; ?> / <?php echo date('d.m.Y', strtotime($protocol['protocol_date'])); ?></strong></p>
                        </td>
                        <td style="width:96.05pt; vertical-align:top;">
                            <p style="font-size:10pt;"><strong>дата: <?php echo date('d.m.Y', strtotime($protocol['protocol_date'])); ?></strong></p>
                        </td>
                    </tr>
					
            <p class="Header" style="font-size:10pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="font-size:12pt;">&nbsp;</span></p>
        </div>
        <table style="margin-left: 3pt; padding: 0pt; border-collapse: collapse; width: 96%;">
	 <thead>
             
	
                </thead>
				
               
     
      
		   <table style="margin-left: 3pt; padding: 0pt; border-collapse: collapse; width: 96%;">
                <thead>
                    <tr>
                        <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>
                        <td><?php echo $index + 1; ?></td>
                        <td><?php echo htmlspecialchars($item['article_name']); ?></td>
                        <td><?php echo htmlspecialchars($item['unit']); ?></td>
						<td><?php echo number_format($item['quantity'], 2, '.', ''); ?></td>
                        <td colspan="2" style="text-align: right;"><?php echo number_format($item['price_per_unit'], 2, '.', ''); ?> лв.</td>
                        <td colspan="3" style="text-align: right; width:53pt; border-top:0.75pt solid #000000; border-left:0.75pt solid #000000; border-bottom:0.75pt solid #000000; padding:0pt 5.4pt 0pt 5.03pt; vertical-align:center;"><?php echo number_format($item['total_price'], 2, '.', ''); ?> лв.</td>
                    </tr>
                    <?php endforeach; ?>
                    <tr class="total-row">
                        <td colspan="6"><strong>Общо артикули: <?php echo count($offer['items']); ?></strong></td>
                        <td><strong><?php echo number_format($total_amount, 2, '.', ''); ?> лв.</strong></td>
                    </tr>
                </tbody>
            </table>
        <?php endif; ?>

        <div class="actions">
            <button onclick="window.print()" class="btn btn-success">Печат</button>
            <a href="edit_offer.php?id=<?php echo $offer['id']; ?>" class="btn">Редактирай</a>
            <a href="offers.php" class="btn">Назад към оферти</a>
        </div>
    </div>
</body>
</html>

