<?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 laporan detail
$query = "SELECT l.*, 
          s.nis, s.nama as nama_siswa, s.jenis_kelamin, s.alamat, s.no_hp, s.nama_orangtua, s.no_hp_orangtua,
          u.nama as nama_pelapor, u.role as role_pelapor,
          ta.tahun_ajaran, ta.semester,
          sk.id_kelas, k.nama_kelas
          FROM bkcare_laporan_masalah l
          LEFT JOIN bkcare_siswa s ON l.id_siswa = s.id_siswa
          LEFT JOIN bkcare_users u ON l.id_pelapor = u.id_user
          LEFT JOIN bkcare_tahun_ajaran ta ON l.id_tahun_ajaran = ta.id_tahun_ajaran
          LEFT JOIN bkcare_siswa_kelas sk ON s.id_siswa = sk.id_siswa AND sk.id_tahun_ajaran = l.id_tahun_ajaran
          LEFT JOIN bkcare_kelas k ON sk.id_kelas = k.id_kelas
          WHERE l.id_laporan = $id";
$result = mysqli_query($conn, $query);
$data = mysqli_fetch_assoc($result);

if (!$data) {
    $_SESSION['modal_title'] = 'Error!';
    $_SESSION['modal_message'] = 'Data laporan tidak ditemukan';
    $_SESSION['modal_type'] = 'danger';
    $_SESSION['modal_redirect'] = 'index.php';
    header('Location: index.php');
    exit;
}

// Ambil riwayat konseling terkait (jika ada)
$konseling_query = "SELECT k.*, j.tanggal as jadwal_tanggal, j.jam as jadwal_jam,
                    g.nama_guru
                    FROM bkcare_konseling k
                    LEFT JOIN bkcare_jadwal_konseling j ON k.id_jadwal = j.id_jadwal
                    LEFT JOIN bkcare_guru g ON k.id_guru_bk = g.id_guru
                    WHERE k.id_siswa = {$data['id_siswa']} 
                    AND k.id_tahun_ajaran = {$data['id_tahun_ajaran']}
                    ORDER BY k.tanggal DESC";
$konseling_result = mysqli_query($conn, $konseling_query);

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

