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

$id = (int)($_GET['id'] ?? 0);
$stmt = $pdo->prepare("SELECT po.*, s.name supplier_name, s.address supplier_address, s.phone supplier_phone,
                        pr.pr_number, u.username created_by_name
                        FROM purchase_orders po
                        JOIN suppliers s ON s.id=po.supplier_id
                        JOIN purchase_requisitions pr ON pr.id = po.pr_id
                        JOIN users u ON u.id = po.created_by
                        WHERE po.id = ?");
$stmt->execute([$id]);
$po = $stmt->fetch();
if (!$po) { die('Purchase order not found.'); }

$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();

$printTitle = 'Purchase Order';
$printSubtitle = $po['po_number'];
include __DIR__ . '/../includes/print_header.php';
?>
<div class="grid grid-cols-1 sm:grid-cols-2 gap-4 text-sm mb-6">
  <div>
    <p class="text-slate-500 mb-1">Supplier</p>
    <p class="font-semibold"><?= e($po['supplier_name']) ?></p>
    <p><?= e($po['supplier_address']) ?></p>
    <p><?= e($po['supplier_phone']) ?></p>
  </div>
  <div class="text-right">
    <p><span class="text-slate-500">From Requisition:</span> <?= e($po['pr_number']) ?></p>
    <p><span class="text-slate-500">Date:</span> <?= date('d M Y', strtotime($po['created_at'])) ?></p>
    <p><span class="text-slate-500">Prepared By:</span> <?= e($po['created_by_name']) ?></p>
    <p><span class="text-slate-500">Status:</span> <span class="capitalize"><?= str_replace('_',' ',e($po['status'])) ?></span></p>
  </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</th><th class="py-2">Unit Cost</th><th class="py-2">Line Total</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'],2) ?> <?= e($l['unit_abbr']) ?></td>
      <td class="py-2">₦<?= number_format($l['unit_cost'],2) ?></td>
      <td class="py-2">₦<?= number_format($l['quantity']*$l['unit_cost'],2) ?></td>
    </tr>
  <?php endforeach; ?>
  </tbody>
  <tfoot><tr class="font-semibold"><td colspan="3" class="py-2 text-right">Total</td><td class="py-2">₦<?= number_format($po['total_cost'],2) ?></td></tr></tfoot>
</table></div>

<div class="grid grid-cols-1 sm:grid-cols-2 gap-6 mt-10 text-sm">
  <div class="border-t border-slate-400 pt-2">Prepared By: <?= e($po['created_by_name']) ?></div>
  <div class="border-t border-slate-400 pt-2">Authorized Signature: ______________________</div>
</div>
<?php include __DIR__ . '/../includes/print_footer.php'; ?>
