                     <div class="row">
                        <div class="col-sm-11">
                            <div class="page-title-box">
                                    <h2 class="page-title">Create Stock Transfer</h2>
                            </div><!--end page-title-box-->
                        </div><!--end col-->
                        <div class="col-sm-1">
                            <div class="page-title-box">
                                    <h2 class="page-title"><a href="stock_transfer"><button  class='btn btn-danger' type="button">Back</button></a></h2>
                            </div><!--end page-title-box-->
                        </div><!--end col-->
                    
                    </div>
                    
                    <div class="row" >
                        <div class="col-sm-12">
                            <div class="card">
                                <div class="card-body">
                                    
                                    <?php 
                                    echo $this->Form->input('from', array(
                                        'div' => array('class' => 'form-group'),
                                        'label' => 'Select Transfer From',
                                        'class' => 'form-control',
                                        'between' => '<span class="desc"></span><div class="controls">',
                                        'after' => '</div>',
                                        'options' => ['Warehouse' => 'Warehouse'], // Use key-value pairs for clarity
                                        'required' => 'required',
                                        'empty' => '- Select Transfer From -',
                                        'id' => 'select_from'
                                    ));
                                    
                                    $transfertooptions=[];
                                    foreach($transferto as $transfer){
                                        $transfertooptions = ['Kitchen'=>'Kitchen'];
                                    }
                                        echo $this->Form->input('to', array(
                                        'div' => array('class' => 'form-group'),
                                        'label' => 'Select Transfer To',
                                        'class' => 'form-control',
                                        'between' => '<span class="desc"></span><div class="controls">',
                                        'after' => '</div>',
                                        'options' => $transfertooptions,
                                        'required' => 'required',
                                        'empty' => '- Select Transfer To -',
                                        'id' => 'select_to',
                                    ));
                                    ?>
                                    <br>
                                    <h5>Items</h5>
                                    <table id="example1" class="display table table-hover table-condensed" cellspacing="0" width="100%">
                                        <thead>
                                            <tr>
                                                <th>#</th>
                                                <th>Name</th>
                                                <th>Brand</th>
                                                <th>Unit Name</th>
                                                <th>Price</th>
                                                <th>Quantity</th>
                                            </tr>
                                        </thead>
                        
                                        <tbody>
                                            <?php $x=1;
                                                foreach ($users as $user): 
                                                   
                                            ?>
                                            <tr>
                                                <td><?php print_r($x);?></td>
                                                <td><?php print_r($user['inventory_skus']['name']);?></td>
                                                <td><?php print_r($user['inventory_skus']['brand']);?></td>
                                                <td><?php print_r($user['inventory_skus']['unit_name']);?></td>
                                                <td><?php print_r($user['inventory_skus']['price']);?></td>
                                                <td><?php echo $this->Form->input('quantity', array(
                                                        'class' => 'form-control',
                                                        'label' => '',
                                                        'required' => 'required',
                                                        'type' => 'number',
                                                        'style' => 'width:auto',
                                                        'value'=>0,
                                                        'id' => 'quantity_' . $user['inventory_skus']['id']
                                                    )); ?>
                                                </td>
                                                <input type="hidden" name='sku_id' id="sku_id_<?php echo htmlspecialchars($user['inventory_skus']['id']); ?>" value="<?php echo $user['inventory_skus']['id'];?>">
                                                <input type="hidden" name='sku_name' id="sku_name_<?php echo htmlspecialchars($user['inventory_skus']['id']); ?>" value="<?php echo $user['inventory_skus']['name'];?>">
                                                <input type="hidden" name='sku_brand' id="sku_brand_<?php echo htmlspecialchars($user['inventory_skus']['id']); ?>" value="<?php echo $user['inventory_skus']['brand'];?>">
                                                <input type="hidden" name='sku_price' id="sku_price_<?php echo htmlspecialchars($user['inventory_skus']['price']); ?>" value="<?php echo $user['inventory_skus']['price'];?>">
                                                <input type="hidden" name='unit_name' id="unit_name_<?php echo htmlspecialchars($user['inventory_skus']['id']); ?>" value="<?php echo $user['inventory_skus']['unit_name'];?>">
                                                <input type="hidden" name='unit_id' id="unit_id_<?php echo htmlspecialchars($user['inventory_skus']['id']); ?>" value="<?php echo $user['inventory_skus']['unit'];?>">
                                            </tr>
                                            <?php $x++;endforeach; ?>            
                                        </tbody>
                                    </table>
                            
                                    <div class="row">
                                        <div class="col-md-12"><br>
                                            <button onclick="getCheckedCheckboxes()" class="btn btn-success" id="dltbtn" style="float:right;">Save</button>
                                            <a href="stock_transfer"><button  class='btn btn-danger' type="button" style="float:right;">Cancel</button></a>
                                        </div>
                                    </div>
                                            
                                        </div>
                                    </div>
                                </div>
                            </section>
                        </div>
                    </div>
                </div>
            </div> 
        </div>
                    

