<?= $this->extend('layouts/main') ?>
<?= $this->section('content') ?>
<div class="page-header"><div><h1><i class="bi bi-headset me-2 text-primary"></i>Help Desk</h1></div>
<a href="<?= base_url('request') ?>" target="_blank" class="btn btn-fm-outline btn-sm">Public Portal</a></div>

<?php if(!empty($kpi)): ?>
<div class="row g-3 mb-3">
  <div class="col-6 col-md-3"><div class="kpi-card kpi-orange"><div class="kpi-label">Pending</div><div class="kpi-value"><?= $kpi['pending']??0 ?></div></div></div>
  <div class="col-6 col-md-3"><div class="kpi-card kpi-blue"><div class="kpi-label">Verified</div><div class="kpi-value"><?= $kpi['reviewed']??0 ?></div></div></div>
  <div class="col-6 col-md-3"><div class="kpi-card kpi-green"><div class="kpi-label">Converted</div><div class="kpi-value"><?= $kpi['converted']??0 ?></div></div></div>
  <div class="col-6 col-md-3"><div class="kpi-card kpi-red"><div class="kpi-label">Rejected</div><div class="kpi-value"><?= $kpi['rejected']??0 ?></div></div></div>
</div>
<?php endif; ?>

<div class="fm-card mb-3"><div class="fm-card-body"><div class="d-flex gap-2 flex-wrap">
<?php foreach(['','pending','reviewed','converted','rejected'] as $s): ?>
<a href="?status=<?= $s ?>" class="btn btn-sm <?= $filterStatus===$s?'btn-fm-primary':'btn-fm-outline' ?>"><?= $s?ucfirst($s):'All' ?></a>
<?php endforeach; ?></div></div></div>
<div class="fm-card"><div class="card-header-fm"><h5><i class="bi bi-inbox"></i>Maintenance Requests (<?= count($requests) ?>)</h5></div>
<div class="fm-card-body p-0"><div class="table-responsive">
<table class="fm-table"><thead><tr><th>Ticket #</th><th>Requester</th><th>Facility</th><th>Category</th><th>Description</th><th>Priority</th><th>Status</th><th>Date</th><th>Actions</th></tr></thead><tbody>
<?php foreach($requests as $r): ?><tr>
<td class="fw-semibold"><a href="<?= base_url('helpdesk/view/'.$r['id']) ?>" class="text-primary"><?= esc($r['ticket_number']) ?></a></td>
<td><div class="small fw-semibold"><?= esc($r['requester_name']) ?></div><div class="x-small text-muted"><?= esc($r['requester_email']??'') ?></div></td>
<td class="small"><?= esc($r['facility_name']??'—') ?></td>
<td class="small"><?= esc($r['category']??'—') ?></td>
<td class="small"><?= esc(substr($r['description'],0,40)) ?>...</td>
<td><span class="fm-badge badge-priority-<?= esc($r['priority']) ?>"><?= ucfirst($r['priority']) ?></span></td>
<td><span class="fm-badge badge-status-<?= esc($r['status']) ?>"><?= ucfirst($r['status']) ?></span></td>
<td class="small text-muted"><?= date('d M Y',strtotime($r['created_at'])) ?></td>
<td><div class="d-flex gap-1">
<a href="<?= base_url('helpdesk/view/'.$r['id']) ?>" class="btn-action bg-primary bg-opacity-10 text-primary"><i class="bi bi-eye"></i></a>
<?php if($r['status']==='pending'||$r['status']==='reviewed'): ?>
<a href="<?= base_url('helpdesk/view/'.$r['id']) ?>" class="btn-action bg-success bg-opacity-10 text-success" title="Convert to WO"><i class="bi bi-arrow-right-circle"></i></a>
<?php endif; ?>
</div></td>
</tr><?php endforeach; ?>
<?php if(empty($requests)): ?><tr><td colspan="9" class="text-center py-4 text-muted">No requests found</td></tr><?php endif; ?>
</tbody></table></div>
<?= view('partials/pagination') ?>
</div></div>
<?= $this->endSection() ?>
