<?php
session_start();
require_once 'functions.php';

if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== true) {
    header('Location: admin_login.php');
    exit;
}

$selectedMonth = isset($_GET['month']) ? intval($_GET['month']) : date('m');
$selectedYear = isset($_GET['year']) ? intval($_GET['year']) : date('Y');

$allRecords = getRecords();
$filtered = filterRecordsByMonthYear($allRecords, $selectedMonth, $selectedYear);

// Group by airline and sum engineer hours (engineer1 + engineer2 hours per record)
$airlineHours = [];
foreach ($filtered as $rec) {
    $airline = $rec['airline'];
    $hours = getTotalEngineerHoursForRecord($rec);
    if (!isset($airlineHours[$airline])) $airlineHours[$airline] = 0;
    $airlineHours[$airline] += $hours;
}
arsort($airlineHours);

$monthNames = [
    1 => 'January', 2 => 'February', 3 => 'March', 4 => 'April', 5 => 'May', 6 => 'June',
    7 => 'July', 8 => 'August', 9 => 'September', 10 => 'October', 11 => 'November', 12 => 'December'
];
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
    <title>Airline Monthly Hours Report</title>
    <style>
        body{font-family:'Segoe UI',sans-serif;background:#f4f6f9;margin:0;padding:16px}.container{max-width:1000px;margin:auto;background:white;padding:20px;border-radius:12px}.btn{background:#1e3c72;color:white;padding:8px 16px;text-decoration:none;border-radius:5px;display:inline-block}table{width:100%;border-collapse:collapse;margin-top:20px}th,td{border:1px solid #ddd;padding:10px;text-align:left}th{background:#1e3c72;color:white}
    </style>
</head>
<body>
<div class="container">
    <h1>✈️ Monthly Engineer Hours by Airline</h1>
    <div style="margin-bottom:20px;">
        <form method="GET" style="display:flex; gap:10px; align-items:center; flex-wrap:wrap;">
            <label>Month: <select name="month"><?php for($m=1;$m<=12;$m++): ?><option value="<?php echo $m; ?>" <?php echo $selectedMonth==$m?'selected':''; ?>><?php echo $monthNames[$m]; ?></option><?php endfor; ?></select></label>
            <label>Year: <select name="year"><?php for($y=date('Y')-2;$y<=date('Y')+2;$y++): ?><option value="<?php echo $y; ?>" <?php echo $selectedYear==$y?'selected':''; ?>><?php echo $y; ?></option><?php endfor; ?></select></label>
            <button type="submit" class="btn">Show Report</button>
            <a href="admin.php" class="btn" style="background:#6c757d;">← Back</a>
        </form>
    </div>
    <?php if(empty($airlineHours)): ?>
        <p>No records for <?php echo $monthNames[$selectedMonth] . ' ' . $selectedYear; ?>.</p>
    <?php else: ?>
        <h3><?php echo $monthNames[$selectedMonth] . ' ' . $selectedYear; ?></h3>
        <table>
            <thead><tr><th>Airline</th><th>Total Engineer Hours</th></tr></thead>
            <tbody>
                <?php foreach($airlineHours as $airline => $hours): ?>
                <tr><td><?php echo htmlspecialchars($airline); ?></td><td><?php echo number_format($hours, 2); ?> hrs</td></tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    <?php endif; ?>
</div>
</body>
</html>