<?php
// public/admin/dashboard_state.php
session_start();
require_once __DIR__ . '/../../config/admin.php';
require_admin_role('STATE_ADMIN');
require_once __DIR__ . '/../../config/db.php';
$state_id = $_SESSION['admin_state_id'];
// Districts in state
$districts = $pdo->prepare('SELECT id, name FROM districts WHERE state_id=? ORDER BY name');
$districts->execute([$state_id]);
$districts = $districts->fetchAll();
// Participants in state
$participants = $pdo->prepare('SELECT COUNT(*) FROM participants WHERE district_id IN (SELECT id FROM districts WHERE state_id=?)');
$participants->execute([$state_id]);
$total_participants = $participants->fetchColumn();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>State Admin Dashboard</title>
    <link rel="stylesheet" href="/school-crm/public/assets/bootstrap.min.css">
</head>
<body>
<div class="container py-4">
    <h2>State Admin Dashboard</h2>
    <div class="row mb-4">
        <div class="col-md-4">
            <div class="card text-center">
                <div class="card-body">
                    <h5>Total Districts</h5>
                    <h2><?= count($districts) ?></h2>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card text-center">
                <div class="card-body">
                    <h5>Total Participants</h5>
                    <h2><?= $total_participants ?></h2>
                </div>
            </div>
        </div>
    </div>
    <h5>Districts in State</h5>
    <ul class="list-group mb-4">
        <?php foreach ($districts as $d): ?>
            <li class="list-group-item"> <?= htmlspecialchars($d['name']) ?> </li>
        <?php endforeach; ?>
    </ul>
    <a href="/school-crm/public/admin/admin_logout.php" class="btn btn-secondary">Logout</a>
</div>
</body>
</html>
