<?php
requirePermission($conn, 'quotations', 'open');
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if (!$id) { echo '<div class="card" style="padding:20px">Quotation not found.</div>'; return; }
$sql = "SELECT q.*, c.customer_name, c.business_name AS customer_business, c.phone AS customer_phone, c.email AS customer_email, c.address AS customer_address,
               i.name AS item_name,
               a.client_name AS account_client_name, a.business_name AS account_business_name, a.phone AS account_phone, a.email AS account_email, a.currency AS account_currency, a.logo AS account_logo
        FROM quotations q
        LEFT JOIN customers c ON c.id=q.customer_id
        LEFT JOIN items i ON i.id=q.item_id
        LEFT JOIN accounts a ON a.id=q.account_id
        WHERE q.id=$id AND q.account_id=$account_id LIMIT 1";
$res = $conn->query($sql);
if (!$res || !$res->num_rows) { echo '<div class="card" style="padding:20px">Quotation not found.</div>'; return; }
$d = $res->fetch_assoc();
$currency = $d['account_currency'] ?: '';
$printNow = isset($_GET['print']);
?>
<style>
.invoice-wrap{max-width:920px;margin:0 auto;background:#fff;border:1px solid var(--border);border-radius:16px;overflow:hidden;box-shadow:var(--shadow-sm)}
.invoice-head{display:flex;justify-content:space-between;gap:20px;padding:28px;border-bottom:1px solid var(--border);background:linear-gradient(135deg,#f8fafc,#fff)}
.invoice-logo{max-width:170px;max-height:90px;object-fit:contain;margin-bottom:10px}
.invoice-title{font-size:30px;font-weight:800;color:var(--primary);margin:0;text-align:right}.invoice-no{text-align:right;color:var(--text-muted);font-size:13px;margin-top:5px}
.invoice-body{padding:28px}.invoice-grid{display:grid;grid-template-columns:1fr 1fr;gap:18px;margin-bottom:24px}.invoice-box{background:var(--bg-base);border:1px solid var(--border);border-radius:12px;padding:16px}.invoice-box h4{margin:0 0 10px;color:var(--text-muted);font-size:12px;text-transform:uppercase;letter-spacing:.04em}.invoice-table{width:100%;border-collapse:collapse;margin-top:16px}.invoice-table th{background:var(--primary);color:#fff;text-align:left;padding:12px}.invoice-table td{border-bottom:1px solid var(--border);padding:12px}.invoice-total{max-width:360px;margin-left:auto;margin-top:18px}.invoice-total-row{display:flex;justify-content:space-between;padding:9px 0;border-bottom:1px solid var(--border)}.invoice-total-row.grand{font-size:20px;font-weight:800;color:var(--primary);border-bottom:0}.invoice-actions{max-width:920px;margin:0 auto 14px;display:flex;gap:8px;justify-content:flex-end}
@media print{#sidebar,#top-header,.invoice-actions{display:none!important}#main-content{margin:0!important}.invoice-wrap{box-shadow:none;border:0;border-radius:0}.invoice-head,.invoice-body{padding:22px}body{background:#fff}.page-breadcrumb,.page-title-area{display:none!important}}
@media(max-width:700px){.invoice-head,.invoice-grid{grid-template-columns:1fr;display:grid}.invoice-title,.invoice-no{text-align:left}}
</style>
<div class="invoice-actions">
  <a href="index.php?page=quotations" class="btn btn-secondary">← Back</a>
  <button class="btn btn-primary" onclick="window.print()">Print</button>
</div>
<div class="invoice-wrap">
  <div class="invoice-head">
    <div>
      <?php if (!empty($d['account_logo'])): ?><img src="<?= htmlspecialchars($d['account_logo']) ?>" class="invoice-logo" alt="Logo"><?php endif; ?>
      <h3 style="margin:0"><?= htmlspecialchars($d['account_business_name'] ?: $d['account_client_name'] ?: 'Business') ?></h3>
      <div class="text-muted text-sm"><?= htmlspecialchars($d['account_phone'] ?: '') ?></div>
      <div class="text-muted text-sm"><?= htmlspecialchars($d['account_email'] ?: '') ?></div>
    </div>
    <div><h1 class="invoice-title">Quotation</h1><div class="invoice-no">QT-<?= str_pad($d['id'],5,'0',STR_PAD_LEFT) ?><br>Date: <?= htmlspecialchars(date('Y-m-d', strtotime($d['date_created']))) ?><br>Status: <?= htmlspecialchars(ucfirst($d['status'])) ?></div></div>
  </div>
  <div class="invoice-body">
    <div class="invoice-grid">
      <div class="invoice-box"><h4>Customer</h4><strong><?= htmlspecialchars($d['customer_name'] ?: '—') ?></strong><br><?= htmlspecialchars($d['customer_business'] ?: '') ?><br><?= htmlspecialchars($d['customer_phone'] ?: '') ?><br><?= htmlspecialchars($d['customer_email'] ?: '') ?><br><?= nl2br(htmlspecialchars($d['customer_address'] ?: '')) ?></div>
      <div class="invoice-box"><h4>Details</h4>Valid until: <?= htmlspecialchars($d['valid_until'] ?: '—') ?><br>Currency: <?= htmlspecialchars($currency ?: '—') ?><br><?php if(!empty($d['customer_details'])): ?><br><?= nl2br(htmlspecialchars($d['customer_details'])) ?><?php endif; ?></div>
    </div>
    <table class="invoice-table"><thead><tr><th>Item</th><th style="width:90px">Qty</th><th style="width:140px">Unit</th><th style="width:140px">Total</th></tr></thead><tbody><tr><td><?= htmlspecialchars($d['item_name'] ?: '—') ?></td><td><?= (int)$d['quantity'] ?></td><td><?= htmlspecialchars($currency) ?> <?= number_format((float)$d['unit_price'],2) ?></td><td><?= htmlspecialchars($currency) ?> <?= number_format(((float)$d['quantity']*(float)$d['unit_price']),2) ?></td></tr></tbody></table>
    <div class="invoice-total"><div class="invoice-total-row"><span>Discount</span><strong><?= htmlspecialchars($currency) ?> <?= number_format((float)$d['discount'],2) ?></strong></div><div class="invoice-total-row"><span>TVA <?= number_format((float)$d['tva_percent'],2) ?>%</span><strong><?= htmlspecialchars($currency) ?> <?= number_format((float)$d['tva_value'],2) ?></strong></div><div class="invoice-total-row grand"><span>Total</span><span><?= htmlspecialchars($currency) ?> <?= number_format((float)$d['total'],2) ?></span></div></div>
    <?php if(!empty($d['notes'])): ?><div style="margin-top:24px"><strong>Notes</strong><br><?= nl2br(htmlspecialchars($d['notes'])) ?></div><?php endif; ?>
  </div>
</div>
<?php if($printNow): ?><script>setTimeout(()=>window.print(),350);</script><?php endif; ?>
