<?php
require_once('includes/sql.php');
page_require_level(3);

// Ambil request_number dari URL
$request_number = $_GET['request_number'] ?? '';

// Pastikan request_number tidak kosong
if (empty($request_number)) {
    die("Error: Request number is missing.");
}

// Query data berdasarkan request_number
$sql = "SELECT rm.request_number, p.part_number, p.name, rm.delivery_quantity
        FROM request_material rm 
        LEFT JOIN products p ON rm.product_id = p.id
        WHERE rm.request_number = '{$request_number}'";

$result = $db->query($sql);

// Pastikan data ditemukan
if ($result->num_rows == 0) {
    die("Error: Material request not found.");
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Warehouse Delivery Slip - <?php echo $request_number; ?></title>
    <style>
        body {
            font-family: Arial, sans-serif;
            line-height: 1.6;
        }
        .container {
            width: 80%;
            margin: 0 auto;
        }
        .header {
            text-align: center;
            margin-bottom: 20px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }
        th, td {
            border: 1px solid #ccc;
            padding: 8px;
            text-align: left;
        }
        th {
            background-color: #f2f2f2;
        }
        .print-button {
            text-align: center;
            margin-top: 20px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h2>Warehouse Delivery Slip</h2>
            <p>Request Number: <?php echo $request_number; ?></p>
        </div>

        <table>
            <thead>
                <tr>
                    <th>Product Part Number</th>
                    <th>Product Name</th>
                    <th>Delivery Quantity</th>
                </tr>
            </thead>
            <tbody>
                <?php while ($row = $result->fetch_assoc()) : ?>
                    <tr>
                        <td><?php echo $row['part_number']; ?></td>
                        <td><?php echo $row['name']; ?></td>
                        <td><?php echo $row['delivery_quantity']; ?></td>
                    </tr>
                <?php endwhile; ?>
            </tbody>
        </table>

        <div class="print-button">
            <button onclick="window.print()">Print</button>
        </div>
    </div>
</body>
</html>
