<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

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) {
    $session->msg('d', 'Purchase request not found.');
    redirect('list_purchase_request.php', false);
}

// Fetch the items associated with the request
$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);
?>

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

<div class="row">
    <div class="col-md-12">
        <h2>Purchase Request Details - <?php echo htmlspecialchars($request['request_number']); ?></h2>
        <?php echo display_msg($msg); ?>
    </div>
</div>

<div class="row">
    <div class="col-md-12">
        <!-- Print Button -->
        <div class="mb-3">
            <a href="print_purchase_request.php?request_number=<?php echo urlencode($request_number); ?>" class="btn btn-primary" target="_blank">Print</a>
        </div>

        <!-- Purchase Request Table -->
        <table class="table table-bordered">
            <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 htmlspecialchars($item['quantity_needed']); ?></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 -->
                        </tr>
                    <?php endwhile; ?>
                <?php else: ?>
                    <tr>
                        <td colspan="7">No items found for this request.</td> <!-- Adjust colspan for additional column -->
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

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