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

<div class="container-fluid px-0 mt-3">
  <div class="d-flex justify-content-between align-items-center mb-3 px-3">
    <h3 class="mb-0">Classroom Equipment</h3>
    <div class="btn-group">
      <a class="btn btn-primary" href="<?= site_url('inventory/create/classroom') ?>">Add Item</a>
      <button class="btn btn-outline-secondary" data-bs-toggle="modal" data-bs-target="#addCategoryModal">Add Category</button>
    </div>
  </div>

  <?= view('partials/flash_messages') ?>

  <div class="card mb-4">
    <div class="card-header d-flex justify-content-between align-items-center">
      <span>Items</span>
      <select class="form-select form-select-sm" id="categoryFilter" style="min-width:220px">
        <option value="">All Categories</option>
        <?php foreach ($categories as $c): ?>
          <option value="<?= esc($c['id']) ?>"><?= esc($c['name']) ?></option>
        <?php endforeach; ?>
      </select>
    </div>
    <div class="card-body table-responsive">
      <table class="table table-striped align-middle" id="inventoryTable">
        <thead>
          <tr>
            <th>Name</th>
            <th>Condition</th>
            <th>Category</th>
            <th>Qty</th>
            <th>Unit</th>
            <th>Updated By</th>
            <th>Updated Date</th>
            <th>SKU</th>
            <th style="width:110px">Actions</th>
          </tr>
        </thead>
        <tbody>
        <?php foreach ($items as $i): ?>
          <tr data-category="<?= esc($i['category_id'] ?? '') ?>">
            <td><?= esc($i['name']) ?></td>
            <td>
              <span class="badge bg-<?php
                $map=['good'=>'success','needs_repair'=>'warning','need_replace'=>'danger','cannot_find'=>'secondary'];
                echo $map[$i['condition'] ?? ''] ?? 'light';
              ?>">
                <?= esc(ucwords(str_replace('_',' ', $i['condition'] ?? ''))) ?>
              </span>
            </td>
            <td><?php
              $cat = array_values(array_filter($categories, fn($c)=> ($c['id']??null) == ($i['category_id']??null)))[0] ?? null;
              echo esc($cat['name'] ?? '—');
            ?></td>
            <td><?= esc($i['quantity']) ?></td>
            <td><?= esc($i['unit']) ?></td>
            <td><?= esc($userNames[(int)($i['updated_by'] ?? 0)] ?? '—') ?></td>

            <td><?= esc($i['updated_at'] ? local_datetime($i['updated_at'], 'm-d-Y H:i') : '—') ?></td>
            <td><?= esc($i['sku']) ?></td>
            <td>
              <a class="btn btn-sm btn-outline-primary" href="<?= site_url('inventory/edit/'.$i['id']) ?>">Edit</a>
              <form action="<?= site_url('inventory/delete/'.$i['id']) ?>" method="post" class="d-inline">
                <?= csrf_field() ?>
                <button class="btn btn-sm btn-outline-danger" onclick="return confirm('Delete this item?')">Del</button>
              </form>
            </td>
          </tr>
        <?php endforeach; ?>
        </tbody>
      </table>
    </div>
  </div>
</div>

<!-- Add Category Modal -->
<div class="modal fade" id="addCategoryModal" tabindex="-1" aria-hidden="true">
  <div class="modal-dialog">
    <form class="modal-content" method="post" action="<?= site_url('inventory/category/save') ?>">
      <?= csrf_field() ?>
      <input type="hidden" name="type" value="classroom">
      <div class="modal-header">
        <h5 class="modal-title">Add Category (Classroom)</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>
      <div class="modal-body">
        <div class="mb-3">
          <label class="form-label">Name</label>
          <input type="text" name="name" class="form-control" required>
        </div>
        <div class="mb-0">
          <label class="form-label">Description (optional)</label>
          <textarea name="description" class="form-control" rows="2"></textarea>
        </div>
      </div>
      <div class="modal-footer"><button class="btn btn-primary">Save Category</button></div>
    </form>
  </div>
</div>

<script>
document.getElementById('categoryFilter')?.addEventListener('change', function() {
  const val = this.value;
  document.querySelectorAll('#inventoryTable tbody tr').forEach(tr => {
    tr.style.display = (!val || tr.getAttribute('data-category') === val) ? '' : 'none';
  });
});
</script>

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