<?php
$stores = isset($stores) ? $stores : array();
$schedules = isset($schedules) ? $schedules : array();
$logs = isset($logs) ? $logs : array();
$staff = isset($staff) ? $staff : 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-tower-broadcast text-primary me-2"></i>Live Compliance Monitor</h4>
            <p class="text-muted mb-0">Real-time status of store check-in tasks and scheduled routines.</p>
        </div>
        <div>
            <button onclick="window.location.reload();" class="btn btn-outline-primary">
                <i class="fa-solid fa-arrows-rotate me-1"></i> Refresh Live Feed
            </button>
        </div>
    </div>

    <!-- Store-by-Store Live Cards -->
    <div class="row g-4 mb-4">
        <?php foreach ($stores as $sId => $s): ?>
            <?php
            // Find recent logs for this store
            $stLogs = array();
            foreach ($logs as $lId => $l) {
                if (isset($l['storeId']) && $l['storeId'] === $sId) {
                    $stLogs[$lId] = $l;
                }
            }
            $isDone = !empty($stLogs);
            ?>
            <div class="col-md-6 col-xl-4">
                <div class="card border-0 shadow-sm rounded-4 h-100">
                    <div class="card-body p-4">
                        <div class="d-flex justify-content-between align-items-start mb-3">
                            <div>
                                <h5 class="fw-bold text-dark mb-1"><?php echo htmlspecialchars(isset($s['name']) ? $s['name'] : $sId); ?></h5>
                                <div class="small text-muted"><i class="fa-solid fa-location-dot me-1"></i><?php echo htmlspecialchars(isset($s['city']) ? $s['city'] : 'Islamabad'); ?></div>
                            </div>
                            <span class="badge <?php echo $isDone ? 'bg-success bg-opacity-10 text-success' : 'bg-warning bg-opacity-10 text-warning'; ?> rounded-pill px-3 py-1">
                                <?php echo $isDone ? 'Checked In' : 'Pending Check-In'; ?>
                            </span>
                        </div>

                        <?php if ($isDone): ?>
                            <?php $latest = reset($stLogs); ?>
                            <div class="bg-light p-3 rounded-3 mb-3">
                                <div class="d-flex justify-content-between small text-muted mb-1">
                                    <span>Staff ID:</span>
                                    <strong class="text-dark"><?php echo htmlspecialchars(isset($latest['staffId']) ? $latest['staffId'] : 'Staff'); ?></strong>
                                </div>
                                <div class="d-flex justify-content-between small text-muted mb-1">
                                    <span>Submitted At:</span>
                                    <strong class="text-dark"><?php echo isset($latest['submittedAt']) ? date('h:i A', $latest['submittedAt'] / 1000) : '-'; ?></strong>
                                </div>
                                <div class="d-flex justify-content-between small text-muted">
                                    <span>Tasks Verified:</span>
                                    <strong class="text-success"><i class="fa-solid fa-check-double me-1"></i><?php echo isset($latest['tasks']) ? count($latest['tasks']) : 0; ?> Completed</strong>
                                </div>
                            </div>
                            <a href="<?php echo base_url('admin/store_details/' . $sId); ?>" class="btn btn-sm btn-outline-primary w-100">
                                View Store Submission Details
                            </a>
                        <?php else: ?>
                            <div class="p-3 text-center text-muted small bg-light rounded-3 mb-3">
                                <i class="fa-regular fa-clock me-1"></i> Awaiting staff check-in submission today.
                            </div>
                            <a href="<?php echo base_url('admin/store_details/' . $sId); ?>" class="btn btn-sm btn-light w-100">
                                Store Details
                            </a>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
</div>
