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

$page_title = 'Jadwal Penerbangan - ANATL Portal';

$flights = [];
$result = $conn->query('SELECT * FROM flights ORDER BY std DESC');
if ($result) {
    while ($row = $result->fetch_assoc()) {
        $flights[] = $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="container">
        <h1 style="color: #003A70; margin-bottom: 32px;">Jadwal Penerbangan</h1>

        <div style="overflow-x: auto; margin-bottom: 48px;">
            <table class="table">
                <thead>
                    <tr>
                        <th>No. Penerbangan</th>
                        <th>Maskapai</th>
                        <th>Asal → Tujuan</th>
                        <th>STD / STA</th>
                        <th>Status</th>
                        <th>Terminal / Gate</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if ($flights): ?>
                        <?php foreach ($flights as $flight): ?>
                        <tr>
                            <td><strong><?= esc($flight['flight_no']) ?></strong></td>
                            <td><?= esc($flight['airline'] ?? '-') ?></td>
                            <td><?= esc($flight['origin'] ?? '-') ?> → <?= esc($flight['destination'] ?? '-') ?></td>
                            <td>
                                <?= format_datetime($flight['std']) ?><br>
                                <?= format_datetime($flight['sta']) ?>
                            </td>
                            <td><span class="badge badge--<?= esc($flight['status']) ?>"><?= esc(ucfirst($flight['status'])) ?></span></td>
                            <td><?= esc($flight['terminal'] ?? '-') ?> / <?= esc($flight['gate'] ?? '-') ?></td>
                        </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr><td colspan="6" style="text-align: center; padding: 32px;">Tidak ada data penerbangan</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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