<?php 
include "session.php"; 
include "../config/koneksi.php"; 
 
$queryAdmin = mysqli_query($conn, "SELECT * FROM profil_admin LIMIT 1");
$dataAdmin = mysqli_fetch_assoc($queryAdmin);

$namaAdmin = $dataAdmin['nama'] ?? 'Admin';
$fotoAdmin = $dataAdmin['foto'] ?? '';

// Statistik Dashboard 
$jProduk = mysqli_num_rows(
    mysqli_query($conn, "SELECT * FROM produk")
); 

$jBerita = mysqli_num_rows(
    mysqli_query($conn, "SELECT * FROM berita")
); 

$jSurat = mysqli_num_rows(
    mysqli_query($conn, "SELECT * FROM surat")
); 

$jUser = mysqli_num_rows(
    mysqli_query($conn, "SELECT * FROM admin")
); 


// =====================================================
// JUMLAH PENDUDUK
// Diambil langsung dari tabel penduduk
// =====================================================

$queryPenduduk = mysqli_query(
    $conn,
    "SELECT COUNT(*) AS total FROM penduduk"
);

$dataPenduduk = mysqli_fetch_assoc($queryPenduduk);

$jPenduduk = (int) ($dataPenduduk['total'] ?? 0);

?>

<!DOCTYPE html>
<html lang="id">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Dashboard Admin</title>

<link rel="stylesheet" href="../assets/css/admin.css">

<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.6.0/css/all.min.css">

</head>

<style>
        /* LINK CARD */
    .card-link {
        display: block;
        text-decoration: none !important;
        color: inherit;
    }

    /* Hilangkan underline semua isi card */
    .card-link i,
    .card-link h2,
    .card-link p {
        text-decoration: none !important;
    }

    /* Warna card Demografi */
    .card-link .hijau i,
    .card-link .hijau h2 {
        color: #0b8d4a;
    }

    .card-link .hijau p {
        color: #666;
    }

    /* Efek hover */
    .card-link:hover .card {
        transform: translateY(-6px);
    }
</style>

<body>

<div class="wrapper">


<!---------------- SIDEBAR ------------------>

<div class="sidebar">

    <div class="logo">

        <img src="../assets/img/logo-desa.png">

        <h2>DESA OELOMIN</h2>

    </div>

    <ul>

        <li class="active">

            <a href="dashboard.php">

                <i class="fa-solid fa-house"></i>

                Dashboard

            </a>

        </li>

        <li>

            <a href="profil_admin.php">

                <i class="fa-solid fa-landmark"></i>

                Profil Desa

            </a>

        </li>

        <li>

            <a href="produk_admin.php">

                <i class="fa-solid fa-store"></i>

                Produk Lokal

            </a>

        </li>

        <li>

            <a href="berita_admin.php">

                <i class="fa-solid fa-newspaper"></i>

                Berita

            </a>

        </li>

        <li>

            <a href="kelola_surat.php">

                <i class="fa-solid fa-envelope"></i>

                Pelayanan Surat

            </a>

        </li>

        <li class="demografi-menu">

            <a href="javascript:void(0);" id="demografiToggle">
                <i class="fa-solid fa-users"></i>
                <span>Manajemen Data</span>
                <i class="fa-solid fa-chevron-down demografi-arrow"></i>
            </a>

            <ul class="demografi-submenu" id="demografiSubmenu">

                <li>
                    <a href="data_demografi.php">
                        Data Demografi
                    </a>
                </li>

                <li>
                    <a href="data_penduduk.php">
                        Data Penduduk
                    </a>
                </li>

            </ul>

        </li>

        <li>

            <a href="../logout.php">

                <i class="fa-solid fa-right-from-bracket"></i>

                Logout

            </a>

        </li>

    </ul>

</div>


<!---------------- CONTENT ------------------>

<div class="content">


    <!---------------- TOPBAR ------------------>

    <div class="topbar">

        <div class="search-box">

            <input
                type="text"
                id="search"
                placeholder="Search..."
            >

            <button>

                <i class="fa-solid fa-magnifying-glass"></i>

            </button>

        </div>

        <div class="admin-info">
            <?php if (!empty($fotoAdmin)): ?>
                <img src="../assets/img/admin/<?= htmlspecialchars($fotoAdmin); ?>"
                    alt="Foto Admin"
                    class="foto-admin">
            <?php else: ?>
                <div class="foto-admin-default">
                    <i class="fa-solid fa-user"></i>
                </div>
            <?php endif; ?>

            <span><?= htmlspecialchars($namaAdmin); ?></span>
        </div>
    </div>


    <!---------------- JUDUL ------------------>

    <h1>Dashboard Admin</h1>

    <p>
        Selamat datang di Portal Desa Digital Desa Oelomin
    </p>


    <!---------------- CARD ------------------>

    <div class="cards">


        <!-- PRODUK LOKAL -->

        <a href="produk_admin.php" class="card-link">
        <div class="card hijau">

            <i class="fa-solid fa-store"></i>

            <h2>
                <?= $jProduk ?>
            </h2>

            <p>
                Produk Lokal
            </p>

        </div>

        <!-- BERITA -->

        <a href="berita_admin.php" class="card-link">
        <div class="card biru">

            <i class="fa-solid fa-newspaper"></i>

            <h2>
                <?= $jBerita ?>
            </h2>

            <p>
                Berita
            </p>

        </div>


        <!-- SURAT -->

        <a href="kelola_surat.php" class="card-link">
        <div class="card orange">

            <i class="fa-solid fa-envelope"></i>

            <h2>
                <?= $jSurat ?>
            </h2>

            <p>
                Surat Masuk
            </p>

        </div>


        <!-- DEMOGRAFI DESA -->

        <a href="data_demografi.php" class="card-link">

            <div class="card hijau">

                <i class="fa-solid fa-users"></i>

                <h2>
                    <?= number_format($jPenduduk) ?>
                </h2>

                <p>
                    Manajemen Data
                </p>

            </div>

        </a>

        <!-- ADMIN -->

        <a href="data_admin.php" class="card-link">
            <div class="card merah">
                <i class="fa-solid fa-user-shield"></i>
                <h2>
                    <?= $jUser ?>
                </h2>
                <p>
                    Admin
                </p>
            </div>
        </a>

    </div>

</div>

</div>

<script src="../assets/js/script.js"></script>

<script>
document.addEventListener("DOMContentLoaded", function () {

    const toggle = document.getElementById("demografiToggle");
    const menu = document.querySelector(".demografi-menu");

    toggle.addEventListener("click", function () {
        menu.classList.toggle("open");
    });

});
</script>

</body>

</html>