<?php
$statusBadge = [
    'new'       => 'bg-primary',
    'approved'  => 'bg-info text-dark',
    'rejected'  => 'bg-secondary',
    'converted' => 'bg-success',
];
$priBadge = [
    'urgent' => 'bg-danger',
    'high'   => 'bg-warning text-dark',
    'medium' => 'bg-secondary',
    'low'    => 'bg-light text-muted border',
];
?>
<div class="d-flex justify-content-between align-items-center mb-3">
    <h5 class="fw-bold mb-0"><i class="fas fa-inbox me-2 text-primary"></i>Work Requests</h5>
    <a class="btn btn-sm btn-primary" href="<?= url('/WorkRequests/create') ?>"><i class="fas fa-plus me-1"></i>New Request</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">New</div><div class="fw-bold fs-5 text-primary"><?= (int) $counts['new_count'] ?></div></div>
    <div class="rs-card p-2 text-center"><div class="text-muted small">Approved</div><div class="fw-bold fs-5 text-info"><?= (int) $counts['approved_count'] ?></div></div>
    <div class="rs-card p-2 text-center"><div class="text-muted small">Converted</div><div class="fw-bold fs-5 text-success"><?= (int) $counts['converted_count'] ?></div></div>
    <div class="rs-card p-2 text-center"><div class="text-muted small">Rejected</div><div class="fw-bold fs-5 text-muted"><?= (int) $counts['rejected_count'] ?></div></div>
</div>

<form method="get" action="<?= url('/WorkRequests') ?>" class="rs-card p-2 mb-3">
    <div class="row g-2 align-items-end">
        <div class="col-md-4"><input type="search" name="q" class="form-control form-control-sm" placeholder="Search title / code…" value="<?= e($filters['q'] ?? '') ?>"></div>
        <div class="col-md-3">
            <select name="status" class="form-select form-select-sm">
                <option value="">All statuses</option>
                <?php foreach (\App\Models\WorkRequest::STATUSES as $s): ?>
                    <option value="<?= $s ?>" <?= ($filters['status'] ?? '') === $s ? 'selected' : '' ?>><?= $s ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="col-md-3">
            <select name="priority" class="form-select form-select-sm">
                <option value="">All priorities</option>
                <?php foreach (\App\Models\WorkRequest::PRIORITIES as $p): ?>
                    <option value="<?= $p ?>" <?= ($filters['priority'] ?? '') === $p ? 'selected' : '' ?>><?= $p ?></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($requests)): ?>
    <div class="rs-card p-4 text-center text-muted">No requests 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>Asset</th><th>Requestor</th>
                <th class="text-center">Priority</th><th class="text-center">Status</th>
                <th>Raised</th><th></th>
            </tr></thead>
            <tbody>
                <?php foreach ($requests as $r): ?>
                    <tr>
                        <td class="small text-muted">#<?= (int) $r['request_id'] ?></td>
                        <td>
                            <a href="<?= url('/WorkRequests/show/' . (int) $r['request_id']) ?>"><strong><?= e($r['title']) ?></strong></a>
                            <?php if (!empty($r['converted_wo_id'])): ?>
                                <a href="<?= url('/WorkOrders/show/' . (int) $r['converted_wo_id']) ?>" class="small ms-1 text-success" title="Converted WO">→ WO #<?= (int) $r['converted_wo_id'] ?></a>
                            <?php endif; ?>
                        </td>
                        <td class="small"><?= e($r['asset_name'] ?? '—') ?></td>
                        <td class="small"><?= e($r['requested_username'] ?? $r['requested_email'] ?? 'anonymous') ?></td>
                        <td class="text-center"><span class="badge <?= $priBadge[$r['priority']] ?? 'bg-secondary' ?> text-uppercase" style="font-size:.6rem;"><?= e($r['priority']) ?></span></td>
                        <td class="text-center"><span class="badge <?= $statusBadge[$r['status']] ?? 'bg-secondary' ?> text-uppercase" style="font-size:.6rem;"><?= e($r['status']) ?></span></td>
                        <td class="small text-muted"><?= e(substr((string) $r['created_at'], 0, 16)) ?></td>
                        <td class="text-end">
                            <a class="btn btn-sm btn-outline-primary" href="<?= url('/WorkRequests/show/' . (int) $r['request_id']) ?>"><i class="fas fa-eye"></i></a>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
<?php endif; ?>
