<?php
session_start();
require_once '../../config/database.php';
require_once '../../config/function.php';

// Cek login
checkLogin();

$current_user = getCurrentUser();

// Ambil ID dari URL
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

// Ambil data siswa lengkap dengan struktur tabel yang benar
$query = "SELECT s.*, 
          k.nama_kelas, 
          u.username, 
          u.is_active as user_active,
          u.created_at as user_created,
          ta.tahun_ajaran,
          ta.semester
          FROM bkcare_siswa s
          LEFT JOIN bkcare_siswa_kelas sk ON s.id_siswa = sk.id_siswa AND sk.status = 'aktif'
          LEFT JOIN bkcare_kelas k ON sk.id_kelas = k.id_kelas
          LEFT JOIN bkcare_tahun_ajaran ta ON k.id_tahun_ajaran = ta.id_tahun_ajaran
          LEFT JOIN bkcare_users u ON s.id_user = u.id_user
          WHERE s.id_siswa = $id";
$result = mysqli_query($conn, $query);
$siswa = mysqli_fetch_assoc($result);

if (!$siswa) {
    $_SESSION['error_message'] = 'Data siswa tidak ditemukan';
    $_SESSION['error_title'] = 'Error!';
    header('Location: index.php');
    exit;
}

// Ambil riwayat konseling
$konseling_query = "SELECT k.*, g.nama_guru,
                   DATE_FORMAT(k.tanggal, '%d %M %Y') as tgl_format
                   FROM bkcare_konseling k
                   LEFT JOIN bkcare_guru g ON k.id_guru_bk = g.id_guru
                   WHERE k.id_siswa = $id
                   ORDER BY k.tanggal DESC
                   LIMIT 5";
$konseling_result = mysqli_query($conn, $konseling_query);

// Ambil riwayat laporan masalah
$laporan_query = "SELECT l.*, u.nama as nama_pelapor,
                  DATE_FORMAT(l.tanggal, '%d %M %Y') as tgl_format
                  FROM bkcare_laporan_masalah l
                  LEFT JOIN bkcare_users u ON l.id_pelapor = u.id_user
                  WHERE l.id_siswa = $id
                  ORDER BY l.tanggal DESC
                  LIMIT 5";
$laporan_result = mysqli_query($conn, $laporan_query);

// Ambil riwayat pelanggaran
$pelanggaran_query = "SELECT * FROM bkcare_pelanggaran 
                      WHERE id_siswa = $id 
                      ORDER BY tanggal DESC 
                      LIMIT 5";
$pelanggaran_result = mysqli_query($conn, $pelanggaran_query);

// Ambil riwayat perkembangan
$perkembangan_query = "SELECT * FROM bkcare_perkembangan_siswa 
                       WHERE id_siswa = $id 
                       ORDER BY tanggal DESC 
                       LIMIT 5";
$perkembangan_result = mysqli_query($conn, $perkembangan_query);

include '../../includes/header.php';
include '../../includes/navbar.php';
?>