<!-- Main Content -->
<main class="container mx-auto px-4 py-6">
    <!-- Header -->
    <div class="flex justify-between items-center mb-6">
        <div class="flex items-center">
            <a href="index.php" class="text-gray-600 hover:text-red-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-file-alt text-red-600 mr-3"></i>
                    Detail Laporan #<?= $id ?>
                </h1>
                <p class="text-gray-600 text-sm mt-1">Informasi lengkap laporan masalah</p>
            </div>
        </div>
        <div class="flex space-x-2">
            <?php if ($data['status'] != 'selesai'): ?>
                <a href="edit.php?id=<?= $id ?>" class="btn-outline-primary text-sm">
                    <i class="fas fa-edit mr-2"></i>Edit
                </a>
            <?php endif; ?>
            <button onclick="window.print()" class="btn-outline-primary text-sm">
                <i class="fas fa-print mr-2"></i>Cetak
            </button>
        </div>
    </div>

    <!-- Status Banner -->
    <div class="mb-6">
        <?php 
        $status_colors = [
            'baru' => ['bg' => 'yellow', 'icon' => 'exclamation-circle', 'text' => 'Baru'],
            'diproses' => ['bg' => 'blue', 'icon' => 'spinner', 'text' => 'Diproses'],
            'selesai' => ['bg' => 'green', 'icon' => 'check-circle', 'text' => 'Selesai']
        ];
        $status = $status_colors[$data['status']];
        ?>
        <div class="bg-<?= $status['bg'] ?>-100 border-l-4 border-<?= $status['bg'] ?>-500 text-<?= $status['bg'] ?>-700 p-4 rounded-lg">
            <div class="flex items-center">
                <i class="fas fa-<?= $status['icon'] ?> text-<?= $status['bg'] ?>-500 mr-3"></i>
                <div>
                    <p class="font-semibold">Status: <?= $status['text'] ?></p>
                    <p class="text-sm">
                        Dilaporkan pada: <?= tgl_indonesia($data['tanggal']) ?> 
                        oleh: <?= $data['nama_pelapor'] ?> (<?= $data['role_pelapor'] ?>)
                    </p>
                </div>
            </div>
        </div>
    </div>

    <!-- Content Grid -->
    <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
        <!-- Main Info - Laporan -->
        <div class="lg:col-span-2 space-y-6">
            <!-- Detail Laporan -->
            <div class="card-stat p-6">
                <h2 class="text-lg font-semibold text-gray-800 mb-4 flex items-center">
                    <i class="fas fa-flag text-red-600 mr-2"></i>
                    Detail Laporan
                </h2>
                <div class="space-y-4">
                    <div>
                        <label class="text-sm text-gray-500">Jenis Masalah</label>
                        <p class="font-medium text-gray-800"><?= htmlspecialchars($data['jenis_masalah']) ?></p>
                    </div>
                    <div>
                        <label class="text-sm text-gray-500">Deskripsi Masalah</label>
                        <p class="text-gray-700 bg-gray-50 p-4 rounded-lg"><?= nl2br(htmlspecialchars($data['deskripsi'])) ?></p>
                    </div>
                    <div class="grid grid-cols-2 gap-4">
                        <div>
                            <label class="text-sm text-gray-500">Tahun Ajaran</label>
                            <p class="font-medium"><?= $data['tahun_ajaran'] ?> - <?= ucfirst($data['semester']) ?></p>
                        </div>
                        <div>
                            <label class="text-sm text-gray-500">Kelas</label>
                            <p class="font-medium"><?= $data['nama_kelas'] ?: '-' ?></p>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Riwayat Konseling Terkait -->
            <div class="card-stat p-6">
                <h2 class="text-lg font-semibold text-gray-800 mb-4 flex items-center">
                    <i class="fas fa-history text-blue-600 mr-2"></i>
                    Riwayat Konseling Siswa
                </h2>
                <?php if (mysqli_num_rows($konseling_result) > 0): ?>
                    <div class="space-y-3">
                        <?php while($konseling = mysqli_fetch_assoc($konseling_result)): ?>
                            <div class="border border-gray-200 rounded-lg p-4 hover:bg-blue-50 transition">
                                <div class="flex justify-between items-start">
                                    <div>
                                        <p class="font-medium text-gray-800">
                                            Konseling dengan <?= $konseling['nama_guru'] ?>
                                        </p>
                                        <p class="text-sm text-gray-600 mt-1">
                                            <i class="fas fa-calendar mr-1"></i><?= tgl_indonesia($konseling['tanggal']) ?>
                                            <i class="fas fa-tag ml-3 mr-1"></i><?= $konseling['jenis_konseling'] ?>
                                        </p>
                                    </div>
                                    <span class="badge-<?= $konseling['status'] == 'selesai' ? 'success' : 'info' ?> px-3 py-1 rounded-full text-xs">
                                        <?= ucfirst($konseling['status']) ?>
                                    </span>
                                </div>
                            </div>
                        <?php endwhile; ?>
                    </div>
                <?php else: ?>
                    <p class="text-gray-500 text-center py-4">
                        <i class="fas fa-info-circle mr-2"></i>
                        Belum ada riwayat konseling untuk siswa ini
                    </p>
                <?php endif; ?>
            </div>
        </div>

        <!-- Sidebar - Info Siswa -->
        <div class="space-y-6">
            <!-- Data Siswa -->
            <div class="card-stat p-6">
                <h2 class="text-lg font-semibold text-gray-800 mb-4 flex items-center">
                    <i class="fas fa-user-graduate text-green-600 mr-2"></i>
                    Data Siswa
                </h2>
                <div class="space-y-3">
                    <div class="flex items-center p-3 bg-gray-50 rounded-lg">
                        <div class="w-10 h-10 bg-green-100 rounded-full flex items-center justify-center text-green-600 mr-3">
                            <i class="fas fa-user"></i>
                        </div>
                        <div>
                            <p class="font-semibold text-gray-800"><?= htmlspecialchars($data['nama_siswa']) ?></p>
                            <p class="text-xs text-gray-500">NIS: <?= $data['nis'] ?></p>
                        </div>
                    </div>
                    
                    <div class="grid grid-cols-2 gap-3 text-sm">
                        <div>
                            <label class="text-xs text-gray-500">Jenis Kelamin</label>
                            <p class="font-medium"><?= $data['jenis_kelamin'] == 'L' ? 'Laki-laki' : 'Perempuan' ?></p>
                        </div>
                        <div>
                            <label class="text-xs text-gray-500">No. HP</label>
                            <p class="font-medium"><?= $data['no_hp'] ?: '-' ?></p>
                        </div>
                    </div>
                    
                    <div>
                        <label class="text-xs text-gray-500">Alamat</label>
                        <p class="text-sm"><?= $data['alamat'] ?: '-' ?></p>
                    </div>
                    
                    <div class="border-t border-gray-200 pt-3 mt-3">
                        <label class="text-xs text-gray-500">Nama Orang Tua</label>
                        <p class="font-medium"><?= $data['nama_orangtua'] ?: '-' ?></p>
                        <p class="text-xs text-gray-600"><?= $data['no_hp_orangtua'] ?: '-' ?></p>
                    </div>
                </div>
            </div>

            <!-- Info Pelapor -->
            <div class="card-stat p-6">
                <h2 class="text-lg font-semibold text-gray-800 mb-4 flex items-center">
                    <i class="fas fa-user text-purple-600 mr-2"></i>
                    Informasi Pelapor
                </h2>
                <div class="space-y-2">
                    <div class="flex items-center">
                        <i class="fas fa-user-circle text-gray-400 w-6"></i>
                        <span class="text-gray-700"><?= $data['nama_pelapor'] ?></span>
                    </div>
                    <div class="flex items-center">
                        <i class="fas fa-tag text-gray-400 w-6"></i>
                        <span class="text-gray-700"><?= ucfirst(str_replace('_', ' ', $data['role_pelapor'])) ?></span>
                    </div>
                    <div class="flex items-center">
                        <i class="fas fa-clock text-gray-400 w-6"></i>
                        <span class="text-gray-700"><?= tgl_indonesia($data['tanggal']) ?></span>
                    </div>
                </div>
            </div>

            <!-- Tombol Aksi Cepat -->
            <div class="card-stat p-4">
                <div class="space-y-2">
                    <a href="../jadwal/tambah.php?id_siswa=<?= $data['id_siswa'] ?>" 
                       class="block w-full bg-green-600 text-white text-center px-4 py-2 rounded-lg hover:bg-green-700 transition text-sm">
                        <i class="fas fa-plus-circle mr-2"></i>Buat Jadwal Konseling
                    </a>
                    <a href="../perkembangan/tambah.php?id_siswa=<?= $data['id_siswa'] ?>" 
                       class="block w-full bg-blue-600 text-white text-center px-4 py-2 rounded-lg hover:bg-blue-700 transition text-sm">
                        <i class="fas fa-chart-line mr-2"></i>Catat Perkembangan
                    </a>
                </div>
            </div>
        </div>
    </div>
</main>

<!-- Print CSS -->
<style media="print">
    @page { size: A4; margin: 2cm; }
    body * { visibility: hidden; }
    .main-content, .main-content * { visibility: visible; }
    .main-content { position: absolute; left: 0; top: 0; width: 100%; }
    .no-print { display: none; }
    .card-stat { box-shadow: none; border: 1px solid #ddd; }
</style>

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