<?php
require_once '../includes/header.php';
require_once '../includes/navbar.php';

// Ambil ID GTK dari URL
$id_gtk = $_GET['id'] ?? 0;

// Ambil data tahun ajaran aktif
$query_tahun = "SELECT * FROM bind_tahunajaran WHERE aktif = 1 LIMIT 1";
$result_tahun = mysqli_query($conn, $query_tahun);
$tahun_aktif = mysqli_fetch_assoc($result_tahun);

if (!$tahun_aktif) {
    echo "<script>
        Swal.fire({
            icon: 'error',
            title: 'Error',
            text: 'Tidak ada tahun ajaran aktif yang ditemukan'
        }).then(() => {
            window.location.href = 'index.php';
        });
    </script>";
    exit;
}

// Ambil data GTK
$query_gtk = "SELECT * FROM bind_gtk WHERE id_gtk = $id_gtk";
$result_gtk = mysqli_query($conn, $query_gtk);
$gtk = mysqli_fetch_assoc($result_gtk);

if (!$gtk) {
    echo "<script>
        Swal.fire({
            icon: 'error',
            title: 'Error',
            text: 'Data GTK tidak ditemukan'
        }).then(() => {
            window.location.href = 'index.php';
        });
    </script>";
    exit;
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // Ambil data dari form
    $nip = $_POST['nip'] ?? '';
    $nuptk = $_POST['nuptk'] ?? '';
    $nik = $_POST['nik'];
    $nama = $_POST['nama'];
    $tempat_lahir = $_POST['tempat_lahir'];
    $tgl_lahir = $_POST['tgl_lahir'];
    $jenis_kelamin = $_POST['jenis_kelamin'];
    $agama = $_POST['agama'];
    $status_pernikahan = $_POST['status_pernikahan'];
    $alamat = $_POST['alamat'];
    $no_hp = $_POST['no_hp'];
    $email = $_POST['email'];
    $sumber_gaji = $_POST['sumber_gaji'];
    $jenis_gtk = $_POST['jenis_gtk'];
    $status_kepegawaian = $_POST['status_kepegawaian'];
    $pendidikan_terakhir = $_POST['pendidikan_terakhir'];
    $mata_pelajaran = $_POST['mata_pelajaran'] ?? '';
    $tunjangan_sertifikasi = isset($_POST['tunjangan_sertifikasi']) ? 1 : 0;
    $jabatan_fungsional = $_POST['jabatan_fungsional'] ?? '';
    $golongan = $_POST['golongan'] ?? '';
    $tmt_kerja = $_POST['tmt_kerja'] ?? '';
    $status_aktif = isset($_POST['status_aktif']) ? 1 : 0;
    
    // Validasi NIP, NUPTK, dan NIK tidak boleh sama kecuali diisi dengan tanda minus (-)
    $errors = [];
    
    // Validasi NIP
    if (!empty($nip) && $nip != '-') {
        $check_nip = $conn->prepare("SELECT id_gtk FROM bind_gtk WHERE nip = ? AND id_gtk != ?");
        $check_nip->bind_param("si", $nip, $id_gtk);
        $check_nip->execute();
        $check_nip->store_result();
        
        if ($check_nip->num_rows > 0) {
            $errors[] = "NIP sudah terdaftar. Silakan gunakan NIP yang berbeda.";
        }
        $check_nip->close();
    }
    
    // Validasi NUPTK
    if (!empty($nuptk) && $nuptk != '-') {
        $check_nuptk = $conn->prepare("SELECT id_gtk FROM bind_gtk WHERE nuptk = ? AND id_gtk != ?");
        $check_nuptk->bind_param("si", $nuptk, $id_gtk);
        $check_nuptk->execute();
        $check_nuptk->store_result();
        
        if ($check_nuptk->num_rows > 0) {
            $errors[] = "NUPTK sudah terdaftar. Silakan gunakan NUPTK yang berbeda.";
        }
        $check_nuptk->close();
    }
    
    // Validasi NIK (wajib, tidak boleh kosong)
    if (!empty($nik) && $nik != '-') {
        $check_nik = $conn->prepare("SELECT id_gtk FROM bind_gtk WHERE nik = ? AND id_gtk != ?");
        $check_nik->bind_param("si", $nik, $id_gtk);
        $check_nik->execute();
        $check_nik->store_result();
        
        if ($check_nik->num_rows > 0) {
            $errors[] = "NIK sudah terdaftar. Silakan gunakan NIK yang berbeda.";
        }
        $check_nik->close();
    }
    
    // Jika ada error, tampilkan pesan error
    if (!empty($errors)) {
        $error_message = implode("<br>", $errors);
        echo "<script>
            Swal.fire({
                icon: 'error',
                title: 'Gagal',
                html: '$error_message'
            });
        </script>";
    } else {
        // Upload foto jika ada perubahan
        $foto = $gtk['foto'];
        if ($_FILES['foto']['name']) {
            $target_dir = "../uploads/foto-gtk/";
            if (!is_dir($target_dir)) {
                mkdir($target_dir, 0777, true);
            }
            
            // Hapus foto lama jika ada
            if ($foto && file_exists($target_dir . $foto)) {
                unlink($target_dir . $foto);
            }
            
            $file_extension = pathinfo($_FILES['foto']['name'], PATHINFO_EXTENSION);
            $foto = "gtk_" . time() . "." . $file_extension;
            $target_file = $target_dir . $foto;
            
            move_uploaded_file($_FILES['foto']['tmp_name'], $target_file);
        }
        
        // Query update dengan id_tahunajaran
        $id_tahunajaran = $tahun_aktif['id_tahunajaran']; // Ambil tahun ajaran aktif

        $sql = "UPDATE bind_gtk SET 
                id_tahunajaran = ?, nip = ?, nuptk = ?, nik = ?, nama = ?, tempat_lahir = ?, tgl_lahir = ?, 
                jenis_kelamin = ?, agama = ?, status_pernikahan = ?, alamat = ?, no_hp = ?, 
                email = ?, sumber_gaji = ?, jenis_gtk = ?, status_kepegawaian = ?, 
                pendidikan_terakhir = ?, mata_pelajaran = ?, tunjangan_sertifikasi = ?, 
                jabatan_fungsional = ?, golongan = ?, tmt_kerja = ?, status_aktif = ?, foto = ?
                WHERE id_gtk = ?";

        $stmt = $conn->prepare($sql);
        $stmt->bind_param("isssssssssssssssssisssisi", 
            $id_tahunajaran, $nip, $nuptk, $nik, $nama, $tempat_lahir, $tgl_lahir, $jenis_kelamin, 
            $agama, $status_pernikahan, $alamat, $no_hp, $email, $sumber_gaji, $jenis_gtk, 
            $status_kepegawaian, $pendidikan_terakhir, $mata_pelajaran, $tunjangan_sertifikasi, 
            $jabatan_fungsional, $golongan, $tmt_kerja, $status_aktif, $foto, $id_gtk);
        
        if ($stmt->execute()) {
            echo "<script>
                Swal.fire({
                    icon: 'success',
                    title: 'Berhasil',
                    text: 'Data GTK berhasil diperbarui'
                }).then(() => {
                    window.location.href = 'index.php';
                });
            </script>";
        } else {
            echo "<script>
                Swal.fire({
                    icon: 'error',
                    title: 'Gagal',
                    text: 'Terjadi kesalahan: " . $conn->error . "'
                });
            </script>";
        }
    }
}
?>

