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

$mainStock = $pdo->query("SELECT i.name, i.code, u.abbreviation unit_abbr, s.quantity, s.avg_cost
                           FROM main_store_stock s JOIN items i ON i.id=s.item_id
                           LEFT JOIN units u ON u.id=i.unit_id ORDER BY i.name")->fetchAll();

$outletStock = $pdo->query("SELECT o.name outlet_name, i.name item_name, u.abbreviation unit_abbr, os.quantity
                             FROM outlet_stock os
                             JOIN outlets o ON o.id = os.outlet_id
                             JOIN items i ON i.id = os.item_id
                             LEFT JOIN units u ON u.id=i.unit_id
                             ORDER BY o.name, i.name")->fetchAll();

$pageTitle = 'Stock Report';
include __DIR__ . '/../includes/header.php';
include __DIR__ . '/../includes/sidebar.php';
?>
<h1 class="text-2xl font-bold mb-6">Stock Report</h1>

<div class="bg-white rounded-xl shadow-sm border border-slate-200 mb-8">
  <div class="px-5 py-4 border-b border-slate-200 font-semibold">Main Store</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">Item</th><th class="text-left px-5 py-2">Qty</th>
      <th class="text-left px-5 py-2">Avg Cost</th><th class="text-left px-5 py-2">Value</th></tr></thead>
    <tbody>
    <?php $total = 0; foreach ($mainStock as $s): $val = $s['quantity']*$s['avg_cost']; $total += $val; ?>
      <tr class="border-t border-slate-100">
        <td class="px-5 py-2.5"><?= e($s['name']) ?></td>
        <td class="px-5 py-2.5"><?= number_format($s['quantity'],2) ?> <?= e($s['unit_abbr']) ?></td>
        <td class="px-5 py-2.5">₦<?= number_format($s['avg_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 Stock Value</td><td class="px-5 py-2.5">₦<?= number_format($total,2) ?></td></tr></tfoot>
  </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">Outlet Stock</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">Outlet</th><th class="text-left px-5 py-2">Item</th><th class="text-left px-5 py-2">Qty</th></tr></thead>
    <tbody>
    <?php foreach ($outletStock as $s): ?>
      <tr class="border-t border-slate-100">
        <td class="px-5 py-2.5"><?= e($s['outlet_name']) ?></td>
        <td class="px-5 py-2.5"><?= e($s['item_name']) ?></td>
        <td class="px-5 py-2.5"><?= number_format($s['quantity'],2) ?> <?= e($s['unit_abbr']) ?></td>
      </tr>
    <?php endforeach; ?>
    <?php if (empty($outletStock)): ?><tr><td colspan="3" class="px-5 py-6 text-center text-slate-400">No outlet stock recorded yet.</td></tr><?php endif; ?>
    </tbody>
  </table></div>
</div>
<?php include __DIR__ . '/../includes/footer.php'; ?>
