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

$statusFilter = $_GET['status'] ?? '';
$sql = "SELECT b.*, s.name supplier_name FROM ap_bills b JOIN suppliers s ON s.id = b.supplier_id";
$params = [];
if ($statusFilter) { $sql .= " WHERE b.status = ?"; $params[] = $statusFilter; }
$sql .= " ORDER BY b.due_date IS NULL, b.due_date ASC, b.created_at DESC";
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$bills = $stmt->fetchAll();

$pageTitle = 'Accounts Payable';
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 Payable</h1>
  <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 Bill</a>
</div>

<div class="flex gap-2 mb-4 text-sm">
  <?php foreach (['' => 'All','open'=>'Open','partially_paid'=>'Partially Paid','paid'=>'Paid','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">Bill No.</th><th class="text-left px-5 py-2">Supplier</th>
      <th class="text-left px-5 py-2">Bill 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">Paid</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 ($bills as $b):
        $overdue = $b['due_date'] && $b['due_date'] < date('Y-m-d') && $b['status'] !== 'paid' && $b['status'] !== 'void';
        $badge = ['open'=>'bg-amber-100 text-amber-700','partially_paid'=>'bg-blue-100 text-blue-700','paid'=>'bg-emerald-100 text-emerald-700','void'=>'bg-slate-200 text-slate-500'][$b['status']];
    ?>
      <tr class="border-t border-slate-100 <?= $overdue ? 'bg-red-50' : '' ?>">
        <td class="px-5 py-2.5 font-medium"><?= e($b['bill_number']) ?></td>
        <td class="px-5 py-2.5"><?= e($b['supplier_name']) ?></td>
        <td class="px-5 py-2.5"><?= date('M j, Y', strtotime($b['bill_date'])) ?></td>
        <td class="px-5 py-2.5 <?= $overdue ? 'text-red-600 font-medium' : '' ?>"><?= $b['due_date'] ? date('M j, Y', strtotime($b['due_date'])) : '—' ?></td>
        <td class="px-5 py-2.5">₦<?= number_format($b['amount'],2) ?></td>
        <td class="px-5 py-2.5">₦<?= number_format($b['amount_paid'],2) ?></td>
        <td class="px-5 py-2.5"><span class="px-2 py-0.5 rounded-full text-xs <?= $badge ?> capitalize"><?= str_replace('_',' ',$b['status']) ?></span></td>
        <td class="px-5 py-2.5 text-right"><a href="view.php?id=<?= $b['id'] ?>" class="text-blue-600 hover:underline">View →</a></td>
      </tr>
    <?php endforeach; ?>
    <?php if (empty($bills)): ?><tr><td colspan="8" class="px-5 py-6 text-center text-slate-400">No bills recorded yet.</td></tr><?php endif; ?>
    </tbody>
  </table>
</div>
</div>
<?php include __DIR__ . '/../includes/footer.php'; ?>
