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

// Cek apakah ada ID yang dikirim via URL
$id = (int)$_GET['id'];
$item = find_by_id('purchase_request_items', $id);

if (!$item) {
    $session->msg("d", "Item not found.");
    redirect('list_purchase_item.php');
}

// Update item jika form edit di-submit
if (isset($_POST['edit'])) {
    $req_fields = ['request_number', 'part_number', 'quantity_needed', 'due_date', 'uom'];
    validate_fields($req_fields);

    if (empty($errors)) {
        $r_number = remove_junk($db->escape($_POST['request_number']));
        $p_number = remove_junk($db->escape($_POST['part_number']));
        $qty = remove_junk($db->escape($_POST['quantity_needed']));
        $due_date = remove_junk($db->escape($_POST['due_date']));
        $uom = remove_junk($db->escape($_POST['uom']));

        $sql = "UPDATE purchase_request_items SET request_number='{$r_number}', part_number='{$p_number}', quantity_needed='{$qty}', due_date='{$due_date}', uom='{$uom}' WHERE id='{$id}'";
        $result = $db->query($sql);

        if ($result) {
            $session->msg('s', "Item updated.");
            redirect('list_purchase_item.php');
        } else {
            $session->msg('d', 'Failed to update item.');
        }
    } else {
        $session->msg("d", $errors);
    }
}

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-6">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">Edit Purchase Request Item</h3>
            </div>
            <div class="panel-body">
                <form method="post" action="edit_purchase_item.php?id=<?php echo $item['id']; ?>">
                    <div class="form-group">
                        <label for="request_number">Request Number</label>
                        <input type="text" class="form-control" name="request_number" value="<?php echo remove_junk($item['request_number']); ?>" required>
                    </div>
                    <div class="form-group">
                        <label for="part_number">Part Number</label>
                        <input type="text" class="form-control" name="part_number" value="<?php echo remove_junk($item['part_number']); ?>" required>
                    </div>
                    <div class="form-group">
                        <label for="quantity_needed">Quantity Needed</label>
                        <input type="number" class="form-control" name="quantity_needed" value="<?php echo remove_junk($item['quantity_needed']); ?>" required>
                    </div>
                    <div class="form-group">
                        <label for="due_date">Due Date</label>
                        <input type="date" class="form-control" name="due_date" value="<?php echo remove_junk($item['due_date']); ?>" required>
                    </div>
                    <div class="form-group">
                        <label for="uom">UoM</label>
                        <input type="text" class="form-control" name="uom" value="<?php echo remove_junk($item['uom']); ?>" required>
                    </div>
                    <button type="submit" name="edit" class="btn btn-primary">Update</button>
                    <a href="list_purchase_item.php" class="btn btn-default">Cancel</a>
                </form>
            </div>
        </div>
    </div>
</div>

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