<?php
$statusBadge = [
    'draft'     => 'bg-secondary',
    'requested' => 'bg-primary',
    'approved'  => 'bg-info text-dark',
    'active'    => 'bg-success',
    'rejected'  => 'bg-danger',
    'closed'    => 'bg-secondary',
    'expired'   => 'bg-dark',
];
?>
<div class="d-flex justify-content-between align-items-center mb-3">
    <h5 class="fw-bold mb-0"><i class="fas fa-shield-halved me-2 text-primary"></i>Permits to Work</h5>
    <a class="btn btn-sm btn-primary" href="<?= url('/Permits/create') ?>"><i class="fas fa-plus me-1"></i>Request Permit</a>
</div>

<?php if (!empty($flash)): ?><div class="alert alert-info py-2 small"><?= e($flash) ?></div><?php endif; ?>

<div class="kpi-grid mb-3">
    <div class="rs-card p-2 text-center"><div class="text-muted small">Total</div><div class="fw-bold fs-5"><?= (int) $counts['total'] ?></div></div>
    <div class="rs-card p-2 text-center"><div class="text-muted small">Pending Review</div><div class="fw-bold fs-5 text-primary"><?= (int) $counts['pending'] ?></div></div>
    <div class="rs-card p-2 text-center"><div class="text-muted small">Active</div><div class="fw-bold fs-5 text-success"><?= (int) $counts['active'] ?></div></div>
    <div class="rs-card p-2 text-center"><div class="text-muted small">Expired</div><div class="fw-bold fs-5 text-warning"><?= (int) $counts['expired'] ?></div></div>
    <div class="rs-card p-2 text-center"><div class="text-muted small">Closed</div><div class="fw-bold fs-5 text-muted"><?= (int) $counts['closed'] ?></div></div>
</div>

<form method="get" action="<?= url('/Permits') ?>" class="rs-card p-2 mb-3">
    <div class="row g-2 align-items-end">
        <div class="col-md-3">
            <select name="status" class="form-select form-select-sm">
                <option value="">All statuses</option>
                <?php foreach (\App\Models\Permit::STATUSES as $s): ?>
                    <option value="<?= $s ?>" <?= ($filters['status'] ?? '') === $s ? 'selected' : '' ?>><?= $s ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="col-md-3">
            <select name="permit_type_id" class="form-select form-select-sm">
                <option value="">All types</option>
                <?php foreach ($types as $t): ?>
                    <option value="<?= (int) $t['permit_type_id'] ?>" <?= (int) ($filters['permit_type_id'] ?? 0) === (int) $t['permit_type_id'] ? 'selected' : '' ?>><?= e($t['name']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="col-md-2"><button type="submit" class="btn btn-sm btn-outline-primary w-100"><i class="fas fa-filter me-1"></i>Filter</button></div>
    </div>
</form>

<?php if (empty($permits)): ?>
    <div class="rs-card p-4 text-center text-muted">No permits match your filter.</div>
<?php else: ?>
    <div class="rs-card p-0">
        <table class="table table-sm table-hover mb-0 align-middle">
            <thead class="table-light"><tr>
                <th>#</th><th>Title</th><th>Type</th><th>Asset / Site</th><th>WO</th>
                <th>Valid</th><th class="text-center">Status</th><th></th>
            </tr></thead>
            <tbody>
                <?php foreach ($permits as $p): ?>
                    <tr>
                        <td class="small text-muted">#<?= (int) $p['permit_id'] ?></td>
                        <td>
                            <a href="<?= url('/Permits/show/' . (int) $p['permit_id']) ?>"><strong><?= e($p['title']) ?></strong></a>
                            <?php if (!empty($p['code'])): ?><code class="small text-muted ms-1"><?= e($p['code']) ?></code><?php endif; ?>
                        </td>
                        <td class="small">
                            <?php if ($p['type_icon']): ?><i class="fas <?= e($p['type_icon']) ?> me-1"></i><?php endif; ?>
                            <?= e($p['type_name']) ?>
                        </td>
                        <td class="small text-muted"><?= e($p['asset_name'] ?? $p['site_name'] ?? '—') ?></td>
                        <td class="small"><?php if (!empty($p['work_order_id'])): ?><a href="<?= url('/WorkOrders/show/' . (int) $p['work_order_id']) ?>">WO #<?= (int) $p['work_order_id'] ?></a><?php else: ?>—<?php endif; ?></td>
                        <td class="small text-muted"><?= e(substr((string) ($p['valid_from'] ?? ''), 0, 10)) ?> → <?= e(substr((string) ($p['valid_to'] ?? ''), 0, 10)) ?></td>
                        <td class="text-center"><span class="badge <?= $statusBadge[$p['status']] ?? 'bg-secondary' ?> text-uppercase" style="font-size:.6rem;"><?= e($p['status']) ?></span></td>
                        <td class="text-end"><a class="btn btn-sm btn-outline-primary" href="<?= url('/Permits/show/' . (int) $p['permit_id']) ?>"><i class="fas fa-eye"></i></a></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
<?php endif; ?>
