<?php
require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../config/auth.php';
require_once __DIR__ . '/../includes/functions.php';

requireLogin();
$id = (int)($_GET['id'] ?? 0);
if (!$id) { die('Invalid invoice ID.'); }

$pdo = getDB();
$stmt = $pdo->prepare("
    SELECT si.*, c.name as customer_name, c.phone as customer_phone
    FROM sales_invoices si
    LEFT JOIN customers c ON si.customer_id = c.id
    WHERE si.id = ?
");
$stmt->execute([$id]);
$invoice = $stmt->fetch();
if (!$invoice) { die('Invoice not found.'); }

$stmt = $pdo->prepare("
    SELECT sii.*, p.product_name as product_name, pi.imei_number
    FROM sales_invoice_items sii
    JOIN products p ON sii.product_id = p.id
    LEFT JOIN product_imei pi ON sii.imei_id = pi.id
    WHERE sii.sales_invoice_id = ?
");
$stmt->execute([$id]);
$items = $stmt->fetchAll();

$shop_name = getSetting('shop_name', 'DigiMobiz');
$shop_address = getSetting('shop_address', '');
$shop_phone = getSetting('shop_phone', '');
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Receipt - <?= htmlspecialchars($invoice['invoice_no']) ?></title>
    <style>
        body { font-family: 'Courier New', monospace; font-size: 12px; width: 80mm; margin: auto; padding: 5px; }
        .text-center { text-align: center; }
        .text-right { text-align: right; }
        .fw-bold { font-weight: bold; }
        table { width: 100%; border-collapse: collapse; }
        th, td { padding: 3px; text-align: left; }
        hr { border: 1px dashed #000; }
        @media print { body { margin: 0; padding: 0; } .no-print { display: none; } }
    </style>
</head>
<body>
    <div class="text-center">
        <img src="<?= htmlspecialchars(getShopLogoUrl()) ?>?v=<?= time() ?>" alt="Logo" style="max-height:40px;margin-bottom:3px;" onerror="this.style.display='none'">
        <h3 style="margin:0;"><?= htmlspecialchars($shop_name) ?></h3>
        <small><?= nl2br(htmlspecialchars($shop_address)) ?></small><br>
        <small>Tel: <?= htmlspecialchars($shop_phone) ?></small>
    </div>
    <hr>
    <div>
        <strong>Invoice:</strong> <?= htmlspecialchars($invoice['invoice_no']) ?><br>
        <strong>Date:</strong> <?= date('Y-m-d H:i', strtotime($invoice['invoice_date'])) ?><br>
        <strong>Customer:</strong> <?= htmlspecialchars($invoice['customer_name'] ?? 'Walk-in') ?><br>
        <strong>Phone:</strong> <?= htmlspecialchars($invoice['customer_phone'] ?? '-') ?>
    </div>
    <hr>
    <table>
        <tr><th>Item</th><th class="text-right">Qty</th><th class="text-right">Price</th></tr>
        <?php foreach ($items as $item): ?>
        <tr>
            <td><?= htmlspecialchars($item['product_name']) ?><?= $item['imei_number'] ? "\n" . $item['imei_number'] : '' ?></td>
            <td class="text-right"><?= $item['quantity'] ?></td>
            <td class="text-right"><?= number_format($item['total_price'], 2) ?></td>
        </tr>
        <?php endforeach; ?>
    </table>
    <hr>
    <table>
        <tr><td>Subtotal</td><td class="text-right"><?= number_format($invoice['subtotal'], 2) ?></td></tr>
        <tr><td>Discount</td><td class="text-right"><?= number_format($invoice['discount_amount'], 2) ?></td></tr>
        <tr><td>Tax</td><td class="text-right"><?= number_format($invoice['tax_amount'], 2) ?></td></tr>
        <tr class="fw-bold"><td>TOTAL</td><td class="text-right"><?= number_format($invoice['total_amount'], 2) ?></td></tr>
        <tr><td>Paid</td><td class="text-right"><?= number_format($invoice['paid_amount'], 2) ?></td></tr>
        <tr><td>Balance</td><td class="text-right"><?= number_format($invoice['due_amount'], 2) ?></td></tr>
    </table>
    <hr>
    <div class="text-center">
        <small>Thank you for your visit!</small><br>
        <small>Warranty valid as per invoice terms</small>
    </div>
    <div class="no-print text-center mt-2">
        <button onclick="window.print()">Print</button>
        <button onclick="window.close()">Close</button>
    </div>
    <script>window.print();</script>
</body>
</html>
