<?php
require_once('includes/load.php');
page_require_level(3); // Pastikan hanya pengguna dengan tingkat akses 3 (Administrator) yang dapat mengakses halaman ini.

if (isset($_POST['filter'])) {
    $selected_month = $_POST['month'];
    $selected_year = $_POST['year'];
    $start_date = date('Y-m-01', strtotime($selected_year . '-' . $selected_month . '-01'));
    $end_date = date('Y-m-t', strtotime($selected_year . '-' . $selected_month . '-01'));

    // Ambil data dari tabel 'productivity' berdasarkan filter bulan
    $productivity_data = find_by_sql("SELECT * FROM productivity WHERE date >= '{$start_date}' AND date <= '{$end_date}'");
} else {
    // Jika halaman dibuka pertama kali atau tanpa filter, tampilkan semua data
    $productivity_data = find_all('productivity');
}

?>

<?php include('layouts/header.php'); ?>

<h1>View Productivity Data by Month</h1>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <label for="month">Select Month:</label>
    <select name="month" id="month">
        <option value="01">January</option>
        <option value="02">February</option>
        <option value="03">March</option>
        <option value="04">April</option>
        <option value="05">May</option>
        <option value="06">June</option>
        <option value="07">July</option>
        <option value="08">August</option>
        <option value="09">September</option>
        <option value="10">October</option>
        <option value="11">November</option>
        <option value="12">December</option>
    </select>

    <label for="year">Select Year:</label>
    <select name="year" id="year">
        <?php
        $current_year = date('Y');
        for ($i = $current_year; $i >= 2000; $i--) {
            echo "<option value=\"$i\">$i</option>";
        }
        ?>
    </select>

    <input type="submit" name="filter" value="Filter">
</form>

<table class="table table-bordered">
    <thead>
        <tr>
            <th>Date</th>
            <th>Work Time</th>
            <th>Name</th>
            <th>Color</th>
            <th>Quantity</th>
            <th>Work Center</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($productivity_data as $row) : ?>
            <tr>
                <td><?php echo date('Y-m-d', strtotime($row['date'])); ?></td>
                <td><?php echo $row['work_time']; ?></td>
                <td><?php echo $row['name']; ?></td>
                <td><?php echo $row['color']; ?></td>
                <td><?php echo $row['quantity']; ?></td>
                <td><?php echo $row['work_center']; ?></td>
            </tr>
        <?php endforeach; ?>
    </tbody>
</table>

<?php include('layouts/footer.php'); ?>
