<?php

require_once "config.php";
header('Content-Type: application/json');

$method = $_SERVER['REQUEST_METHOD'];

switch ($method) {
    case 'GET':
        // Optimized query: fetch all required fields at once
        $query = $conn->query("SELECT ckode, cDepartment AS cnama, cfileimage, ciconimage, cflyerimage FROM tbdepartment order by ckode asc");

        if ($query) {
            $result = $query->fetch_all(MYSQLI_ASSOC);
            echo json_encode($result);
        } else {
            echo json_encode(['error' => 'Query failed']);
        }

        break;

    default:
        echo json_encode(['error' => 'Unsupported HTTP method']);
        break;
}

mysqli_close($conn);
?>
