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

// Periksa apakah parameter id ada
if (!isset($_GET['id']) || empty($_GET['id'])) {
    echo "<script>
        Swal.fire({
            icon: 'error',
            title: 'Error',
            text: 'ID orang tua tidak valid!'
        }).then(() => {
            window.location.href = 'index.php';
        });
    </script>";
    exit();
}

$id_orangtua = $_GET['id'];

// Query untuk mengambil data orang tua beserta data siswa (QUERY DIPERBAIKI)
$query = "SELECT 
            ot.*, 
            s.id_siswa, 
            s.nisn, 
            s.nis, 
            s.nama_lengkap as nama_siswa,
            s.jenis_kelamin as jk_siswa,
            t.tahun_ajaran
          FROM bind_orang_tua ot
          JOIN bind_siswa s ON ot.id_siswa = s.id_siswa
          JOIN bind_datasiswa ds ON s.id_siswa = ds.id_siswa
          JOIN bind_tahunajaran t ON ds.id_tahunajaran = t.id_tahunajaran
          WHERE ot.id_orangtua = '$id_orangtua'";

$result = mysqli_query($conn, $query);

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

$data = mysqli_fetch_assoc($result);

// Format penghasilan jika ada
$penghasilan = ($data['penghasilan_bulanan'] != NULL) ? 'Rp ' . number_format($data['penghasilan_bulanan'], 0, ',', '.') : '-';

// Format tanggal lahir jika ada
$tanggal_lahir = ($data['tanggal_lahir'] != NULL && $data['tanggal_lahir'] != '0000-00-00') ? 
                 date('d F Y', strtotime($data['tanggal_lahir'])) : '-';

// Hitung umur jika tanggal lahir ada
$umur = '';
if ($data['tanggal_lahir'] != NULL && $data['tanggal_lahir'] != '0000-00-00') {
    $birthDate = new DateTime($data['tanggal_lahir']);
    $today = new DateTime();
    $age = $today->diff($birthDate);
    $umur = $age->y . ' tahun';
}

// Status hidup dengan badge
$status_badge = ($data['status_hidup'] == 'Hidup') ? 
                '<span class="badge bg-success">Hidup</span>' : 
                '<span class="badge bg-danger">Meninggal</span>';
?>

