<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);

if(!isset($_SESSION['username'])){
    header("Location: Login.php");
    exit();
}

require "koneksi.php";
$con = open_connection();

// Hitung total data
$total_murid = mysqli_fetch_assoc(
    mysqli_query($con, "SELECT COUNT(*) as jumlah FROM murid")
)['jumlah'];

$total_guru = mysqli_fetch_assoc(
    mysqli_query($con, "SELECT COUNT(*) as jumlah FROM guru")
)['jumlah'];

$total_perkembangan = mysqli_fetch_assoc(
    mysqli_query($con, "SELECT COUNT(*) as total FROM perkembangan")
)['total'];

// Ambil 4 aktivitas terbaru
$log_aktivitas = mysqli_query($con,
    "SELECT * FROM log_aktivitas ORDER BY waktu DESC LIMIT 4");

// Ubah waktu jadi format "X yang lalu"
function waktu_lalu($waktu){
    $selisih = time() - strtotime($waktu);

    if($selisih < 60) return "Baru saja";
    if($selisih < 3600) return floor($selisih / 60) . " menit yang lalu";
    if($selisih < 86400) return floor($selisih / 3600) . " jam yang lalu";
    if($selisih < 2592000) return floor($selisih / 86400) . " hari yang lalu";
    return date('d M Y', strtotime($waktu));
}

// Ikon & warna berdasarkan tipe aktivitas
function icon_aktivitas($tipe){
    switch($tipe){
        case 'murid':
            return ['bi-person-fill', '#e5f6ec', '#2fa66a'];
        case 'guru':
            return ['bi-person-workspace', '#fdeaea', '#e2566d'];
        case 'perkembangan':
            return ['bi-graph-up-arrow', '#fef6e3', '#d99a1f'];
        default:
            return ['bi-file-earmark-text', '#eaf4fd', '#4a9bd6'];
    }
}
?>

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

<h3 class="dashboard-title">Dashboard</h3>
<p class="text-muted">Selamat datang di Sistem Informasi Little King Preschool</p>

<div class="row g-3 mt-1">

    <!-- Total Murid -->
    <div class="col-md-6">
        <a href="murid.php" class="text-decoration-none">
            <div class="card-dashboard card-green d-flex flex-row justify-content-between align-items-center">
                <div>
                    <div class="card-title">Total Murid</div>
                    <h2><?php echo $total_murid; ?></h2>
                    <span class="card-link">Lihat detail murid &rarr;</span>
                </div>
                <div class="card-icon">
                    <i class="bi bi-people-fill"></i>
                </div>
            </div>
        </a>
    </div>

    <!-- Total Guru -->
    <div class="col-md-6">
        <a href="guru.php" class="text-decoration-none">
            <div class="card-dashboard card-yellow d-flex flex-row justify-content-between align-items-center">
                <div>
                    <div class="card-title">Total Guru</div>
                    <h2><?php echo $total_guru; ?></h2>
                    <span class="card-link">Lihat detail guru &rarr;</span>
                </div>
                <div class="card-icon">
                    <i class="bi bi-person-workspace"></i>
                </div>
            </div>
        </a>
    </div>

    <!-- Data Perkembangan -->
    <div class="col-md-6">
        <a href="perkembangan.php" class="text-decoration-none">
            <div class="card-dashboard card-green d-flex flex-row justify-content-between align-items-center">
                <div>
                    <div class="card-title">Data Perkembangan</div>
                    <h2><?php echo $total_perkembangan; ?></h2>
                    <span class="card-link">Lihat data perkembangan &rarr;</span>
                </div>
                <div class="card-icon">
                    <i class="bi bi-graph-up-arrow"></i>
                </div>
            </div>
        </a>
    </div>

    <!-- Laporan Murid -->
    <div class="col-md-6">
        <a href="laporan_murid.php" class="text-decoration-none">
            <div class="card-dashboard card-yellow d-flex flex-row justify-content-between align-items-center">
                <div>
                    <div class="card-title">Laporan Murid</div>
                    <h2><?php echo $total_murid; ?></h2>
                    <span class="card-link">Lihat laporan murid &rarr;</span>
                </div>
                <div class="card-icon">
                    <i class="bi bi-file-earmark-text"></i>
                </div>
            </div>
        </a>
    </div>

    <!-- Laporan Perkembangan (full width) -->
    <div class="col-12">
        <a href="laporan_perkembangan.php" class="text-decoration-none">
            <div class="card-dashboard card-yellow d-flex flex-row justify-content-between align-items-center">
                <div>
                    <div class="card-title">Laporan Perkembangan</div>
                    <h2><?php echo $total_perkembangan; ?></h2>
                    <span class="card-link">Lihat laporan perkembangan &rarr;</span>
                </div>
                <div class="card-icon">
                    <i class="bi bi-bar-chart-line"></i>
                </div>
            </div>
        </a>
    </div>

</div>

<div class="row g-3 mt-1">

    <!-- Informasi Sekolah -->
    <div class="col-md-6">
        <div class="info-card d-flex gap-3">
            <div class="info-icon">
                <i class="bi bi-mortarboard-fill"></i>
            </div>
            <div>
                <h5>Informasi Sekolah</h5>
                <p>Little King Preschool berkomitmen memberikan pendidikan terbaik untuk membentuk generasi cerdas, ceria, dan berakhlak mulia.</p>
                <a href="about_us.php" class="btn btn-info-more">Selengkapnya</a>
            </div>
        </div>
    </div>

    <!-- Aktivitas Terbaru -->
    <div class="col-md-6">
        <div class="activity-card">
            <h5>Aktivitas Terbaru</h5>

            <?php if(mysqli_num_rows($log_aktivitas) == 0): ?>
                <p class="text-muted mb-0">Belum ada aktivitas tercatat.</p>
            <?php endif; ?>

            <?php while($log = mysqli_fetch_assoc($log_aktivitas)):
                list($icon, $bg, $color) = icon_aktivitas($log['tipe']);
            ?>
                <div class="activity-item">
                    <div class="activity-left">
                        <div class="activity-icon" style="background:<?php echo $bg; ?>; color:<?php echo $color; ?>;">
                            <i class="bi <?php echo $icon; ?>"></i>
                        </div>
                        <span class="activity-text"><?php echo htmlspecialchars($log['deskripsi']); ?></span>
                    </div>
                    <span class="activity-time"><?php echo waktu_lalu($log['waktu']); ?></span>
                </div>
            <?php endwhile; ?>

        </div>
    </div>

</div>

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