<?php
require_once __DIR__ . '/../config/config.php';
require_permission('accounting.ar_manage');

$statusFilter = $_GET['status'] ?? '';
$sql = "SELECT i.*, c.name customer_name FROM ar_invoices i JOIN ar_customers c ON c.id = i.customer_id";
$params = [];
if ($statusFilter) { $sql .= " WHERE i.status = ?"; $params[] = $statusFilter; }
$sql .= " ORDER BY i.due_date IS NULL, i.due_date ASC, i.created_at DESC";
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$invoices = $stmt->fetchAll();

$pageTitle = 'Accounts Receivable';
include __DIR__ . '/../includes/header.php';
include __DIR__ . '/../includes/sidebar.php';
?>
<div class="flex items-center justify-between mb-6">
  <h1 class="text-2xl font-bold">Accounts Receivable</h1>
  <div class="flex gap-2">
    <a href="customers.php" class="bg-slate-100 hover:bg-slate-200 text-slate-700 text-sm font-medium px-4 py-2 rounded-lg">Customers</a>
    <a href="create.php" class="bg-blue-700 hover:bg-blue-800 text-white text-sm font-medium px-4 py-2 rounded-lg">+ New Invoice</a>
  </div>
</div>

<div class="flex gap-2 mb-4 text-sm">
  <?php foreach (['' => 'All','open'=>'Open','partially_received'=>'Partially Received','received'=>'Received','void'=>'Void'] as $val=>$label): ?>
    <a href="?status=<?= $val ?>" class="px-3 py-1.5 rounded-full <?= $statusFilter===$val ? 'bg-blue-700 text-white' : 'bg-white border border-slate-200 text-slate-600' ?>"><?= $label ?></a>
  <?php endforeach; ?>
</div>

<div class="overflow-x-auto">
<div class="bg-white rounded-xl shadow-sm border border-slate-200">
  <table class="w-full text-sm">
    <thead class="bg-slate-50 text-slate-500 text-xs uppercase"><tr>
      <th class="text-left px-5 py-2">Invoice No.</th><th class="text-left px-5 py-2">Customer</th>
      <th class="text-left px-5 py-2">Invoice Date</th><th class="text-left px-5 py-2">Due Date</th>
      <th class="text-left px-5 py-2">Amount</th><th class="text-left px-5 py-2">Received</th>
      <th class="text-left px-5 py-2">Status</th><th class="text-right px-5 py-2">Action</th></tr></thead>
    <tbody>
    <?php foreach ($invoices as $inv):
        $overdue = $inv['due_date'] && $inv['due_date'] < date('Y-m-d') && $inv['status'] !== 'received' && $inv['status'] !== 'void';
        $badge = ['open'=>'bg-amber-100 text-amber-700','partially_received'=>'bg-blue-100 text-blue-700','received'=>'bg-emerald-100 text-emerald-700','void'=>'bg-slate-200 text-slate-500'][$inv['status']];
    ?>
      <tr class="border-t border-slate-100 <?= $overdue ? 'bg-red-50' : '' ?>">
        <td class="px-5 py-2.5 font-medium"><?= e($inv['invoice_number']) ?></td>
        <td class="px-5 py-2.5"><?= e($inv['customer_name']) ?></td>
        <td class="px-5 py-2.5"><?= date('M j, Y', strtotime($inv['invoice_date'])) ?></td>
        <td class="px-5 py-2.5 <?= $overdue ? 'text-red-600 font-medium' : '' ?>"><?= $inv['due_date'] ? date('M j, Y', strtotime($inv['due_date'])) : '—' ?></td>
        <td class="px-5 py-2.5">₦<?= number_format($inv['amount'],2) ?></td>
        <td class="px-5 py-2.5">₦<?= number_format($inv['amount_received'],2) ?></td>
        <td class="px-5 py-2.5"><span class="px-2 py-0.5 rounded-full text-xs <?= $badge ?> capitalize"><?= str_replace('_',' ',$inv['status']) ?></span></td>
        <td class="px-5 py-2.5 text-right"><a href="view.php?id=<?= $inv['id'] ?>" class="text-blue-600 hover:underline">View →</a></td>
      </tr>
    <?php endforeach; ?>
    <?php if (empty($invoices)): ?><tr><td colspan="8" class="px-5 py-6 text-center text-slate-400">No invoices recorded yet.</td></tr><?php endif; ?>
    </tbody>
  </table>
</div>
</div>
<?php include __DIR__ . '/../includes/footer.php'; ?>