<div class="container mt-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2><i class="fas fa-user me-2"></i>Detail Data Orang Tua / Wali</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=<?= $data['id_orangtua'] ?>" class="btn btn-warning">
                <i class="fas fa-edit me-1"></i> Edit
            </a>
            <button class="btn btn-danger" onclick="confirmDelete(<?= $data['id_orangtua'] ?>)">
                <i class="fas fa-trash me-1"></i> Hapus
            </button>
        </div>
    </div>
    
    <div class="row">
        <div class="col-md-4">
            <div class="card border-0 shadow-lg mb-4">
                <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 Orang Tua/Wali</h5>
                </div>
                <div class="card-body">
                    <div class="text-center mb-4">
                        <div class="bg-<?= ($data['hubungan'] == 'Ayah') ? 'primary' : (($data['hubungan'] == 'Ibu') ? 'danger' : 'warning') ?> rounded-circle d-inline-flex align-items-center justify-content-center" style="width: 100px; height: 100px;">
                            <i class="fas fa-<?= ($data['hubungan'] == 'Ayah') ? 'male' : (($data['hubungan'] == 'Ibu') ? 'female' : 'user-friends') ?> fa-3x text-white"></i>
                        </div>
                        <h4 class="mt-3"><?= htmlspecialchars($data['nama']) ?></h4>
                        <span class="badge bg-<?= ($data['hubungan'] == 'Ayah') ? 'primary' : (($data['hubungan'] == 'Ibu') ? 'danger' : 'warning') ?> fs-6">
                            <?= $data['hubungan'] ?>
                        </span>
                        <?= $status_badge ?>
                    </div>
                    
                    <div class="mb-3">
                        <strong><i class="fas fa-birthday-cake me-2 text-primary"></i>Tanggal Lahir:</strong><br>
                        <?= $tanggal_lahir ?>
                        <?= ($umur != '') ? '(' . $umur . ')' : '' ?>
                    </div>
                    
                    <div class="mb-3">
                        <strong><i class="fas fa-map-marker-alt me-2 text-primary"></i>Tempat Lahir:</strong><br>
                        <?= (!empty($data['tempat_lahir'])) ? htmlspecialchars($data['tempat_lahir']) : '-' ?>
                    </div>
                    
                    <div class="mb-3">
                        <strong><i class="fas fa-pray me-2 text-primary"></i>Agama:</strong><br>
                        <?= (!empty($data['agama'])) ? htmlspecialchars($data['agama']) : '-' ?>
                    </div>
                    
                    <div class="mb-3">
                        <strong><i class="fas fa-globe me-2 text-primary"></i>Kewarganegaraan:</strong><br>
                        <?= (!empty($data['kewarganegaraan'])) ? htmlspecialchars($data['kewarganegaraan']) : '-' ?>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="col-md-4">
            <div class="card border-0 shadow-lg mb-4">
                <div class="card-header bg-info text-white py-3">
                    <h5 class="card-title mb-0"><i class="fas fa-graduation-cap me-2"></i>Pendidikan & Pekerjaan</h5>
                </div>
                <div class="card-body">
                    <div class="mb-3">
                        <strong><i class="fas fa-graduation-cap me-2 text-info"></i>Pendidikan Terakhir:</strong><br>
                        <?= (!empty($data['pendidikan_terakhir'])) ? htmlspecialchars($data['pendidikan_terakhir']) : '-' ?>
                    </div>
                    
                    <div class="mb-3">
                        <strong><i class="fas fa-briefcase me-2 text-info"></i>Pekerjaan:</strong><br>
                        <?= (!empty($data['pekerjaan'])) ? htmlspecialchars($data['pekerjaan']) : '-' ?>
                    </div>
                    
                    <div class="mb-3">
                        <strong><i class="fas fa-money-bill-wave me-2 text-info"></i>Penghasilan Bulanan:</strong><br>
                        <?= $penghasilan ?>
                    </div>
                    
                    <div class="mb-3">
                        <strong><i class="fas fa-phone me-2 text-info"></i>No. HP:</strong><br>
                        <?= (!empty($data['no_hp'])) ? htmlspecialchars($data['no_hp']) : '-' ?>
                    </div>
                </div>
            </div>
            
            <div class="card border-0 shadow-lg">
                <div class="card-header bg-secondary text-white py-3">
                    <h5 class="card-title mb-0"><i class="fas fa-home me-2"></i>Alamat</h5>
                </div>
                <div class="card-body">
                    <?= (!empty($data['alamat'])) ? nl2br(htmlspecialchars($data['alamat'])) : '-' ?>
                </div>
            </div>
        </div>
        
        <div class="col-md-4">
            <div class="card border-0 shadow-lg">
                <div class="card-header bg-success text-white py-3">
                    <h5 class="card-title mb-0"><i class="fas fa-user-graduate me-2"></i>Data Siswa</h5>
                </div>
                <div class="card-body">
                    <div class="text-center mb-4">
                        <div class="bg-success rounded-circle d-inline-flex align-items-center justify-content-center" style="width: 80px; height: 80px;">
                            <i class="fas fa-user-graduate fa-2x text-white"></i>
                        </div>
                        <h5 class="mt-3"><?= htmlspecialchars($data['nama_siswa']) ?></h5>
                        <p class="mb-1">NISN: <?= $data['nisn'] ?></p>
                        <p class="mb-1">NIS: <?= $data['nis'] ?></p>
                        <p class="mb-1">Tahun Ajaran: <?= $data['tahun_ajaran'] ?></p>
                        <span class="badge bg-<?= ($data['jk_siswa'] == 'L') ? 'primary' : 'danger' ?>">
                            <?= ($data['jk_siswa'] == 'L') ? 'Laki-laki' : 'Perempuan' ?>
                        </span>
                    </div>
                    
                    <div class="d-grid gap-2">
                        <a href="../siswa/detail.php?id=<?= $data['id_siswa'] ?>" class="btn btn-outline-success">
                            <i class="fas fa-external-link-alt me-1"></i> Lihat Profil Lengkap Siswa
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
function confirmDelete(id) {
    Swal.fire({
        title: 'Apakah Anda yakin?',
        text: "Data orang tua/wali akan dihapus secara permanen!",
        icon: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#d33',
        cancelButtonColor: '#3085d6',
        confirmButtonText: 'Ya, Hapus!',
        cancelButtonText: 'Batal'
    }).then((result) => {
        if (result.isConfirmed) {
            window.location.href = 'hapus.php?id=' + id;
        }
    });
}
</script>

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