<?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 jadwal detail
$query = "SELECT j.*, 
          s.nis, s.nama as nama_siswa, s.jenis_kelamin, s.alamat, s.no_hp, s.nama_orangtua, s.no_hp_orangtua,
          g.nama_guru as nama_guru_bk, g.nip as nip_guru,
          ta.tahun_ajaran, ta.semester,
          sk.id_kelas, k.nama_kelas
          FROM bkcare_jadwal_konseling j
          LEFT JOIN bkcare_siswa s ON j.id_siswa = s.id_siswa
          LEFT JOIN bkcare_guru g ON j.id_guru_bk = g.id_guru
          LEFT JOIN bkcare_tahun_ajaran ta ON j.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 = j.id_tahun_ajaran
          LEFT JOIN bkcare_kelas k ON sk.id_kelas = k.id_kelas
          WHERE j.id_jadwal = $id";
$result = mysqli_query($conn, $query);
$data = mysqli_fetch_assoc($result);

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

// Ambil sesi konseling terkait (jika ada)
$konseling_query = "SELECT k.*, 
                    ks.cerita_siswa, ks.analisis_emosi, ks.refleksi,
                    rt.target_perubahan, rt.status as rencana_status
                    FROM bkcare_konseling k
                    LEFT JOIN bkcare_sesi_konseling ks ON k.id_konseling = ks.id_konseling
                    LEFT JOIN bkcare_rencana_tindakan rt ON k.id_konseling = rt.id_konseling
                    WHERE k.id_jadwal = $id
                    ORDER BY k.tanggal DESC";
