<?php
// modules/konseling/detail.php
require_once '../../config/database.php';
require_once '../../config/function.php';

checkLogin();

$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

// Ambil data konseling
$query = "SELECT k.*, s.nama as nama_siswa, s.nis, s.jenis_kelamin, s.alamat, s.no_hp,
          g.nama_guru as nama_guru_bk, ta.tahun_ajaran, ta.semester,
          r.target_perubahan, r.langkah_siswa, r.langkah_guru, r.tanggal_evaluasi, r.status as rencana_status
          FROM bkcare_konseling k
          JOIN bkcare_siswa s ON k.id_siswa = s.id_siswa
          LEFT JOIN bkcare_guru g ON k.id_guru_bk = g.id_guru
          JOIN bkcare_tahun_ajaran ta ON k.id_tahun_ajaran = ta.id_tahun_ajaran
          LEFT JOIN bkcare_rencana_tindakan r ON k.id_konseling = r.id_konseling
          WHERE k.id_konseling = $id";

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

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

// Ambil sesi konseling
$sesi_query = "SELECT * FROM bkcare_sesi_konseling WHERE id_konseling = $id ORDER BY id_sesi";
$sesi_result = mysqli_query($conn, $sesi_query);
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Detail Konseling - BKCare</title>
    <link rel="stylesheet" href="../../assets/css/style.css">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
