<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once('includes/load.php');
page_require_level(3);

// Default filter untuk bulan berjalan
$selected_month = date('Y-m');  // Format: YYYY-MM

// Cek jika bulan dipilih melalui filter
if (isset($_GET['month'])) {
    $selected_month = $_GET['month'];
}

// Fetch purchase requests berdasarkan bulan yang dipilih
$sql = "SELECT * FROM purchase_requests WHERE DATE_FORMAT(request_date, '%Y-%m') = '$selected_month'";
$requests = $db->query($sql);
?>

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

<div class="row">
    <div class="col-md-12">
        <h2>Purchase Requests</h2>
        <?php echo display_msg($msg); ?>
    </div>
</div>

<div class="row">
    <div class="col-md-12">
        <!-- Filter Bulan -->
        <form method="get" action="">
            <div class="form-group">
                <label for="month">Select Month:</label>
                <select name="month" class="form-control" id="month">
                    <?php
                    // Menampilkan bulan-bulan yang dapat dipilih
                    for ($i = 0; $i < 12; $i++) {
                        $month = date('Y-m', strtotime("-$i months"));
                        $selected = ($month == $selected_month) ? 'selected' : '';
                        echo "<option value=\"$month\" $selected>" . date('F Y', strtotime($month)) . "</option>";
                    }
                    ?>
                </select>
            </div>
            <button type="submit" class="btn btn-primary">Filter</button>
        </form>

        <table class="table table-bordered">
            <thead>
                <tr>
                    <th>Request Number</th>
                    <th>Request Date</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
                <?php if ($requests && $db->num_rows($requests) > 0): ?>
                    <?php while ($request = $db->fetch_assoc($requests)): ?>
                        <tr>
                            <td><?php echo htmlspecialchars($request['request_number']); ?></td>
                            <td><?php echo htmlspecialchars($request['request_date']); ?></td>
                            <td>
                                <a href="view_purchase_request.php?request_number=<?php echo urlencode($request['request_number']); ?>" class="btn btn-primary">View Details</a>
                                <a href="delete_purchase_request.php?request_number=<?php echo urlencode($request['request_number']); ?>" class="btn btn-danger" onclick="return confirm('Are you sure you want to delete this request?');">Delete</a>
                            </td>
                        </tr>
                    <?php endwhile; ?>
                <?php else: ?>
                    <tr>
                        <td colspan="3">No purchase requests found.</td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

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