<?php

include "session.php";
include "../config/koneksi.php";

/* =====================================================
   AMBIL DATA ADMIN + PROFIL ADMIN
   ===================================================== */

$query = mysqli_query(
    $conn,
    "SELECT 
        a.id_admin,
        a.nama,
        a.username,
        a.level,
        p.id_profil,
        p.nama_lengkap,
        p.foto,
        p.tempat_lahir,
        p.tanggal_lahir,
        p.jenis_kelamin,
        p.alamat,
        p.no_hp,
        p.email,
        p.jabatan
     FROM admin a
     LEFT JOIN profil_admin p
        ON a.id_admin = p.id_admin
     ORDER BY a.id_admin ASC"
);

?>

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

<head>

    <meta charset="UTF-8">

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

    <title>Data Admin - Desa Oelomin</title>

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

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

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

</head>


<body>

<div class="wrapper">


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

    <div class="sidebar">

        <div class="logo">

            <img src="../assets/img/logo-desa.png"
                 alt="Logo Desa Oelomin">

            <h2>DESA OELOMIN</h2>

        </div>


        <ul>

            <li>

                <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>

                <a href="data_demografi.php">

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

                    Manajemen Data

                </a>

            </li>


            <li class="active">

                <a href="data_admin.php">

                    <i class="fa-solid fa-user-shield"></i>

                    Data Admin

                </a>

            </li>


            <li>

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

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

                    Logout

                </a>

            </li>

        </ul>

    </div>
    
        <!-- ================= HALAMAN ADMIN ================= -->

        <div class="profil-admin-container">


            <!-- HEADER -->

            <div class="profil-header">

                <div>

                    <h1>Data Admin</h1>

                    <p>
                        Informasi pengelola sistem Portal Desa Digital Desa Oelomin.
                    </p>

                </div>


                <a href="dashboard.php"
                   class="btn-kembali">

                    <i class="fa-solid fa-arrow-left"></i>

                    Kembali

                </a>

            </div>



            <?php if (mysqli_num_rows($query) > 0) : ?>

                <?php while ($admin = mysqli_fetch_assoc($query)) : ?>


                    <!-- ================= PROFIL ================= -->

                    <div class="profil-card">


                        <!-- BAGIAN ATAS -->

                        <div class="profil-top">


                            <!-- FOTO -->

                            <div class="foto-wrapper">

                                <?php if (
                                    !empty($admin['foto']) &&
                                    file_exists("../assets/img/admin/" . $admin['foto'])
                                ) : ?>

                                    <img
                                        src="../assets/img/admin/<?= htmlspecialchars($admin['foto']); ?>"
                                        alt="Foto Admin"
                                    >

                                <?php else : ?>

                                    <div class="foto-default">

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

                                    </div>

                                <?php endif; ?>

                            </div>



                            <!-- NAMA -->

                            <div class="profil-nama">

                                <h2>

                                    <?= htmlspecialchars(
                                        $admin['nama_lengkap']
                                        ?: $admin['nama']
                                    ); ?>

                                </h2>


                                <span class="badge-admin">

                                    <i class="fa-solid fa-shield-halved"></i>

                                    <?= htmlspecialchars(
                                        $admin['jabatan']
                                        ?: 'Administrator Sistem'
                                    ); ?>

                                </span>

                            </div>



                            <!-- EDIT -->

                            <a
                                href="edit_data_admin.php?id=<?= $admin['id_admin']; ?>"
                                class="btn-edit"
                            >

                                <i class="fa-solid fa-pen-to-square"></i>

                                Edit Profil

                            </a>

                        </div>



                        <!-- ================= BIODATA ================= -->

                        <div class="biodata-title">

                            <i class="fa-solid fa-id-card"></i>

                            Biodata Pengelola Sistem

                        </div>


                        <div class="biodata-grid">


                            <!-- NAMA -->

                            <div class="biodata-item">

                                <span>Nama Lengkap</span>

                                <strong>

                                    <?= htmlspecialchars(
                                        $admin['nama_lengkap']
                                        ?: $admin['nama']
                                        ?: '-'
                                    ); ?>

                                </strong>

                            </div>



                            <!-- USERNAME -->

                            <div class="biodata-item">

                                <span>Username</span>

                                <strong>

                                    <?= htmlspecialchars(
                                        $admin['username'] ?: '-'
                                    ); ?>

                                </strong>

                            </div>



                            <!-- TEMPAT LAHIR -->

                            <div class="biodata-item">

                                <span>Tempat Lahir</span>

                                <strong>

                                    <?= htmlspecialchars(
                                        $admin['tempat_lahir'] ?: '-'
                                    ); ?>

                                </strong>

                            </div>



                            <!-- TANGGAL LAHIR -->

                            <div class="biodata-item">

                                <span>Tanggal Lahir</span>

                                <strong>

                                    <?php

                                    if (!empty($admin['tanggal_lahir'])) {

                                        echo date(
                                            'd F Y',
                                            strtotime(
                                                $admin['tanggal_lahir']
                                            )
                                        );

                                    } else {

                                        echo '-';

                                    }

                                    ?>

                                </strong>

                            </div>



                            <!-- JENIS KELAMIN -->

                            <div class="biodata-item">

                                <span>Jenis Kelamin</span>

                                <strong>

                                    <?= htmlspecialchars(
                                        $admin['jenis_kelamin'] ?: '-'
                                    ); ?>

                                </strong>

                            </div>



                            <!-- JABATAN -->

                            <div class="biodata-item">

                                <span>Jabatan</span>

                                <strong>

                                    <?= htmlspecialchars(
                                        $admin['jabatan']
                                        ?: 'Administrator Sistem'
                                    ); ?>

                                </strong>

                            </div>



                            <!-- NOMOR HP -->

                            <div class="biodata-item">

                                <span>Nomor HP</span>

                                <strong>

                                    <?= htmlspecialchars(
                                        $admin['no_hp'] ?: '-'
                                    ); ?>

                                </strong>

                            </div>



                            <!-- EMAIL -->

                            <div class="biodata-item">

                                <span>Email</span>

                                <strong>

                                    <?= htmlspecialchars(
                                        $admin['email'] ?: '-'
                                    ); ?>

                                </strong>

                            </div>



                            <!-- ALAMAT -->

                            <div class="biodata-item biodata-full">

                                <span>Alamat</span>

                                <strong>

                                    <?= htmlspecialchars(
                                        $admin['alamat'] ?: '-'
                                    ); ?>

                                </strong>

                            </div>



                            <!-- LEVEL -->

                            <div class="biodata-item">

                                <span>Level Akses</span>

                                <strong>

                                    <?= htmlspecialchars(
                                        $admin['level'] ?: 'admin'
                                    ); ?>

                                </strong>

                            </div>



                            <!-- STATUS -->

                            <div class="biodata-item">

                                <span>Status</span>

                                <strong class="status-aktif">

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

                                    Aktif

                                </strong>

                            </div>

                        </div>



                        <!-- ================= INFORMASI ================= -->

                        <div class="info-admin">

                            <i class="fa-solid fa-circle-info"></i>

                            <p>

                                Admin bertugas mengelola dan memperbarui
                                informasi pada Portal Desa Digital Desa
                                Oelomin, termasuk data profil desa,
                                produk lokal, berita, dan pelayanan surat.

                            </p>

                        </div>


                    </div>


                <?php endwhile; ?>


            <?php else : ?>


                <!-- JIKA BELUM ADA ADMIN -->

                <div class="data-kosong">

                    <i class="fa-solid fa-user-slash"></i>

                    <h3>Data Admin Belum Tersedia</h3>

                    <p>
                        Belum terdapat data admin yang terdaftar.
                    </p>

                </div>


            <?php endif; ?>


        </div>

    </div>

</div>

</body>

</html>