<?= $this->extend('layout/management_layout') ?>
<?= $this->section('content') ?>

<?php
// Safe fallbacks
$selectedYear = $selectedYear ?? ($currentYear ?? '');
$currentYear  = $currentYear  ?? '';
$schoolYears  = $schoolYears  ?? [];
$rows         = $rows         ?? [];
$totals       = array_merge([
  'opening'=>0,'received'=>0,'distributed'=>0,'other_out'=>0,'adjust_net'=>0,'ending'=>0,'onhand'=>0,'variance'=>0
], $totals ?? []);
?>

<div class="container-fluid px-0 mt-3">
  <div class="px-3 mb-2"><?= $this->include('partials/academic_filter') ?></div>
  <div class="d-flex justify-content-between align-items-center mb-3 px-3">
    <h3 class="mb-0">
      Inventory Summary — <?= esc(strtolower((string)$selectedYear)==='all' ? 'All Years' : $selectedYear) ?>
    </h3>
    <div><a class="btn btn-outline-secondary" href="<?= site_url('inventory/book') ?>">Back to Items</a></div>
  </div>

  <div class="card mb-3">
    <div class="card-header d-flex gap-2 flex-wrap align-items-center justify-content-between">
      <div>Filter</div>
      <div class="d-flex gap-2">
        <select id="yearFilter" class="form-select form-select-sm" style="min-width:220px">
          <option value="__current__" <?= (strtolower((string)$selectedYear)!=='all' && $selectedYear===$currentYear)?'selected':'' ?>>
            Current (<?= esc($currentYear) ?>)
          </option>
          <option value="all" <?= strtolower((string)$selectedYear)==='all'?'selected':'' ?>>All Years</option>
          <?php foreach ($schoolYears as $y): ?>
            <?php if ($y !== $currentYear): // avoid duplicate of "Current" ?>
              <option value="<?= esc($y) ?>" <?= ($selectedYear===$y)?'selected':'' ?>><?= esc($y) ?></option>
            <?php endif; ?>
          <?php endforeach; ?>
        </select>
      </div>
    </div>

    <div class="card-body table-responsive">
      <table class="table table-striped align-middle" id="summaryTable" data-no-mgmt-sticky="1">
        <thead>
          <tr>
            <th>Type</th>
            <th>Category</th>
            <th>Name</th>
            <th class="text-end">Opening</th>
            <th class="text-end">Received</th>
            <th class="text-end">Distributed</th>
            <th class="text-end">Other Out</th>
            <th class="text-end">Adjust ±</th>
            <th class="text-end">Ending</th>
            <th class="text-end">On Hand (Now)</th>
            <th class="text-end">Variance</th>
          </tr>
        </thead>
        <tbody>
          <?php if (empty($rows)): ?>
            <tr>
              <td colspan="11" class="text-center text-muted py-4">
                No data to display for <?= esc(strtolower((string)$selectedYear)==='all' ? 'All Years' : $selectedYear) ?>.
              </td>
            </tr>
          <?php else: ?>
            <?php foreach ($rows as $r): ?>
              <?php
                $opening     = (int)($r['opening'] ?? 0);
                $received    = (int)($r['received'] ?? 0);
                $distributed = (int)($r['distributed'] ?? 0);
                $otherOut    = (int)($r['other_out'] ?? 0);
                $adjustNet   = (int)($r['adjust_net'] ?? 0);
                $ending      = (int)($r['ending'] ?? 0);
                $onhand      = (int)($r['onhand'] ?? 0);
                $variance    = (int)($r['variance'] ?? 0);
              ?>
              <tr>
                <td><?= esc($r['type'] ?? '') ?></td>
                <td><?= esc($r['category'] ?? '') ?></td>
                <td><?= esc($r['name'] ?? '') ?></td>
                <td class="text-end"><?= number_format($opening) ?></td>
                <td class="text-end"><?= number_format($received) ?></td>
                <td class="text-end"><?= number_format($distributed) ?></td>
                <td class="text-end"><?= number_format($otherOut) ?></td>
                <td class="text-end"><?= number_format($adjustNet) ?></td>
                <td class="text-end fw-semibold"><?= number_format($ending) ?></td>
                <td class="text-end"><?= number_format($onhand) ?></td>
                <td class="text-end">
                  <?php if ($variance === 0): ?>
                    <span class="text-success"><?= number_format($variance) ?></span>
                  <?php else: ?>
                    <span class="text-danger fw-semibold"><?= number_format($variance) ?></span>
                  <?php endif; ?>
                </td>
              </tr>
            <?php endforeach; ?>
          <?php endif; ?>
        </tbody>
        <tfoot>
          <tr class="table-light">
            <th colspan="3" class="text-end">Totals</th>
            <th class="text-end"><?= number_format((int)$totals['opening']) ?></th>
            <th class="text-end"><?= number_format((int)$totals['received']) ?></th>
            <th class="text-end"><?= number_format((int)$totals['distributed']) ?></th>
            <th class="text-end"><?= number_format((int)$totals['other_out']) ?></th>
            <th class="text-end"><?= number_format((int)$totals['adjust_net']) ?></th>
            <th class="text-end"><?= number_format((int)$totals['ending']) ?></th>
            <th class="text-end"><?= number_format((int)$totals['onhand']) ?></th>
            <th class="text-end"><?= number_format((int)$totals['variance']) ?></th>
          </tr>
        </tfoot>
      </table>
    </div>
  </div>
</div>

<script>
document.getElementById('yearFilter')?.addEventListener('change', function() {
  const v = this.value, url = new URL(window.location.href);
  if (v === '__current__') url.searchParams.delete('school_year');
  else url.searchParams.set('school_year', v);
  window.location.href = url.toString();
});
</script>

<?= $this->endSection() ?>
