<?php
require_once __DIR__ . '/../config/config.php';
require_login();
$u = current_user();

$statusFilter = $_GET['status'] ?? '';
$canViewAll = has_permission($pdo, $u, 'requisitions.view_all');

$sql = "SELECT sr.*, u.username requester, o.name outlet_name, o.department_id
        FROM store_requisitions sr
        JOIN users u ON u.id = sr.requested_by
        JOIN outlets o ON o.id = sr.outlet_id";
$conditions = [];
$params = [];

if (!$canViewAll) {
    $conditions[] = "(sr.requested_by = ? OR o.department_id " . ($u['department_id'] !== null ? "= ?" : "IS NULL") . ")";
    $params[] = $u['id'];
    if ($u['department_id'] !== null) $params[] = $u['department_id'];
}
if ($statusFilter) { $conditions[] = "sr.status = ?"; $params[] = $statusFilter; }

if ($conditions) { $sql .= " WHERE " . implode(' AND ', $conditions); }
$sql .= " ORDER BY sr.created_at DESC";
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$srs = $stmt->fetchAll();

$pageTitle = 'Store Requisitions';
include __DIR__ . '/../includes/header.php';
include __DIR__ . '/../includes/sidebar.php';
?>
<div class="flex items-center justify-between mb-2">
  <h1 class="text-2xl font-bold">Store Requisitions</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 Requisition</a>
</div>
<p class="text-xs text-slate-400 mb-4"><?= $canViewAll ? 'Showing requisitions from all departments.' : 'Showing requisitions from your department and your own requests only.' ?></p>

<div class="flex gap-2 mb-4 text-sm">
  <?php foreach (['' => 'All','pending'=>'Pending','approved'=>'Approved','issued'=>'Issued','rejected'=>'Rejected'] 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="bg-white rounded-xl shadow-sm border border-slate-200">
  <div class="overflow-x-auto"><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">SR Number</th><th class="text-left px-5 py-2">Outlet</th>
      <th class="text-left px-5 py-2">Requested By</th><th class="text-left px-5 py-2">Stage</th>
      <th class="text-left px-5 py-2">Status</th><th class="text-left px-5 py-2">Date</th>
      <th class="text-right px-5 py-2">Action</th></tr></thead>
    <tbody>
    <?php foreach ($srs as $sr): ?>
      <?php $badge = ['pending'=>'bg-amber-100 text-amber-700','approved'=>'bg-emerald-100 text-emerald-700',
                      'rejected'=>'bg-red-100 text-red-700','issued'=>'bg-blue-100 text-blue-700',
                      'cancelled'=>'bg-slate-200 text-slate-500'][$sr['status']]; ?>
      <tr class="border-t border-slate-100">
        <td class="px-5 py-2.5 font-medium"><?= e($sr['sr_number']) ?></td>
        <td class="px-5 py-2.5"><?= e($sr['outlet_name']) ?></td>
        <td class="px-5 py-2.5"><?= e($sr['requester']) ?></td>
        <td class="px-5 py-2.5"><?= $sr['status']==='pending' ? 'Stage ' . $sr['current_stage'] : '—' ?></td>
        <td class="px-5 py-2.5"><span class="px-2 py-0.5 rounded-full text-xs <?= $badge ?> capitalize"><?= $sr['status'] ?></span></td>
        <td class="px-5 py-2.5 text-slate-500"><?= date('d M Y', strtotime($sr['created_at'])) ?></td>
        <td class="px-5 py-2.5 text-right"><a href="view.php?id=<?= $sr['id'] ?>" class="text-blue-600 hover:underline">View →</a></td>
      </tr>
    <?php endforeach; ?>
    <?php if (empty($srs)): ?><tr><td colspan="7" class="px-5 py-6 text-center text-slate-400">No store requisitions found.</td></tr><?php endif; ?>
    </tbody>
  </table></div>
</div>
<?php include __DIR__ . '/../includes/footer.php'; ?>
