<?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 pr.*, u.username requester, d.name dept_name, o.name outlet_name
        FROM purchase_requisitions pr
        JOIN users u ON u.id = pr.requested_by
        LEFT JOIN departments d ON d.id = pr.department_id
        LEFT JOIN outlets o ON o.id = pr.outlet_id";
$conditions = [];
$params = [];

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

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

$pageTitle = 'Purchase 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">Purchase 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','rejected'=>'Rejected','ordered'=>'Ordered','received'=>'Received'] 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">PR Number</th><th class="text-left px-5 py-2">Requested By</th>
      <th class="text-left px-5 py-2">Department/Outlet</th><th class="text-left px-5 py-2">Est. Cost</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 ($prs as $pr): ?>
      <?php $badge = ['pending'=>'bg-amber-100 text-amber-700','approved'=>'bg-emerald-100 text-emerald-700',
                      'rejected'=>'bg-red-100 text-red-700','ordered'=>'bg-blue-100 text-blue-700',
                      'received'=>'bg-slate-200 text-slate-700','cancelled'=>'bg-slate-200 text-slate-500'][$pr['status']]; ?>
      <tr class="border-t border-slate-100">
        <td class="px-5 py-2.5 font-medium"><?= e($pr['pr_number']) ?></td>
        <td class="px-5 py-2.5"><?= e($pr['requester']) ?></td>
        <td class="px-5 py-2.5"><?= e($pr['outlet_name'] ?? $pr['dept_name'] ?? '—') ?></td>
        <td class="px-5 py-2.5">₦<?= number_format($pr['total_estimated_cost'],2) ?></td>
        <td class="px-5 py-2.5"><?= $pr['status']==='pending' ? 'Stage ' . $pr['current_stage'] : '—' ?></td>
        <td class="px-5 py-2.5"><span class="px-2 py-0.5 rounded-full text-xs <?= $badge ?> capitalize"><?= $pr['status'] ?></span></td>
        <td class="px-5 py-2.5 text-slate-500"><?= date('d M Y', strtotime($pr['created_at'])) ?></td>
        <td class="px-5 py-2.5 text-right"><a href="view.php?id=<?= $pr['id'] ?>" class="text-blue-600 hover:underline">View →</a></td>
      </tr>
    <?php endforeach; ?>
    <?php if (empty($prs)): ?><tr><td colspan="8" class="px-5 py-6 text-center text-slate-400">No purchase requisitions found.</td></tr><?php endif; ?>
    </tbody>
  </table></div>
</div>
<?php include __DIR__ . '/../includes/footer.php'; ?>
