<div class="row">
    <div class="col-sm-12">
        <div class="page-title-box"><h2 class="page-title">Create Stock Transfer</h2></div>
    </div>
</div>
<div class="row">
    <div class="col-sm-12">
        <div class="card">
            <div class="card-body">
                <?php echo $this->Form->create(null, ["url" => "create_stock_transfer"]); ?>
                <div class="row">
                    <div class="col-md-6 form-group">
                        <label>From Location</label>
                        <select name="transfer_from" id="fromLoc" class="form-control" required>
                            <option value="">Select</option>
                            <?php foreach($locations as $l): ?>
                            <option value="<?= $l["Location"]["id"] ?>"><?= h($l["Location"]["name"]) ?> (<?= $l["Location"]["type"] ?>)</option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="col-md-6 form-group">
                        <label>To Location</label>
                        <select name="transfer_to" id="toLoc" class="form-control" required>
                            <option value="">Select</option>
                            <?php foreach($locations as $l): ?>
                            <option value="<?= $l["Location"]["id"] ?>"><?= h($l["Location"]["name"]) ?> (<?= $l["Location"]["type"] ?>)</option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                </div>
                <h5 class="mt-3">Items</h5>
                <table class="table" id="itemsTable">
                    <thead><tr><th>SKU</th><th>Quantity</th><th></th></tr></thead>
                    <tbody>
                        <tr class="item-row">
                            <td><select name="items[0][sku_id]" class="form-control select2" required>
                                <option value="">Select SKU</option>
                                <?php foreach($skus as $s): ?>
                                <option value="<?= $s["Inventory_sku"]["id"] ?>"><?= h($s["Inventory_sku"]["name"]) ?> (<?= h($s["Inventory_sku"]["unit_name"]) ?>)</option>
                                <?php endforeach; ?>
                            </select></td>
                            <td><input type="number" name="items[0][quantity]" class="form-control" step="0.01" required></td>
                            <td><button type="button" class="btn btn-danger btn-sm remove-row">X</button></td>
                        </tr>
                    </tbody>
                </table>
                <button type="button" class="btn btn-secondary btn-sm mb-3" id="addRow">+ Add Item</button>
                <br>
                <?= $this->Form->button("Create Transfer", ["class" => "btn btn-primary"]) ?>
                <?= $this->Form->end() ?>
            </div>
        </div>
    </div>
</div>
<script>
var rowIdx = 1;
$("#addRow").click(function(){
    var row = $(".item-row:first").clone();
    row.find("input, select").each(function(){
        var name = $(this).attr("name").replace(/\[\d+\]/,"["+rowIdx+"]");
        $(this).attr("name", name).val("");
    });
    $("#itemsTable tbody").append(row);
    rowIdx++;
});
$(document).on("click",".remove-row",function(){
    if($(".item-row").length > 1) $(this).closest("tr").remove();
});
$("form").submit(function(e){
    if($("#fromLoc").val() === $("#toLoc").val()){
        e.preventDefault();
        alert("From and To locations cannot be the same!");
    }
});
</script>
