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

$page_title = 'ANATL Portal - Home';

// Get flights
$flights = [];
$result = $conn->query('SELECT * FROM flights ORDER BY std DESC LIMIT 10');
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'; ?>

<section style="background: linear-gradient(135deg, #003A70 0%, #005BAA 100%); color: white; padding: 64px 16px; text-align: center;">
    <div class="container">
        <h1 style="font-size: 48px; margin-bottom: 16px;">Selamat Datang di ANATL Portal</h1>
        <p style="font-size: 18px; margin-bottom: 32px;">Autoridade Nacional de Aviação Civil e Transportes Aéreos de Timor-Leste</p>
    </div>
</section>

<section style="padding: 48px 16px;">
    <div class="container">
        <h2 style="font-size: 28px; margin-bottom: 32px; color: #003A70;">Penerbangan Terbaru</h2>

        <div style="overflow-x: auto;">
            <table class="table">
                <thead>
                    <tr>
                        <th>No. Penerbangan</th>
                        <th>Maskapai</th>
                        <th>Asal</th>
                        <th>Tujuan</th>
                        <th>Waktu (STD)</th>
                        <th>Status</th>
                        <th>Terminal</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'] ?? '-') ?></td>
                            <td><?= esc($flight['destination'] ?? '-') ?></td>
                            <td><?= format_datetime($flight['std']) ?></td>
                            <td>
                                <span class="badge badge--<?= esc($flight['status']) ?>">
                                    <?= esc(ucfirst($flight['status'])) ?>
                                </span>
                            </td>
                            <td><?= esc($flight['terminal'] ?? '-') ?></td>
                        </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="7" style="text-align: center; padding: 32px;">Tidak ada data penerbangan</td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

        <div style="text-align: center; margin-top: 32px;">
            <a href="<?= BASE_URL ?>/penerbangan.php" class="btn btn--primary">Lihat Semua Penerbangan</a>
        </div>
    </div>
</section>

<section style="background: #F4F6F9; padding: 48px 16px;">
    <div class="container">
        <h2 style="font-size: 28px; margin-bottom: 32px; color: #003A70;">Menu Cepat</h2>

        <div class="grid grid--3">
            <div class="card">
                <div class="card__title">✈️ Penerbangan</div>
                <p style="color: #5A6A7A; margin: 16px 0;">Jadwal lengkap dan informasi penerbangan bandara Dili.</p>
                <a href="<?= BASE_URL ?>/penerbangan.php" class="btn btn--secondary">Selengkapnya</a>
            </div>

            <div class="card">
                <div class="card__title">🏢 Fasilitas</div>
                <p style="color: #5A6A7A; margin: 16px 0;">Informasi lengkap fasilitas dan layanan bandara.</p>
                <a href="<?= BASE_URL ?>/fasilitas.php" class="btn btn--secondary">Selengkapnya</a>
            </div>

            <div class="card">
                <div class="card__title">📋 Panduan</div>
                <p style="color: #5A6A7A; margin: 16px 0;">Panduan dan FAQ untuk penumpang bandara.</p>
                <a href="<?= BASE_URL ?>/panduan.php" class="btn btn--secondary">Selengkapnya</a>
            </div>

            <div class="card">
                <div class="card__title">🛂 Kepabeanan</div>
                <p style="color: #5A6A7A; margin: 16px 0;">Info visa, barang bawaan, dan deklarasi.</p>
                <a href="<?= BASE_URL ?>/kepabeanan.php" class="btn btn--secondary">Selengkapnya</a>
            </div>

            <div class="card">
                <div class="card__title">🏪 Unit Sewa</div>
                <p style="color: #5A6A7A; margin: 16px 0;">Galeri unit bisnis yang tersedia di bandara.</p>
                <a href="<?= BASE_URL ?>/tenant-unit.php" class="btn btn--secondary">Selengkapnya</a>
            </div>

            <div class="card">
                <div class="card__title">📢 Laporan</div>
                <p style="color: #5A6A7A; margin: 16px 0;">Laporkan masalah fasilitas kepada kami.</p>
                <a href="<?= BASE_URL ?>/laporan.php" class="btn btn--secondary">Selengkapnya</a>
            </div>
        </div>
    </div>
</section>

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