<?php
require_once __DIR__ . '/../library/auth.php'; 
$school_id = $_SESSION['school_id'];
allow_roles(['admin', 'accountant']);

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

$referer = $_SERVER['HTTP_REFERER'] ?? 'index.php';



// ======================================================
// HANDLE POST REQUESTS
// ======================================================

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    // ======================================================
    // ADD VEHICLE
    // ======================================================

    if ($action == 'add_vehicle') {

        $vehicle_no    = trim($_POST['vehicle_no']);
        $vehicle_model = trim($_POST['vehicle_model']);


        if (!empty($vehicle_no)) {

            // Duplicate Check

            $check = $pdo->prepare("
                SELECT id
                FROM vehicles
                WHERE school_id = ?
                AND vehicle_no = ?
            ");

            $check->execute([
                $school_id,
                $vehicle_no
            ]);


            if ($check->rowCount() == 0) {

                $stmt = $pdo->prepare("
                    INSERT INTO vehicles
                    (
                        school_id,
                        vehicle_no,
                        vehicle_model
                    )
                    VALUES
                    (
                        ?, ?, ?
                    )
                ");

                $stmt->execute([
                    $school_id,
                    $vehicle_no,
                    $vehicle_model
                ]);
            }
        }
    }



    // ======================================================
    // ADD DRIVER
    // ======================================================

    elseif ($action == 'add_driver') {

        $name       = trim($_POST['name']);
        $license_no = trim($_POST['license_no']);
        $phone      = trim($_POST['phone']);


        if (!empty($name)) {

            $stmt = $pdo->prepare("
                INSERT INTO drivers
                (
                    school_id,
                    name,
                    license_no,
                    phone
                )
                VALUES
                (
                    ?, ?, ?, ?
                )
            ");

            $stmt->execute([
                $school_id,
                $name,
                $license_no,
                $phone
            ]);
        }
    }



    // ======================================================
    // ADD ROUTE
    // ======================================================

    elseif ($action == 'add_route') {

        $route_title = trim($_POST['route_title']);
        $fare        = floatval($_POST['fare']);


        if (!empty($route_title)) {

            $stmt = $pdo->prepare("
                INSERT INTO routes
                (
                    school_id,
                    route_title,
                    fare
                )
                VALUES
                (
                    ?, ?, ?
                )
            ");

            $stmt->execute([
                $school_id,
                $route_title,
                $fare
            ]);
        }
    }



    // ======================================================
    // ASSIGN TRANSPORT
    // ======================================================

    elseif ($action == 'assign_transport') {

        $user_type   = $_POST['user_type'];
        $reference_id = intval($_POST['reference_id']);
        $route_id     = intval($_POST['route_id']);
        $vehicle_id   = intval($_POST['vehicle_id']);


        // Duplicate Check

        $check = $pdo->prepare("
            SELECT id
            FROM transport_allocation
            WHERE school_id = ?
            AND user_type = ?
            AND reference_id = ?
        ");

        $check->execute([
            $school_id,
            $user_type,
            $reference_id
        ]);


        if ($check->rowCount() == 0) {

            $stmt = $pdo->prepare("
                INSERT INTO transport_allocation
                (
                    school_id,
                    user_type,
                    reference_id,
                    route_id,
                    vehicle_id
                )
                VALUES
                (
                    ?, ?, ?, ?, ?
                )
            ");

            $stmt->execute([
                $school_id,
                $user_type,
                $reference_id,
                $route_id,
                $vehicle_id
            ]);
        }
    }


    header("Location: $referer");
    exit;
}




// ======================================================
// HANDLE DELETE
// ======================================================

if ($action == 'delete') {

    $allowed_tables = [

        'vehicles',
        'drivers',
        'routes',
        'transport_allocation'

    ];


    $table = $_GET['table'] ?? '';

    $id = intval($_GET['id'] ?? 0);


    // Security Check

    if (!in_array($table, $allowed_tables)) {

        exit('Invalid Table');
    }


    $stmt = $pdo->prepare("
        DELETE FROM {$table}
        WHERE id = ?
        AND school_id = ?
    ");

    $stmt->execute([
        $id,
        $school_id
    ]);


    header("Location: $referer");
    exit;
}

?>