</head>
<body>
    <?php include_once '../../includes/navbar.php'; ?>
    
    <div class="container-fluid">
        <div class="row">
            <?php include_once '../../includes/sidebar.php'; ?>
            
            <main class="col-md-9 ms-sm-auto col-lg-10 px-md-4">
                <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
                    <h1 class="h2">
                        <i class="fas fa-info-circle me-2"></i> Detail Konseling
                    </h1>
                    <div class="btn-toolbar mb-2 mb-md-0">
                        <a href="index.php" class="btn btn-secondary">
                            <i class="fas fa-arrow-left me-1"></i> Kembali
                        </a>
                    </div>
                </div>

                <div class="row">
                    <div class="col-md-6">
                        <div class="card mb-4">
                            <div class="card-header bg-primary text-white">
                                <h5 class="mb-0">
                                    <i class="fas fa-chalkboard-user me-2"></i> Informasi Konseling
                                </h5>
                            </div>
                            <div class="card-body">
                                <table class="table">
                                    <tr>
                                        <th width="40%"><i class="fas fa-calendar"></i> Tanggal</th>
                                        <td><?= date('d/m/Y', strtotime($data['tanggal'])) ?></td>
                                    </tr>
                                    <tr>
                                        <th><i class="fas fa-tag"></i> Jenis Konseling</th>
                                        <td><?= htmlspecialchars($data['jenis_konseling']) ?></td>
                                    </tr>
                                    <tr>
                                        <th><i class="fas fa-map-marker-alt"></i> Lokasi</th>
                                        <td><?= htmlspecialchars($data['lokasi']) ?></td>
                                    </tr>
                                    <tr>
                                        <th><i class="fas fa-info-circle"></i> Status</th>
                                        <td>
                                            <?php if ($data['status'] == 'proses'): ?>
                                                <span class="badge bg-warning">Proses</span>
                                            <?php else: ?>
                                                <span class="badge bg-success">Selesai</span>
                                            <?php endif; ?>
                                        </td>
                                    </tr>
                                    <tr>
                                        <th><i class="fas fa-graduation-cap"></i> Tahun Ajaran</th>
                                        <td><?= $data['tahun_ajaran'] ?> (<?= $data['semester'] ?>)</td>
                                    </tr>
                                </table>
                            </div>
                        </div>
                    </div>
                    
                    <div class="col-md-6">
                        <div class="card mb-4">
                            <div class="card-header bg-info text-white">
                                <h5 class="mb-0">
                                    <i class="fas fa-user-graduate me-2"></i> Data Siswa
                                </h5>
                            </div>
                            <div class="card-body">
                                <table class="table">
                                    <tr>
                                        <th width="40%"><i class="fas fa-id-card"></i> NIS</th>
                                        <td><?= $data['nis'] ?></td>
                                    </tr>
                                    <tr>
                                        <th><i class="fas fa-user"></i> Nama</th>
                                        <td><?= htmlspecialchars($data['nama_siswa']) ?></td>
                                    </tr>
                                    <tr>
                                        <th><i class="fas fa-venus-mars"></i> Jenis Kelamin</th>
                                        <td><?= $data['jenis_kelamin'] == 'L' ? 'Laki-laki' : 'Perempuan' ?></td>
                                    </tr>
                                    <tr>
                                        <th><i class="fas fa-home"></i> Alamat</th>
                                        <td><?= htmlspecialchars($data['alamat'] ?? '-') ?></td>
                                    </tr>
                                    <tr>
                                        <th><i class="fas fa-phone"></i> No. HP</th>
                                        <td><?= $data['no_hp'] ?? '-' ?></td>
                                    </tr>
                                    <tr>
                                        <th><i class="fas fa-chalkboard-user"></i> Guru BK</th>
                                        <td><?= $data['nama_guru_bk'] ?? '-' ?></td>
                                    </tr>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Sesi Konseling -->
                <div class="card mb-4">
                    <div class="card-header bg-success text-white">
                        <h5 class="mb-0">
                            <i class="fas fa-comments me-2"></i> Sesi Konseling
                            <a href="sesi.php?id=<?= $id ?>" class="btn btn-sm btn-light float-end">
                                <i class="fas fa-plus me-1"></i> Tambah Sesi
                            </a>
                        </h5>
                    </div>
                    <div class="card-body">
                        <?php if (mysqli_num_rows($sesi_result) > 0): ?>
                            <div class="accordion" id="accordionSesi">
                                <?php $no = 1; while ($sesi = mysqli_fetch_assoc($sesi_result)): ?>
                                    <div class="accordion-item">
                                        <h2 class="accordion-header">
                                            <button class="accordion-button <?= $no > 1 ? 'collapsed' : '' ?>" 
                                                    type="button" data-bs-toggle="collapse" 
                                                    data-bs-target="#collapse<?= $sesi['id_sesi'] ?>">
                                                <strong>Sesi Konseling #<?= $no++ ?></strong>
                                            </button>
                                        </h2>
                                        <div id="collapse<?= $sesi['id_sesi'] ?>" 
                                             class="accordion-collapse collapse <?= $no == 2 ? 'show' : '' ?>" 
                                             data-bs-parent="#accordionSesi">
                                            <div class="accordion-body">
                                                <div class="row">
                                                    <div class="col-md-6 mb-3">
                                                        <label class="form-label fw-bold">
                                                            <i class="fas fa-book-open"></i> Cerita Siswa
                                                        </label>
                                                        <p><?= nl2br(htmlspecialchars($sesi['cerita_siswa'] ?? '-')) ?></p>
                                                    </div>
                                                    <div class="col-md-6 mb-3">
                                                        <label class="form-label fw-bold">
                                                            <i class="fas fa-brain"></i> Analisis Emosi
                                                        </label>
                                                        <p><?= htmlspecialchars($sesi['analisis_emosi'] ?? '-') ?></p>
                                                    </div>
                                                    <div class="col-md-6 mb-3">
                                                        <label class="form-label fw-bold">
                                                            <i class="fas fa-comment-dots"></i> Refleksi
                                                        </label>
                                                        <p><?= nl2br(htmlspecialchars($sesi['refleksi'] ?? '-')) ?></p>
                                                    </div>
                                                    <div class="col-md-6 mb-3">
                                                        <label class="form-label fw-bold">
                                                            <i class="fas fa-hand-fist"></i> Konfrontasi
                                                        </label>
                                                        <p><?= nl2br(htmlspecialchars($sesi['konfrontasi'] ?? '-')) ?></p>
                                                    </div>
                                                    <div class="col-md-12 mb-3">
                                                        <label class="form-label fw-bold">
                                                            <i class="fas fa-heart"></i> Penguatan
                                                        </label>
                                                        <p><?= nl2br(htmlspecialchars($sesi['penguatan'] ?? '-')) ?></p>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                <?php endwhile; ?>
                            </div>
                        <?php else: ?>
                            <div class="alert alert-info">
                                <i class="fas fa-info-circle me-2"></i> Belum ada sesi konseling. 
                                <a href="sesi.php?id=<?= $id ?>" class="alert-link">Klik di sini untuk menambahkan sesi</a>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>

                <!-- Rencana Tindakan -->
                <?php if ($data['target_perubahan']): ?>
                    <div class="card">
                        <div class="card-header bg-warning">
                            <h5 class="mb-0">
                                <i class="fas fa-clipboard-list me-2"></i> Rencana Tindakan
                            </h5>
                        </div>
                        <div class="card-body">
                            <div class="row">
                                <div class="col-md-6 mb-3">
                                    <label class="form-label fw-bold">
                                        <i class="fas fa-bullseye"></i> Target Perubahan
                                    </label>
                                    <p><?= nl2br(htmlspecialchars($data['target_perubahan'])) ?></p>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label class="form-label fw-bold">
                                        <i class="fas fa-user-check"></i> Langkah Siswa
                                    </label>
                                    <p><?= nl2br(htmlspecialchars($data['langkah_siswa'])) ?></p>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label class="form-label fw-bold">
                                        <i class="fas fa-chalkboard-user"></i> Langkah Guru
                                    </label>
                                    <p><?= nl2br(htmlspecialchars($data['langkah_guru'])) ?></p>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label class="form-label fw-bold">
                                        <i class="fas fa-calendar-check"></i> Tanggal Evaluasi
                                    </label>
                                    <p><?= $data['tanggal_evaluasi'] ? date('d/m/Y', strtotime($data['tanggal_evaluasi'])) : '-' ?></p>
                                </div>
                            </div>
                        </div>
                    </div>
                <?php endif; ?>
            </main>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>