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

require_login('ground_handler');
$page_title = 'Penerbangan Non-Reguler - ANATL Portal';

$flights = [];
$result = $conn->query('SELECT * FROM irregular_flights ORDER BY arrival_dt 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="portal">
        <div class="portal__sidebar">
            <div class="sidebar__title">Menu Ground Handling</div>
            <a href="<?= BASE_URL ?>/ground-handling/index.php" class="sidebar-item">📊 Dashboard</a>
            <a href="<?= BASE_URL ?>/ground-handling/notif.php" class="sidebar-item sidebar-item--active">📢 Penerbangan</a>
            <a href="<?= BASE_URL ?>/ground-handling/invoice.php" class="sidebar-item">💰 Invoice</a>
        </div>

        <div class="portal__content">
            <h1 style="color: #003A70; margin-bottom: 32px;">Penerbangan Non-Reguler</h1>

            <?php if (empty($flights)): ?>
            <div class="empty-state">
                <div class="empty-state__message">Tidak ada penerbangan non-reguler saat ini</div>
            </div>
            <?php else: ?>

            <div style="overflow-x: auto;">
                <table class="table">
                    <thead>
                        <tr>
                            <th>No. Penerbangan</th>
                            <th>Maskapai</th>
                            <th>Asal</th>
                            <th>Waktu Tiba</th>
                            <th>Penumpang</th>
                            <th>Catatan</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?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><?= format_datetime($flight['arrival_dt']) ?></td>
                            <td><?= $flight['pax_count'] ?></td>
                            <td><?= esc(substr($flight['notes'] ?? '', 0, 50)) ?>...</td>
                            <td><a href="#" class="btn btn--small btn--secondary">Detail</a></td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>

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

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