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

// Fungsi untuk mendapatkan data dari tabel purchase_request_items berdasarkan ID
function get_purchase_request_item($id) {
    global $db;
    $sql = "SELECT pri.id, pri.request_number, pri.part_number, p.name as product_name, pri.quantity_needed, 
            pri.due_date, p.uom, pri.incoming, pri.status 
            FROM purchase_request_items pri
            LEFT JOIN products p ON pri.part_number = p.part_number
            WHERE pri.id = '{$db->escape($id)}'";
    $result = $db->query($sql);
    return $db->fetch_assoc($result);
}

// Cek apakah ID diterima dari parameter GET
if (isset($_GET['id'])) {
    $id = (int)$_GET['id'];
    $item = get_purchase_request_item($id);
    if (!$item) {
        $session->msg("d", "Item not found.");
        redirect('list_purchase_item.php');
    }
} else {
    $session->msg("d", "No item ID specified.");
    redirect('list_purchase_item.php');
}

// Proses form jika ada pengiriman data POST
if (isset($_POST['submit'])) {
    $incoming_qty = (int)$_POST['incoming_qty'];
    $status = $db->escape($_POST['status']);
    
    // Update data di tabel purchase_request_items
    $sql = "UPDATE purchase_request_items SET incoming = '{$incoming_qty}', status = '{$status}' WHERE id = '{$id}'";
    if ($db->query($sql)) {
        $session->msg("s", "Incoming item updated.");
        redirect('list_purchase_item.php');
    } else {
        $session->msg("d", "Failed to update item.");
        redirect('add_incoming.php?id=' . $id);
    }
}

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">Add Incoming</h3>
            </div>
            <div class="panel-body">
                <form action="add_incoming.php?id=<?php echo $id; ?>" method="post">
                    <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']); ?>" readonly>
                    </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']); ?>" readonly>
                    </div>
                    <div class="form-group">
                        <label for="product_name">Product Name</label>
                        <input type="text" class="form-control" name="product_name" value="<?php echo remove_junk($item['product_name']); ?>" readonly>
                    </div>
                    <div class="form-group">
                        <label for="quantity_needed">Quantity Needed</label>
                        <input type="text" class="form-control" name="quantity_needed" value="<?php echo remove_junk($item['quantity_needed']); ?>" readonly>
                    </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']); ?>" readonly>
                    </div>
                    <div class="form-group">
                        <label for="incoming_qty">Incoming Quantity</label>
                        <input type="number" class="form-control" name="incoming_qty" required>
                    </div>
                    <div class="form-group">
                        <label for="status">Status</label>
                        <select class="form-control" name="status" required>
                            <option value="pending" <?php echo $item['status'] == 'pending' ? 'selected' : ''; ?>>Pending</option>
                            <option value="received" <?php echo $item['status'] == 'received' ? 'selected' : ''; ?>>Received</option>
                        </select>
                    </div>
                    <button type="submit" name="submit" class="btn btn-primary">Update Incoming</button>
                </form>
            </div>
        </div>
    </div>
</div>

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