<?php
require_once dirname(__DIR__) . '/config/config.php';
require_once dirname(__DIR__) . '/includes/functions.php';
startSecureSession();
requireCustomer();
$pageTitle = 'My Invoices';

$custId = $_SESSION['customer_id'];
$filter = trim($_GET['status'] ?? '');
$params = [$custId];
$where  = "WHERE customer_id=?";
if ($filter) { $where .= " AND status=?"; $params[] = $filter; }
$invoices = Database::fetchAll("SELECT * FROM invoices $where ORDER BY created_at DESC", $params);

$totalOwed = Database::fetchOne("SELECT COALESCE(SUM(total-amount_paid),0) as a FROM invoices WHERE customer_id=? AND status IN ('sent','partial','overdue')", [$custId])['a'] ?? 0;

include dirname(__DIR__) . '/includes/header.php';
?>

<div class="max-w-5xl mx-auto px-4 py-10">
  <div class="flex items-center justify-between mb-8">
    <div>
      <nav class="text-xs text-gray-400 mb-1"><a href="/customer/dashboard.php" class="hover:text-orange-500">Dashboard</a> / Invoices</nav>
      <h1 class="text-2xl font-bold">My Invoices</h1>
    </div>
  </div>

  <?php if ($totalOwed > 0): ?>
  <div class="pdf-strip mb-6 flex items-center justify-between">
    <div><div class="font-semibold text-sm">Outstanding Balance</div><div class="text-xs text-gray-400">Amount due on pending invoices</div></div>
    <div class="text-xl font-bold text-orange-500"><?= formatPrice($totalOwed) ?></div>
  </div>
  <?php endif; ?>

  <!-- Filter -->
  <div class="flex gap-2 mb-5">
    <?php foreach ([''=>'All','sent'=>'Pending','paid'=>'Paid','partial'=>'Partial','overdue'=>'Overdue'] as $v=>$l): ?>
    <a href="?status=<?= $v ?>" class="text-xs px-3 py-1.5 rounded <?= $filter===$v?'bg-orange-600 text-white':'bg-navy-700 text-gray-300' ?>"><?= $l ?></a>
    <?php endforeach; ?>
  </div>

  <div class="card-dark overflow-hidden">
    <?php if (empty($invoices)): ?>
    <div class="p-8 text-center text-gray-400"><div class="text-4xl mb-3">💰</div><p>No invoices found.</p></div>
    <?php else: ?>
    <table class="data-table">
      <thead><tr><th>Invoice #</th><th>Amount</th><th>Paid</th><th>Balance</th><th>Status</th><th>Due Date</th><th></th></tr></thead>
      <tbody>
      <?php foreach ($invoices as $inv): ?>
      <?php $balance = $inv['total'] - $inv['amount_paid']; ?>
      <tr>
        <td class="font-mono text-xs text-orange-400"><?= e($inv['invoice_number']) ?></td>
        <td class="font-semibold text-sm"><?= formatPrice($inv['total']) ?></td>
        <td class="text-sm text-green-400"><?= formatPrice($inv['amount_paid']) ?></td>
        <td class="text-sm <?= $balance > 0 ? 'text-red-400' : 'text-green-400' ?>"><?= formatPrice($balance) ?></td>
        <td><?= statusBadge($inv['status']) ?></td>
        <td class="text-xs text-gray-400"><?= $inv['due_date'] ? formatDate($inv['due_date']) : '—' ?></td>
        <td>
          <?php if ($inv['pdf_path']): ?>
          <a href="/pdf/<?= e($inv['pdf_path']) ?>" target="_blank" class="text-xs text-blue-400 hover:underline">📄 Download</a>
          <?php endif; ?>
        </td>
      </tr>
      <?php endforeach; ?>
      </tbody>
    </table>
    <?php endif; ?>
  </div>

  <div class="card-dark p-5 mt-6 text-sm text-gray-300">
    <h3 class="font-semibold mb-2">Payment Information</h3>
    <p class="text-xs text-gray-400"><?= e(setting('invoice_terms')) ?></p>
    <div class="mt-3 text-xs text-gray-500">GST: <?= e(setting('gst_number')) ?> | NTN: <?= e(setting('ntn_number')) ?></div>
  </div>
</div>

<?php include dirname(__DIR__) . '/includes/footer.php'; ?>
