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

require_login('admin');
$page_title = 'Admin Dashboard - ANATL Portal';
$current_user = get_user();

// Get statistics
$stats = [];

// Total users
$result = $conn->query("SELECT COUNT(*) as count FROM users WHERE role != 'admin'");
$stats['users'] = $result->fetch_assoc()['count'];

// Pending approvals
$result = $conn->query("SELECT
  (SELECT COUNT(*) FROM slot_requests WHERE status='submitted') +
  (SELECT COUNT(*) FROM flight_plans WHERE status='submitted') +
  (SELECT COUNT(*) FROM rental_applications WHERE status='submitted')
  as count");
$stats['pending'] = $result->fetch_assoc()['count'];

// Facility reports
$result = $conn->query("SELECT COUNT(*) as count FROM facility_reports WHERE status='open'");
$stats['reports'] = $result->fetch_assoc()['count'];

// Safety reports
$result = $conn->query("SELECT COUNT(*) as count FROM safety_reports WHERE status='submitted'");
$stats['safety'] = $result->fetch_assoc()['count'];
?>
<!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 Admin</div>
            <a href="<?= BASE_URL ?>/admin/index.php" class="sidebar-item sidebar-item--active">📊 Dashboard</a>
            <a href="<?= BASE_URL ?>/admin/users.php" class="sidebar-item">👥 Kelola User</a>
            <a href="<?= BASE_URL ?>/admin/slot.php" class="sidebar-item">✈️ Slot Time</a>
            <a href="<?= BASE_URL ?>/admin/flight-plan.php" class="sidebar-item">📋 Flight Plan</a>
            <a href="<?= BASE_URL ?>/admin/tenant.php" class="sidebar-item">🏢 Tenant</a>
            <a href="<?= BASE_URL ?>/admin/ground-handling.php" class="sidebar-item">🚗 Ground Handler</a>
            <a href="<?= BASE_URL ?>/admin/cms.php" class="sidebar-item">📰 CMS</a>
        </div>

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

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

            <div class="grid grid--4" style="margin-bottom: 48px;">
                <div class="stat-card">
                    <div style="font-size: 24px;">👥</div>
                    <div class="stat-card__value"><?= $stats['users'] ?></div>
                    <div class="stat-card__label">Total User</div>
                </div>

                <div class="stat-card">
                    <div style="font-size: 24px;">⏳</div>
                    <div class="stat-card__value"><?= $stats['pending'] ?></div>
                    <div class="stat-card__label">Pending Approval</div>
                </div>

                <div class="stat-card">
                    <div style="font-size: 24px;">📋</div>
                    <div class="stat-card__value"><?= $stats['reports'] ?></div>
                    <div class="stat-card__label">Open Reports</div>
                </div>

                <div class="stat-card">
                    <div style="font-size: 24px;">⚠️</div>
                    <div class="stat-card__value"><?= $stats['safety'] ?></div>
                    <div class="stat-card__label">Safety Reports</div>
                </div>
            </div>

            <div class="card">
                <div class="card__header">
                    <div class="card__title">Quick Actions</div>
                </div>
                <div class="grid grid--3">
                    <a href="<?= BASE_URL ?>/admin/slot.php" class="btn btn--primary" style="text-align: center;">Review Slot Time</a>
                    <a href="<?= BASE_URL ?>/admin/flight-plan.php" class="btn btn--primary" style="text-align: center;">Review Flight Plans</a>
                    <a href="<?= BASE_URL ?>/admin/tenant.php" class="btn btn--primary" style="text-align: center;">Review Tenants</a>
                    <a href="<?= BASE_URL ?>/admin/users.php" class="btn btn--secondary" style="text-align: center;">Manage Users</a>
                    <a href="<?= BASE_URL ?>/admin/cms.php" class="btn btn--secondary" style="text-align: center;">Edit CMS</a>
                    <a href="<?= BASE_URL ?>/admin/ground-handling.php" class="btn btn--secondary" style="text-align: center;">Ground Handling</a>
                </div>
            </div>
        </div>
    </div>
</div>

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