<?php require_once $_SERVER['DOCUMENT_ROOT'] . '/auth/cek_login.php'; ?>
<?php
// 1. Aktifkan laporan error untuk debugging
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// 2. Sertakan koneksi database
include $_SERVER['DOCUMENT_ROOT'] . '/assets/db/db.php';

// 3. Pastikan data dikirim melalui POST
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    
    // Ambil ID data yang akan diedit
    $id = mysqli_real_escape_string($conn, $_POST['id']);
    
    // Ambil data identitas terupdate (Termasuk NIK dan Jenis Kelamin)
    $nama           = mysqli_real_escape_string($conn, $_POST['nama_lengkap']);
    $nik            = mysqli_real_escape_string($conn, $_POST['nik']); // Tambahan baru
    $jenis_kelamin  = mysqli_real_escape_string($conn, $_POST['jenis_kelamin']); // Tambahan baru
    $paspor         = mysqli_real_escape_string($conn, $_POST['no_paspor']);
    $tgl_lahir      = $_POST['tgl_lahir'];
    $no_icv         = mysqli_real_escape_string($conn, $_POST['no_icv']);
    
    // Ambil data Vaksin 1 terupdate
    $v1_nama    = mysqli_real_escape_string($conn, $_POST['vaksin_1_nama']);
    $v1_batch   = mysqli_real_escape_string($conn, $_POST['vaksin_1_batch']);
    $v1_tgl     = !empty($_POST['vaksin_1_tgl_admin']) ? $_POST['vaksin_1_tgl_admin'] : NULL;
    $v1_valid   = !empty($_POST['vaksin_1_valid_sampai']) ? $_POST['vaksin_1_valid_sampai'] : NULL;

    // Ambil data Vaksin 2 terupdate
    $v2_nama    = mysqli_real_escape_string($conn, $_POST['vaksin_2_nama']);
    $v2_batch   = mysqli_real_escape_string($conn, $_POST['vaksin_2_batch']);
    $v2_tgl     = !empty($_POST['vaksin_2_tgl_admin']) ? $_POST['vaksin_2_tgl_admin'] : NULL;
    $v2_valid   = !empty($_POST['vaksin_2_valid_sampai']) ? $_POST['vaksin_2_valid_sampai'] : NULL;

    // 4. Query Update dengan penambahan kolom nik dan jenis_kelamin
    $query = "UPDATE sertifikat_vaksin SET 
                nama_lengkap = '$nama', 
                nik = '$nik', 
                jenis_kelamin = '$jenis_kelamin', 
                no_paspor = '$paspor', 
                tgl_lahir = '$tgl_lahir', 
                no_icv = '$no_icv',
                vaksin_1_nama = '$v1_nama',
                vaksin_1_batch = '$v1_batch',
                vaksin_1_tgl_admin = " . ($v1_tgl ? "'$v1_tgl'" : "NULL") . ",
                vaksin_1_valid_sampai = " . ($v1_valid ? "'$v1_valid'" : "NULL") . ",
                vaksin_2_nama = '$v2_nama',
                vaksin_2_batch = '$v2_batch',
                vaksin_2_tgl_admin = " . ($v2_tgl ? "'$v2_tgl'" : "NULL") . ",
                vaksin_2_valid_sampai = " . ($v2_valid ? "'$v2_valid'" : "NULL") . "
              WHERE id = '$id'";

    if ($conn->query($query) === TRUE) {
        echo "<script>
                alert('Data Sertifikat Berhasil Diperbarui!');
                window.location.href='list_sertifikat.php';
              </script>";
    } else {
        echo "<h4>Gagal Memperbarui Data</h4>";
        echo "Error: " . $conn->error;
        echo "<br><br><a href='edit.php?id=$id'>Kembali ke Form Edit</a>";
    }
} else {
    echo "Akses langsung tidak diizinkan.";
}
?>