<?php
require_once('includes/load.php');
page_require_level(3); // Memerlukan tingkat akses minimal 3 untuk mengakses halaman ini

// Set default start and end dates to the beginning of the current month and today's date
$default_start_date = date('Y-m-01');
$default_end_date = date('Y-m-d');

// Initialize filter values with default dates
$start_date = isset($_GET['start_date']) ? $_GET['start_date'] : $default_start_date;
$end_date = isset($_GET['end_date']) ? $_GET['end_date'] : $default_end_date;
$part_number = isset($_GET['part_number']) ? $_GET['part_number'] : '';

// Pagination settings
$per_page = isset($_GET['per_page']) ? (int)$_GET['per_page'] : 200;
$current_page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($current_page - 1) * $per_page;

// Construct SQL query with filters
$sql = "SELECT COUNT(*) as total FROM reject_prod_loc r
        WHERE r.input_date BETWEEN '$start_date' AND '$end_date'";
if (!empty($part_number)) {
    $sql .= " AND r.part_number LIKE '%$part_number%'";
}
$total_result = find_by_sql($sql);
$total_records = $total_result[0]['total'];
$total_pages = ceil($total_records / $per_page);

$sql = "SELECT r.part_number, r.quantity, r.input_date, r.remark, p.name
        FROM reject_prod_loc r
        LEFT JOIN products p ON r.part_number = p.part_number
        WHERE r.input_date BETWEEN '$start_date' AND '$end_date'";
if (!empty($part_number)) {
    $sql .= " AND r.part_number LIKE '%$part_number%'";
}
$sql .= " ORDER BY r.input_date DESC"; // Order by input_date from newest to oldest
$sql .= " LIMIT $per_page OFFSET $offset";
$rejects = find_by_sql($sql);

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

<form method="get" action="list_reject_prod_loc.php">
    <label for="start_date">Start Date:</label>
    <input type="date" name="start_date" value="<?php echo $start_date; ?>">
    <label for="end_date">End Date:</label>
    <input type="date" name="end_date" value="<?php echo $end_date; ?>">
    <label for="part_number">Part Number:</label>
    <input type="text" name="part_number" value="<?php echo $part_number; ?>">
    <label for="per_page">Records Per Page:</label>
    <input type="number" name="per_page" value="<?php echo $per_page; ?>" min="1">
    <button type="submit">Filter</button>
</form>

<?php if (!empty($rejects)) : ?>
    <table class="table table-bordered">
        <thead>
            <tr>
                <th>Part Number</th>
                <th>Name</th>
                <th>Quantity</th>
                <th>Input Date</th>
                <th>Remark</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($rejects as $reject): ?>
                <tr>
                    <td><?php echo remove_junk($reject['part_number']); ?></td>
                    <td><?php echo remove_junk($reject['name']); ?></td>
                    <td><?php echo remove_junk($reject['quantity']); ?></td>
                    <td><?php echo remove_junk($reject['input_date']); ?></td>
                    <td><?php echo remove_junk($reject['remark']); ?></td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>

    <!-- Pagination -->
    <nav>
        <ul class="pagination justify-content-center">
            <?php if ($total_pages > 1): ?>
                <?php for ($i = 1; $i <= $total_pages; $i++): ?>
                    <li class="page-item <?php if ($i == $current_page) echo 'active'; ?>">
                        <a class="page-link" href="list_reject_prod_loc.php?page=<?php echo $i; ?>&start_date=<?php echo $start_date; ?>&end_date=<?php echo $end_date; ?>&part_number=<?php echo $part_number; ?>&per_page=<?php echo $per_page; ?>"><?php echo $i; ?></a>
                    </li>
                <?php endfor; ?>
            <?php endif; ?>
        </ul>
    </nav>
<?php else: ?>
    <p>No reject records found for the selected date range and part number.</p>
<?php endif; ?>

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