<?php
require_once __DIR__ . '/../config/config.php';
require_permission('waste.manage');
$u = current_user();

$id = (int)($_GET['id'] ?? 0);
$stmt = $pdo->prepare("SELECT w.*, u.username recorded_by_name, o.name outlet_name, o.department_id
                        FROM waste_records w
                        JOIN users u ON u.id = w.recorded_by
                        LEFT JOIN outlets o ON o.id = w.outlet_id
                        WHERE w.id = ?");
$stmt->execute([$id]);
$waste = $stmt->fetch();
if (!$waste) { flash('error', 'Waste record not found.'); redirect('waste/list.php'); }

$canViewAll = has_permission($pdo, $u, 'requisitions.view_all');
if (!$canViewAll && $waste['recorded_by'] != $u['id'] && $waste['location_type'] === 'outlet'
    && $waste['department_id'] !== null && $waste['department_id'] != $u['department_id']) {
    flash('error', 'This waste record belongs to another department.');
    redirect('waste/list.php');
}

$lineStmt = $pdo->prepare("SELECT wi.*, i.name item_name, i.code item_code, un.abbreviation unit_abbr, wr.name reason_name
                            FROM waste_items wi
                            JOIN items i ON i.id = wi.item_id
                            LEFT JOIN units un ON un.id = i.unit_id
                            JOIN waste_reasons wr ON wr.id = wi.reason_id
                            WHERE wi.waste_id = ?");
$lineStmt->execute([$id]);
$lines = $lineStmt->fetchAll();

$pageTitle = 'Waste ' . $waste['waste_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($waste['waste_number']) ?></h1>
    <p class="text-slate-500 text-sm">
      Location: <?= $waste['location_type']==='main_store' ? 'Main Store' : e($waste['outlet_name']) ?> ·
      Recorded by <?= e($waste['recorded_by_name']) ?> on <?= date('M j, Y', strtotime($waste['created_at'])) ?>
    </p>
  </div>
  <span class="px-3 py-1 rounded-full text-sm bg-orange-100 text-orange-700">Total: ₦<?= number_format($waste['total_value'],2) ?></span>
</div>

<div class="overflow-x-auto">
<div class="bg-white rounded-xl shadow-sm border border-slate-200 mb-6">
  <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">Code</th><th class="text-left px-5 py-2">Item</th>
      <th class="text-left px-5 py-2">Qty</th><th class="text-left px-5 py-2">Unit Cost</th>
      <th class="text-left px-5 py-2">Value</th><th class="text-left px-5 py-2">Reason</th>
      <th class="text-left px-5 py-2">Notes</th></tr></thead>
    <tbody>
    <?php foreach ($lines as $l): $val = $l['quantity'] * $l['unit_cost']; ?>
      <tr class="border-t border-slate-100">
        <td class="px-5 py-2.5 font-mono text-xs text-slate-500"><?= e($l['item_code']) ?></td>
        <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['unit_cost'],2) ?></td>
        <td class="px-5 py-2.5">₦<?= number_format($val,2) ?></td>
        <td class="px-5 py-2.5"><span class="px-2 py-0.5 rounded-full text-xs bg-orange-100 text-orange-700"><?= e($l['reason_name']) ?></span></td>
        <td class="px-5 py-2.5 text-slate-500"><?= e($l['notes']) ?></td>
      </tr>
    <?php endforeach; ?>
    </tbody>
  </table>
</div>
</div>

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