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

// Get the request number from URL
$request_number = isset($_GET['request_number']) ? $db->escape($_GET['request_number']) : '';

// Fetch the request details
$sql = "SELECT * FROM purchase_requests WHERE request_number = '{$request_number}'";
$request = $db->query($sql);
$request = $db->fetch_assoc($request);

if (!$request) {
    die('Purchase request not found.');
}

// Fetch the items associated with the request and join with products to get the UOM, new_code, and remark
$sql_items = "SELECT pri.part_number, p.name AS product_name, pri.quantity_needed, pri.due_date, p.uom, p.new_code, pri.remark 
              FROM purchase_request_items pri
              JOIN products p ON pri.part_number = p.part_number
              WHERE pri.request_number = '{$request_number}'";
$items = $db->query($sql_items);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Print Purchase Request</title>
    <style>
        @media print {
            body * {
                visibility: hidden;
            }
            #printableArea, #printableArea * {
                visibility: visible;
            }
            #printableArea {
                position: absolute;
                left: 0;
                top: 0;
            }
            .signature-section {
                border-top: 1px solid #000;
                padding-top: 20px;
                margin-top: 40px;
                display: flex;
                justify-content: space-between;
            }
            .signature-box {
                width: 30%;
                border-top: 1px solid #000;
                text-align: center;
                padding: 10px;
            }
        }
        .signature-section {
            border-top: 1px solid #000;
            padding-top: 20px;
            margin-top: 40px;
            display: flex;
            justify-content: space-between;
        }
        .signature-box {
            width: 30%;
            border-top: 1px solid #000;
            text-align: center;
            padding: 10px;
        }
    </style>
</head>
<body>
    <div id="printableArea">
        <h2 style="text-align: center;">Purchase Request</h2>
        <p><strong>Request Number:</strong> <?php echo htmlspecialchars($request['request_number']); ?></p>
        <p><strong>Request Date:</strong> <?php echo htmlspecialchars($request['request_date']); ?></p>

        <table border="1" cellpadding="5" cellspacing="0" style="width: 100%; border-collapse: collapse;">
            <thead>
                <tr>
                    <th>Part Number</th>
                    <th>Product Name</th>
                    <th>New Code</th>
                    <th>Quantity Needed</th>
                    <th>Due Date</th>
                    <th>UOM</th>
                    <th>Remark</th> <!-- Added Remark column -->
                </tr>
            </thead>
            <tbody>
                <?php if ($items && $db->num_rows($items) > 0): ?>
                    <?php while ($item = $db->fetch_assoc($items)): ?>
                        <tr>
                            <td><?php echo htmlspecialchars($item['part_number']); ?></td>
                            <td><?php echo htmlspecialchars($item['product_name']); ?></td>
                            <td><?php echo htmlspecialchars($item['new_code']); ?></td>
                            <td><?php echo number_format($item['quantity_needed'], 0, ',', '.'); ?></td>
                            <td><?php echo htmlspecialchars($item['due_date']); ?></td>
                            <td><?php echo htmlspecialchars($item['uom']); ?></td>
                            <td><?php echo htmlspecialchars($item['remark']); ?></td> <!-- Display Remark here -->
                        </tr>
                    <?php endwhile; ?>
                <?php else: ?>
                    <tr>
                        <td colspan="7">No items found for this request.</td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>

        <!-- Signature Section -->
        <div class="signature-section">
            <div class="signature-box">
                <p><strong>Requested By:</strong></p>
                <p>__________________________</p>
            </div>
            <div class="signature-box">
                <p><strong>Received By:</strong></p>
                <p>__________________________</p>
            </div>
            <div class="signature-box">
                <p><strong>Approved By:</strong></p>
                <p>__________________________</p>
            </div>
        </div>
    </div>
    <script>
        window.onload = function() {
            window.print();
        }
    </script>
</body>
</html>
