<?php $pageTitle = 'Calibration Requests'; include __DIR__ . '/header.php'; ?>

<div class="page-header">
  <div>
    <div class="page-title"><i class="bi bi-clipboard-check me-2"></i>Calibration Requests</div>
    <div class="page-subtitle"><?= count($requests) ?> request(s) found</div>
  </div>
  <?php if (in_array($_SESSION['role'], ['admin','front_desk'])): ?>
  <a href="<?= APP_URL ?>/index.php?page=requests&action=create" class="btn btn-primary">
    <i class="bi bi-plus-circle me-1"></i>New Request
  </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="requests">
      <input type="text" name="q" class="form-control form-control-sm" placeholder="Search company or request no..." value="<?= e($_GET['q'] ?? '') ?>" style="max-width:280px;">
      <select name="status" class="form-select form-select-sm" style="width:160px;">
        <option value="">All Statuses</option>
        <option value="pending"     <?= ($_GET['status']??'') === 'pending'      ? 'selected':'' ?>>Pending</option>
        <option value="in_progress" <?= ($_GET['status']??'') === 'in_progress'  ? 'selected':'' ?>>In Progress</option>
        <option value="completed"   <?= ($_GET['status']??'') === 'completed'    ? 'selected':'' ?>>Completed</option>
        <option value="cancelled"   <?= ($_GET['status']??'') === 'cancelled'    ? 'selected':'' ?>>Cancelled</option>
      </select>
      <button type="submit" class="btn btn-sm btn-primary"><i class="bi bi-search me-1"></i>Filter</button>
      <a href="<?= APP_URL ?>/index.php?page=requests" 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>Request No.</th><th>Company</th><th>Equipment</th><th>Laboratory</th><th>Priority</th><th>Status</th><th>Date</th><th>Actions</th></tr>
        </thead>
        <tbody>
        <?php foreach ($requests as $r): ?>
          <tr>
            <td><code><?= e($r['request_number']) ?></code></td>
            <td class="fw-semibold"><?= e($r['company_name']) ?></td>
            <td><?= e($r['equipment_name']) ?></td>
            <td><small><?= e($r['lab_name']) ?></small></td>
            <td>
              <?php if ($r['priority'] === 'urgent'): ?>
                <span class="badge bg-danger"><i class="bi bi-exclamation-triangle me-1"></i>Urgent</span>
              <?php else: ?>
                <span class="badge bg-light text-dark">Normal</span>
              <?php endif; ?>
            </td>
            <td>
              <span class="badge badge-<?= $r['status'] ?>">
                <?= ucfirst(str_replace('_',' ', $r['status'])) ?>
              </span>
            </td>
            <td><small><?= date('d M Y', strtotime($r['date_created'])) ?></small></td>
            <td>
              <a href="<?= APP_URL ?>/index.php?page=requests&action=view&id=<?= $r['request_id'] ?>" class="btn btn-sm btn-outline-primary">
                <i class="bi bi-eye me-1"></i>View
              </a>
            </td>
          </tr>
        <?php endforeach; ?>
        <?php if (empty($requests)): ?>
          <tr><td colspan="8" class="text-center text-muted py-5">
            <i class="bi bi-inbox fs-2 d-block mb-2"></i>No requests found.
          </td></tr>
        <?php endif; ?>
        </tbody>
      </table>
    </div>
  </div>
</div>

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