<script>

    
    function getCheckedCheckboxes() {
        
        var selectFrom = document.getElementById('select_from');
        var selectTo = document.getElementById('select_to');
        
        if (selectFrom.value !== '' && selectTo.value !== '') {
            var skurows = $("#example1").dataTable().fnGetNodes();
            var checkedData =[];
    	    
    	    for(var i=0;i<skurows.length;i++){
    	
        		var inputdata=$(skurows[i]).find('input');
        		var currentquantity = inputdata[0].value.trim();
        		var currentSkuPrice = inputdata[4].value.trim();
        		var currentSkuId = inputdata[1].value.trim();
        		var currentSkuname = inputdata[2].value.trim();
        		var currentSkubrand = inputdata[3].value.trim();
        		var currentSkuunitname = inputdata[5].value.trim();
        		var currentSkuunitid = inputdata[6].value.trim();
        		if(currentquantity!="" && currentquantity!="0"){
        			checkedData.push({
                        sku_name: currentSkuname,
                        sku_brand: currentSkubrand,
                        unit_name: currentSkuunitname,
                        unit_id: currentSkuunitid,
                        sku_quantity: currentquantity,
                        id: currentSkuId,
                        price: currentSkuPrice
                    });
                }
	        }
	
        	$.ajax({
                url: '<?php echo $this->Html->url(['controller' => 'dashboard', 'action' => 'add_stock_transfer']); ?>',
                type: 'POST',
                data: { tranferfrom:selectFrom.value,transferto:selectTo.value,checkedData: JSON.stringify(checkedData) }, // Send as JSON string
                success: function(response) {
                    Swal.fire({
                        icon: 'success',
                        title: 'Saved!',
                        text: 'Stock Transfer successfully saved.',
                        confirmButtonText: 'OK'
                    }).then(() => {
                        window.location.href = 'stock_transfer';
                    });
                },
                error: function(xhr, status, error) {
                    Swal.fire({
                        icon: 'error',
                        title: 'Error!',
                        text: 'An error occurred: ' + error
                    });
                }
            });
        }else{
            Swal.fire({
                icon: 'warning',
                title: 'Required!',
                text: 'Transfer From and To are required.'
            });
        }
    }


    $(document).ready(function(){
        var table = $('#example1').DataTable(); // Initialize DataTable
        
       
    });
    
//     document.addEventListener('DOMContentLoaded', function () {
//     var selectFrom = document.getElementById('select_from');
//     var selectTo = document.getElementById('select_to');

//     selectFrom.addEventListener('change', function () {
//         var fromValue = selectFrom.value;

//         // Clear existing options in "Select To"
//         selectTo.innerHTML = "";

//         if (fromValue === 'Bar') {
//             // Enable "Select To" and add "Kitchen" option
//             selectTo.disabled = false;
//             var option = document.createElement('option');
//             option.value = 'Kitchen';
//             option.textContent = 'Kitchen';
//             selectTo.appendChild(option);
//         }else if(fromValue === 'Kitchen') {
//             selectTo.disabled = false;
//             var option = document.createElement('option');
//             option.value = 'Bar';
//             option.textContent = 'Bar';
//             selectTo.appendChild(option);
//         }else {
//             // Disable "Select To"
//             selectTo.disabled = true;
//         }
//     });
// });
</script>

                