<!-- Main Content -->
<main class="container mx-auto px-4 py-6">
    <!-- Header -->
    <div class="flex items-center mb-6">
        <a href="index.php" class="text-gray-600 hover:text-purple-600 transition mr-4">
            <i class="fas fa-arrow-left text-xl"></i>
        </a>
        <div>
            <h1 class="text-2xl font-bold text-gray-800 flex items-center">
                <i class="fas fa-user-graduate text-purple-600 mr-3"></i>
                Detail Siswa
            </h1>
            <p class="text-gray-600 text-sm mt-1">Informasi lengkap data siswa</p>
        </div>
    </div>

    <!-- Profile Card -->
    <div class="grid grid-cols-1 lg:grid-cols-3 gap-6 mb-6">
        <!-- Foto/Identitas -->
        <div class="card-stat p-6 col-span-1">
            <div class="flex flex-col items-center text-center">
                <div class="w-32 h-32 bg-gradient-to-br from-purple-100 to-purple-200 rounded-full flex items-center justify-center mb-4">
                    <i class="fas fa-user-graduate text-5xl text-purple-600"></i>
                </div>
                <h2 class="text-xl font-bold text-gray-800"><?= htmlspecialchars($siswa['nama']) ?></h2>
                <p class="text-purple-600 font-medium">NIS: <?= htmlspecialchars($siswa['nis']) ?></p>
                
                <div class="w-full mt-4 pt-4 border-t border-gray-200">
                    <div class="flex justify-between items-center mb-2">
                        <span class="text-sm text-gray-600">Status Siswa:</span>
                        <?php if ($siswa['status'] == 'aktif'): ?>
                            <span class="bg-green-100 text-green-800 px-3 py-1 rounded-full text-xs font-medium inline-flex items-center">
                                <i class="fas fa-check-circle mr-1"></i>Aktif
                            </span>
                        <?php elseif ($siswa['status'] == 'lulus'): ?>
                            <span class="bg-blue-100 text-blue-800 px-3 py-1 rounded-full text-xs font-medium inline-flex items-center">
                                <i class="fas fa-graduation-cap mr-1"></i>Lulus
                            </span>
                        <?php else: ?>
                            <span class="bg-yellow-100 text-yellow-800 px-3 py-1 rounded-full text-xs font-medium inline-flex items-center">
                                <i class="fas fa-exclamation-triangle mr-1"></i>Pindah
                            </span>
                        <?php endif; ?>
                    </div>
                    
                    <div class="flex justify-between items-center">
                        <span class="text-sm text-gray-600">Akun Login:</span>
                        <?php if ($siswa['id_user']): ?>
                            <span class="bg-green-100 text-green-800 px-3 py-1 rounded-full text-xs font-medium inline-flex items-center" 
                                  data-bs-toggle="tooltip" 
                                  title="Username: <?= $siswa['username'] ?>">
                                <i class="fas fa-check-circle mr-1"></i>Tersedia
                            </span>
                        <?php else: ?>
                            <span class="bg-red-100 text-red-800 px-3 py-1 rounded-full text-xs font-medium inline-flex items-center">
                                <i class="fas fa-times-circle mr-1"></i>Tidak Ada
                            </span>
                        <?php endif; ?>
                    </div>
                </div>
                
                <div class="flex space-x-2 mt-4">
                    <a href="edit.php?id=<?= $id ?>" class="btn-outline-primary text-sm px-4 py-2">
                        <i class="fas fa-edit mr-1"></i>Edit
                    </a>
                </div>
            </div>
        </div>

        <!-- Informasi Detail -->
        <div class="card-stat p-6 col-span-1 lg:col-span-2">
            <h3 class="text-lg font-semibold text-gray-800 mb-4 flex items-center">
                <i class="fas fa-info-circle text-purple-600 mr-2"></i>
                Informasi Pribadi
            </h3>
            
            <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                <div>
                    <p class="text-xs text-gray-500 mb-1">Jenis Kelamin</p>
                    <p class="text-gray-800 font-medium">
                        <?php if ($siswa['jenis_kelamin'] == 'L'): ?>
                            <i class="fas fa-mars text-blue-500 mr-1"></i>Laki-laki
                        <?php else: ?>
                            <i class="fas fa-venus text-pink-500 mr-1"></i>Perempuan
                        <?php endif; ?>
                    </p>
                </div>
                
                <div>
                    <p class="text-xs text-gray-500 mb-1">Kelas Saat Ini</p>
                    <p class="text-gray-800 font-medium">
                        <i class="fas fa-school text-purple-500 mr-1"></i>
                        <?= htmlspecialchars($siswa['nama_kelas'] ?? 'Belum ditempatkan') ?>
                        <?php if ($siswa['tahun_ajaran']): ?>
                            <span class="text-xs text-gray-500 block">
                                (TA: <?= $siswa['tahun_ajaran'] ?> - <?= ucfirst($siswa['semester']) ?>)
                            </span>
                        <?php endif; ?>
                    </p>
                </div>
                
                <div>
                    <p class="text-xs text-gray-500 mb-1">No. HP Siswa</p>
                    <p class="text-gray-800 font-medium">
                        <?php if ($siswa['no_hp']): ?>
                            <i class="fab fa-whatsapp text-green-500 mr-1"></i>
                            <?= htmlspecialchars($siswa['no_hp']) ?>
                        <?php else: ?>
                            <span class="text-gray-400">-</span>
                        <?php endif; ?>
                    </p>
                </div>
                
                <div>
                    <p class="text-xs text-gray-500 mb-1">Alamat</p>
                    <p class="text-gray-800 font-medium">
                        <i class="fas fa-map-marker-alt text-red-500 mr-1"></i>
                        <?= htmlspecialchars($siswa['alamat'] ?: '-') ?>
                    </p>
                </div>
            </div>
            
            <h3 class="text-lg font-semibold text-gray-800 mt-6 mb-4 flex items-center">
                <i class="fas fa-users text-purple-600 mr-2"></i>
                Informasi Orang Tua
            </h3>
            
            <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                <div>
                    <p class="text-xs text-gray-500 mb-1">Nama Orang Tua</p>
                    <p class="text-gray-800 font-medium">
                        <i class="fas fa-user-tie text-purple-500 mr-1"></i>
                        <?= htmlspecialchars($siswa['nama_orangtua'] ?: '-') ?>
                    </p>
                </div>
                
                <div>
                    <p class="text-xs text-gray-500 mb-1">No. HP Orang Tua</p>
                    <p class="text-gray-800 font-medium">
                        <?php if ($siswa['no_hp_orangtua']): ?>
                            <i class="fab fa-whatsapp text-green-500 mr-1"></i>
                            <?= htmlspecialchars($siswa['no_hp_orangtua']) ?>
                        <?php else: ?>
                            <span class="text-gray-400">-</span>
                        <?php endif; ?>
                    </p>
                </div>
            </div>
            
            <?php if ($siswa['id_user']): ?>
            <h3 class="text-lg font-semibold text-gray-800 mt-6 mb-4 flex items-center">
                <i class="fas fa-user-circle text-purple-600 mr-2"></i>
                Informasi Akun
            </h3>
            
            <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                <div>
                    <p class="text-xs text-gray-500 mb-1">Username</p>
                    <p class="text-gray-800 font-medium">
                        <i class="fas fa-user text-purple-500 mr-1"></i>
                        <?= htmlspecialchars($siswa['username']) ?>
                    </p>
                </div>
                
                <div>
                    <p class="text-xs text-gray-500 mb-1">Status Akun</p>
                    <p class="text-gray-800 font-medium">
                        <?php if ($siswa['user_active'] == '1'): ?>
                            <span class="bg-green-100 text-green-800 px-3 py-1 rounded-full text-xs font-medium inline-flex items-center">
                                <i class="fas fa-check-circle mr-1"></i>Aktif
                            </span>
                        <?php else: ?>
                            <span class="bg-red-100 text-red-800 px-3 py-1 rounded-full text-xs font-medium inline-flex items-center">
                                <i class="fas fa-times-circle mr-1"></i>Nonaktif
                            </span>
                        <?php endif; ?>
                    </p>
                </div>
                
                <div>
                    <p class="text-xs text-gray-500 mb-1">Password Default</p>
                    <p class="text-gray-800 font-medium">
                        <i class="fas fa-key text-yellow-500 mr-1"></i>
                        <span class="font-mono">123</span>
                        <span class="text-xs text-gray-500 ml-1">(disarankan untuk diganti)</span>
                    </p>
                </div>
                
                <div>
                    <p class="text-xs text-gray-500 mb-1">Akun Dibuat</p>
                    <p class="text-gray-800 font-medium">
                        <i class="fas fa-calendar-alt text-purple-500 mr-1"></i>
                        <?= date('d M Y', strtotime($siswa['user_created'] ?? $siswa['created_at'])) ?>
                    </p>
                </div>
            </div>
            <?php endif; ?>
        </div>
    </div>

    <!-- Riwayat Konseling -->
    <div class="card-stat p-6 mb-6">
        <div class="flex justify-between items-center mb-4">
            <h3 class="text-lg font-semibold text-gray-800 flex items-center">
                <i class="fas fa-hand-holding-heart text-purple-600 mr-2"></i>
                Riwayat Konseling (5 Terakhir)
            </h3>
            <a href="../konseling/?id_siswa=<?= $id ?>" class="text-sm text-purple-600 hover:text-purple-800">
                Lihat Semua <i class="fas fa-arrow-right ml-1"></i>
            </a>
        </div>
        
        <div class="overflow-x-auto">
            <table class="w-full">
                <thead class="bg-gray-50">
                    <tr>
                        <th class="px-4 py-2 text-left text-xs font-medium text-gray-500">Tanggal</th>
                        <th class="px-4 py-2 text-left text-xs font-medium text-gray-500">Guru BK</th>
                        <th class="px-4 py-2 text-left text-xs font-medium text-gray-500">Jenis Konseling</th>
                        <th class="px-4 py-2 text-left text-xs font-medium text-gray-500">Status</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (mysqli_num_rows($konseling_result) > 0): ?>
                        <?php while($row = mysqli_fetch_assoc($konseling_result)): ?>
                            <tr class="border-b border-gray-100 hover:bg-purple-50">
                                <td class="px-4 py-3 text-sm"><?= $row['tgl_format'] ?></td>
                                <td class="px-4 py-3 text-sm"><?= htmlspecialchars($row['nama_guru'] ?? '-') ?></td>
                                <td class="px-4 py-3 text-sm"><?= htmlspecialchars($row['jenis_konseling']) ?></td>
                                <td class="px-4 py-3">
                                    <span class="<?= $row['status'] == 'selesai' ? 'bg-green-100 text-green-800' : 'bg-yellow-100 text-yellow-800' ?> px-3 py-1 rounded-full text-xs font-medium inline-flex items-center">
                                        <?= ucfirst($row['status']) ?>
                                    </span>
                                </td>
                            </tr>
                        <?php endwhile; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="4" class="px-4 py-4 text-center text-gray-500">
                                Belum ada riwayat konseling
                            </td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>

    <!-- Riwayat Laporan, Pelanggaran, dan Perkembangan -->
    <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
        <!-- Laporan Masalah -->
        <div class="card-stat p-6">
            <div class="flex justify-between items-center mb-4">
                <h3 class="text-lg font-semibold text-gray-800 flex items-center">
                    <i class="fas fa-exclamation-triangle text-yellow-500 mr-2"></i>
                    Laporan Masalah
                </h3>
                <a href="../laporan/?id_siswa=<?= $id ?>" class="text-sm text-purple-600 hover:text-purple-800">
                    Lihat Semua <i class="fas fa-arrow-right ml-1"></i>
                </a>
            </div>
            
            <div class="overflow-x-auto">
                <table class="w-full">
                    <thead class="bg-gray-50">
                        <tr>
                            <th class="px-4 py-2 text-left text-xs font-medium text-gray-500">Tanggal</th>
                            <th class="px-4 py-2 text-left text-xs font-medium text-gray-500">Jenis</th>
                            <th class="px-4 py-2 text-left text-xs font-medium text-gray-500">Status</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (mysqli_num_rows($laporan_result) > 0): ?>
                            <?php while($row = mysqli_fetch_assoc($laporan_result)): ?>
                                <tr class="border-b border-gray-100 hover:bg-purple-50">
                                    <td class="px-4 py-3 text-sm"><?= $row['tgl_format'] ?></td>
                                    <td class="px-4 py-3 text-sm"><?= htmlspecialchars($row['jenis_masalah']) ?></td>
                                    <td class="px-4 py-3">
                                        <?php
                                        $badge_class = '';
                                        switch($row['status']) {
                                            case 'baru': $badge_class = 'bg-yellow-100 text-yellow-800'; break;
                                            case 'diproses': $badge_class = 'bg-blue-100 text-blue-800'; break;
                                            case 'selesai': $badge_class = 'bg-green-100 text-green-800'; break;
                                        }
                                        ?>
                                        <span class="<?= $badge_class ?> px-3 py-1 rounded-full text-xs font-medium inline-flex items-center">
                                            <?= ucfirst($row['status']) ?>
                                        </span>
                                    </td>
                                </tr>
                            <?php endwhile; ?>
                        <?php else: ?>
                            <tr>
                                <td colspan="3" class="px-4 py-4 text-center text-gray-500">
                                    Tidak ada laporan
                                </td>
                            </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>

        <!-- Pelanggaran -->
        <div class="card-stat p-6">
            <div class="flex justify-between items-center mb-4">
                <h3 class="text-lg font-semibold text-gray-800 flex items-center">
                    <i class="fas fa-ban text-red-500 mr-2"></i>
                    Pelanggaran
                </h3>
                <a href="../pelanggaran/?id_siswa=<?= $id ?>" class="text-sm text-purple-600 hover:text-purple-800">
                    Lihat Semua <i class="fas fa-arrow-right ml-1"></i>
                </a>
            </div>
            
            <div class="overflow-x-auto">
                <table class="w-full">
                    <thead class="bg-gray-50">
                        <tr>
                            <th class="px-4 py-2 text-left text-xs font-medium text-gray-500">Tanggal</th>
                            <th class="px-4 py-2 text-left text-xs font-medium text-gray-500">Jenis</th>
                            <th class="px-4 py-2 text-left text-xs font-medium text-gray-500">Poin</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (mysqli_num_rows($pelanggaran_result) > 0): ?>
                            <?php while($row = mysqli_fetch_assoc($pelanggaran_result)): ?>
                                <tr class="border-b border-gray-100 hover:bg-purple-50">
                                    <td class="px-4 py-3 text-sm"><?= date('d M Y', strtotime($row['tanggal'])) ?></td>
                                    <td class="px-4 py-3 text-sm"><?= htmlspecialchars($row['jenis_pelanggaran']) ?></td>
                                    <td class="px-4 py-3">
                                        <span class="bg-red-100 text-red-800 px-3 py-1 rounded-full text-xs font-medium inline-flex items-center">
                                            <?= $row['poin'] ?> Poin
                                        </span>
                                    </td>
                                </tr>
                            <?php endwhile; ?>
                        <?php else: ?>
                            <tr>
                                <td colspan="3" class="px-4 py-4 text-center text-gray-500">
                                    Tidak ada pelanggaran
                                </td>
                            </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>

        <!-- Perkembangan Siswa -->
        <div class="card-stat p-6">
            <div class="flex justify-between items-center mb-4">
                <h3 class="text-lg font-semibold text-gray-800 flex items-center">
                    <i class="fas fa-chart-line text-green-500 mr-2"></i>
                    Perkembangan
                </h3>
                <a href="../perkembangan/?id_siswa=<?= $id ?>" class="text-sm text-purple-600 hover:text-purple-800">
                    Lihat Semua <i class="fas fa-arrow-right ml-1"></i>
                </a>
            </div>
            
            <div class="overflow-x-auto">
                <table class="w-full">
                    <thead class="bg-gray-50">
                        <tr>
                            <th class="px-4 py-2 text-left text-xs font-medium text-gray-500">Tanggal</th>
                            <th class="px-4 py-2 text-left text-xs font-medium text-gray-500">Kategori</th>
                            <th class="px-4 py-2 text-left text-xs font-medium text-gray-500">Catatan</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (mysqli_num_rows($perkembangan_result) > 0): ?>
                            <?php while($row = mysqli_fetch_assoc($perkembangan_result)): ?>
                                <tr class="border-b border-gray-100 hover:bg-purple-50">
                                    <td class="px-4 py-3 text-sm"><?= date('d M Y', strtotime($row['tanggal'])) ?></td>
                                    <td class="px-4 py-3">
                                        <span class="bg-blue-100 text-blue-800 px-3 py-1 rounded-full text-xs font-medium inline-flex items-center">
                                            <?= ucfirst($row['kategori']) ?>
                                        </span>
                                    </td>
                                    <td class="px-4 py-3 text-sm"><?= htmlspecialchars(substr($row['catatan'], 0, 50)) ?>...</td>
                                </tr>
                            <?php endwhile; ?>
                        <?php else: ?>
                            <tr>
                                <td colspan="3" class="px-4 py-4 text-center text-gray-500">
                                    Belum ada catatan perkembangan
                                </td>
                            </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</main>

<script>
$(document).ready(function() {
    // Tooltip initialization
    var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
    tooltipTriggerList.map(function(tooltipTriggerEl) {
        return new bootstrap.Tooltip(tooltipTriggerEl);
    });
});
</script>

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