<?php
require_once('includes/load.php');
page_require_level(3); // Require login level 3 (adjust as per your authentication system)

// Retrieve date and kurir from query string
$search_date = isset($_GET['date']) ? $_GET['date'] : '';
$search_kurir = isset($_GET['kurir']) ? $_GET['kurir'] : '';

// Function to fetch AWB data based on filters
function fetch_awb_data($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);
}

// Fetch AWB data based on filters
$awbs = fetch_awb_data($search_date, $search_kurir);

// Function to count unique AWB IDs
function count_unique_awb_ids($awbs) {
    $awb_ids = array();
    foreach ($awbs as $awb) {
        $awb_ids[$awb['resi']] = 1;
    }
    return count($awb_ids);
}

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

// Calculate totals
$total_resi = count_unique_awb_ids($awbs);
$total_quantity = calculate_total_quantity($awbs);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Print Serah Terima</title>
    <style>
        body {
            font-family: Arial, sans-serif;
        }
        .container {
            max-width: 800px;
            margin: auto;
            padding: 20px;
            position: relative; /* Position container relative for absolute positioning inside */
        }
        .header-info {
            position: absolute;
            top: 20px; /* Adjust top position as needed */
            right: 20px; /* Adjust right position as needed */
            text-align: right;
        }
        .totals p {
            margin: 5px 0;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }
        table, th, td {
            border: 1px solid #ddd;
            padding: 2px; /* Mengurangi padding agar lebih banyak data masuk */
            font-size: 8px; /* Ukuran font yang lebih kecil */
        }
        th {
            background-color: #f2f2f2;
            text-align: left;
        }
        .signatures {
            margin-top: 20px; /* Mengurangi margin top pada tanda tangan */
            text-align: center;
            display: flex;
            justify-content: space-between; /* Mengatur jarak antara tanda tangan kanan dan kiri */
        }
        .signature {
            flex: 1; /* Meratakan lebar tanda tangan */
        }
    </style>
</head>
<body>

<div class="container">
    <div class="header-info">
        <p><strong>Total Resi:</strong> <?php echo htmlspecialchars($total_resi); ?></p>
        <p><strong>Total Barang:</strong> <?php echo htmlspecialchars($total_quantity); ?></p>
    </div>

    <h2>Serah Terima</h2>
    <p><strong>Tanggal:</strong> <?php echo htmlspecialchars($search_date); ?></p>
    <p><strong>Kurir:</strong> <?php echo htmlspecialchars($search_kurir); ?></p>

    <table>
        <thead>
            <tr>
                <th>#</th>
                <th>Resi</th>
                <th>SKU</th>
                <th>Quantity</th>
                <th>Scan Status</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($awbs as $awb): ?>
                <tr>
                    <td><?php echo count_id(); ?></td>
                    <td><?php echo htmlspecialchars($awb['resi']); ?></td>
                    <td><?php echo htmlspecialchars($awb['part_number']); ?></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>

    <div class="signatures">
        <div class="signature">
            <p>Tanda Tangan yang Menyerahkan: _____________________</p>
        </div>
        <div class="signature">
            <p>Tanda Tangan yang Menerima: ________________________</p>
        </div>
    </div>
</div>

<script>
    // Uncomment the line below to automatically open the print dialog on page load
    // window.print();
</script>

</body>
</html>
