<div class="d-flex justify-content-between align-items-center mb-3">
    <h5 class="fw-bold mb-0"><i class="fas fa-route me-2 text-primary"></i>Operator Rounds</h5>
    <?php if ($canManage): ?>
        <a class="btn btn-sm btn-primary" href="<?= url('/Rounds/createTemplate') ?>"><i class="fas fa-plus me-1"></i>New Round</a>
    <?php endif; ?>
</div>

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

<?php
$byStatus = ['overdue' => [], 'never_run' => [], 'on_time' => []];
foreach (($dueStatus ?? []) as $d) { $byStatus[$d['status']][] = $d; }
$overdueCount   = count($byStatus['overdue']);
$neverRunCount  = count($byStatus['never_run']);
$onTimeCount    = count($byStatus['on_time']);
?>
<div class="row g-2 mb-3">
    <div class="col-md-3"><div class="rs-card p-2 text-center"><div class="text-muted small">Active rounds</div><div class="fw-bold fs-5"><?= count($dueStatus ?? []) ?></div></div></div>
    <div class="col-md-3"><div class="rs-card p-2 text-center"><div class="text-muted small">Overdue</div><div class="fw-bold fs-5 text-danger"><?= $overdueCount ?></div></div></div>
    <div class="col-md-3"><div class="rs-card p-2 text-center"><div class="text-muted small">Never run</div><div class="fw-bold fs-5 text-warning"><?= $neverRunCount ?></div></div></div>
    <div class="col-md-3"><div class="rs-card p-2 text-center"><div class="text-muted small">On time</div><div class="fw-bold fs-5 text-success"><?= $onTimeCount ?></div></div></div>
</div>

<?php if ($overdueCount > 0 || $neverRunCount > 0): ?>
    <div class="rs-card p-3 mb-3" style="border-left: 4px solid var(--rs-danger);">
        <h6 class="fw-bold mb-2"><i class="fas fa-triangle-exclamation me-1 text-danger"></i>Action needed today</h6>
        <div class="row g-2">
            <?php foreach (array_merge($byStatus['overdue'], $byStatus['never_run']) as $d):
                $isOverdue = $d['status'] === 'overdue';
                $badge = $isOverdue ? 'bg-danger' : 'bg-warning text-dark';
                $label = $isOverdue
                    ? 'overdue by ' . round(($d['hours_since'] ?? 0) - 24, 1) . 'h'
                    : 'never run';
            ?>
                <div class="col-md-6">
                    <div class="d-flex align-items-center justify-content-between border rounded px-2 py-1">
                        <div>
                            <strong><?= e($d['name']) ?></strong>
                            <span class="small text-muted"><?= e($d['frequency']) ?></span>
                        </div>
                        <div class="d-flex gap-2 align-items-center">
                            <span class="badge <?= $badge ?>"><?= e($label) ?></span>
                            <form method="post" action="<?= url('/Rounds/start/' . (int) $d['round_template_id']) ?>" class="d-inline">
                                <button type="submit" class="btn btn-sm btn-primary"><i class="fas fa-play me-1"></i>Start</button>
                            </form>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
    </div>
<?php endif; ?>

<p class="text-muted small">
    A round is a fixed route through a set of assets — the operator walks the route, records a reading
    or observation at each stop, and marks it OK or flagged. Great for shift patrols and daily plant sweeps.
</p>

<div class="rs-card p-3 mb-3">
    <h6 class="fw-bold mb-2"><i class="fas fa-list me-1"></i>Available Rounds</h6>
    <?php if (empty($templates)): ?>
        <p class="text-muted small mb-0">No rounds defined yet.</p>
    <?php else: ?>
        <table class="table table-sm table-hover mb-0 align-middle">
            <thead><tr><th>Name</th><th>Frequency</th><th class="text-center">Stops</th><th>Last run</th><th class="text-end" style="width:270px;">Actions</th></tr></thead>
            <tbody>
                <?php foreach ($templates as $t): ?>
                    <tr>
                        <td>
                            <strong><?= e($t['name']) ?></strong>
                            <?php if (!$t['is_active']): ?><span class="badge bg-secondary ms-1">inactive</span><?php endif; ?>
                            <?php if (!empty($t['description'])): ?><div class="small text-muted"><?= e($t['description']) ?></div><?php endif; ?>
                        </td>
                        <td class="small"><?= e($t['frequency']) ?></td>
                        <td class="text-center small"><?= (int) $t['stop_count'] ?></td>
                        <td class="small text-muted"><?= e(substr((string) ($t['last_run_at'] ?? ''), 0, 16) ?: '—') ?></td>
                        <td class="text-end">
                            <?php if ((int) $t['stop_count'] > 0 && $t['is_active']): ?>
                                <form method="post" action="<?= url('/Rounds/start/' . (int) $t['round_template_id']) ?>" class="d-inline">
                                    <button type="submit" class="btn btn-sm btn-success"><i class="fas fa-play me-1"></i>Start</button>
                                </form>
                            <?php endif; ?>
                            <a class="btn btn-sm btn-outline-secondary" href="<?= url('/Rounds/trends/' . (int) $t['round_template_id']) ?>" title="Reading trends"><i class="fas fa-chart-line"></i></a>
                            <?php if ($canManage): ?>
                                <a class="btn btn-sm btn-outline-primary" href="<?= url('/Rounds/editTemplate/' . (int) $t['round_template_id']) ?>"><i class="fas fa-pen"></i></a>
                            <?php endif; ?>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    <?php endif; ?>
</div>

<div class="rs-card p-3">
    <h6 class="fw-bold mb-2"><i class="fas fa-clock-rotate-left me-1"></i>Recent Runs</h6>
    <?php if (empty($recent)): ?>
        <p class="text-muted small mb-0">No rounds have been run yet.</p>
    <?php else: ?>
        <table class="table table-sm table-hover mb-0 align-middle">
            <thead><tr><th>Route</th><th>Operator</th><th>Started</th><th class="text-center">Progress</th><th class="text-center">Flagged</th><th class="text-center">Status</th><th class="text-end"></th></tr></thead>
            <tbody>
                <?php foreach ($recent as $r):
                    $badge = ['in_progress' => 'bg-primary', 'completed' => 'bg-success', 'abandoned' => 'bg-secondary'][$r['status']] ?? 'bg-secondary';
                ?>
                    <tr>
                        <td><?= e($r['template_name']) ?></td>
                        <td class="small"><?= e($r['operator_username'] ?? '—') ?></td>
                        <td class="small text-muted"><?= e(substr((string) $r['started_at'], 0, 16)) ?></td>
                        <td class="text-center small"><?= (int) $r['stops_done'] ?> / <?= (int) $r['stops_total'] ?></td>
                        <td class="text-center small"><?= (int) $r['stops_flagged'] > 0 ? '<span class="text-danger fw-bold">' . (int) $r['stops_flagged'] . '</span>' : '—' ?></td>
                        <td class="text-center"><span class="badge <?= $badge ?> text-uppercase" style="font-size:.6rem;"><?= e($r['status']) ?></span></td>
                        <td class="text-end"><a class="btn btn-sm btn-outline-primary" href="<?= url('/Rounds/run/' . (int) $r['run_id']) ?>"><i class="fas fa-eye"></i></a></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    <?php endif; ?>
</div>
