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

if (isset($_POST['submit'])) {
    $product_id = $db->escape($_POST['product_id']); // Sanitize input
    $quantity = $db->escape($_POST['quantity']);     // Sanitize input

    // Insert into database
    $sql = "INSERT INTO purchase_requests (product_id, quantity, request_date)
            VALUES ('$product_id', '$quantity', NOW())";
    $result = $db->query($sql);

    if ($result) {
        $session->msg('s', "Purchase request has been submitted!");
        redirect('purchase.php');
    } else {
        $session->msg('d', "Failed to submit purchase request.");
        redirect('purchase.php'); // Handle error redirection
    }
}

$raw_materials = find_raw_materials_below_quantity(1000); // Replace with your actual function

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

<div class="row">
    <div class="col-md-12">
        <?php echo display_msg($msg); ?>
    </div>
    <div class="col-md-12">
        <div class="panel panel-default">
            <div class="panel-heading clearfix">
                <div class="pull-right">
                    <a href="#" class="btn btn-success submenu-toggle">
                        <i class="glyphicon glyphicon-purchase"></i>
                        <span>Purchase Request</span>
                    </a>
                </div>
            </div>
            <div class="panel-body">
                <table class="table table-bordered">
                    <thead>
                        <tr>
                            <th class="text-center">#</th>
                            <th>Part Number</th>
                            <th>Product</th>
                            <th class="text-center">In-Stock</th>
                            <th class="text-center">Requested Qty</th>
                            <th class="text-center">Qty to Request</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($raw_materials as $material) : ?>
                            <tr>
                                <td class="text-center"><?php echo count_id(); ?></td>
                                <td><?php echo isset($material['part_number']) ? htmlspecialchars($material['part_number']) : 'N/A'; ?></td>
                                <td><?php echo remove_junk($material['name']); ?></td>
                                <td class="text-center"><?php echo get_product_stock($material['id']); ?></td>
                                <td class="text-center"><?php echo get_requested_quantity($material['id']); ?></td>
                                <td class="text-center">
                                    <form method="post" action="purchase.php">
                                        <input type="hidden" name="product_id" value="<?php echo (int) $material['id']; ?>">
                                        <input type="number" name="quantity" min="1" max="<?php echo get_product_stock($material['id']); ?>">
                                        <button type="submit" name="submit" class="btn btn-primary">Request Purchase</button>
                                    </form>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
<?php include_once('layouts/footer.php'); ?>
