<?php
$page_title = 'Packer Summary';
require_once('includes/load.php');
page_require_level(7);

include_once('layouts/header.php');

// Set default date range to current date
$current_date = date('Y-m-d');
$start_date = $current_date;
$end_date = $current_date;

// Handle form submission
if (isset($_POST['filter'])) {
    $start_date = $_POST['start_date'];
    $end_date = $_POST['end_date'];
}

// Query to get summary data by packer for Surabaya (where loc is NULL) within the selected date range
$sql_summary_by_packer_sby = "
    SELECT 
        packer_no, 
        'Surabaya' as location, 
        COUNT(DISTINCT resi) as total
    FROM awb
    WHERE loc = 'sidoarjo'
    AND DATE(scan_status) BETWEEN '{$start_date}' AND '{$end_date}'
    GROUP BY packer_no
    ORDER BY packer_no ASC";

$result_summary_by_packer_sby = $db->query($sql_summary_by_packer_sby);
$summary_by_packer_sby = [];
while ($row = $result_summary_by_packer_sby->fetch_assoc()) {
    $summary_by_packer_sby[] = $row;
}

// Query to get summary data by packer for Jakarta within the selected date range
$sql_summary_by_packer_jkt = "
    SELECT 
        packer_no, 
        'Jakarta' as location, 
        COUNT(DISTINCT resi) as total
    FROM awb
    WHERE loc = 'Jakarta'
    AND DATE(scan_status) BETWEEN '{$start_date}' AND '{$end_date}'
    GROUP BY packer_no
    ORDER BY packer_no ASC";

$result_summary_by_packer_jkt = $db->query($sql_summary_by_packer_jkt);
$summary_by_packer_jkt = [];
while ($row = $result_summary_by_packer_jkt->fetch_assoc()) {
    $summary_by_packer_jkt[] = $row;
}

// Query to get summary data by packer for Semarang within the selected date range
$sql_summary_by_packer_smg = "
    SELECT 
        packer_no, 
        'Semarang' as location, 
        COUNT(DISTINCT resi) as total
    FROM awb
    WHERE loc = 'Semarang'
    AND DATE(scan_status) BETWEEN '{$start_date}' AND '{$end_date}'
    GROUP BY packer_no
    ORDER BY packer_no ASC";

$result_summary_by_packer_smg = $db->query($sql_summary_by_packer_smg);
$summary_by_packer_smg = [];
while ($row = $result_summary_by_packer_smg->fetch_assoc()) {
    $summary_by_packer_smg[] = $row;
}
?>

<div class="row">
    <div class="col-md-6">
        <!-- Form Filter -->
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">Filter by Date Range</h3>
            </div>
            <div class="panel-body">
                <form method="post" action="packer_sby.php">
                    <div class="form-group">
                        <label for="start_date">Start Date</label>
                        <input type="text" class="form-control datepicker" name="start_date" id="start_date" value="<?php echo htmlspecialchars($start_date); ?>" required>
                    </div>
                    <div class="form-group">
                        <label for="end_date">End Date</label>
                        <input type="text" class="form-control datepicker" name="end_date" id="end_date" value="<?php echo htmlspecialchars($end_date); ?>" required>
                    </div>
                    <button type="submit" name="filter" class="btn btn-primary">Filter</button>
                </form>
            </div>
        </div>

        <!-- Display results by packer for Surabaya (loc IS NULL) -->
        <?php if (!empty($summary_by_packer_sby)): ?>
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title">Packer Surabaya (<?php echo htmlspecialchars($start_date); ?> - <?php echo htmlspecialchars($end_date); ?>)</h3>
                </div>
                <div class="panel-body">
                    <div class="table-responsive">
                        <table class="table table-bordered table-striped">
                            <thead>
                                <tr>
                                    <th>Packer Number</th>
                                    <th>Total Resi Terpacking</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($summary_by_packer_sby as $row): ?>
                                    <tr>
                                        <td><?php echo htmlspecialchars($row['packer_no']); ?></td>
                                        <td><?php echo htmlspecialchars($row['total']); ?></td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        <?php else: ?>
            <div class="alert alert-info">Tidak ada data packer untuk Surabaya dalam rentang tanggal yang dipilih.</div>
        <?php endif; ?>

        <!-- Display results by packer for Jakarta (loc = 'Jakarta') -->
        <?php if (!empty($summary_by_packer_jkt)): ?>
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title">Packer Jakarta (<?php echo htmlspecialchars($start_date); ?> - <?php echo htmlspecialchars($end_date); ?>)</h3>
                </div>
                <div class="panel-body">
                    <div class="table-responsive">
                        <table class="table table-bordered table-striped">
                            <thead>
                                <tr>
                                    <th>Packer Number</th>
                                    <th>Total Resi Terpacking</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($summary_by_packer_jkt as $row): ?>
                                    <tr>
                                        <td><?php echo htmlspecialchars($row['packer_no']); ?></td>
                                        <td><?php echo htmlspecialchars($row['total']); ?></td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        <?php else: ?>
            <div class="alert alert-info">Tidak ada data packer untuk Jakarta dalam rentang tanggal yang dipilih.</div>
        <?php endif; ?>

        <!-- Display results by packer for Semarang (loc = 'Semarang') -->
        <?php if (!empty($summary_by_packer_smg)): ?>
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title">Packer Semarang (<?php echo htmlspecialchars($start_date); ?> - <?php echo htmlspecialchars($end_date); ?>)</h3>
                </div>
                <div class="panel-body">
                    <div class="table-responsive">
                        <table class="table table-bordered table-striped">
                            <thead>
                                <tr>
                                    <th>Packer Number</th>
                                    <th>Total Resi Terpacking</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($summary_by_packer_smg as $row): ?>
                                    <tr>
                                        <td><?php echo htmlspecialchars($row['packer_no']); ?></td>
                                        <td><?php echo htmlspecialchars($row['total']); ?></td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        <?php else: ?>
            <div class="alert alert-info">Tidak ada data packer untuk Semarang dalam rentang tanggal yang dipilih.</div>
        <?php endif; ?>
    </div>
</div>

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