<?php
$page_title = 'Production MRP';
require_once('includes/load.php');
page_require_level(1);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $product_id = (int)$_POST['product_id'];
    $order_quantity = (int)$_POST['order_quantity'];
    $schedule_date = $db->escape($_POST['schedule_date']);

    // Calculate MRP using the existing function
    $mrp_results = calculate_material_requirements($product_id, $order_quantity);

    if ($mrp_results !== null) {
        // Display success message
        $msg = "Production schedule and material calculation successful!<br>";
        $msg .= "Product ID: " . $product_id . "<br>";
        $msg .= "Order Quantity: " . $order_quantity . "<br>";
        $msg .= "Schedule Date: " . $schedule_date . "<br>";
    } else {
        $msg = "Failed to calculate MRP and schedule production. Please check the inputs.";
    }
}

$products = find_all('products'); // Retrieve distinct products for dropdown

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

<div class="row">
    <div class="col-md-12">
        <?php echo display_msg($msg); ?>
    </div>
</div>

<div class="row">
    <div class="col-md-12">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">Production MRP</h3>
            </div>
            <div class="panel-body">
                <form method="post" action="production_mrp.php" class="clearfix">
                    <div class="form-group">
                        <label for="product_id">Select Product:</label>
                        <select class="form-control" name="product_id" id="product_id">
                            <option value="">Select Product</option>
                            <?php foreach ($products as $product) : ?>
                                <option value="<?php echo (int)$product['id']; ?>"><?php echo htmlspecialchars($product['part_number'] . ' - ' . $product['name']); ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="order_quantity">Order Quantity:</label>
                        <input type="number" class="form-control" name="order_quantity" required>
                    </div>
                    <div class="form-group">
                        <label for="schedule_date">Schedule Date:</label>
                        <input type="date" class="form-control" name="schedule_date" required>
                    </div>
                    <button type="submit" class="btn btn-primary">Calculate Production MRP</button>
                </form>
            </div>
        </div>
    </div>
</div>

<?php if ($mrp_results !== null) : ?>
<div class="row">
    <div class="col-md-12">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">Production Schedule</h3>
            </div>
            <div class="panel-body">
                <table class="table table-bordered">
                    <thead>
                        <tr>
                            <th>Material ID</th>
                            <th>Material Name</th>
                            <th>Required Quantity</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($mrp_results['materials'] as $material) : ?>
                        <tr>
                            <td><?php echo $material['material_id']; ?></td>
                            <td><?php echo $material['material_name']; ?></td>
                            <td><?php echo $material['required_quantity']; ?></td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
                <p>Total MRP: <?php echo $mrp_results['total_mrp']; ?></p>
            </div>
        </div>
    </div>
</div>
<?php endif; ?>

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