<?php
header('Content-Type: application/json');

// 1. Prevent PHP from outputting HTML error tags
ini_set('display_errors', 0); 
error_reporting(E_ALL);

try {
require("../../includes/config.php");

    // 3. Get the JSON data from JavaScript fetch
    $input = file_get_contents('php://input');
    $data = json_decode($input, true);

    if (isset($data['id']) && isset($data['status'])) {
        $id = (int)$data['id'];
        $status = (int)$data['status'];

        // 4. Update query using Prepared Statements
        $sql = "UPDATE fee_types SET status = :status WHERE id = :id";
        $stmt = $pdo->prepare($sql);
        $stmt->execute(['status' => $status, 'id' => $id]);

        echo json_encode(['status' => 'success']);
    } else {
        echo json_encode(['status' => 'error', 'message' => 'Missing ID or Status']);
    }

} catch (\PDOException $e) {
    // If database fails, send the error as JSON
    echo json_encode(['status' => 'error', 'message' => 'Database error: ' . $e->getMessage()]);
} catch (Exception $e) {
    echo json_encode(['status' => 'error', 'message' => 'General error: ' . $e->getMessage()]);
}
?>