<?php

require_once __DIR__ . '/../config/koneksi.php';

/*
|--------------------------------------------------------------------------
| HAPUS DATA PENDUDUK
|--------------------------------------------------------------------------
| Data dihapus berdasarkan id_penduduk.
| Sebelum menghapus, sistem menampilkan halaman konfirmasi.
|--------------------------------------------------------------------------
*/

// =====================================================
// CEK ID
// =====================================================

if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    header("Location: data_penduduk.php");
    exit;
}

$id_penduduk = (int) $_GET['id'];


// =====================================================
// AMBIL DATA PENDUDUK
// =====================================================

$stmt = mysqli_prepare(
    $conn,
    "SELECT 
        id_penduduk,
        pas_foto,
        nama_lengkap,
        nik,
        jenis_kelamin,
        desa,
        dusun,
        rt,
        rw,
        status_perkawinan,
        pendidikan,
        agama,
        pekerjaan,
        tanggal_lahir
     FROM penduduk
     WHERE id_penduduk = ?"
);

mysqli_stmt_bind_param($stmt, "i", $id_penduduk);
mysqli_stmt_execute($stmt);

$result = mysqli_stmt_get_result($stmt);
$data = mysqli_fetch_assoc($result);

mysqli_stmt_close($stmt);


// =====================================================
// DATA TIDAK DITEMUKAN
// =====================================================

if (!$data) {
    header("Location: data_penduduk.php");
    exit;
}


// =====================================================
// PROSES HAPUS
// =====================================================

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    // Simpan nama foto sebelum data dihapus
    $pas_foto = $data['pas_foto'];

    // Hapus data dari database
    $stmt_delete = mysqli_prepare(
        $conn,
        "DELETE FROM penduduk WHERE id_penduduk = ?"
    );

    mysqli_stmt_bind_param(
        $stmt_delete,
        "i",
        $id_penduduk
    );

    $berhasil = mysqli_stmt_execute($stmt_delete);

    mysqli_stmt_close($stmt_delete);


    // =================================================
    // JIKA BERHASIL
    // =================================================

    if ($berhasil) {

        /*
         * Hapus file foto jika ada.
         *
         * Sesuaikan folder jika lokasi upload
         * foto penduduk berbeda.
         */
        if (!empty($pas_foto)) {

            $file_foto = __DIR__ . '/../uploads/' . $pas_foto;

            if (file_exists($file_foto)) {
                unlink($file_foto);
            }
        }

        header("Location: data_penduduk.php?status=hapus_berhasil");
        exit;

    } else {

        $error = "Data penduduk gagal dihapus.";
    }
}

?>

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

<head>

    <meta charset="UTF-8">

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

    <title>Hapus Penduduk | Desa Oelomin</title>

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

</head>


<body>


<div class="delete-page">


    <!-- =====================================================
         CARD KONFIRMASI
    ====================================================== -->

    <div class="delete-card">


        <!-- ICON -->

        <div class="delete-icon">
            🗑️
        </div>


        <!-- JUDUL -->

        <h1>
            Hapus Data Penduduk?
        </h1>


        <p class="delete-description">

            Apakah Anda yakin ingin menghapus data penduduk
            berikut?

        </p>


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

        <div class="person-card">


            <!-- FOTO -->

            <div class="person-photo">

                <?php if (!empty($data['pas_foto'])): ?>

                    <img
                        src="../uploads/<?= htmlspecialchars($data['pas_foto']); ?>"
                        alt="Foto <?= htmlspecialchars($data['nama_lengkap']); ?>"
                        onerror="this.style.display='none'; this.nextElementSibling.style.display='flex';"
                    >

                    <div
                        class="default-photo"
                        style="display:none;"
                    >
                        👤
                    </div>

                <?php else: ?>

                    <div class="default-photo">
                        👤
                    </div>

                <?php endif; ?>

            </div>


            <!-- INFORMASI -->

            <div class="person-info">

                <h2>
                    <?= htmlspecialchars($data['nama_lengkap']); ?>
                </h2>


                <div class="info-row">

                    <span>NIK</span>

                    <strong>
                        <?= htmlspecialchars($data['nik']); ?>
                    </strong>

                </div>


                <div class="info-row">

                    <span>Jenis Kelamin</span>

                    <strong>
                        <?= htmlspecialchars($data['jenis_kelamin']); ?>
                    </strong>

                </div>


                <div class="info-row">

                    <span>Dusun</span>

                    <strong>
                        <?= htmlspecialchars($data['dusun'] ?? '-'); ?>
                    </strong>

                </div>


                <div class="info-row">

                    <span>Pekerjaan</span>

                    <strong>
                        <?= htmlspecialchars($data['pekerjaan'] ?? '-'); ?>
                    </strong>

                </div>

            </div>

        </div>


        <!-- =================================================
             PERINGATAN
        ================================================== -->

        <div class="warning-box">

            <span class="warning-icon">
                ⚠️
            </span>

            <div>

                <strong>
                    Perhatian
                </strong>

                <p>
                    Data yang sudah dihapus tidak dapat
                    dikembalikan. Pastikan Anda benar-benar
                    ingin menghapus data ini.
                </p>

            </div>

        </div>


        <!-- ERROR -->

        <?php if (isset($error)): ?>

            <div class="error-message">

                ❌

                <?= htmlspecialchars($error); ?>

            </div>

        <?php endif; ?>


        <!-- =================================================
             BUTTON
        ================================================== -->

        <div class="delete-actions">


            <!-- BATAL -->

            <a
                href="data_penduduk.php"
                class="btn-cancel"
            >
                ← Batal
            </a>


            <!-- HAPUS -->

            <form
                method="POST"
                action=""
                onsubmit="return confirm('Apakah Anda benar-benar yakin ingin menghapus data <?= htmlspecialchars($data['nama_lengkap']); ?>?');"
            >

                <button
                    type="submit"
                    class="btn-confirm-delete"
                >
                    🗑️
                    Ya, Hapus Data
                </button>

            </form>

        </div>


    </div>


</div>


</body>

</html>