<?php 
// 1. Cek Keamanan Login
require_once $_SERVER['DOCUMENT_ROOT'] . '/auth/cek_login.php'; 

// 2. Hubungkan ke database
include $_SERVER['DOCUMENT_ROOT'] . '/assets/db/db.php'; 

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

$id = mysqli_real_escape_string($conn, $_GET['id']);
$query = "SELECT * FROM sertifikat_vaksin WHERE id = '$id'";
$result = $conn->query($query);
$data = $result->fetch_assoc();

if (!$data) { die("Data tidak ditemukan."); }

// Daftar Master Batch Number
$batch_list_1 = [
    "BIOFARMA B20241126-1",
    "BIOFARMA B20241201-2",
    "BIOFARMA B20241017-2"
];

$batch_list_2 = [
    "BIOFARMA 21000125",
    "BIOFARMA 21000525"
];
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Edit Sertifikat - <?php echo $data['nama_lengkap']; ?></title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@400;600;700&display=swap" rel="stylesheet">
    <style>
        body { background-color: #f0f2f5; font-family: 'Open Sans', sans-serif; }
        .card-header-custom { background: #ffc107; color: #000; padding: 20px; font-weight: bold; border-radius: 12px 12px 0 0; }
        .section-title { color: #40ada6; font-weight: bold; border-left: 4px solid #40ada6; padding-left: 10px; margin: 20px 0; text-transform: uppercase; font-size: 0.9rem; }
        .vaksin-box { background: #f8f9fa; padding: 15px; border-radius: 8px; border: 1px solid #ddd; height: 100%; }
        .form-control { border-radius: 8px; }
        .btn-warning { color: #212529; font-weight: bold; border-radius: 8px; }
    </style>
</head>
<body>

<div class="container mt-5 mb-5">
    <div class="card shadow border-0" style="border-radius: 12px;">
        <div class="card-header-custom d-flex justify-content-between align-items-center">
            <span><i class="fas fa-edit mr-2"></i> EDIT DATA SERTIFIKAT</span>
            <a href="list_sertifikat.php" class="btn btn-dark btn-sm">Batal / Kembali</a>
        </div>
        <div class="card-body p-4">
            <form action="proses_edit.php" method="POST">
                <input type="hidden" name="id" value="<?php echo $data['id']; ?>">

                <div class="section-title">Informasi Pasien</div>
                <div class="row">
                    <div class="col-md-6 form-group">
                        <label>Nama Lengkap</label>
                        <input type="text" name="nama_lengkap" class="form-control" value="<?php echo $data['nama_lengkap']; ?>" required>
                    </div>
                    <div class="col-md-3 form-group">
                        <label>NIK (KTP)</label>
                        <input type="text" name="nik" class="form-control" value="<?php echo $data['nik']; ?>" required maxlength="16">
                    </div>
                    <div class="col-md-3 form-group">
                        <label>Jenis Kelamin</label>
                        <select name="jenis_kelamin" class="form-control" required>
                            <option value="Laki-laki" <?php if($data['jenis_kelamin'] == 'Laki-laki') echo 'selected'; ?>>Laki-laki</option>
                            <option value="Perempuan" <?php if($data['jenis_kelamin'] == 'Perempuan') echo 'selected'; ?>>Perempuan</option>
                        </select>
                    </div>
                </div>

                <div class="row">
                    <div class="col-md-3 form-group">
                        <label>Nomor Paspor</label>
                        <input type="text" name="no_paspor" class="form-control" value="<?php echo $data['no_paspor']; ?>" required>
                    </div>
                    <div class="col-md-3 form-group">
                        <label>Tanggal Lahir</label>
                        <input type="date" name="tgl_lahir" class="form-control" value="<?php echo $data['tgl_lahir']; ?>" required>
                    </div>
                    <div class="col-md-6 form-group">
                        <label>Nomor Kode ICV (eICV / Barcode)</label>
                        <input type="text" name="no_icv" class="form-control" value="<?php echo $data['no_icv']; ?>" required>
                    </div>
                </div>

                <div class="row mt-3">
                    <div class="col-md-6 mb-3">
                        <div class="vaksin-box">
                            <div class="section-title mt-0">Vaksin 1 (Utama)</div>
                            <div class="form-group">
                                <label>Nama Vaksin</label>
                                <input type="text" name="vaksin_1_nama" class="form-control" value="<?php echo $data['vaksin_1_nama']; ?>">
                            </div>
                            <div class="form-group">
                                <label>Batch Number 1</label>
                                <select name="vaksin_1_batch" class="form-control">
                                    <?php foreach($batch_list_1 as $batch): ?>
                                        <option value="<?php echo $batch; ?>" <?php echo ($data['vaksin_1_batch'] == $batch) ? 'selected' : ''; ?>>
                                            <?php echo $batch; ?>
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            <div class="row">
                                <div class="col-6"><label>Tgl Vaksin</label><input type="date" name="vaksin_1_tgl_admin" class="form-control" value="<?php echo $data['vaksin_1_tgl_admin']; ?>"></div>
                                <div class="col-6"><label>Valid Sampai</label><input type="date" name="vaksin_1_valid_sampai" class="form-control" value="<?php echo $data['vaksin_1_valid_sampai']; ?>"></div>
                            </div>
                            <div class="form-group mt-3">
                                <label>Faskes / Dokter</label>
                                <textarea name="vaksin_1_faskes" class="form-control" rows="2"><?php echo $data['vaksin_1_faskes']; ?></textarea>
                            </div>
                        </div>
                    </div>
                    
                    <div class="col-md-6 mb-3">
                        <div class="vaksin-box">
                            <div class="section-title mt-0">Vaksin 2 (Tambahan)</div>
                            <div class="form-group">
                                <label>Nama Vaksin</label>
                                <input type="text" name="vaksin_2_nama" class="form-control" value="<?php echo $data['vaksin_2_nama']; ?>">
                            </div>
                            <div class="form-group">
                                <label>Batch Number 2</label>
                                <select name="vaksin_2_batch" class="form-control">
                                    <option value="">-- Pilih Batch (Opsional) --</option>
                                    <?php foreach($batch_list_2 as $batch): ?>
                                        <option value="<?php echo $batch; ?>" <?php echo ($data['vaksin_2_batch'] == $batch) ? 'selected' : ''; ?>>
                                            <?php echo $batch; ?>
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            <div class="row">
                                <div class="col-6"><label>Tgl Vaksin</label><input type="date" name="vaksin_2_tgl_admin" class="form-control" value="<?php echo $data['vaksin_2_tgl_admin']; ?>"></div>
                                <div class="col-6"><label>Valid Sampai</label><input type="date" name="vaksin_2_valid_sampai" class="form-control" value="<?php echo $data['vaksin_2_valid_sampai']; ?>"></div>
                            </div>
                            <div class="form-group mt-3">
                                <label>Faskes / Dokter</label>
                                <textarea name="vaksin_2_faskes" class="form-control" rows="2"><?php echo $data['vaksin_2_faskes']; ?></textarea>
                            </div>
                        </div>
                    </div>
                </div>

                <button type="submit" class="btn btn-warning btn-block btn-lg mt-4 shadow-sm">
                    <i class="fas fa-save mr-2"></i> SIMPAN PERUBAHAN DATA
                </button>
            </form>
        </div>
    </div>
</div>

</body>
</html>