<?php
require_once __DIR__ . '/../config/config.php';
require_permission('goods_received.view');

$id = (int)($_GET['id'] ?? 0);
$stmt = $pdo->prepare("SELECT g.*, po.po_number, po.pr_id, s.name supplier_name, u.username received_by_name
                        FROM goods_received_notes g
                        JOIN purchase_orders po ON po.id = g.po_id
                        JOIN suppliers s ON s.id = po.supplier_id
                        JOIN users u ON u.id = g.received_by
                        WHERE g.id = ?");
$stmt->execute([$id]);
$grn = $stmt->fetch();
if (!$grn) { flash('error', 'GRN not found.'); redirect('goods_received/list.php'); }

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

$pageTitle = 'GRN ' . $grn['grn_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($grn['grn_number']) ?></h1>
    <p class="text-slate-500 text-sm">PO <?= e($grn['po_number']) ?> · Supplier: <?= e($grn['supplier_name']) ?> ·
      Received by <?= e($grn['received_by_name']) ?> on <?= e($grn['received_date']) ?></p>
  </div>
  <a href="../purchase_orders/view.php?id=<?= $grn['po_id'] ?>" class="text-blue-600 hover:underline text-sm">View Purchase Order →</a>
  <a href="print.php?id=<?= $grn['id'] ?>" target="_blank" class="ml-3 text-sm text-blue-600 hover:underline">🖨️ Print / PDF</a>
</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">Qty Received</th>
      <th class="text-left px-5 py-2">Unit Cost</th><th class="text-left px-5 py-2">Value</th></tr></thead>
    <tbody>
    <?php $total = 0; foreach ($lines as $l): $val = $l['quantity_received']*$l['unit_cost']; $total += $val; ?>
      <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_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($val,2) ?></td>
      </tr>
    <?php endforeach; ?>
    </tbody>
    <tfoot><tr class="border-t border-slate-200 font-semibold"><td colspan="3" class="px-5 py-2.5 text-right">Total Value</td><td class="px-5 py-2.5">₦<?= number_format($total,2) ?></td></tr></tfoot>
  </table></div>
</div>

<?php if ($grn['notes']): ?>
  <div class="bg-white rounded-xl shadow-sm border border-slate-200 p-5 mb-6">
    <p class="text-sm font-semibold mb-1">Notes</p>
    <p class="text-sm text-slate-600"><?= e($grn['notes']) ?></p>
  </div>
<?php endif; ?>

<a href="../return_to_supplier/create.php?po_id=<?= $grn['po_id'] ?>" class="inline-block bg-amber-600 hover:bg-amber-700 text-white text-sm font-medium px-4 py-2 rounded-lg">
  Return Some of These Goods to Supplier
</a>
<?php if (has_permission($pdo, current_user(), 'accounting.ap_manage')): ?>
<a href="../accounts_payable/create.php" class="inline-block bg-slate-700 hover:bg-slate-800 text-white text-sm font-medium px-4 py-2 rounded-lg ml-2">
  Record Supplier Bill for This Delivery
</a>
<?php endif; ?>
<?php include __DIR__ . '/../includes/footer.php'; ?>
