<?php use App\Helpers\Helper; ?>
<div class="d-flex justify-content-between align-items-center mb-4">
  <div>
    <h1 class="admin-page-title">Maintenance Requests</h1>
  </div>
</div>

<!-- Stats -->
<div class="row g-3 mb-4">
  <?php foreach ([
    ['label'=>'Open','value'=>$stats['open'],'color'=>'blue','icon'=>'bi-door-open'],
    ['label'=>'In Progress','value'=>$stats['in_progress'],'color'=>'orange','icon'=>'bi-tools'],
    ['label'=>'Completed','value'=>$stats['completed'],'color'=>'green','icon'=>'bi-check-circle'],
    ['label'=>'Emergency','value'=>$stats['emergency'],'color'=>'red','icon'=>'bi-exclamation-triangle'],
  ] as $s): ?>
  <div class="col-md-3">
    <div class="admin-stat-card">
      <div class="stat-icon-wrap <?= $s['color'] ?>"><i class="bi <?= $s['icon'] ?>"></i></div>
      <div><div class="stat-val"><?= $s['value'] ?></div><div class="stat-lbl"><?= $s['label'] ?></div></div>
    </div>
  </div>
  <?php endforeach; ?>
</div>

<!-- Filters -->
<div class="form-card mb-3">
  <form method="GET" class="d-flex gap-2 flex-wrap">
    <select name="priority" class="form-select form-select-sm" style="width:160px">
      <option value="">All Priorities</option>
      <?php foreach (['emergency','high','medium','low'] as $p): ?>
      <option value="<?= $p ?>" <?= $priority===$p?'selected':'' ?>><?= ucfirst($p) ?></option>
      <?php endforeach; ?>
    </select>
    <select name="status" class="form-select form-select-sm" style="width:160px">
      <option value="">All Status</option>
      <?php foreach (['open','in_progress','on_hold','completed','cancelled'] as $s): ?>
      <option value="<?= $s ?>" <?= $status===$s?'selected':'' ?>><?= ucfirst(str_replace('_',' ',$s)) ?></option>
      <?php endforeach; ?>
    </select>
    <button class="btn btn-sm btn-primary">Filter</button>
    <a href="<?= APP_URL ?>/admin/maintenance" class="btn btn-sm btn-outline-secondary">Clear</a>
  </form>
</div>

<div class="data-card">
  <div class="table-responsive">
    <table class="table mb-0">
      <thead><tr><th>Ref</th><th>Issue</th><th>Property / Unit</th><th>Tenant</th><th>Priority</th><th>Status</th><th>Reported</th><th>Action</th></tr></thead>
      <tbody>
        <?php foreach ($requests as $r): ?>
        <tr class="priority-<?= $r['priority'] ?>">
          <td class="fw-600 text-primary small"><?= Helper::e($r['request_number']) ?></td>
          <td>
            <div class="fw-600 small"><?= Helper::e($r['title']) ?></div>
            <div class="text-muted" style="font-size:0.72rem"><?= Helper::e($r['category'] ?? '') ?></div>
          </td>
          <td class="small"><?= Helper::e($r['property_name']) ?> / Unit <?= Helper::e($r['unit_number']) ?></td>
          <td class="small"><?= Helper::e($r['tenant_name'] ?? '—') ?></td>
          <td><?= Helper::statusBadge($r['priority']) ?></td>
          <td><?= Helper::statusBadge($r['status']) ?></td>
          <td class="small text-muted"><?= Helper::timeAgo($r['created_at']) ?></td>
          <td><a href="<?= APP_URL ?>/admin/maintenance/<?= $r['id'] ?>" class="btn btn-sm btn-outline-primary">View</a></td>
        </tr>
        <?php endforeach; ?>
        <?php if (empty($requests)): ?>
        <tr><td colspan="8" class="text-center py-5 text-muted"><i class="bi bi-tools fs-1 d-block mb-2"></i>No maintenance requests.</td></tr>
        <?php endif; ?>
      </tbody>
    </table>
  </div>
</div>
