<?php
session_start();
$_SESSION["user_id"] = 1;
$_GET["id"] = 1;
require_once __DIR__ . "/includes/functions.php";
require_once __DIR__ . "/vendor/autoload.php";
use Dompdf\Dompdf;
use Dompdf\Options;
$db = getDB();
$id = (int)($_GET['id'] ?? 0);
$stmt = $db->prepare("SELECT i.*, c.name as cust_name, c.address as cust_address, c.city as cust_city, c.state as cust_state, c.pincode as cust_pincode, c.email as cust_email, c.phone as cust_phone, c.gstin as cust_gstin FROM invoices i JOIN customers c ON i.customer_id = c.id WHERE i.id = ?");
$stmt->execute([$id]);
$inv = $stmt->fetch();
$items = $db->prepare("SELECT * FROM invoice_items WHERE invoice_id = ?");
$items->execute([$id]);
$lineItems = $items->fetchAll();
ob_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Invoice <?= sanitize($inv['invoice_number']) ?></title>
    <style>
        body { font-family: 'DejaVu Sans', sans-serif; background: white; margin: 0; padding: 20px; color: #333; }
        .invoice-wrapper { background: white; border-radius: 10px; overflow: hidden; box-shadow: none; }
        .header { background: linear-gradient(135deg, #1e1f52 0%, #4143d0 100%); color: white; padding: 30px; }
        .header h1 { margin: 0; font-size: 28px; font-weight: bold; }
        .header p { margin: 5px 0 0 0; opacity: 0.9; }
        .invoice-details { display: flex; justify-content: space-between; margin-top: 20px; }
        .invoice-details div { flex: 1; }
        .invoice-details h2 { margin: 0 0 10px 0; font-size: 18px; }
        .invoice-details p { margin: 2px 0; font-size: 14px; }
        .customer-details { margin-top: 30px; padding: 20px; background: #f8f9fa; border-radius: 8px; }
        .customer-details h3 { margin: 0 0 15px 0; font-size: 16px; color: #1e1f52; }
        .customer-details p { margin: 3px 0; font-size: 14px; }
        .items-table { width: 100%; border-collapse: collapse; margin-top: 30px; }
        .items-table th, .items-table td { padding: 12px; text-align: left; border-bottom: 1px solid #e9ecef; }
        .items-table th { background: #f8f9fa; font-weight: bold; font-size: 14px; }
        .items-table td { font-size: 13px; }
        .items-table .text-right { text-align: right; }
        .total-section { margin-top: 30px; display: flex; justify-content: flex-end; }
        .total-box { background: #f8f9fa; padding: 20px; border-radius: 8px; min-width: 200px; }
        .total-box div { display: flex; justify-content: space-between; margin-bottom: 8px; font-size: 14px; }
        .total-box .total { font-weight: bold; font-size: 16px; color: #1e1f52; border-top: 1px solid #dee2e6; padding-top: 10px; }
        .notes { margin-top: 30px; padding: 20px; background: #f8f9fa; border-radius: 8px; }
        .notes h3 { margin: 0 0 10px 0; font-size: 16px; color: #1e1f52; }
        .notes p { margin: 0; font-size: 14px; }
    </style>
</head>
<body>
    <div class="invoice-wrapper">
        <div class="header">
            <h1>JBR Sofa</h1>
            <p>Your Business Address Line 1<br>City, State, PIN 000000<br>GST: 22AAAAA0000A1Z5</p>
            <div class="invoice-details">
                <div>
                    <h2>Invoice #<?= sanitize($inv['invoice_number']) ?></h2>
                    <p><strong>Date:</strong> <?= date('d M Y', strtotime($inv['invoice_date'])) ?></p>
                    <?php if ($inv['due_date']): ?>
                    <p><strong>Due Date:</strong> <?= date('d M Y', strtotime($inv['due_date'])) ?></p>
                    <?php endif; ?>
                </div>
                <div style="text-align: right;">
                    <h2>Status: <?= strtoupper($inv['status']) ?></h2>
                </div>
            </div>
        </div>
        <div class="customer-details">
            <h3>Bill To:</h3>
            <p><strong><?= sanitize($inv['cust_name']) ?></strong></p>
            <?php if ($inv['cust_address']): ?><p><?= sanitize($inv['cust_address']) ?></p><?php endif; ?>
            <?php if ($inv['cust_city'] || $inv['cust_state'] || $inv['cust_pincode']): ?>
            <p><?= sanitize($inv['cust_city']) ?> <?= sanitize($inv['cust_state']) ?> <?= sanitize($inv['cust_pincode']) ?></p>
            <?php endif; ?>
            <?php if ($inv['cust_email']): ?><p>Email: <?= sanitize($inv['cust_email']) ?></p><?php endif; ?>
            <?php if ($inv['cust_phone']): ?><p>Phone: <?= sanitize($inv['cust_phone']) ?></p><?php endif; ?>
            <?php if ($inv['cust_gstin']): ?><p>GSTIN: <?= sanitize($inv['cust_gstin']) ?></p><?php endif; ?>
        </div>
        <table class="items-table">
            <thead>
                <tr>
                    <th>Description</th>
                    <th>Qty</th>
                    <th>Rate</th>
                    <th>GST%</th>
                    <th class="text-right">Amount</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($lineItems as $item): ?>
                <tr>
                    <td><?= sanitize($item['product_name']) ?></td>
                    <td><?= (int)$item['quantity'] ?></td>
                    <td><?= number_format($item['unit_price'], 2) ?></td>
                    <td><?= number_format($item['gst_percent'], 2) ?>%</td>
                    <td class="text-right"><?= number_format($item['line_total'], 2) ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
        <div class="total-section">
            <div class="total-box">
                <div><span>Subtotal:</span><span><?= number_format($inv['subtotal'], 2) ?></span></div>
                <?php if ($inv['discount_amount'] > 0): ?>
                <div><span>Discount:</span><span>-<?= number_format($inv['discount_amount'], 2) ?></span></div>
                <?php endif; ?>
                <div><span>GST:</span><span><?= number_format($inv['gst_amount'], 2) ?></span></div>
                <div class="total"><span>Total:</span><span><?= number_format($inv['total_amount'], 2) ?></span></div>
            </div>
        </div>
        <?php if ($inv['notes']): ?>
        <div class="notes">
            <h3>Notes:</h3>
            <p><?= nl2br(sanitize($inv['notes'])) ?></p>
        </div>
        <?php endif; ?>
    </div>
</body>
</html>
<?php
$html = ob_get_clean();
file_put_contents(__DIR__ . '/tmp_pdf_full_test.html', $html);
$options = new Options();
$options->set('isHtml5ParserEnabled', true);
$options->set('isRemoteEnabled', false);
$dompdf = new Dompdf($options);
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
try {
    $dompdf->render();
    echo "RENDER_OK";
} catch (Exception $e) {
    echo "ERROR:" . get_class($e) . "|" . $e->getMessage();
}
