<?php use App\Helpers\Helper; ?>
<div class="d-flex justify-content-between align-items-center mb-4">
  <h1 class="admin-page-title">Maintenance Requests</h1>
  <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#newRequestModal"><i class="bi bi-plus me-1"></i>New Request</button>
</div>

<div class="data-card">
  <div class="table-responsive">
    <table class="table mb-0">
      <thead><tr><th>Ref</th><th>Title</th><th>Category</th><th>Priority</th><th>Status</th><th>Date</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">Unit: <?= Helper::e($r['unit_number']) ?></div>
          </td>
          <td class="small"><?= Helper::e($r['category']??'General') ?></td>
          <td><?= Helper::statusBadge($r['priority']) ?></td>
          <td><?= Helper::statusBadge($r['status']) ?></td>
          <td class="small text-muted"><?= Helper::date($r['created_at']) ?></td>
        </tr>
        <?php endforeach; ?>
        <?php if (empty($requests)): ?><tr><td colspan="6" class="text-center py-5 text-muted"><i class="bi bi-tools fs-1 d-block mb-2"></i>No requests yet.</td></tr><?php endif; ?>
      </tbody>
    </table>
  </div>
</div>

<!-- New Request Modal -->
<div class="modal fade" id="newRequestModal" tabindex="-1">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title fw-700">Submit Maintenance Request</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>
      <form method="POST" action="<?= APP_URL ?>/tenant/maintenance">
        <?= Helper::csrf() ?>
        <div class="modal-body">
          <div class="mb-3">
            <label class="form-label">Title / Issue *</label>
            <input type="text" name="title" class="form-control" placeholder="Brief description of issue" required>
          </div>
          <div class="mb-3">
            <label class="form-label">Category</label>
            <select name="category" class="form-select">
              <?php foreach (['Plumbing','Electrical','HVAC','IT/Network','Structural','Appliances','Pest Control','Cleaning','Other'] as $c): ?>
              <option><?= $c ?></option>
              <?php endforeach; ?>
            </select>
          </div>
          <div class="mb-3">
            <label class="form-label">Priority *</label>
            <select name="priority" class="form-select" required>
              <option value="low">Low — Not urgent</option>
              <option value="medium" selected>Medium — Needs attention soon</option>
              <option value="high">High — Urgent</option>
              <option value="emergency">Emergency — Immediate danger</option>
            </select>
          </div>
          <div class="mb-3">
            <label class="form-label">Full Description *</label>
            <textarea name="description" class="form-control" rows="4" placeholder="Describe the problem in detail..." required></textarea>
          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancel</button>
          <button type="submit" class="btn btn-primary"><i class="bi bi-send me-1"></i>Submit Request</button>
        </div>
      </form>
    </div>
  </div>
</div>
