<?php
require_once __DIR__ . '/../config/config.php';
require_permission('purchase_orders.manage');

$id = (int)($_GET['id'] ?? 0);
$stmt = $pdo->prepare("SELECT po.*, s.name supplier_name, pr.pr_number FROM purchase_orders po
                        JOIN suppliers s ON s.id=po.supplier_id
                        JOIN purchase_requisitions pr ON pr.id = po.pr_id
                        WHERE po.id = ?");
$stmt->execute([$id]);
$po = $stmt->fetch();
if (!$po) { flash('error', 'Purchase order not found.'); redirect('purchase_orders/list.php'); }

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

$grns = $pdo->prepare("SELECT g.*, u.username received_by_name FROM goods_received_notes g JOIN users u ON u.id=g.received_by WHERE g.po_id=? ORDER BY g.created_at DESC");
$grns->execute([$id]);
$grnList = $grns->fetchAll();

$pageTitle = 'PO ' . $po['po_number'];
include __DIR__ . '/../includes/header.php';
include __DIR__ . '/../includes/sidebar.php';
?>
<div class="flex items-center justify-between mb-6">
  <div>
    <h1 class="text-2xl font-bold"><?= e($po['po_number']) ?></h1>
    <p class="text-slate-500 text-sm">Supplier: <?= e($po['supplier_name']) ?> · From <?= e($po['pr_number']) ?></p>
  </div>
  <div class="flex gap-2">
    <a href="print.php?id=<?= $po['id'] ?>" target="_blank" class="text-sm text-blue-600 hover:underline self-center mr-2">🖨️ Print / PDF</a>
    <?php if ($po['status'] !== 'received'): ?>
      <a href="receive.php?po_id=<?= $po['id'] ?>" class="bg-blue-700 hover:bg-blue-800 text-white text-sm font-medium px-4 py-2 rounded-lg">Receive Goods (GRN)</a>
    <?php endif; ?>
    <a href="../return_to_supplier/create.php?po_id=<?= $po['id'] ?>" class="bg-amber-600 hover:bg-amber-700 text-white text-sm font-medium px-4 py-2 rounded-lg">Return Goods</a>
  </div>
</div>

<div class="bg-white rounded-xl shadow-sm border border-slate-200 mb-6">
  <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">Item</th><th class="text-left px-5 py-2">Ordered</th>
      <th class="text-left px-5 py-2">Received</th><th class="text-left px-5 py-2">Unit Cost</th>
      <th class="text-left px-5 py-2">Line Total</th></tr></thead>
    <tbody>
    <?php foreach ($lines as $l): ?>
      <tr class="border-t border-slate-100">
        <td class="px-5 py-2.5"><?= e($l['item_name']) ?></td>
        <td class="px-5 py-2.5"><?= number_format($l['quantity'],2) ?> <?= e($l['unit_abbr']) ?></td>
        <td class="px-5 py-2.5"><?= number_format($l['quantity_received'],2) ?> <?= e($l['unit_abbr']) ?></td>
        <td class="px-5 py-2.5">₦<?= number_format($l['unit_cost'],2) ?></td>
        <td class="px-5 py-2.5">₦<?= number_format($l['quantity']*$l['unit_cost'],2) ?></td>
      </tr>
    <?php endforeach; ?>
    </tbody>
  </table></div>
</div>

<div class="bg-white rounded-xl shadow-sm border border-slate-200">
  <div class="px-5 py-4 border-b border-slate-200 font-semibold">Goods Received Notes</div>
  <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">GRN Number</th><th class="text-left px-5 py-2">Received By</th>
      <th class="text-left px-5 py-2">Date</th></tr></thead>
    <tbody>
    <?php foreach ($grnList as $g): ?>
      <tr class="border-t border-slate-100">
        <td class="px-5 py-2.5"><?= e($g['grn_number']) ?></td>
        <td class="px-5 py-2.5"><?= e($g['received_by_name']) ?></td>
        <td class="px-5 py-2.5"><?= e($g['received_date']) ?></td>
      </tr>
    <?php endforeach; ?>
    <?php if (empty($grnList)): ?><tr><td colspan="3" class="px-5 py-6 text-center text-slate-400">No goods received yet.</td></tr><?php endif; ?>
    </tbody>
  </table></div>
</div>
<?php include __DIR__ . '/../includes/footer.php'; ?>
