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

checkLogin();
checkRole(['admin', 'guru_bk']);

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

// Cek apakah konseling ada
$cek_query = "SELECT k.*, s.nama as nama_siswa FROM bkcare_konseling k 
              JOIN bkcare_siswa s ON k.id_siswa = s.id_siswa 
              WHERE k.id_konseling = $id_konseling";
$cek_result = mysqli_query($conn, $cek_query);

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

$konseling = mysqli_fetch_assoc($cek_result);

// Proses form
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $cerita_siswa = mysqli_real_escape_string($conn, $_POST['cerita_siswa']);
    $analisis_emosi = mysqli_real_escape_string($conn, $_POST['analisis_emosi']);
    $refleksi = mysqli_real_escape_string($conn, $_POST['refleksi']);
    $konfrontasi = mysqli_real_escape_string($conn, $_POST['konfrontasi']);
    $penguatan = mysqli_real_escape_string($conn, $_POST['penguatan']);
    
    // Insert sesi
    $query = "INSERT INTO bkcare_sesi_konseling (id_konseling, cerita_siswa, analisis_emosi, refleksi, konfrontasi, penguatan) 
              VALUES ('$id_konseling', '$cerita_siswa', '$analisis_emosi', '$refleksi', '$konfrontasi', '$penguatan')";
    
    if (mysqli_query($conn, $query)) {
        // Update status konseling jika diperlukan
        if (isset($_POST['selesai_konseling'])) {
            $update = "UPDATE bkcare_konseling SET status = 'selesai' WHERE id_konseling = $id_konseling";
            mysqli_query($conn, $update);
        }
        
        $_SESSION['success_message'] = "Sesi konseling berhasil ditambahkan!";
        header("Location: detail.php?id=$id_konseling");
        exit();
    } else {
        $_SESSION['error_message'] = "Gagal menambahkan sesi konseling: " . mysqli_error($conn);
    }
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Tambah Sesi 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-comments me-2"></i> Tambah Sesi Konseling
                    </h1>
                    <div class="btn-toolbar mb-2 mb-md-0">
                        <a href="detail.php?id=<?= $id_konseling ?>" class="btn btn-secondary">
                            <i class="fas fa-arrow-left me-1"></i> Kembali
                        </a>
                    </div>
                </div>

                <div class="alert alert-info">
                    <i class="fas fa-info-circle me-2"></i>
                    <strong>Siswa:</strong> <?= htmlspecialchars($konseling['nama_siswa']) ?> | 
                    <strong>Tanggal:</strong> <?= date('d/m/Y', strtotime($konseling['tanggal'])) ?> | 
                    <strong>Jenis:</strong> <?= htmlspecialchars($konseling['jenis_konseling']) ?>
                </div>

                <div class="card">
                    <div class="card-body">
                        <form method="POST" action="" id="formSesi">
                            <div class="mb-3">
                                <label for="cerita_siswa" class="form-label">
                                    <i class="fas fa-book-open me-1"></i> Cerita Siswa
                                </label>
                                <textarea name="cerita_siswa" id="cerita_siswa" rows="4" 
                                          class="form-control" placeholder="Ceritakan apa yang disampaikan siswa..." required></textarea>
                                <div class="form-text">Cerita dari siswa tentang masalah yang dihadapi</div>
                            </div>
                            
                            <div class="mb-3">
                                <label for="analisis_emosi" class="form-label">
                                    <i class="fas fa-brain me-1"></i> Analisis Emosi
                                </label>
                                <input type="text" name="analisis_emosi" id="analisis_emosi" 
                                       class="form-control" placeholder="Contoh: Sedih, Marah, Cemas, Senang" required>
                                <div class="form-text">Analisis kondisi emosi siswa saat konseling</div>
                            </div>
                            
                            <div class="mb-3">
                                <label for="refleksi" class="form-label">
                                    <i class="fas fa-comment-dots me-1"></i> Refleksi
                                </label>
                                <textarea name="refleksi" id="refleksi" rows="4" 
                                          class="form-control" placeholder="Refleksi dari guru BK terhadap masalah siswa..." required></textarea>
                                <div class="form-text">Refleksi dari guru BK terhadap masalah yang dihadapi siswa</div>
                            </div>
                            
                            <div class="mb-3">
                                <label for="konfrontasi" class="form-label">
                                    <i class="fas fa-hand-fist me-1"></i> Konfrontasi
                                </label>
                                <textarea name="konfrontasi" id="konfrontasi" rows="4" 
                                          class="form-control" placeholder="Konfrontasi terhadap pemikiran/perilaku siswa..." required></textarea>
                                <div class="form-text">Konfrontasi terhadap pemikiran atau perilaku siswa yang perlu diluruskan</div>
                            </div>
                            
                            <div class="mb-3">
                                <label for="penguatan" class="form-label">
                                    <i class="fas fa-heart me-1"></i> Penguatan
                                </label>
                                <textarea name="penguatan" id="penguatan" rows="4" 
                                          class="form-control" placeholder="Penguatan positif untuk siswa..." required></textarea>
                                <div class="form-text">Penguatan positif yang diberikan kepada siswa</div>
                            </div>
                            
                            <div class="mb-3 form-check">
                                <input type="checkbox" name="selesai_konseling" id="selesai_konseling" class="form-check-input">
                                <label for="selesai_konseling" class="form-check-label">
                                    <i class="fas fa-check-circle me-1"></i> Tandai konseling selesai
                                </label>
                                <div class="form-text">Centang jika konseling sudah selesai</div>
                            </div>
                            
                            <hr>
                            <button type="submit" class="btn btn-primary">
                                <i class="fas fa-save me-1"></i> Simpan Sesi
                            </button>
                            <button type="reset" class="btn btn-secondary">
                                <i class="fas fa-undo me-1"></i> Reset
                            </button>
                        </form>
                    </div>
                </div>
            </main>
        </div>
    </div>

    <!-- Modal Konfirmasi -->
    <div class="modal fade" id="confirmModal" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">
                        <i class="fas fa-question-circle me-2"></i> Konfirmasi
                    </h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    Apakah Anda yakin ingin menyimpan sesi konseling ini?
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
                        <i class="fas fa-times me-1"></i> Batal
                    </button>
                    <button type="button" class="btn btn-primary" id="confirmSave">
                        <i class="fas fa-check me-1"></i> Ya, Simpan
                    </button>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        document.getElementById('formSesi').addEventListener('submit', function(e) {
            e.preventDefault();
            var confirmModal = new bootstrap.Modal(document.getElementById('confirmModal'));
            confirmModal.show();
            
            document.getElementById('confirmSave').onclick = function() {
                document.getElementById('formSesi').submit();
            };
        });
    </script>
</body>
</html>