<?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');

$engineerHours = getEngineerHoursByMonthYear($selectedMonth, $selectedYear);
$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, maximum-scale=1.0, user-scalable=yes">
    <title>Engineer Hours Report</title>
    <style>
        * { box-sizing: border-box; }
        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;
        }
        h1 { color: #1e3c72; font-size: 1.8rem; }
        .filters {
            margin-bottom: 20px;
            background: #f8f9fa;
            padding: 12px;
            border-radius: 8px;
            display: flex;
            flex-wrap: wrap;
            gap: 10px;
            align-items: center;
        }
        .btn {
            background: #1e3c72;
            color: white;
            padding: 8px 16px;
            text-decoration: none;
            border-radius: 5px;
            display: inline-block;
        }
        .table-wrapper {
            overflow-x: auto;
            -webkit-overflow-scrolling: touch;
        }
        table {
            width: 100%;
            border-collapse: collapse;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 10px;
            text-align: left;
        }
        th { background: #1e3c72; color: white; }
        tr:nth-child(even) { background: #f9f9f9; }
        @media (max-width: 640px) {
            .container { padding: 12px; }
            th, td { padding: 6px; font-size: 0.8rem; }
            .btn { padding: 6px 12px; font-size: 0.8rem; }
        }
    </style>
</head>
<body>
<div class="container">
    <h1>👨‍🔧 Engineer Hours Report</h1>
    <div class="filters">
        <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">Filter</button>
            <a href="engineer_hours.php" class="btn" style="background:#6c757d;">Reset</a>
            <a href="admin.php" class="btn" style="background:#6c757d;">← Back to Dashboard</a>
        </form>
    </div>

    <?php if (empty($engineerHours)): ?>
        <p>No engineer hours recorded for <?php echo $monthNames[$selectedMonth] . ' ' . $selectedYear; ?>.</p>
    <?php else: ?>
        <h3>Hours for <?php echo $monthNames[$selectedMonth] . ' ' . $selectedYear; ?></h3>
        <div class="table-wrapper">
            <table>
                <thead>
                    <tr><th>Engineer Name</th><th>Total Hours Worked</th></tr>
                </thead>
                <tbody>
                    <?php foreach ($engineerHours as $name => $hours): ?>
                    <tr>
                        <td><?php echo htmlspecialchars($name); ?></td>
                        <td><?php echo $hours; ?> hrs</td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <p><strong>Total site labor hours:</strong> <?php echo array_sum($engineerHours); ?> hrs</p>
    <?php endif; ?>
</div>
</body>
</html>