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

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

// 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;
}

// Format tanggal
function formatTanggal($date) {
    if (!$date || $date == '0000-00-00') return '-';
    return date('d/m/Y', strtotime($date));
}

// Format status aktif
function formatStatusAktif($status) {
    return $status == 1 ? 
        '<span class="badge bg-success">Aktif</span>' : 
        '<span class="badge bg-secondary">Non-Aktif</span>';
}

// Format tunjangan sertifikasi
function formatTunjangan($status) {
    return $status == 1 ? 
        '<span class="badge bg-success">Ya</span>' : 
        '<span class="badge bg-secondary">Tidak</span>';
}
?>

<div class="container mt-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2><i class="fas fa-user-circle me-2"></i>Detail Data GTK</h2>
        <div>
            <a href="index.php" class="btn btn-secondary">
                <i class="fas fa-arrow-left me-1"></i> Kembali
            </a>
            <a href="edit.php?id=<?php echo $gtk['id_gtk']; ?>" class="btn btn-warning">
                <i class="fas fa-edit me-1"></i> Edit
            </a>
        </div>
    </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>Informasi Detail GTK</h5>
        </div>
        <div class="card-body p-4">
            <div class="row mb-4">
                <div class="col-md-3 text-center">
                    <?php if ($gtk['foto']): ?>
                        <img src="../uploads/foto-gtk/<?php echo $gtk['foto']; ?>" alt="Foto GTK" class="img-thumbnail" style="max-width: 200px;">
                    <?php else: ?>
                        <div class="bg-light rounded-circle d-flex align-items-center justify-content-center" style="width: 150px; height: 150px; margin: 0 auto;">
                            <i class="fas fa-user fa-4x text-secondary"></i>
                        </div>
                    <?php endif; ?>
                    <h4 class="mt-3"><?php echo htmlspecialchars($gtk['nama']); ?></h4>
                    <p class="text-muted"><?php echo htmlspecialchars($gtk['jenis_gtk']); ?></p>
                    <div class="mb-2"><?php echo formatStatusAktif($gtk['status_aktif']); ?></div>
                </div>
                <div class="col-md-9">
                    <div class="row">
                        <div class="col-md-6">
                            <table class="table table-borderless">
                                <tr>
                                    <th width="40%">NIP</th>
                                    <td><?php echo $gtk['nip'] ? htmlspecialchars($gtk['nip']) : '-'; ?></td>
                                </tr>
                                <tr>
                                    <th>NUPTK</th>
                                    <td><?php echo $gtk['nuptk'] ? htmlspecialchars($gtk['nuptk']) : '-'; ?></td>
                                </tr>
                                <tr>
                                    <th>NIK</th>
                                    <td><?php echo htmlspecialchars($gtk['nik']); ?></td>
                                </tr>
                                <tr>
                                    <th>Tempat, Tgl Lahir</th>
                                    <td><?php echo htmlspecialchars($gtk['tempat_lahir']) . ', ' . formatTanggal($gtk['tgl_lahir']); ?></td>
                                </tr>
                                <tr>
                                    <th>Jenis Kelamin</th>
                                    <td><?php echo $gtk['jenis_kelamin'] == 'L' ? 'Laki-laki' : 'Perempuan'; ?></td>
                                </tr>
                                <tr>
                                    <th>Agama</th>
                                    <td><?php echo htmlspecialchars($gtk['agama']); ?></td>
                                </tr>
                                <tr>
                                    <th>Status Pernikahan</th>
                                    <td><?php echo htmlspecialchars($gtk['status_pernikahan']); ?></td>
                                </tr>
                            </table>
                        </div>
                        <div class="col-md-6">
                            <table class="table table-borderless">
                                <tr>
                                    <th width="40%">Alamat</th>
                                    <td><?php echo nl2br(htmlspecialchars($gtk['alamat'])); ?></td>
                                </tr>
                                <tr>
                                    <th>No. HP</th>
                                    <td><?php echo htmlspecialchars($gtk['no_hp']); ?></td>
                                </tr>
                                <tr>
                                    <th>Email</th>
                                    <td><?php echo $gtk['email'] ? htmlspecialchars($gtk['email']) : '-'; ?></td>
                                </tr>
                                <tr>
                                    <th>Sumber Gaji</th>
                                    <td><?php echo htmlspecialchars($gtk['sumber_gaji']); ?></td>
                                </tr>
                                <tr>
                                    <th>Status Kepegawaian</th>
                                    <td><?php echo htmlspecialchars($gtk['status_kepegawaian']); ?></td>
                                </tr>
                                <tr>
                                    <th>Pendidikan Terakhir</th>
                                    <td><?php echo htmlspecialchars($gtk['pendidikan_terakhir']); ?></td>
                                </tr>
                                <tr>
                                    <th>Mata Pelajaran</th>
                                    <td><?php echo $gtk['mata_pelajaran'] ? htmlspecialchars($gtk['mata_pelajaran']) : '-'; ?></td>
                                </tr>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
            
            <hr>
            
            <div class="row">
                <div class="col-md-6">
                    <h5 class="mb-3"><i class="fas fa-info-circle me-2"></i>Informasi Lainnya</h5>
                    <table class="table table-borderless">
                        <tr>
                            <th width="40%">Tunjangan Sertifikasi</th>
                            <td><?php echo formatTunjangan($gtk['tunjangan_sertifikasi']); ?></td>
                        </tr>
                        <tr>
                            <th>Jabatan Fungsional</th>
                            <td><?php echo $gtk['jabatan_fungsional'] ? htmlspecialchars($gtk['jabatan_fungsional']) : '-'; ?></td>
                        </tr>
                        <tr>
                            <th>Golongan</th>
                            <td><?php echo $gtk['golongan'] ? htmlspecialchars($gtk['golongan']) : '-'; ?></td>
                        </tr>
                        <tr>
                            <th>TMT Kerja</th>
                            <td><?php echo formatTanggal($gtk['tmt_kerja']); ?></td>
                        </tr>
                    </table>
                </div>
                <div class="col-md-6">
                    <h5 class="mb-3"><i class="fas fa-history me-2"></i>Informasi Sistem</h5>
                    <table class="table table-borderless">
                        <tr>
                            <th width="40%">ID GTK</th>
                            <td><?php echo $gtk['id_gtk']; ?></td>
                        </tr>
                        <tr>
                            <th>Tanggal Dibuat</th>
                            <td><?php echo date('d/m/Y H:i', strtotime($gtk['created_at'])); ?></td>
                        </tr>
                        <tr>
                            <th>Terakhir Diupdate</th>
                            <td><?php echo date('d/m/Y H:i', strtotime($gtk['updated_at'])); ?></td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

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