<?php
session_start();
require_once '../koneksi.php';
require_once '../auth.php';

// Fungsi pengecekan akses
cekAdminAtauGuru(); 

// Fungsi hitung jumlah data
function hitungJumlah($koneksi, $tabel, $kondisi = "") {
    $sql = "SELECT COUNT(*) as total FROM $tabel $kondisi";
    $query = mysqli_query($koneksi, $sql);
    $data  = mysqli_fetch_array($query);
    return $data['total'] ?? 0;
}

$jml_guru   = hitungJumlah($koneksi, 'master_guru');
$jml_siswa  = hitungJumlah($koneksi, 'master_siswa', "WHERE status = 'Aktif'"); 
$jml_rombel = hitungJumlah($koneksi, 'rombel');
$jml_mapel  = hitungJumlah($koneksi, 'master_mapel');

include 'layout_header.php';
?>

<style>
    /* CSS Khusus Dashboard */
    .dashboard-card { transition: transform 0.2s; border: none; }
    .dashboard-card:hover { transform: translateY(-5px); }
    .bg-dashboard { background-color: rgba(255, 255, 255, 0.95); border-radius: 10px; }
</style>

<div class="container-fluid mt-4">
    <h2 class="mb-4">Dashboard Admin</h2>
    <hr>

    <div class="row g-4 mb-5">
        <div class="col-md-3">
            <div class="card bg-primary text-white p-4 dashboard-card shadow">
                <div class="d-flex justify-content-between align-items-center">
                    <h5>Guru</h5>
                    <i class="fas fa-chalkboard-teacher fa-2x opacity-50"></i>
                </div>
                <h3><?php echo $jml_guru; ?></h3>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card bg-success text-white p-4 dashboard-card shadow">
                <div class="d-flex justify-content-between align-items-center">
                    <h5>Siswa</h5>
                    <i class="fas fa-user-graduate fa-2x opacity-50"></i>
                </div>
                <h3><?php echo $jml_siswa; ?></h3>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card bg-warning text-white p-4 dashboard-card shadow">
                <div class="d-flex justify-content-between align-items-center">
                    <h5>Rombel</h5>
                    <i class="fas fa-door-open fa-2x opacity-50"></i>
                </div>
                <h3><?php echo $jml_rombel; ?></h3>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card bg-info text-white p-4 dashboard-card shadow">
                <div class="d-flex justify-content-between align-items-center">
                    <h5>Mapel</h5>
                    <i class="fas fa-book-open fa-2x opacity-50"></i>
                </div>
                <h3><?php echo $jml_mapel; ?></h3>
            </div>
        </div>
    </div>

    <h4 class="mb-3 text-secondary"><i class="fas fa-database"></i> Akses Cepat Data Akademik</h4>
    <div class="card p-4 bg-dashboard shadow-sm border-0">
        <div class="row g-3">
            <?php
            $menu = [
                ['url' => '../guru/tujuan_pembelajaran.php', 'icon' => 'fa-book', 'title' => 'Tujuan Pembelajaran', 'class' => 'btn-outline-dark'],
                ['url' => '../guru/input_nilai.php', 'icon' => 'fa-edit', 'title' => 'Input Nilai', 'class' => 'btn-outline-danger'],
                ['url' => '../guru/leger.php', 'icon' => 'fa-table', 'title' => 'Leger Nilai', 'class' => 'btn-outline-primary'],
                ['url' => '../guru/input_nilai_ekskul.php', 'icon' => 'fa-futbol', 'title' => 'Nilai Ekskul', 'class' => 'btn-outline-success'],
                ['url' => '../guru/siswa.php', 'icon' => 'fa-user-friends', 'title' => 'Data Siswa', 'class' => 'btn-outline-info'],
                ['url' => '../guru/absensi_siswa.php', 'icon' => 'fa-calendar-check', 'title' => 'Rekap Absensi', 'class' => 'btn-outline-warning']
            ];
            foreach ($menu as $m) {
                echo "<div class='col-md-2 col-6'>
                        <a href='{$m['url']}' class='btn {$m['class']} w-100 py-3'>
                            <i class='fas {$m['icon']} fa-2x mb-2'></i><br>
                            <small>{$m['title']}</small>
                        </a>
                      </div>";
            }
            ?>
        </div>
    </div>
</div>

<?php include 'layout_footer.php'; ?>