<?php

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

/* =====================================================
   CEK ID SURAT
===================================================== */

if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    die("ID surat tidak valid.");
}

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


/* =====================================================
   AMBIL DATA SURAT
===================================================== */

$query = mysqli_query(
    $conn,
    "SELECT * FROM surat WHERE id_surat = '$id_surat'"
);

if (!$query) {
    die("Query gagal: " . mysqli_error($conn));
}

if (mysqli_num_rows($query) == 0) {
    die("Data surat tidak ditemukan.");
}

$data = mysqli_fetch_assoc($query);


/* =====================================================
   FORMAT TANGGAL
===================================================== */

$tanggalPengajuan = $data['tanggal'];

$bulanIndo = [
    1  => 'Januari',
    2  => 'Februari',
    3  => 'Maret',
    4  => 'April',
    5  => 'Mei',
    6  => 'Juni',
    7  => 'Juli',
    8  => 'Agustus',
    9  => 'September',
    10 => 'Oktober',
    11 => 'November',
    12 => 'Desember'
];

$timestamp = strtotime($tanggalPengajuan);

$tanggal = date('d', $timestamp);
$bulan   = $bulanIndo[(int) date('n', $timestamp)];
$tahun   = date('Y', $timestamp);


/* =====================================================
   STATUS
===================================================== */

$status = trim($data['status']);

?>

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

<head>

    <meta charset="UTF-8">

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

    <title>Detail Pengajuan Surat</title>

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

</head>

<body>

<div class="container">

    <!-- =================================================
         HEADER
    ================================================== -->

    <div class="header">

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

            ← Kembali

        </a>

        <h1>
            📄 Detail Pengajuan Surat
        </h1>

    </div>


    <!-- =================================================
         DATA PEMOHON
    ================================================== -->

    <div class="card">

        <div class="section">

            <h2>
                👤 Data Pemohon
            </h2>

            <div class="detail-row">

                <span class="label">
                    Nama
                </span>

                <span class="value">
                    <?= htmlspecialchars($data['nama']); ?>
                </span>

            </div>


            <div class="detail-row">

                <span class="label">
                    NIK
                </span>

                <span class="value">
                    <?= htmlspecialchars($data['nik']); ?>
                </span>

            </div>


            <div class="detail-row">

                <span class="label">
                    Tempat Lahir
                </span>

                <span class="value">
                    <?= htmlspecialchars($data['tempat_lahir']); ?>
                </span>

            </div>


            <div class="detail-row">

                <span class="label">
                    Tanggal Lahir
                </span>

                <span class="value">
                    <?= htmlspecialchars($data['tanggal_lahir']); ?>
                </span>

            </div>


            <div class="detail-row">

                <span class="label">
                    Jenis Kelamin
                </span>

                <span class="value">
                    <?= htmlspecialchars($data['jenis_kelamin']); ?>
                </span>

            </div>


            <div class="detail-row">

                <span class="label">
                    Agama
                </span>

                <span class="value">
                    <?= htmlspecialchars($data['agama']); ?>
                </span>

            </div>


            <div class="detail-row">

                <span class="label">
                    Pekerjaan
                </span>

                <span class="value">
                    <?= htmlspecialchars($data['pekerjaan']); ?>
                </span>

            </div>


            <div class="detail-row">

                <span class="label">
                    Status Perkawinan
                </span>

                <span class="value">
                    <?= htmlspecialchars($data['status_perkawinan']); ?>
                </span>

            </div>


            <div class="detail-row">

                <span class="label">
                    Alamat
                </span>

                <span class="value">
                    <?= htmlspecialchars($data['alamat']); ?>
                </span>

            </div>

        </div>


        <!-- =================================================
             DATA SURAT
        ================================================== -->

        <div class="section">

            <h2>
                📁 Data Surat
            </h2>


            <div class="detail-row">

                <span class="label">
                    Jenis Surat
                </span>

                <span class="value">
                    <?= htmlspecialchars($data['jenis_surat']); ?>
                </span>

            </div>


            <div class="detail-row">

                <span class="label">
                    Keperluan
                </span>

                <span class="value">
                    <?= htmlspecialchars($data['keperluan']); ?>
                </span>

            </div>


            <div class="detail-row">

                <span class="label">
                    Tanggal Pengajuan
                </span>

                <span class="value">
                    <?= $tanggal . " " . $bulan . " " . $tahun; ?>
                </span>

            </div>

        </div>


        <!-- =================================================
             STATUS PENGAJUAN
        ================================================== -->

        <div class="section status-section">

            <h2>
                📂 Status Pengajuan
            </h2>


            <div class="detail-row">

                <span class="label">
                    Status Saat Ini
                </span>

                <span class="value">

                    <?php if ($status == 'Diproses') { ?>

                        <span class="status diproses">
                            🟡 Diproses
                        </span>

                    <?php } elseif ($status == 'Selesai') { ?>

                        <span class="status selesai">
                            🟢 Selesai
                        </span>

                    <?php } elseif ($status == 'Ditolak') { ?>

                        <span class="status ditolak">
                            🔴 Ditolak
                        </span>

                    <?php } else { ?>

                        <span class="status">
                            <?= htmlspecialchars($status); ?>
                        </span>

                    <?php } ?>

                </span>

            </div>


            <!-- =================================================
                 FORM UBAH STATUS
            ================================================== -->

            <form action="edit_status.php"
                  method="POST">

                <input
                    type="hidden"
                    name="id_surat"
                    value="<?= $data['id_surat']; ?>">


                <div class="detail-row">

                    <span class="label">
                        Ubah Status
                    </span>

                    <span class="value">

                        <select
                            name="status"
                            class="status-select"
                            required>

                            <option value="Diproses"
                                <?= $status === 'Diproses' ? 'selected' : ''; ?>>

                                Diproses

                            </option>

                            <option value="Selesai"
                                <?= $status === 'Selesai' ? 'selected' : ''; ?>>

                                Selesai

                            </option>

                            <option value="Ditolak"
                                <?= $status === 'Ditolak' ? 'selected' : ''; ?>>

                                Ditolak

                            </option>

                        </select>

                    </span>

                </div>


                <!-- =================================================
                     TOMBOL
                ================================================== -->

                <div class="action-buttons">

                    <button
                        type="submit"
                        class="btn-simpan">

                        💾 Simpan Status

                    </button>


                    <a
                        href="cetak_surat.php?id=<?= $data['id_surat']; ?>"
                        class="btn-cetak">

                        🖨 Cetak Surat

                    </a>

                </div>

            </form>

        </div>

    </div>

</div>

</body>
</html>