$konseling_result = mysqli_query($conn, $konseling_query);
$konseling = mysqli_fetch_assoc($konseling_result);

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-blue-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-calendar-check text-blue-600 mr-3"></i>
                    Detail Jadwal Konseling #<?= $id ?>
                </h1>
                <p class="text-gray-600 text-sm mt-1">Informasi lengkap jadwal konseling</p>
            </div>
        </div>
        <div class="flex space-x-2">
            <?php if ($data['status'] != 'selesai' && $data['status'] != 'dibatalkan'): ?>
                <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 = [
            'diajukan' => ['bg' => 'yellow', 'icon' => 'clock', 'text' => 'Diajukan'],
            'disetujui' => ['bg' => 'green', 'icon' => 'check-circle', 'text' => 'Disetujui'],
            'ditolak' => ['bg' => 'red', 'icon' => 'times-circle', 'text' => 'Ditolak'],
            'selesai' => ['bg' => 'blue', 'icon' => 'flag-checkered', 'text' => 'Selesai'],
            'dibatalkan' => ['bg' => 'gray', 'icon' => 'ban', 'text' => 'Dibatalkan']
        ];
        $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">
                        Jadwal untuk tanggal: <?= tgl_indonesia($data['tanggal']) ?> jam <?= date('H:i', strtotime($data['jam'])) ?>
                    </p>
                </div>
            </div>
        </div>
    </div>

    <!-- Content Grid -->
    <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
        <!-- Main Info - Jadwal -->
        <div class="lg:col-span-2 space-y-6">
            <!-- Detail Jadwal -->
            <div class="card-stat p-6">
                <h2 class="text-lg font-semibold text-gray-800 mb-4 flex items-center">
                    <i class="fas fa-info-circle text-blue-600 mr-2"></i>
                    Detail Jadwal
                </h2>
                <div class="grid grid-cols-2 gap-4">
                    <div>
                        <label class="text-sm text-gray-500">Tanggal Konseling</label>
                        <p class="font-medium text-gray-800"><?= tgl_indonesia($data['tanggal']) ?></p>
                    </div>
                    <div>
                        <label class="text-sm text-gray-500">Jam Konseling</label>
                        <p class="font-medium text-gray-800"><?= date('H:i', strtotime($data['jam'])) ?> WIB</p>
                    </div>
                    <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 class="col-span-2">
                        <label class="text-sm text-gray-500">Keterangan</label>
                        <p class="text-gray-700 bg-gray-50 p-3 rounded-lg"><?= $data['keterangan'] ?: '-' ?></p>
                    </div>
                </div>
            </div>

            <!-- Sesi Konseling -->
            <div class="card-stat p-6">
                <h2 class="text-lg font-semibold text-gray-800 mb-4 flex items-center">
                    <i class="fas fa-comments text-green-600 mr-2"></i>
                    Sesi Konseling
                </h2>
                <?php if ($konseling): ?>
                    <div class="space-y-4">
                        <div class="border border-green-200 rounded-lg p-4 bg-green-50">
                            <div class="flex justify-between items-start mb-3">
                                <div>
                                    <p class="font-medium text-gray-800">
                                        <i class="fas fa-calendar-alt text-green-600 mr-2"></i>
                                        <?= tgl_indonesia($konseling['tanggal']) ?>
                                    </p>
                                    <p class="text-sm text-gray-600 mt-1">
                                        <i class="fas fa-tag mr-1"></i><?= $konseling['jenis_konseling'] ?>
                                        <i class="fas fa-map-marker-alt ml-3 mr-1"></i><?= $konseling['lokasi'] ?>
                                    </p>
                                </div>
                                <span class="badge-<?= $konseling['status'] == 'selesai' ? 'success' : 'info' ?> px-3 py-1 rounded-full text-xs">
                                    <i class="fas fa-<?= $konseling['status'] == 'selesai' ? 'check-circle' : 'spinner' ?> mr-1"></i>
                                    <?= ucfirst($konseling['status']) ?>
                                </span>
                            </div>
                            
                            <?php if ($konseling['cerita_siswa']): ?>
                            <div class="mt-3">
                                <label class="text-xs font-medium text-gray-500">Cerita Siswa</label>
                                <p class="text-sm text-gray-700 mt-1"><?= nl2br(htmlspecialchars($konseling['cerita_siswa'])) ?></p>
                            </div>
                            <?php endif; ?>
                            
                            <?php if ($konseling['target_perubahan']): ?>
                            <div class="mt-3">
                                <label class="text-xs font-medium text-gray-500">Rencana Tindakan</label>
                                <p class="text-sm text-gray-700 mt-1"><?= nl2br(htmlspecialchars($konseling['target_perubahan'])) ?></p>
                                <span class="badge-<?= $konseling['rencana_status'] == 'selesai' ? 'success' : 'warning' ?> text-xs mt-2 inline-block">
                                    Status Rencana: <?= ucfirst($konseling['rencana_status']) ?>
                                </span>
                            </div>
                            <?php endif; ?>
                        </div>
                        
                        <a href="../konseling/detail.php?id=<?= $konseling['id_konseling'] ?>" 
                           class="inline-flex items-center text-blue-600 hover:text-blue-800 text-sm">
                            <i class="fas fa-arrow-right mr-1"></i>Lihat Detail Konseling
                        </a>
                    </div>
                <?php else: ?>
                    <p class="text-gray-500 text-center py-4">
                        <i class="fas fa-info-circle mr-2"></i>
                        Belum ada sesi konseling untuk jadwal ini
                    </p>
                    <?php if ($data['status'] == 'disetujui'): ?>
                    <div class="mt-4 text-center">
                        <a href="../konseling/tambah.php?id_jadwal=<?= $id ?>" 
                           class="btn-primary text-sm">
                            <i class="fas fa-plus-circle mr-2"></i>Buat Sesi Konseling
                        </a>
                    </div>
                    <?php endif; ?>
                <?php endif; ?>
            </div>
        </div>

        <!-- Sidebar - Info Peserta -->
        <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>

            <!-- Data Guru BK -->
            <div class="card-stat p-6">
                <h2 class="text-lg font-semibold text-gray-800 mb-4 flex items-center">
                    <i class="fas fa-chalkboard-teacher text-purple-600 mr-2"></i>
                    Guru BK
                </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_guru_bk'] ?></span>
                    </div>
                    <div class="flex items-center">
                        <i class="fas fa-id-card text-gray-400 w-6"></i>
                        <span class="text-gray-700">NIP: <?= $data['nip_guru'] ?: '-' ?></span>
                    </div>
                </div>
            </div>

            <!-- Tombol Aksi Cepat -->
            <div class="card-stat p-4">
                <div class="space-y-2">
                    <?php if ($data['status'] == 'disetujui' && !$konseling): ?>
                    <a href="../konseling/tambah.php?id_jadwal=<?= $id ?>" 
                       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>Mulai Sesi Konseling
                    </a>
                    <?php endif; ?>
                    
                    <a href="../laporan/tambah.php?id_siswa=<?= $data['id_siswa'] ?>" 
                       class="block w-full bg-red-600 text-white text-center px-4 py-2 rounded-lg hover:bg-red-700 transition text-sm">
                        <i class="fas fa-flag mr-2"></i>Buat Laporan Masalah
                    </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'; ?>