<?php
$statusOf = function (?int $days): array {
    if ($days === null)      return ['bg-secondary text-uppercase', 'no due date'];
    if ($days < 0)           return ['bg-danger text-uppercase',    'overdue ' . abs($days) . 'd'];
    if ($days <= 30)         return ['bg-warning text-dark text-uppercase', 'due in ' . $days . 'd'];
    return ['bg-success text-uppercase', 'ok · ' . $days . 'd'];
};
?>
<div class="d-flex justify-content-between align-items-center mb-3">
    <h5 class="fw-bold mb-0"><i class="fas fa-ruler-combined me-2 text-primary"></i>Calibration</h5>
    <?php if ($canManage): ?>
        <a class="btn btn-sm btn-primary" href="<?= url('/Calibration/create') ?>"><i class="fas fa-plus me-1"></i>New Schedule</a>
    <?php endif; ?>
</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">Active</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">Overdue</div><div class="fw-bold fs-5 text-danger"><?= (int) $counts['overdue'] ?></div></div>
    <div class="rs-card p-2 text-center"><div class="text-muted small">Due ≤ 30d</div><div class="fw-bold fs-5 text-warning"><?= (int) $counts['due_soon'] ?></div></div>
    <div class="rs-card p-2 text-center"><div class="text-muted small">OK</div><div class="fw-bold fs-5 text-success"><?= (int) $counts['ok_count'] ?></div></div>
</div>

<form method="get" action="<?= url('/Calibration') ?>" 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 schedules</option>
                <option value="overdue"  <?= ($filters['status'] ?? '') === 'overdue'  ? 'selected' : '' ?>>Overdue only</option>
                <option value="due_soon" <?= ($filters['status'] ?? '') === 'due_soon' ? 'selected' : '' ?>>Due within 30 days</option>
                <option value="ok"       <?= ($filters['status'] ?? '') === 'ok'       ? 'selected' : '' ?>>OK</option>
            </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($schedules)): ?>
    <div class="rs-card p-4 text-center text-muted">No calibration schedules 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>Instrument</th><th>Asset</th><th>Standard</th>
                <th class="text-center">Interval</th><th class="text-center">Last</th>
                <th class="text-center">Next</th><th class="text-center">Status</th>
                <th class="text-end"></th>
            </tr></thead>
            <tbody>
                <?php foreach ($schedules as $c):
                    $badge = $statusOf($c['days_to_due'] !== null ? (int) $c['days_to_due'] : null);
                ?>
                    <tr>
                        <td><strong><?= e($c['name']) ?></strong><?php if ($c['standard']): ?><div class="small text-muted"><?= e($c['standard']) ?></div><?php endif; ?></td>
                        <td class="small"><a href="<?= url('/Assets/show/' . (int) $c['asset_id']) ?>"><?= e($c['asset_name']) ?></a></td>
                        <td class="small text-muted"><?= e($c['standard'] ?? '—') ?></td>
                        <td class="text-center small"><?= (int) $c['interval_days'] ?>d</td>
                        <td class="text-center small text-muted"><?= e($c['last_calibrated_at'] ?? '—') ?></td>
                        <td class="text-center small"><?= e($c['next_due_at'] ?? '—') ?></td>
                        <td class="text-center"><span class="badge <?= $badge[0] ?>" style="font-size:.6rem;"><?= e($badge[1]) ?></span></td>
                        <td class="text-end"><a class="btn btn-sm btn-outline-primary" href="<?= url('/Calibration/show/' . (int) $c['cal_schedule_id']) ?>"><i class="fas fa-eye"></i></a></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
<?php endif; ?>
