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

// Cek login
checkLogin();


// Ambil data siswa dan guru BK untuk dropdown
$siswa_query = "SELECT id_siswa, nis, nama FROM bkcare_siswa WHERE status = 'aktif' ORDER BY nama";
$siswa_result = mysqli_query($conn, $siswa_query);

$guru_query = "SELECT g.id_guru, g.nama_guru, u.role 
               FROM bkcare_guru g 
               JOIN bkcare_users u ON g.id_user = u.id_user 
               WHERE u.role = 'guru_bk' AND u.is_active = 1 
               ORDER BY g.nama_guru";
$guru_result = mysqli_query($conn, $guru_query);

// Ambil tahun ajaran aktif
$ta_query = "SELECT id_tahun_ajaran, tahun_ajaran, semester 
             FROM bkcare_tahun_ajaran 
             WHERE status = 'aktif' 
             LIMIT 1";
$ta_result = mysqli_query($conn, $ta_query);
$tahun_ajaran = mysqli_fetch_assoc($ta_result);

// Proses form submission
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $id_siswa = $_POST['id_siswa'];
    $id_guru_bk = $_POST['id_guru_bk'];
    $tanggal = $_POST['tanggal'];
    $jenis_konseling = mysqli_real_escape_string($conn, $_POST['jenis_konseling']);
    $lokasi = mysqli_real_escape_string($conn, $_POST['lokasi']);
    $status = 'proses';
    
    $query = "INSERT INTO bkcare_konseling (id_siswa, id_guru_bk, id_tahun_ajaran, tanggal, jenis_konseling, lokasi, status) 
              VALUES ('$id_siswa', '$id_guru_bk', '{$tahun_ajaran['id_tahun_ajaran']}', '$tanggal', '$jenis_konseling', '$lokasi', '$status')";
    
    if (mysqli_query($conn, $query)) {
        $id_konseling = mysqli_insert_id($conn);
        
        // Tambah notifikasi
        $notif_query = "INSERT INTO bkcare_notifikasi (id_user, pesan, status) 
                        SELECT id_user, 'Konseling baru telah ditambahkan untuk Anda', 'belum_dibaca' 
                        FROM bkcare_guru WHERE id_guru = '$id_guru_bk'";
        mysqli_query($conn, $notif_query);
        
        $_SESSION['success_message'] = "Data konseling berhasil ditambahkan!";
        header("Location: index.php");
        exit();
    } else {
        $_SESSION['error_message'] = "Gagal menambahkan data konseling: " . mysqli_error($conn);
    }
}
include '../../includes/header.php';
include '../../includes/navbar.php';
?>


<style>
    .card-stat {
        background: white;
        border-radius: 1rem;
        box-shadow: 0 1px 3px rgba(0,0,0,0.1);
    }
    
    .form-icon {
        position: absolute;
        left: 15px;
        top: 50%;
        transform: translateY(-50%);
        color: #9ca3af;
    }
    
    .form-input-icon {
        padding-left: 40px;
    }
</style>

    
    <div class="container-fluid">
        <div class="row">
            
            <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-plus-circle me-2"></i> Tambah 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="card">
                    <div class="card-body">
                        <form method="POST" action="" id="formKonseling">
                            <div class="row">
                                <div class="col-md-6 mb-3">
                                    <label for="id_siswa" class="form-label">
                                        <i class="fas fa-user-graduate me-1"></i> Siswa
                                    </label>
                                    <select name="id_siswa" id="id_siswa" class="form-select" required>
                                        <option value="">Pilih Siswa</option>
                                        <?php while ($siswa = mysqli_fetch_assoc($siswa_result)): ?>
                                            <option value="<?= $siswa['id_siswa'] ?>">
                                                <?= htmlspecialchars($siswa['nama']) ?> (NIS: <?= $siswa['nis'] ?>)
                                            </option>
                                        <?php endwhile; ?>
                                    </select>
                                </div>
                                
                                <div class="col-md-6 mb-3">
                                    <label for="id_guru_bk" class="form-label">
                                        <i class="fas fa-chalkboard-user me-1"></i> Guru BK
                                    </label>
                                    <select name="id_guru_bk" id="id_guru_bk" class="form-select" required>
                                        <option value="">Pilih Guru BK</option>
                                        <?php while ($guru = mysqli_fetch_assoc($guru_result)): ?>
                                            <option value="<?= $guru['id_guru'] ?>">
                                                <?= htmlspecialchars($guru['nama_guru']) ?>
                                            </option>
                                        <?php endwhile; ?>
                                    </select>
                                </div>
                                
                                <div class="col-md-6 mb-3">
                                    <label for="tanggal" class="form-label">
                                        <i class="fas fa-calendar me-1"></i> Tanggal
                                    </label>
                                    <input type="date" name="tanggal" id="tanggal" class="form-control" 
                                           value="<?= date('Y-m-d') ?>" required>
                                </div>
                                
                                <div class="col-md-6 mb-3">
                                    <label for="jenis_konseling" class="form-label">
                                        <i class="fas fa-tag me-1"></i> Jenis Konseling
                                    </label>
                                    <input type="text" name="jenis_konseling" id="jenis_konseling" 
                                           class="form-control" placeholder="Contoh: Konseling Individu, Konseling Kelompok" required>
                                </div>
                                
                                <div class="col-md-12 mb-3">
                                    <label for="lokasi" class="form-label">
                                        <i class="fas fa-map-marker-alt me-1"></i> Lokasi
                                    </label>
                                    <input type="text" name="lokasi" id="lokasi" class="form-control" 
                                           placeholder="Ruang BK, Kelas, dll" required>
                                </div>
                                
                                <div class="col-12">
                                    <hr>
                                    <button type="submit" class="btn btn-primary">
                                        <i class="fas fa-save me-1"></i> Simpan
                                    </button>
                                    <button type="reset" class="btn btn-secondary">
                                        <i class="fas fa-undo me-1"></i> Reset
                                    </button>
                                </div>
                            </div>
                        </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 data 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://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<script>
    <script>
        document.getElementById('formKonseling').addEventListener('submit', function(e) {
            e.preventDefault();
            var confirmModal = new bootstrap.Modal(document.getElementById('confirmModal'));
            confirmModal.show();
            
            document.getElementById('confirmSave').onclick = function() {
                document.getElementById('formKonseling').submit();
            };
        });
    </script>
</body>
</html>