<?php
$page_title = 'Serah Terima';
require_once('includes/load.php');
// page_require_level(3);

// Variables for search filters
$search_date = date('Y-m-d'); // Default current date
$search_kurir = "";

// Check if filter form is submitted
if (isset($_POST['filter'])) {
    $search_date = $_POST['search_date'];
    $search_kurir = $_POST['search_kurir'];

    if (!empty($search_date)) {
        $awbs = filter_awb($search_date, $search_kurir); // Implement filter_awb function to handle search logic
    } else {
        $awbs = []; // Empty array if date is empty
    }
} else {
    $awbs = []; // Initialize as empty array if no search is performed
}

// Function to filter AWB based on search filters
function filter_awb($date, $kurir)
{
    global $db;
    $sql = "SELECT awb.*, products.name AS product_name 
            FROM awb 
            LEFT JOIN products ON awb.part_number = products.part_number 
            WHERE 1=1";

    if (!empty($date)) {
        $sql .= " AND DATE(awb.scan_status) = '$date'";
    }

    if (!empty($kurir)) {
        $sql .= " AND awb.kurir = '$kurir'";
    }

    return find_by_sql($sql);
}

// Function to fetch unique courier names from AWB table
function get_unique_couriers()
{
    global $db;
    $sql = "SELECT DISTINCT kurir FROM awb ORDER BY kurir ASC";
    $result = find_by_sql($sql);
    return $result;
}

// Function to calculate total unique resi
function count_unique_resi($awbs)
{
    $resi_count = [];
    foreach ($awbs as $awb) {
        $resi_count[$awb['resi']] = 1; // Using an associative array to count unique resi
    }
    return count($resi_count);
}

// Function to calculate total quantity of products
function calculate_total_quantity($awbs)
{
    $total_quantity = 0;
    foreach ($awbs as $awb) {
        $total_quantity += $awb['quantity'];
    }
    return $total_quantity;
}

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

<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 clearfix">
                <div class="pull-left">
                    <h3>Serah Terima</h3>
                </div>
                <div class="pull-right">
                    <form method="post" action="serah_terima.php" class="form-inline">
                        <div class="form-group">
                            <label for="search_date">Scan Status Date:</label>
                            <input type="date" class="form-control" name="search_date"
                                value="<?php echo htmlspecialchars($search_date); ?>">
                        </div>
                        <div class="form-group">
                            <label for="search_kurir">Kurir:</label>
                            <select class="form-control" name="search_kurir">
                                <option value="">Pilih Kurir</option>
                                <?php foreach (get_unique_couriers() as $courier): ?>
                                    <option value="<?php echo htmlspecialchars($courier['kurir']); ?>"
                                        <?php echo ($search_kurir == $courier['kurir']) ? 'selected' : ''; ?>>
                                        <?php echo htmlspecialchars($courier['kurir']); ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <button type="submit" name="filter" class="btn btn-default">Filter</button>
                        <a href="print_serah_terima.php?date=<?php echo urlencode($search_date); ?>&kurir=<?php echo urlencode($search_kurir); ?>"
                            target="_blank" class="btn btn-primary">Print</a>
                    </form>
                </div>
            </div>
            <div class="panel-body">
                <?php if (!empty($awbs)): ?>
                    <p><strong>Total Unique Resi:</strong> <?php echo count_unique_resi($awbs); ?></p>
                    <p><strong>Total Quantity:</strong> <?php echo calculate_total_quantity($awbs); ?></p>
                    <table class="table table-bordered">
                        <thead>
                            <tr>
                                <th class="text-center">#</th>
                                <th>Resi</th>
                                <th>Kurir</th>
                                <th>Part Number</th>
                                <th>Product Name</th>
                                <th>Quantity</th>
                                <th>Scan Out</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($awbs as $awb): ?>
                                <tr>
                                    <td class="text-center"><?php echo count_id(); ?></td>
                                    <td><?php echo htmlspecialchars($awb['resi']); ?></td>
                                    <td><?php echo htmlspecialchars($awb['kurir']); ?></td>
                                    <td><?php echo htmlspecialchars($awb['part_number']); ?></td>
                                    <td><?php echo htmlspecialchars($awb['product_name']); ?></td>
                                    <td><?php echo htmlspecialchars($awb['quantity']); ?></td>
                                    <td><?php echo isset($awb['scan_status']) ? htmlspecialchars($awb['scan_status']) : 'N/A'; ?></td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                <?php else: ?>
                    <p>No search results available.</p>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

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