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

// Cek login
checkLogin();

// Cek method dan ID
if ($_SERVER['REQUEST_METHOD'] != 'GET' || !isset($_GET['id'])) {
    $_SESSION['modal_title'] = 'Error!';
    $_SESSION['modal_message'] = 'Akses tidak valid';
    $_SESSION['modal_type'] = 'danger';
    header('Location: index.php');
    exit;
}

$id = (int)$_GET['id'];

// CEK PERTAMA: Apakah tahun ajaran ini sedang aktif?
$check_active = mysqli_query($conn, "SELECT status, tahun_ajaran, semester FROM bkcare_tahun_ajaran WHERE id_tahun_ajaran = $id");
$data_active = mysqli_fetch_assoc($check_active);

if ($data_active && $data_active['status'] == 'aktif') {
    $_SESSION['modal_title'] = 'Tidak Dapat Dihapus!';
    $_SESSION['modal_message'] = 'Tahun ajaran <strong>' . $data_active['tahun_ajaran'] . ' - Semester ' . ucfirst($data_active['semester']) . '</strong> sedang AKTIF dan tidak dapat dihapus.<br><br>Nonaktifkan terlebih dahulu atau pindahkan status aktif ke tahun ajaran lain.';
    $_SESSION['modal_type'] = 'warning';
    header('Location: index.php');
    exit;
}

// CEK KEDUA: Apakah tahun ajaran digunakan di tabel lain
$check_kelas = mysqli_query($conn, "SELECT COUNT(*) as total FROM bkcare_kelas WHERE id_tahun_ajaran = $id");
$check_laporan = mysqli_query($conn, "SELECT COUNT(*) as total FROM bkcare_laporan_masalah WHERE id_tahun_ajaran = $id");
$check_jadwal = mysqli_query($conn, "SELECT COUNT(*) as total FROM bkcare_jadwal_konseling WHERE id_tahun_ajaran = $id");
$check_konseling = mysqli_query($conn, "SELECT COUNT(*) as total FROM bkcare_konseling WHERE id_tahun_ajaran = $id");
$check_perkembangan = mysqli_query($conn, "SELECT COUNT(*) as total FROM bkcare_perkembangan_siswa WHERE id_tahun_ajaran = $id");
$check_pelanggaran = mysqli_query($conn, "SELECT COUNT(*) as total FROM bkcare_pelanggaran WHERE id_tahun_ajaran = $id");

$total_used = 0;
$used_in = [];

if ($row = mysqli_fetch_assoc($check_kelas)) {
    if ($row['total'] > 0) {
        $total_used += $row['total'];
        $used_in[] = $row['total'] . ' data kelas';
    }
}
if ($row = mysqli_fetch_assoc($check_laporan)) {
    if ($row['total'] > 0) {
        $total_used += $row['total'];
        $used_in[] = $row['total'] . ' laporan masalah';
    }
}
if ($row = mysqli_fetch_assoc($check_jadwal)) {
    if ($row['total'] > 0) {
        $total_used += $row['total'];
        $used_in[] = $row['total'] . ' jadwal konseling';
    }
}
if ($row = mysqli_fetch_assoc($check_konseling)) {
    if ($row['total'] > 0) {
        $total_used += $row['total'];
        $used_in[] = $row['total'] . ' sesi konseling';
    }
}
if ($row = mysqli_fetch_assoc($check_perkembangan)) {
    if ($row['total'] > 0) {
        $total_used += $row['total'];
        $used_in[] = $row['total'] . ' data perkembangan';
    }
}
if ($row = mysqli_fetch_assoc($check_pelanggaran)) {
    if ($row['total'] > 0) {
        $total_used += $row['total'];
        $used_in[] = $row['total'] . ' data pelanggaran';
    }
}

if ($total_used > 0) {
    $_SESSION['modal_title'] = 'Tidak Dapat Dihapus!';
    $_SESSION['modal_message'] = 'Tahun ajaran ini sedang digunakan oleh: <br><strong>' . implode(', ', $used_in) . '</strong><br><br>Hapus atau pindahkan data terkait terlebih dahulu.';
    $_SESSION['modal_type'] = 'warning';
    header('Location: index.php');
    exit;
}

// Ambil data untuk informasi (jika belum diambil)
if (!$data_active) {
    $query = "SELECT tahun_ajaran, semester FROM bkcare_tahun_ajaran WHERE id_tahun_ajaran = $id";
    $result = mysqli_query($conn, $query);
    $data = mysqli_fetch_assoc($result);
    
    if (!$data) {
        $_SESSION['modal_title'] = 'Error!';
        $_SESSION['modal_message'] = 'Data tahun ajaran tidak ditemukan';
        $_SESSION['modal_type'] = 'danger';
        header('Location: index.php');
        exit;
    }
} else {
    $data = $data_active;
}

// Proses hapus
$delete = mysqli_query($conn, "DELETE FROM bkcare_tahun_ajaran WHERE id_tahun_ajaran = $id");

if ($delete) {
    $_SESSION['modal_title'] = 'Berhasil!';
    $_SESSION['modal_message'] = 'Data tahun ajaran <strong>' . $data['tahun_ajaran'] . ' - Semester ' . ucfirst($data['semester']) . '</strong> berhasil dihapus';
    $_SESSION['modal_type'] = 'success';
} else {
    $_SESSION['modal_title'] = 'Gagal!';
    $_SESSION['modal_message'] = 'Gagal menghapus data: ' . mysqli_error($conn);
    $_SESSION['modal_type'] = 'danger';
}

header('Location: index.php');
exit;
?>