<?php $this->start('body'); ?>
<?php $inv = $this->data['inv']; $lines = $this->data['lines'];
$out = (float)$inv['total_amount'] - (float)$inv['amount_paid']; ?>

<style>
  @media print { .no-print { display: none !important; } body { background: #fff; } }
  .invoice-print { max-width: 800px; margin: 20px auto; background: #fff; padding: 32px 40px; font-family: 'Open Sans', Arial, sans-serif; }
  .invoice-print h1 { color: #2626F5; letter-spacing: 2px; }
  .invoice-print .header-row { display: flex; justify-content: space-between; align-items: flex-start; border-bottom: 3px solid #2626F5; padding-bottom: 16px; margin-bottom: 24px; }
  .invoice-print .meta-box { border: 1px solid #ccc; padding: 12px; border-radius: 6px; margin-bottom: 16px; }
  .invoice-print table { width: 100%; border-collapse: collapse; margin-top: 12px; }
  .invoice-print th, .invoice-print td { padding: 8px 10px; border-bottom: 1px solid #eee; }
  .invoice-print thead th { background: #2626F5; color: #fff; text-transform: uppercase; font-size: 12px; }
  .invoice-print .money { text-align: right; font-variant-numeric: tabular-nums; }
  .invoice-print .totals { margin-top: 12px; }
  .invoice-print .footer-note { border-top: 1px solid #ddd; margin-top: 24px; padding-top: 16px; color: #666; font-size: 13px; }
  .invoice-print .stamp-void { color: #dc2626; font-size: 4rem; font-weight: bold; opacity: 0.15; position: absolute; transform: rotate(-15deg); left: 40%; top: 40%; }
</style>

<div class="no-print p-3 text-end">
  <button class="btn btn-sm btn-primary" onclick="window.print();"><i class="fas fa-print me-1"></i>Print</button>
  <a href="<?= PROOT; ?>CustomerInvoices/view/<?= (int)$inv['c0d']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Back</a>
</div>

<div class="invoice-print position-relative">
  <?php if ($inv['status'] === 'void'): ?><div class="stamp-void">VOID</div><?php endif; ?>

  <div class="header-row">
    <div>
      <h1 class="mb-0" style="font-size: 32px;"><?= htmlspecialchars(defined('COMPANY_NAME') ? COMPANY_NAME : 'Pacific Logistics'); ?></h1>
      <div style="color:#666;font-size:13px;"><?= defined('COMPANY_TAGLINE') ? htmlspecialchars(COMPANY_TAGLINE) : ''; ?></div>
      <?php if ($inv['billing_address']): ?><div class="mt-2" style="font-size:12px;color:#444;white-space:pre-line;"><?= htmlspecialchars($inv['billing_address']); ?></div><?php endif; ?>
    </div>
    <div style="text-align:right;">
      <h2 style="color:#2626F5;margin:0;">INVOICE</h2>
      <div style="font-family:monospace;font-size:18px;font-weight:bold;"><?= htmlspecialchars($inv['invoice_no']); ?></div>
      <div style="color:#666;font-size:13px;margin-top:4px;">Date: <?= humanDate($inv['invoice_date']); ?></div>
      <?php if ($inv['due_date']): ?><div style="color:#666;font-size:13px;">Due: <?= humanDate($inv['due_date']); ?></div><?php endif; ?>
    </div>
  </div>

  <div class="meta-box">
    <div style="text-transform:uppercase;font-size:11px;color:#888;">Bill to</div>
    <div style="font-weight:600;font-size:15px;"><?= htmlspecialchars($inv['customer_name'] ?: '—'); ?></div>
    <?php if ($inv['customer_phone']): ?><div style="font-size:13px;color:#555;"><?= htmlspecialchars($inv['customer_phone']); ?></div><?php endif; ?>
    <?php if ($inv['customer_email']): ?><div style="font-size:13px;color:#555;"><?= htmlspecialchars($inv['customer_email']); ?></div><?php endif; ?>
    <?php $addr = trim(($inv['physical_address'] ?? '') . "\n" . ($inv['customer_city'] ?? '')); ?>
    <?php if ($addr): ?><div style="font-size:13px;color:#555;white-space:pre-line;"><?= htmlspecialchars($addr); ?></div><?php endif; ?>
  </div>

  <table>
    <thead><tr><th>#</th><th>Description</th><th class="money">Qty</th><th class="money">Unit price</th><th class="money">Net</th><th class="money">VAT</th><th class="money">Total</th></tr></thead>
    <tbody>
      <?php foreach ($lines as $ln): ?>
        <tr>
          <td><?= (int)$ln['line_no']; ?></td>
          <td><?= htmlspecialchars($ln['description']); ?></td>
          <td class="money"><?= number_format((float)$ln['quantity'], 0); ?></td>
          <td class="money"><?= number_format((float)$ln['unit_price'], 2, '.', ','); ?></td>
          <td class="money"><?= number_format((float)$ln['net_amount'], 2, '.', ','); ?></td>
          <td class="money"><?= number_format((float)$ln['vat_amount'], 2, '.', ','); ?></td>
          <td class="money"><?= number_format((float)$ln['total_amount'], 2, '.', ','); ?></td>
        </tr>
      <?php endforeach; ?>
    </tbody>
  </table>

  <table class="totals" style="margin-left:auto;width:50%;">
    <tr><td style="border:none;">Subtotal</td><td class="money" style="border:none;"><?= htmlspecialchars($inv['currency']); ?> <?= number_format((float)$inv['subtotal'], 2, '.', ','); ?></td></tr>
    <tr><td style="border:none;">VAT</td><td class="money" style="border:none;"><?= number_format((float)$inv['vat_amount'], 2, '.', ','); ?></td></tr>
    <tr style="border-top:2px solid #2626F5;"><td style="font-weight:bold;font-size:16px;">TOTAL DUE</td><td class="money" style="font-weight:bold;font-size:16px;"><?= htmlspecialchars($inv['currency']); ?> <?= number_format((float)$inv['total_amount'], 2, '.', ','); ?></td></tr>
    <?php if ((float)$inv['amount_paid'] > 0): ?>
      <tr><td style="color:#16a34a;">Amount paid</td><td class="money" style="color:#16a34a;"><?= number_format((float)$inv['amount_paid'], 2, '.', ','); ?></td></tr>
      <tr><td style="font-weight:bold;">Outstanding</td><td class="money" style="font-weight:bold;color:<?= $out > 0 ? '#dc2626' : '#16a34a'; ?>;"><?= number_format($out, 2, '.', ','); ?></td></tr>
    <?php endif; ?>
  </table>

  <?php if ($inv['footer_note']): ?>
    <div class="footer-note"><?= nl2br(htmlspecialchars($inv['footer_note'])); ?></div>
  <?php endif; ?>
</div>

<?php $this->end(); ?>
