<?php
require_once '../includes/auth.php';
require_once '../includes/functions.php';
require_once '../includes/upload.php';

require_login('airline');
$page_title = 'Pengajuan Slot Time - ANATL Portal';
$user_id = $_SESSION['user_id'];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!validate_csrf($_POST['csrf_token'] ?? '')) {
        die('Invalid request');
    }

    $flight_no = trim($_POST['flight_no'] ?? '');
    $type = trim($_POST['type'] ?? '');
    $flight_date = trim($_POST['flight_date'] ?? '');
    $requested_time = trim($_POST['requested_time'] ?? '');
    $aircraft_type = trim($_POST['aircraft_type'] ?? '');

    if ($flight_no && $type && $flight_date && $requested_time) {
        $doc_path = handle_upload('doc', 'slot');

        $stmt = $conn->prepare('INSERT INTO slot_requests (user_id, flight_no, type, flight_date, requested_time, aircraft_type, doc_path) VALUES (?, ?, ?, ?, ?, ?, ?)');
        $stmt->bind_param('issssss', $user_id, $flight_no, $type, $flight_date, $requested_time, $aircraft_type, $doc_path);

        if ($stmt->execute()) {
            set_flash('success', 'Pengajuan slot time berhasil dikirim');
        } else {
            set_flash('error', 'Gagal mengirim pengajuan');
        }
        $stmt->close();
    } else {
        set_flash('error', 'Semua field harus diisi');
    }

    header('Location: ' . BASE_URL . '/maskapai/slot.php');
    exit;
}

// Get user's slots
$slots = [];
$result = $conn->query("SELECT * FROM slot_requests WHERE user_id = $user_id ORDER BY created_at DESC");
if ($result) {
    while ($row = $result->fetch_assoc()) {
        $slots[] = $row;
    }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= $page_title ?></title>
    <link rel="stylesheet" href="<?= BASE_URL ?>/assets/css/style.css">
</head>
<body>
<?php require '../includes/header.php'; ?>

<div class="main">
    <div class="portal">
        <div class="portal__sidebar">
            <div class="sidebar__title">Menu Maskapai</div>
            <a href="<?= BASE_URL ?>/maskapai/index.php" class="sidebar-item">📊 Dashboard</a>
            <a href="<?= BASE_URL ?>/maskapai/slot.php" class="sidebar-item sidebar-item--active">✈️ Slot Time</a>
            <a href="<?= BASE_URL ?>/maskapai/flight-plan.php" class="sidebar-item">📋 Flight Plan</a>
            <a href="<?= BASE_URL ?>/maskapai/safety.php" class="sidebar-item">⚠️ Safety Report</a>
        </div>

        <div class="portal__content">
            <h1 style="color: #003A70; margin-bottom: 32px;">Pengajuan Slot Time</h1>

            <?php if ($flash = get_flash()): ?>
            <div class="alert alert--<?= esc($flash['type']) ?>">
                <?= esc($flash['msg']) ?>
            </div>
            <?php endif; ?>

            <div class="card" style="margin-bottom: 48px;">
                <div class="card__header">
                    <div class="card__title">Form Pengajuan Slot Time</div>
                </div>
                <form method="POST" class="form" enctype="multipart/form-data">
                    <input type="hidden" name="csrf_token" value="<?= csrf_token() ?>">

                    <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 16px;">
                        <div class="form-group">
                            <label for="flight_no">No. Penerbangan</label>
                            <input type="text" id="flight_no" name="flight_no" placeholder="GA101" required>
                        </div>

                        <div class="form-group">
                            <label for="type">Tipe</label>
                            <select id="type" name="type" required>
                                <option value="">-- Pilih Tipe --</option>
                                <option value="arrival">Arrival</option>
                                <option value="departure">Departure</option>
                            </select>
                        </div>

                        <div class="form-group">
                            <label for="flight_date">Tanggal Penerbangan</label>
                            <input type="date" id="flight_date" name="flight_date" required>
                        </div>

                        <div class="form-group">
                            <label for="requested_time">Waktu yang Diminta</label>
                            <input type="time" id="requested_time" name="requested_time" required>
                        </div>

                        <div class="form-group" style="grid-column: 1 / -1;">
                            <label for="aircraft_type">Tipe Aircraft</label>
                            <input type="text" id="aircraft_type" name="aircraft_type" placeholder="Boeing 737">
                        </div>

                        <div class="form-group" style="grid-column: 1 / -1;">
                            <label for="doc">Dokumen Pendukung</label>
                            <input type="file" id="doc" name="doc" accept=".pdf,.jpg,.jpeg,.png">
                            <small style="color: #5A6A7A;">PDF atau gambar (max 5MB)</small>
                        </div>
                    </div>

                    <button type="submit" class="btn btn--primary">Kirim Pengajuan</button>
                </form>
            </div>

            <div class="card">
                <div class="card__header">
                    <div class="card__title">Riwayat Pengajuan</div>
                </div>

                <?php if (empty($slots)): ?>
                <div class="empty-state">
                    <div class="empty-state__message">Belum ada pengajuan slot time</div>
                </div>
                <?php else: ?>

                <div style="overflow-x: auto;">
                    <table class="table">
                        <thead>
                            <tr>
                                <th>No. Penerbangan</th>
                                <th>Tipe</th>
                                <th>Tanggal</th>
                                <th>Waktu</th>
                                <th>Status</th>
                                <th>Catatan Admin</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($slots as $slot): ?>
                            <tr>
                                <td><?= esc($slot['flight_no']) ?></td>
                                <td><?= esc($slot['type']) ?></td>
                                <td><?= format_date($slot['flight_date']) ?></td>
                                <td><?= $slot['requested_time'] ?></td>
                                <td><span class="badge badge--<?= esc($slot['status']) ?>"><?= esc(ucfirst($slot['status'])) ?></span></td>
                                <td><?= esc($slot['admin_notes'] ?? '-') ?></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>

                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

<?php require '../includes/footer.php'; ?>
