<?php
$page_title = 'Filter AWB Data';
require_once('includes/load.php');
page_require_level(15);

include_once('layouts/header.php');

// Check if start and end dates are provided and validate the format              
$start_date = isset($_GET['start_date']) ? $_GET['start_date'] : date('Y-m-d');
$end_date = isset($_GET['end_date']) ? $_GET['end_date'] : date('Y-m-d');

if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $start_date)) {
    $start_date = date('Y-m-d');
    $msg = ['type' => 'danger', 'message' => 'Invalid start date format. Defaulting to today.'];
}

if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $end_date)) {
    $end_date = date('Y-m-d');
    if (isset($msg)) {
        $msg['message'] .= ' Invalid end date format. Defaulting to today.';
    } else {
        $msg = ['type' => 'danger', 'message' => 'Invalid end date format. Defaulting to today.'];
    }
}

// Fetch only necessary data based on the selected date range without pagination              
$query = "SELECT id, tanggal_pesenan_dibuat, invoice, resi, kurir, part_number, date_scan, quantity, scan_in, scan_status, scan_retur, packer_no, batas_pengiriman, uniq_code, unit_price, total_price, loc, waktu_proses 
          FROM awb 
          WHERE date_scan BETWEEN ? AND ? 
          AND scan_in = '' 
          AND scan_status = ''";
$stmt = $db->prepare($query);
$stmt->bind_param("ss", $start_date, $end_date);
$stmt->execute();
$result = $stmt->get_result();
$data_found = $result->num_rows > 0;

if (!$data_found) {
    $msg = ['type' => 'info', 'message' => 'No data found for the selected date range.'];
}
?>

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

<div class="row">
    <div class="col-md-12">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">Filter AWB Data</h3>
                <form id="dateFilterForm" class="form-inline">
                    <div class="form-group">
                        <label for="startDate">Start Date:</label>
                        <input type="date" id="startDate" name="start_date" class="form-control"
                            value="<?php echo htmlspecialchars($start_date); ?>">
                    </div>
                    <div class="form-group">
                        <label for="endDate">End Date:</label>
                        <input type="date" id="endDate" name="end_date" class="form-control"
                            value="<?php echo htmlspecialchars($end_date); ?>">
                    </div>
                    <button type="submit" class="btn btn-primary">Filter</button>
                </form>
            </div>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-md-12">
        <table class="table table-bordered">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Tanggal Pesenan Dibuat</th>
                    <th>Invoice</th>
                    <th>Resi</th>
                    <th>Kurir</th>
                    <th>Part Number</th>
                    <th>Date Scan</th>
                    <th>Quantity</th>
                    <th>Scan In</th>
                    <th>Scan Status</th>
                    <th>Scan Retur</th>
                    <th>Packer No</th>
                    <th>Batas Pengiriman</th>
                    <th>Uniq Code</th>
                    <th>Unit Price</th>
                    <th>Total Price</th>
                    <th>Loc</th>
                </tr>
            </thead>
            <tbody>
                <?php
                if ($data_found) {
                    while ($row = $result->fetch_assoc()) {
                        echo "<tr>";
                        echo "<td>" . htmlspecialchars($row['id']) . "</td>";
                        echo "<td>" . htmlspecialchars($row['tanggal_pesenan_dibuat']) . "</td>";
                        echo "<td>" . htmlspecialchars($row['invoice']) . "</td>";
                        echo "<td>" . htmlspecialchars($row['resi']) . "</td>";
                        echo "<td>" . htmlspecialchars($row['kurir']) . "</td>";
                        echo "<td>" . htmlspecialchars($row['part_number']) . "</td>";
                        echo "<td>" . htmlspecialchars($row['date_scan']) . "</td>";
                        echo "<td>" . htmlspecialchars($row['quantity']) . "</td>";
                        echo "<td>" . htmlspecialchars($row['scan_in']) . "</td>";
                        echo "<td>" . htmlspecialchars($row['scan_status']) . "</td>";
                        echo "<td>" . htmlspecialchars($row['scan_retur']) . "</td>";
                        echo "<td>" . htmlspecialchars($row['packer_no']) . "</td>";
                        echo "<td>" . htmlspecialchars($row['batas_pengiriman']) . "</td>";
                        echo "<td>" . htmlspecialchars($row['uniq_code']) . "</td>";
                        echo "<td>" . htmlspecialchars($row['unit_price']) . "</td>";
                        echo "<td>" . htmlspecialchars($row['total_price']) . "</td>";
                        echo "<td>" . htmlspecialchars($row['loc']) . "</td>";
                        echo "</tr>";
                    }
                }
                ?>
            </tbody>
        </table>
    </div>
</div>

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