<?php
$schedules = isset($schedules) ? $schedules : array();
$stores = isset($stores) ? $stores : array();
?>

<div class="container-fluid py-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h4 class="fw-bold mb-1"><i class="fa-solid fa-triangle-exclamation text-danger me-2"></i>Overdue Check-In Tasks</h4>
            <p class="text-muted mb-0">Missed deadlines and pending store routine alerts requiring escalation.</p>
        </div>
        <a href="<?php echo base_url('admin/escalation_settings'); ?>" class="btn btn-outline-danger">
            <i class="fa-solid fa-bell me-1"></i> Escalation Rules
        </a>
    </div>

    <div class="card border-0 shadow-sm rounded-4">
        <div class="card-body p-4">
            <div class="row g-4">
                <?php
                $hasOverdue = false;
                foreach ($schedules as $id => $sc) {
                    if (!empty($sc['storeStatus'])) {
                        foreach ($sc['storeStatus'] as $sId => $status) {
                            if (strtolower((string)$status) === 'pending') {
                                $hasOverdue = true;
                                $sName = isset($stores[$sId]['name']) ? $stores[$sId]['name'] : $sId;
                                ?>
                                <div class="col-md-6 col-xl-4">
                                    <div class="card border border-warning bg-warning bg-opacity-10 rounded-3 p-3">
                                        <div class="d-flex justify-content-between align-items-start mb-2">
                                            <h6 class="fw-bold text-dark mb-0"><?php echo htmlspecialchars($sName); ?></h6>
                                            <span class="badge bg-danger rounded-pill">Pending Check-In</span>
                                        </div>
                                        <div class="small text-muted mb-2">
                                            <i class="fa-solid fa-clipboard-list me-1"></i><?php echo htmlspecialchars(isset($sc['title']) ? $sc['title'] : 'Check-In Routine'); ?>
                                        </div>
                                        <div class="small text-muted mb-3">
                                            <i class="fa-regular fa-clock me-1"></i>Scheduled: <?php echo isset($sc['scheduledAt']) ? date('h:i A', $sc['scheduledAt'] / 1000) : 'Daily'; ?>
                                        </div>
                                        <a href="<?php echo base_url('admin/store_details/' . $sId); ?>" class="btn btn-sm btn-outline-danger w-100">
                                            Notify Store Supervisor
                                        </a>
                                    </div>
                                </div>
                                <?php
                            }
                        }
                    }
                }
                if (!$hasOverdue):
                ?>
                    <div class="col-12 text-center py-5 text-success">
                        <i class="fa-solid fa-circle-check fs-1 mb-3 d-block"></i>
                        <h5>All store branches are up to date!</h5>
                        <p class="text-muted">No overdue or pending check-in routines detected.</p>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>
