<?php $pageTitle = 'Equipment'; include __DIR__ . '/../shared/header.php'; ?>

<div class="page-header">
  <div>
    <div class="page-title"><i class="bi bi-cpu me-2"></i>Equipment</div>
    <div class="page-subtitle">All registered equipment across companies</div>
  </div>
  <?php if (in_array($_SESSION['role'], ['admin','front_desk'])): ?>
  <a href="<?= APP_URL ?>/index.php?page=equipment&action=create" class="btn btn-primary">
    <i class="bi bi-plus-circle me-1"></i>Add Equipment
  </a>
  <?php endif; ?>
</div>

<!-- Filters -->
<div class="card mb-3">
  <div class="card-body py-2">
    <form method="GET" class="d-flex gap-2 flex-wrap align-items-center">
      <input type="hidden" name="page" value="equipment">
      <select name="company_id" class="form-select form-select-sm" style="width:220px;">
        <option value="">All Companies</option>
        <?php foreach ($companies as $co): ?>
          <option value="<?= $co['company_id'] ?>" <?= (int)($_GET['company_id'] ?? 0) === (int)$co['company_id'] ? 'selected' : '' ?>><?= e($co['company_name']) ?></option>
        <?php endforeach; ?>
      </select>
      <input type="text" name="q" class="form-control form-control-sm" placeholder="Search name or serial..." value="<?= e($_GET['q'] ?? '') ?>" style="max-width:260px;">
      <button type="submit" class="btn btn-sm btn-primary"><i class="bi bi-search me-1"></i>Search</button>
      <a href="<?= APP_URL ?>/index.php?page=equipment" class="btn btn-sm btn-outline-secondary">Clear</a>
    </form>
  </div>
</div>

<div class="card">
  <div class="card-body p-0">
    <div class="table-responsive">
      <table class="table table-hover mb-0">
        <thead class="table-light">
          <tr><th>Equipment</th><th>Company</th><th>Manufacturer</th><th>Model</th><th>Serial No.</th><th>Range/Capacity</th><th>Actions</th></tr>
        </thead>
        <tbody>
        <?php foreach ($equipments as $eq): ?>
          <tr>
            <td class="fw-semibold"><?= e($eq['equipment_name']) ?></td>
            <td><?= e($eq['company_name']) ?></td>
            <td><?= e($eq['manufacturer'] ?? '—') ?></td>
            <td><?= e($eq['model'] ?? '—') ?></td>
            <td><code><?= e($eq['serial_number'] ?? '—') ?></code></td>
            <td><?= e($eq['range_capacity'] ?? '—') ?></td>
            <td class="text-nowrap">
              <?php if (in_array($_SESSION['role'], ['admin','front_desk'])): ?>
              <a href="<?= APP_URL ?>/index.php?page=equipment&action=edit&id=<?= $eq['equipment_id'] ?>" class="btn btn-sm btn-outline-secondary me-1"><i class="bi bi-pencil"></i></a>
              <a href="<?= APP_URL ?>/index.php?page=equipment&action=delete&id=<?= $eq['equipment_id'] ?>" class="btn btn-sm btn-outline-danger" data-confirm="Delete this equipment?"><i class="bi bi-trash"></i></a>
              <?php endif; ?>
            </td>
          </tr>
        <?php endforeach; ?>
        <?php if (empty($equipments)): ?>
          <tr><td colspan="7" class="text-center text-muted py-4">No equipment found.</td></tr>
        <?php endif; ?>
        </tbody>
      </table>
    </div>
  </div>
</div>

<?php include __DIR__ . '/../shared/footer.php'; ?>