<!-- HTML form tetap sama seperti sebelumnya -->
<div class="container mt-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2><i class="fas fa-user-edit me-2"></i>Edit Data GTK</h2>
        <a href="index.php" class="btn btn-secondary">
            <i class="fas fa-arrow-left me-1"></i> Kembali
        </a>
    </div>
    
    <div class="card border-0 shadow-lg">
        <div class="card-header bg-primary text-white py-3">
            <h5 class="card-title mb-0"><i class="fas fa-info-circle me-2"></i>Formulir Edit Data GTK</h5>
        </div>
        <div class="card-body p-4">
            <!-- Tambahkan informasi tahun ajaran aktif -->
            <div class="alert alert-info mb-4">
                <i class="fas fa-info-circle me-2"></i>
                Data akan disimpan untuk tahun ajaran: <strong><?php echo htmlspecialchars($tahun_aktif['tahun_ajaran']); ?></strong>
            </div>
            
            <form method="POST" enctype="multipart/form-data" id="formEditGtk">
                <!-- Progress Bar -->
                <div class="row mb-4">
                    <div class="col-12">
                        <div class="progress-steps">
                            <div class="step active" data-step="1">
                                <span class="step-number">1</span>
                                <span class="step-text">Data Pribadi</span>
                            </div>
                            <div class="step" data-step="2">
                                <span class="step-number">2</span>
                                <span class="step-text">Data Kepegawaian</span>
                            </div>
                            <div class="step" data-step="3">
                                <span class="step-number">3</span>
                                <span class="step-text">Data Lainnya</span>
                            </div>
                        </div>
                    </div>
                </div>
                
                <!-- Step 1: Data Pribadi -->
                <div class="step-content" id="step1">
                    <h4 class="text-primary mb-4"><i class="fas fa-user me-2"></i>Data Pribadi</h4>
                    
                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="nip" class="form-label">NIP</label>
                                <div class="input-group">
                                    <span class="input-group-text"><i class="fas fa-id-card"></i></span>
                                    <input type="text" class="form-control" id="nip" name="nip" 
                                        value="<?php echo htmlspecialchars($gtk['nip'] ?? ''); ?>" placeholder="Masukkan NIP atau '-' jika tidak memiliki">
                                </div>
                                <div class="form-text">Isi dengan tanda minus (-) jika tidak memiliki NIP</div>
                            </div>
                            
                            <div class="mb-3">
                                <label for="nuptk" class="form-label">NUPTK</label>
                                <div class="input-group">
                                    <span class="input-group-text"><i class="fas fa-id-card-alt"></i></span>
                                    <input type="text" class="form-control" id="nuptk" name="nuptk" 
                                        value="<?php echo htmlspecialchars($gtk['nuptk'] ?? ''); ?>" placeholder="Masukkan NUPTK atau '-' jika tidak memiliki">
                                </div>
                                <div class="form-text">Isi dengan tanda minus (-) jika tidak memiliki NUPTK</div>
                            </div>
                            
                            <div class="mb-3">
                                <label for="nik" class="form-label">NIK <span class="text-danger">*</span></label>
                                <div class="input-group">
                                    <span class="input-group-text"><i class="fas fa-address-card"></i></span>
                                    <input type="text" class="form-control" id="nik" name="nik" required 
                                        value="<?php echo htmlspecialchars($gtk['nik']); ?>" placeholder="Masukkan NIK">
                                </div>
                            </div>
                            
                            <div class="mb-3">
                                <label for="nama" class="form-label">Nama Lengkap <span class="text-danger">*</span></label>
                                <div class="input-group">
                                    <span class="input-group-text"><i class="fas fa-user"></i></span>
                                    <input type="text" class="form-control" id="nama" name="nama" required 
                                        value="<?php echo htmlspecialchars($gtk['nama']); ?>" placeholder="Masukkan nama lengkap">
                                </div>
                            </div>
                            
                            <div class="mb-3">
                                <label for="tempat_lahir" class="form-label">Tempat Lahir <span class="text-danger">*</span></label>
                                <div class="input-group">
                                    <span class="input-group-text"><i class="fas fa-map-marker-alt"></i></span>
                                    <input type="text" class="form-control" id="tempat_lahir" name="tempat_lahir" required 
                                        value="<?php echo htmlspecialchars($gtk['tempat_lahir']); ?>" placeholder="Masukkan tempat lahir">
                                </div>
                            </div>
                            
                            <div class="mb-3">
                                <label for="tgl_lahir" class="form-label">Tanggal Lahir <span class="text-danger">*</span></label>
                                <div class="input-group">
                                    <span class="input-group-text"><i class="fas fa-calendar"></i></span>
                                    <input type="date" class="form-control" id="tgl_lahir" name="tgl_lahir" required 
                                        value="<?php echo htmlspecialchars($gtk['tgl_lahir']); ?>">
                                </div>
                            </div>
                        </div>
                        
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="jenis_kelamin" class="form-label">Jenis Kelamin <span class="text-danger">*</span></label>
                                <div class="input-group">
                                    <span class="input-group-text"><i class="fas fa-venus-mars"></i></span>
                                    <select class="form-select" id="jenis_kelamin" name="jenis_kelamin" required>
                                        <option value="">Pilih Jenis Kelamin</option>
                                        <option value="L" <?php echo ($gtk['jenis_kelamin'] == 'L') ? 'selected' : ''; ?>>Laki-laki</option>
                                        <option value="P" <?php echo ($gtk['jenis_kelamin'] == 'P') ? 'selected' : ''; ?>>Perempuan</option>
                                    </select>
                                </div>
                            </div>
                            
                            <div class="mb-3">
                                <label for="agama" class="form-label">Agama <span class="text-danger">*</span></label>
                                <div class="input-group">
                                    <span class="input-group-text"><i class="fas fa-pray"></i></span>
                                    <select class="form-select" id="agama" name="agama" required>
                                        <option value="">Pilih Agama</option>
                                        <option value="Islam" <?php echo ($gtk['agama'] == 'Islam') ? 'selected' : ''; ?>>Islam</option>
                                        <option value="Kristen" <?php echo ($gtk['agama'] == 'Kristen') ? 'selected' : ''; ?>>Kristen</option>
                                        <option value="Katolik" <?php echo ($gtk['agama'] == 'Katolik') ? 'selected' : ''; ?>>Katolik</option>
                                        <option value="Hindu" <?php echo ($gtk['agama'] == 'Hindu') ? 'selected' : ''; ?>>Hindu</option>
                                        <option value="Budha" <?php echo ($gtk['agama'] == 'Budha') ? 'selected' : ''; ?>>Budha</option>
                                        <option value="Khonghucu" <?php echo ($gtk['agama'] == 'Khonghucu') ? 'selected' : ''; ?>>Khonghucu</option>
                                        <option value="Lainnya" <?php echo ($gtk['agama'] == 'Lainnya') ? 'selected' : ''; ?>>Lainnya</option>
                                    </select>
                                </div>
                            </div>
                            
                            <div class="mb-3">
                                <label for="status_pernikahan" class="form-label">Status Pernikahan <span class="text-danger">*</span></label>
                                <div class="input-group">
                                    <span class="input-group-text"><i class="fas fa-heart"></i></span>
                                    <select class="form-select" id="status_pernikahan" name="status_pernikahan" required>
                                        <option value="">Pilih Status</option>
                                        <option value="Belum Kawin" <?php echo ($gtk['status_pernikahan'] == 'Belum Kawin') ? 'selected' : ''; ?>>Belum Kawin</option>
                                        <option value="Kawin" <?php echo ($gtk['status_pernikahan'] == 'Kawin') ? 'selected' : ''; ?>>Kawin</option>
                                        <option value="Cerai Hidup" <?php echo ($gtk['status_pernikahan'] == 'Cerai Hidup') ? 'selected' : ''; ?>>Cerai Hidup</option>
                                        <option value="Cerai Mati" <?php echo ($gtk['status_pernikahan'] == 'Cerai Mati') ? 'selected' : ''; ?>>Cerai Mati</option>
                                    </select>
                                </div>
                            </div>
                            
                            <div class="mb-3">
                                <label for="alamat" class="form-label">Alamat <span class="text-danger">*</span></label>
                                <div class="input-group">
                                    <span class="input-group-text"><i class="fas fa-home"></i></span>
                                    <textarea class="form-control" id="alamat" name="alamat" rows="3" required 
                                        placeholder="Masukkan alamat lengkap"><?php echo htmlspecialchars($gtk['alamat']); ?></textarea>
                                </div>
                            </div>
                            
                            <div class="mb-3">
                                <label for="no_hp" class="form-label">No. HP <span class="text-danger">*</span></label>
                                <div class="input-group">
                                    <span class="input-group-text"><i class="fas fa-phone"></i></span>
                                    <input type="text" class="form-control" id="no_hp" name="no_hp" required 
                                        value="<?php echo htmlspecialchars($gtk['no_hp']); ?>" placeholder="Masukkan nomor handphone">
                                </div>
                            </div>
                            
                            <div class="mb-3">
                                <label for="email" class="form-label">Email</label>
                                <div class="input-group">
                                    <span class="input-group-text"><i class="fas fa-envelope"></i></span>
                                    <input type="email" class="form-control" id="email" name="email" 
                                        value="<?php echo htmlspecialchars($gtk['email'] ?? ''); ?>" placeholder="Masukkan email">
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="d-flex justify-content-between mt-4">
                        <div></div> <!-- Spacer -->
                        <button type="button" class="btn btn-primary next-step" data-next="2">
                            Selanjutnya <i class="fas fa-arrow-right ms-1"></i>
                        </button>
                    </div>
                </div>
                
                <!-- Step 2: Data Kepegawaian -->
                <div class="step-content d-none" id="step2">
                    <h4 class="text-primary mb-4"><i class="fas fa-briefcase me-2"></i>Data Kepegawaian</h4>
                    
                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="sumber_gaji" class="form-label">Sumber Gaji <span class="text-danger">*</span></label>
                                <div class="input-group">
                                    <span class="input-group-text"><i class="fas fa-money-bill-wave"></i></span>
                                    <select class="form-select" id="sumber_gaji" name="sumber_gaji" required>
                                        <option value="">Pilih Sumber Gaji</option>
                                        <option value="APBN" <?php echo ($gtk['sumber_gaji'] == 'APBN') ? 'selected' : ''; ?>>APBN</option>
                                        <option value="APBD" <?php echo ($gtk['sumber_gaji'] == 'APBD') ? 'selected' : ''; ?>>APBD</option>
                                        <option value="Sekolah" <?php echo ($gtk['sumber_gaji'] == 'Sekolah') ? 'selected' : ''; ?>>Sekolah</option>
                                        <option value="Yayasan" <?php echo ($gtk['sumber_gaji'] == 'Yayasan') ? 'selected' : ''; ?>>Yayasan</option>
                                        <option value="Lainnya" <?php echo ($gtk['sumber_gaji'] == 'Lainnya') ? 'selected' : ''; ?>>Lainnya</option>
                                    </select>
                                </div>
                            </div>
                            
                            <div class="mb-3">
                                <label for="jenis_gtk" class="form-label">Jenis GTK <span class="text-danger">*</span></label>
                                <div class="input-group">
                                    <span class="input-group-text"><i class="fas fa-user-tie"></i></span>
                                    <select class="form-select" id="jenis_gtk" name="jenis_gtk" required>
                                        <option value="">Pilih Jenis GTK</option>
                                        <option value="Guru Mapel" <?php echo ($gtk['jenis_gtk'] == 'Guru Mapel') ? 'selected' : ''; ?>>Guru Mapel</option>
                                        <option value="Guru BK" <?php echo ($gtk['jenis_gtk'] == 'Guru BK') ? 'selected' : ''; ?>>Guru BK</option>
                                        <option value="Kepala Sekolah" <?php echo ($gtk['jenis_gtk'] == 'Kepala Sekolah') ? 'selected' : ''; ?>>Kepala Sekolah</option>
                                        <option value="Tendik" <?php echo ($gtk['jenis_gtk'] == 'Tendik') ? 'selected' : ''; ?>>Tendik</option>
                                        <option value="Lainnya" <?php echo ($gtk['jenis_gtk'] == 'Lainnya') ? 'selected' : ''; ?>>Lainnya</option>
                                    </select>
                                </div>
                            </div>
                            
                            <div class="mb-3">
                                <label for="status_kepegawaian" class="form-label">Status Kepegawaian <span class="text-danger">*</span></label>
                                <div class="input-group">
                                    <span class="input-group-text"><i class="fas fa-briefcase"></i></span>
                                    <select class="form-select" id="status_kepegawaian" name="status_kepegawaian" required>
                                        <option value="">Pilih Status</option>
                                        <option value="PNS" <?php echo ($gtk['status_kepegawaian'] == 'PNS') ? 'selected' : ''; ?>>PNS</option>
                                        <option value="PPPK" <?php echo ($gtk['status_kepegawaian'] == 'PPPK') ? 'selected' : ''; ?>>PPPK</option>
                                        <option value="GTT" <?php echo ($gtk['status_kepegawaian'] == 'GTT') ? 'selected' : ''; ?>>GTT</option>
                                        <option value="PTT" <?php echo ($gtk['status_kepegawaian'] == 'PTT') ? 'selected' : ''; ?>>PTT</option>
                                    </select>
                                </div>
                            </div>
                            
                            <div class="mb-3">
                                <label for="pendidikan_terakhir" class="form-label">Pendidikan Terakhir <span class="text-danger">*</span></label>
                                <div class="input-group">
                                    <span class="input-group-text"><i class="fas fa-graduation-cap"></i></span>
                                    <select class="form-select" id="pendidikan_terakhir" name="pendidikan_terakhir" required>
                                        <option value="">Pilih Pendidikan</option>
                                        <option value="SMA" <?php echo ($gtk['pendidikan_terakhir'] == 'SMA') ? 'selected' : ''; ?>>SMA</option>
                                        <option value="D3" <?php echo ($gtk['pendidikan_terakhir'] == 'D3') ? 'selected' : ''; ?>>D3</option>
                                        <option value="S1" <?php echo ($gtk['pendidikan_terakhir'] == 'S1') ? 'selected' : ''; ?>>S1</option>
                                        <option value="S2" <?php echo ($gtk['pendidikan_terakhir'] == 'S2') ? 'selected' : ''; ?>>S2</option>
                                        <option value="S3" <?php echo ($gtk['pendidikan_terakhir'] == 'S3') ? 'selected' : ''; ?>>S3</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                        
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="mata_pelajaran" class="form-label">Mata Pelajaran</label>
                                <div class="input-group">
                                    <span class="input-group-text"><i class="fas fa-book"></i></span>
                                    <input type="text" class="form-control" id="mata_pelajaran" name="mata_pelajaran" 
                                        value="<?php echo htmlspecialchars($gtk['mata_pelajaran'] ?? ''); ?>" placeholder="Masukkan mata pelajaran">
                                </div>
                            </div>
                            
                            <div class="mb-3">
                                <div class="form-check">
                                    <input class="form-check-input" type="checkbox" id="tunjangan_sertifikasi" name="tunjangan_sertifikasi" value="1"
                                        <?php echo ($gtk['tunjangan_sertifikasi'] == 1) ? 'checked' : ''; ?>>
                                    <label class="form-check-label" for="tunjangan_sertifikasi">
                                        <i class="fas fa-award me-1"></i> Menerima Tunjangan Sertifikasi
                                    </label>
                                </div>
                            </div>
                            
                            <div class="mb-3">
                                <label for="jabatan_fungsional" class="form-label">Jabatan Fungsional</label>
                                <div class="input-group">
                                    <span class="input-group-text"><i class="fas fa-user-graduate"></i></span>
                                    <input type="text" class="form-control" id="jabatan_fungsional" name="jabatan_fungsional" 
                                        value="<?php echo htmlspecialchars($gtk['jabatan_fungsional'] ?? ''); ?>" placeholder="Masukkan jabatan fungsional">
                                </div>
                            </div>
                            
                            <div class="mb-3">
                                <label for="golongan" class="form-label">Golongan</label>
                                <div class="input-group">
                                    <span class="input-group-text"><i class="fas fa-layer-group"></i></span>
                                    <input type="text" class="form-control" id="golongan" name="golongan" 
                                        value="<?php echo htmlspecialchars($gtk['golongan'] ?? ''); ?>" placeholder="Masukkan golongan">
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="d-flex justify-content-between mt-4">
                        <button type="button" class="btn btn-secondary prev-step" data-prev="1">
                            <i class="fas fa-arrow-left me-1"></i> Sebelumnya
                        </button>
                        <button type="button" class="btn btn-primary next-step" data-next="3">
                            Selanjutnya <i class="fas fa-arrow-right ms-1"></i>
                        </button>
                    </div>
                </div>
                
                <!-- Step 3: Data Lainnya -->
                <div class="step-content d-none" id="step3">
                    <h4 class="text-primary mb-4"><i class="fas fa-info-circle me-2"></i>Data Lainnya</h4>
                    
                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="tmt_kerja" class="form-label">TMT Kerja</label>
                                <div class="input-group">
                                    <span class="input-group-text"><i class="fas fa-calendar-day"></i></span>
                                    <input type="date" class="form-control" id="tmt_kerja" name="tmt_kerja" 
                                        value="<?php echo htmlspecialchars($gtk['tmt_kerja'] ?? ''); ?>">
                                </div>
                            </div>
                            
                            <div class="mb-3">
                                <div class="form-check">
                                    <input class="form-check-input" type="checkbox" id="status_aktif" name="status_aktif" value="1"
                                        <?php echo ($gtk['status_aktif'] == 1) ? 'checked' : ''; ?>>
                                    <label class="form-check-label" for="status_aktif">
                                        <i class="fas fa-check-circle me-1"></i> Status Aktif
                                    </label>
                                </div>
                            </div>
                        </div>
                        
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="foto" class="form-label">Foto</label>
                                <div class="input-group">
                                    <span class="input-group-text"><i class="fas fa-camera"></i></span>
                                    <input type="file" class="form-control" id="foto" name="foto" accept="image/*">
                                </div>
                                <div class="form-text">Maksimal ukuran file: 2MB. Format: JPG, PNG, JPEG</div>
                                
                                <?php if (!empty($gtk['foto'])): ?>
                                <div class="mt-3">
                                    <p>Foto Saat Ini:</p>
                                    <img src="../uploads/foto-gtk/<?php echo htmlspecialchars($gtk['foto']); ?>" 
                                         alt="Foto GTK" class="img-thumbnail" style="max-height: 150px;">
                                </div>
                                <?php endif; ?>
                                
                                <div class="mt-2" id="preview-container" style="display: none;">
                                    <p>Preview Foto Baru:</p>
                                    <img id="preview-image" src="#" alt="Preview" class="img-thumbnail mt-2" style="max-height: 150px;">
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="d-flex justify-content-between mt-4">
                        <button type="button" class="btn btn-secondary prev-step" data-prev="2">
                            <i class="fas fa-arrow-left me-1"></i> Sebelumnya
                        </button>
                        <button type="submit" class="btn btn-success">
                            <i class="fas fa-save me-1"></i> Simpan Perubahan
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- JavaScript dan CSS tetap sama -->
<script>
document.addEventListener('DOMContentLoaded', function() {
    // Multi-step form functionality
    const nextButtons = document.querySelectorAll('.next-step');
    const prevButtons = document.querySelectorAll('.prev-step');
    const steps = document.querySelectorAll('.step');
    const stepContents = document.querySelectorAll('.step-content');
    
    nextButtons.forEach(button => {
        button.addEventListener('click', function() {
            const currentStep = this.closest('.step-content').id.replace('step', '');
            const nextStep = this.getAttribute('data-next');
            
            // Validate current step before proceeding
            let isValid = true;
            const currentInputs = document.querySelectorAll(`#step${currentStep} [required]`);
            
            currentInputs.forEach(input => {
                if (!input.value.trim()) {
                    isValid = false;
                    input.classList.add('is-invalid');
                } else {
                    input.classList.remove('is-invalid');
                }
            });
            
            if (!isValid) {
                Swal.fire({
                    icon: 'warning',
                    title: 'Perhatian',
                    text: 'Harap isi semua field yang wajib diisi!',
                    confirmButtonColor: '#3085d6'
                });
                return;
            }
            
            // Move to next step
            document.getElementById(`step${currentStep}`).classList.add('d-none');
            document.getElementById(`step${nextStep}`).classList.remove('d-none');
            
            // Update progress steps
            steps.forEach(step => {
                step.classList.remove('active');
                if (parseInt(step.getAttribute('data-step')) <= parseInt(nextStep)) {
                    step.classList.add('active');
                }
            });
            
            // Scroll to top of form
            document.querySelector('.card-body').scrollTop = 0;
        });
    });
    
    prevButtons.forEach(button => {
        button.addEventListener('click', function() {
            const currentStep = this.closest('.step-content').id.replace('step', '');
            const prevStep = this.getAttribute('data-prev');
            
            document.getElementById(`step${currentStep}`).classList.add('d-none');
            document.getElementById(`step${prevStep}`).classList.remove('d-none');
            
            // Update progress steps
            steps.forEach(step => {
                step.classList.remove('active');
                if (parseInt(step.getAttribute('data-step')) <= parseInt(prevStep)) {
                    step.classList.add('active');
                }
            });
            
            // Scroll to top of form
            document.querySelector('.card-body').scrollTop = 0;
        });
    });
    
    // Image preview functionality
    const fotoInput = document.getElementById('foto');
    const previewContainer = document.getElementById('preview-container');
    const previewImage = document.getElementById('preview-image');
    
    fotoInput.addEventListener('change', function() {
        const file = this.files[0];
        if (file) {
            const reader = new FileReader();
            
            reader.addEventListener('load', function() {
                previewImage.setAttribute('src', this.result);
                previewContainer.style.display = 'block';
            });
            
            reader.readAsDataURL(file);
        } else {
            previewContainer.style.display = 'none';
        }
    });
    
    // Form validation before submit
    document.getElementById('formEditGtk').addEventListener('submit', function(e) {
        let isValid = true;
        const requiredInputs = this.querySelectorAll('[required]');
        
        requiredInputs.forEach(input => {
            if (!input.value.trim()) {
                isValid = false;
                input.classList.add('is-invalid');
            } else {
                input.classList.remove('is-invalid');
            }
        });
        
        if (!isValid) {
            e.preventDefault();
            Swal.fire({
                icon: 'warning',
                title: 'Perhatian',
                text: 'Harap isi semua field yang wajib diisi!',
                confirmButtonColor: '#3085d6'
            });
        }
    });
});
</script>

<style>
.progress-steps {
    display: flex;
    justify-content: space-between;
    margin-bottom: 2rem;
    position: relative;
}

.progress-steps::before {
    content: '';
    position: absolute;
    top: 15px;
    left: 0;
    right: 0;
    height: 2px;
    background-color: #e9ecef;
    z-index: 1;
}

.step {
    display: flex;
    flex-direction: column;
    align-items: center;
    position: relative;
    z-index: 2;
    flex: 1;
}

.step-number {
    width: 30px;
    height: 30px;
    border-radius: 50%;
    background-color: #e9ecef;
    color: #6c757d;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: bold;
    margin-bottom: 0.5rem;
    border: 2px solid #e9ecef;
}

.step.active .step-number {
    background-color: #007bff;
    color: white;
    border-color: #007bff;
}

.step-text {
    font-size: 0.875rem;
    color: #6c757d;
    text-align: center;
}

.step.active .step-text {
    color: #007bff;
    font-weight: bold;
}

.step-content {
    transition: all 0.3s ease;
}
</style>

<?php
require_once '../includes/footer.php';
?>