<?php
include 'config.php';
// FIXED: Allow both roles
if(!isset($_SESSION['user_id']) || ($_SESSION['user_role'] != 'SuperAdmin' && $_SESSION['user_role'] != 'AgencyOwner')) { 
    exit("Access Denied"); 
}

if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['add_airline'])) {
    $name = $_POST['name'];
    $target_dir = "assets/airlines/";
    if (!file_exists($target_dir)) { mkdir($target_dir, 0777, true); }

    $file_ext = pathinfo($_FILES["logo"]["name"], PATHINFO_EXTENSION);
    $file_name = time() . "_" . preg_replace("/[^a-zA-Z0-9.]/", "_", $_FILES["logo"]["name"]);
    $target_file = $target_dir . $file_name;

    if(move_uploaded_file($_FILES["logo"]["tmp_name"], $target_file)){
        $stmt = $conn->prepare("INSERT INTO airlines (name, logo_path) VALUES (?, ?)");
        $stmt->bind_param("ss", $name, $target_file);
        $stmt->execute();
        header("Location: manage_airlines.php?msg=success");
    }
}
?>