<?php
// ADD CLASS
if (isset($_POST['add_class_btn'])) {
    $new_class_name = trim($_POST['new_class_name']);
    $class_level = $_POST['class_level'];

    if (!empty($new_class_name)) {
        $stmt = $connection->prepare("INSERT INTO `classes` (`classes`, `level`) VALUES (?, ?)");
        $stmt->bind_param("ss", $new_class_name, $class_level);

        if ($stmt->execute()) {
            $_SESSION['msg'] = "Class Added Successfully";
            $_SESSION['type'] = "success";
        } else {
            $_SESSION['msg'] = "Error! Could Not Add Class";
            $_SESSION['type'] = "danger";
        }
        $stmt->close();
    } else {
        $_SESSION['msg'] = "Class name cannot be empty";
        $_SESSION['type'] = "danger";
    }

    header("Location: class");
    exit();
}


// UPDATE CLASS
if (isset($_POST['update_class_btn'])) {
    $edit_class_id = $_POST['edit_class_id'];
    $edit_class_name = $_POST['edit_class_name'];
    $edit_class_level = $_POST['edit_class_level'];

    $stmt = $connection->prepare("UPDATE `classes` SET `classes` = ?, `level` = ? WHERE `id` = ?");
    $stmt->bind_param("ssi", $edit_class_name, $edit_class_level, $edit_class_id);

    if ($stmt->execute()) {
        $_SESSION['msg'] = "Class Updated Successfully";
        $_SESSION['type'] = "success";
    } else {
        $_SESSION['msg'] = "Error! Could Not Update Record";
        $_SESSION['type'] = "danger";
    }
    $stmt->close();

    header("Location: class");
    exit();
}


// DELETE CLASS (secure version)
if (isset($_POST['delete_class_btn'])) {
    $hide_class_del = $_POST['hide_class_del'];

    $stmt = $connection->prepare("DELETE FROM `classes` WHERE `id` = ?");
    $stmt->bind_param("i", $hide_class_del);

    if ($stmt->execute()) {
        $_SESSION['msg'] = "Class Deleted Successfully";
        $_SESSION['type'] = "success";
    } else {
        $_SESSION['msg'] = "Error! Could Not Delete Record";
        $_SESSION['type'] = "danger";
    }
    $stmt->close();

    header("Location: class");
    exit();
}
?>

