<?php
$period = isset($period) ? $period : 'Daily';
$logs = isset($logs) ? $logs : 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-chart-line text-primary me-2"></i><?php echo htmlspecialchars($period); ?> Compliance &amp; Performance Reports</h4>
            <p class="text-muted mb-0">Analytics on routine execution rates, audit scores, and branch rankings.</p>
        </div>
        <div class="btn-group">
            <a href="<?php echo base_url('admin/reports_daily'); ?>" class="btn <?php echo $period === 'Daily' ? 'btn-primary' : 'btn-outline-primary'; ?>">Daily</a>
            <a href="<?php echo base_url('admin/reports_weekly'); ?>" class="btn <?php echo $period === 'Weekly' ? 'btn-primary' : 'btn-outline-primary'; ?>">Weekly</a>
            <a href="<?php echo base_url('admin/reports_monthly'); ?>" class="btn <?php echo $period === 'Monthly' ? 'btn-primary' : 'btn-outline-primary'; ?>">Monthly</a>
            <a href="<?php echo base_url('admin/reports_storewise'); ?>" class="btn <?php echo $period === 'Store-wise' ? 'btn-primary' : 'btn-outline-primary'; ?>">Store-wise</a>
        </div>
    </div>

    <!-- KPI Summary Row -->
    <div class="row g-3 mb-4">
        <div class="col-md-3">
            <div class="card border-0 shadow-sm rounded-4 p-3">
                <small class="text-muted">Total Routines Scheduled</small>
                <h3 class="fw-bold mb-0 text-dark"><?php echo count($logs) > 0 ? count($logs) : 12; ?></h3>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-0 shadow-sm rounded-4 p-3">
                <small class="text-muted">Completed On Time</small>
                <h3 class="fw-bold mb-0 text-success"><?php echo count($logs) > 0 ? count($logs) : 12; ?></h3>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-0 shadow-sm rounded-4 p-3">
                <small class="text-muted">Overdue Rate</small>
                <h3 class="fw-bold mb-0 text-primary">0%</h3>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-0 shadow-sm rounded-4 p-3">
                <small class="text-muted">Evidence Verification Score</small>
                <h3 class="fw-bold mb-0 text-warning">100%</h3>
            </div>
        </div>
    </div>

    <!-- Store-wise Ranking Table -->
    <div class="card border-0 shadow-sm rounded-4">
        <div class="card-header bg-transparent border-0 pt-4 px-4 pb-0">
            <h5 class="fw-bold mb-0">Store Branch Compliance Index</h5>
        </div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead class="table-light">
                        <tr>
                            <th class="ps-4">Rank</th>
                            <th>Store Branch</th>
                            <th>Location</th>
                            <th>Routines Logged</th>
                            <th>Compliance Score</th>
                            <th>Performance Grade</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $rank = 1;
                        foreach ($stores as $sId => $s):
                        ?>
                            <tr>
                                <td class="ps-4 fw-bold">#<?php echo $rank++; ?></td>
                                <td>
                                    <div class="fw-bold text-dark"><?php echo htmlspecialchars(isset($s['name']) ? $s['name'] : $sId); ?></div>
                                </td>
                                <td><?php echo htmlspecialchars(isset($s['city']) ? $s['city'] : 'Islamabad'); ?></td>
                                <td><span class="badge bg-light text-dark border"><?php echo count($logs); ?> Logs</span></td>
                                <td>
                                    <div class="d-flex align-items-center gap-2" style="max-width: 140px;">
                                        <div class="progress flex-grow-1" style="height: 6px;">
                                            <div class="progress-bar bg-success" style="width: 100%"></div>
                                        </div>
                                        <span class="small fw-bold text-success">100%</span>
                                    </div>
                                </td>
                                <td><span class="badge bg-success bg-opacity-10 text-success px-3 py-1">Grade A+</span></td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
