<?php
require_once __DIR__ . '/../config/config.php';
require_login();

$id = (int)($_GET['id'] ?? 0);
$stmt = $pdo->prepare("SELECT sr.*, u.username requester, o.name outlet_name, o.department_id outlet_department_id
                        FROM store_requisitions sr
                        JOIN users u ON u.id = sr.requested_by
                        JOIN outlets o ON o.id = sr.outlet_id
                        WHERE sr.id = ?");
$stmt->execute([$id]);
$sr = $stmt->fetch();
if (!$sr) { die('Requisition not found.'); }
if (!can_view_requisition($pdo, current_user(), $sr['outlet_department_id'], $sr['requested_by'])) {
    die('You do not have access to this requisition.');
}

$lineStmt = $pdo->prepare("SELECT sri.*, i.name item_name, un.abbreviation unit_abbr
                            FROM store_requisition_items sri
                            JOIN items i ON i.id = sri.item_id
                            LEFT JOIN units un ON un.id = i.unit_id
                            WHERE sri.sr_id = ?");
$lineStmt->execute([$id]);
$lines = $lineStmt->fetchAll();

$logStmt = $pdo->prepare("SELECT al.*, u.username, u.role, u.signature_path FROM approval_logs al JOIN users u ON u.id=al.approver_id
                           WHERE module='store_requisition' AND reference_id=? ORDER BY al.created_at");
$logStmt->execute([$id]);
$logs = $logStmt->fetchAll();

$printTitle = 'Store Requisition';
$printSubtitle = $sr['sr_number'];
include __DIR__ . '/../includes/print_header.php';
?>
<div class="grid grid-cols-1 sm:grid-cols-2 gap-4 text-sm mb-6">
  <div><span class="text-slate-500">Outlet:</span> <?= e($sr['outlet_name']) ?></div>
  <div><span class="text-slate-500">Date:</span> <?= date('d M Y', strtotime($sr['created_at'])) ?></div>
  <div><span class="text-slate-500">Requested By:</span> <?= e($sr['requester']) ?></div>
  <div><span class="text-slate-500">Status:</span> <span class="capitalize"><?= e($sr['status']) ?></span></div>
</div>

<div class="overflow-x-auto"><table class="w-full text-sm mb-6 border-collapse">
  <thead><tr class="border-b-2 border-slate-800 text-left text-xs uppercase text-slate-500">
    <th class="py-2">Item</th><th class="py-2">Qty Requested</th><th class="py-2">Qty Issued</th></tr></thead>
  <tbody>
  <?php foreach ($lines as $l): ?>
    <tr class="border-b border-slate-100">
      <td class="py-2"><?= e($l['item_name']) ?></td>
      <td class="py-2"><?= number_format($l['quantity_requested'],2) ?> <?= e($l['unit_abbr']) ?></td>
      <td class="py-2"><?= number_format($l['quantity_issued'],2) ?> <?= e($l['unit_abbr']) ?></td>
    </tr>
  <?php endforeach; ?>
  </tbody>
</table></div>

<?php if ($sr['notes']): ?><p class="text-sm mb-6"><span class="text-slate-500">Notes:</span> <?= e($sr['notes']) ?></p><?php endif; ?>

<h2 class="text-sm font-bold uppercase tracking-wide text-slate-600 mb-3 mt-8">Approval Signatures</h2>
<div class="grid grid-cols-1 sm:grid-cols-2 gap-6">
  <?php foreach ($logs as $lg): ?>
    <div class="border border-slate-200 rounded-lg p-4">
      <p class="text-xs text-slate-500 uppercase">Stage <?= $lg['stage'] ?> · <?= e(ucfirst($lg['role'])) ?></p>
      <div class="h-14 flex items-end my-2">
        <?php if (!empty($lg['signature_path'])): ?>
          <img src="<?= SIGNATURE_UPLOAD_URL . e($lg['signature_path']) ?>" class="h-12">
        <?php else: ?>
          <span class="text-slate-300 text-xs italic">No signature on file</span>
        <?php endif; ?>
      </div>
      <p class="text-sm font-semibold"><?= e($lg['username']) ?></p>
      <p class="text-xs <?= $lg['action']==='approved' ? 'text-emerald-600' : 'text-red-600' ?> capitalize"><?= e($lg['action']) ?> — <?= date('d M Y H:i', strtotime($lg['created_at'])) ?></p>
      <?php if ($lg['comments']): ?><p class="text-xs text-slate-500 mt-1">"<?= e($lg['comments']) ?>"</p><?php endif; ?>
    </div>
  <?php endforeach; ?>
  <?php if (empty($logs)): ?><p class="text-sm text-slate-400 col-span-2">No approvals recorded yet.</p><?php endif; ?>
</div>
<?php include __DIR__ . '/../includes/print_footer.php'; ?>
