<?php
// Include database connection
header("Content-Type: application/json");
require("../../includes/config.php");

// Prepare the response
$response = array('status' => '', 'message' => '', 'data' => array());

try {
    // UPDATED SQL: Added WHERE status = 1 to filter only active fees
    $sql = "SELECT id, fee_title, description, amount, class_id, academic_year_id, km_range, stream_id 
        FROM fee_types WHERE id = ?";
            
    $stmt = $pdo->prepare($sql);
    $stmt->execute();

    // Fetch all the active fee types
    $fee_types = $stmt->fetchAll(PDO::FETCH_ASSOC);

    if ($fee_types) {
        $response['status'] = 'success';
        $response['data'] = $fee_types;
    } else {
        $response['status'] = 'error';
        $response['message'] = 'No active fee types found.';
    }
} catch (PDOException $e) {
    $response['status'] = 'error';
    $response['message'] = 'Database Error: ' . $e->getMessage();
    error_log('Error fetching fee types: ' . $e->getMessage()); 
}

// Return the response as JSON
echo json_encode($response);
?>