<?php

require_once '../../../config.php';

$conn = getDBConnection();



$action = $_POST['action'] ?? '';



if ($action === 'add') {

    $subject_name = $_POST['subject_name'] ?? '';

    $subject_code = $_POST['subject_code'] ?? '';

    

    $stmt = $conn->prepare("INSERT INTO subjects (subject_name, subject_code) VALUES (?, ?)");

    $stmt->bind_param("ss", $subject_name, $subject_code);

    $stmt->execute();

    $stmt->close();

    

    echo json_encode(['success' => true]);

} 

elseif ($action === 'edit') {

    $id = $_POST['id'] ?? 0;

    $subject_name = $_POST['subject_name'] ?? '';

    $subject_code = $_POST['subject_code'] ?? '';

    

    $stmt = $conn->prepare("UPDATE subjects SET subject_name = ?, subject_code = ? WHERE id = ?");

    $stmt->bind_param("ssi", $subject_name, $subject_code, $id);

    $stmt->execute();

    $stmt->close();

    

    echo json_encode(['success' => true]);

}

elseif ($action === 'toggle') {

    $id = $_POST['id'] ?? 0;

    $active = $_POST['active'] ?? 'yes';

    

    $stmt = $conn->prepare("UPDATE subjects SET active = ? WHERE id = ?");

    $stmt->bind_param("si", $active, $id);

    $stmt->execute();

    $stmt->close();

    

    echo json_encode(['success' => true]);

}



$conn->close();



