<script>

$(document).ready(function() {
var count = 1;
$("#add_row_btn").click(function(){
    
    $("#add_row_btn").hide();
    
                var row=`<tr class="old_gold_row">
                                                <td>
                                                    <select name="old_product_ids[]" id="product_ids_`+count+`" class="form-control select22" style="width: 100%;">
                                                        <?php $statement = $pdo->prepare("SELECT * FROM `tbl_ids_products` WHERE status=1");
                                                            $statement->execute();
                                                            $resultss = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                                            foreach ($resultss as $ress) { ?>
                                                                <option value="<?=$ress['id']?>"><?=$ress['name']?></option>
                                                        <?php } ?>
                                                    </select>
                                                </td>
                                                <td>
                                                    <select name="old_purity[]" id="purity_ids_`+count+`" onchange="GetPriceValues(`+count+`)" class="form-control purity_ids select22" style="width: 100%;">
                                                        <?php $statement = $pdo->prepare("SELECT * FROM `tbl_ids_purity` WHERE status=1");
                                                            $statement->execute();
                                                            $resultss = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                                            foreach ($resultss as $ress) { ?>
                                                                <option value="<?=$ress['id']?>"><?=$ress['name']?></option>
                                                        <?php } ?>
                                                    </select>
                                                </td>
                                                
                                                <td>
                                                    <select name="old_unit[]" id="unit_`+count+`" class="form-control select22 old_unit" style="width: 100%;">
                                                        <?php $statement = $pdo->prepare("SELECT * FROM `tbl_units` WHERE status=1");
                                                            $statement->execute();
                                                            $resultss = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                                            foreach ($resultss as $ress) { ?>
                                                                <option value="<?=$ress['id']?>"><?=$ress['name']?></option>
                                                        <?php } ?>
                                                    </select>
                                                </td>
                                                
                                                <td>
                                                    <select name="old_product_type[]" id="old_product_type_`+count+`" class="form-control select22 old_product_type" style="width: 100%;">
                                                        <option value="1">OWN</option>
                                                        <option value="2">OTHER</option>
                                                    </select>
                                                </td>
                                                
                                                <td>
                                                    <input type="text" name="old_weight[]" class="form-control table_input old_weight " id="weight_`+count+`" placeholder="Weight" onkeypress="return isNumberKey(this, event);" required data-count="`+count+`">
                                                </td>
                                                
                                                
                                                <td>
                                                    <input type="text" name="old_price[]" class="form-control table_input old_price" id="old_price_`+count+`" placeholder="Price" onkeypress="return isNumberKey(this, event);" required>
                                                </td>
                                                <td>
                                                    <input type="text" name="old_sub_total[]" class="form-control table_input old_total" id="old_sub_total_`+count+`" placeholder="Total" onkeypress="return isNumberKey(this, event);" required>
                                                </td>
                                                
                                                <td style="background: white;">
                                                    <span class="btn btn-danger btn-sm remove_product"  style="margin-top: 0px;"><i class="bi bi-trash3-fill"></i></span>
                                                </td>
                                            </tr>`;
    
        $("#old_append_tbody").append(row);
        $("#add_row_btn").show();
        $(".select22").select2();
        GetPurity(count);
        count++;
});
});

function GetPurity(count){
    console.log("Get Purity");
    $('#purity_ids_'+count).empty();  
    var product_ids = $("#product_ids_"+count).val();
    $.ajax({
            url: 'api/get-values-api.php',
            type: 'post',
            dataType: 'json',
            data: {type:'get_purity',product_ids:product_ids},
            success: function (response) {
                console.log(response);
                if(response['error']==false){
                    var m='';
                    for(var n=0;n<response['data'].length;n++){
                        m+='<option value='+response['data'][n]['id']+'>'+response['data'][n]['name']+'</option>';
                    }
                 $('#purity_ids_'+count).append(m);  
                 $('#purity_ids_'+count).trigger('change');
                 
                }
                GetPriceValues(count);
             
            }
        });
    
}


$(document).on('click', '.remove_product', function() {
        $(this).closest('.old_gold_row').remove();
        grand_old_pro_total();
        calculateTotal();
        CrossStoneNet();
});

function GetPriceValues(count){
    console.log("Get Values");
    var product_ids = $("#product_ids_"+count).val();
    var purity_ids = $("#purity_ids_"+count).val();
    
    $.ajax({
        url: 'api/get-values-api.php',
        type: 'post',
        dataType: 'json',
        data: {type:'get_today_rates',product_ids:product_ids, purity_ids:purity_ids},
        success: function (response) {
            console.log(response);
            if(response['error']==false){
                console.log(response);
                $("#old_price_"+count).val(response['today_rate']);
                OldSectionTotal(count);
            }
        }
    });
    
    
    
}



function OldSectionTotal(count){
    var unit = $("#unit_"+count).val();
    var weight = $("#weight_"+count).val();
    var price = $("#old_price_"+count).val();
    
    if(unit==1){
        cal_s = price*weight;
        $("#old_sub_total_"+count).val(cal_s.toFixed(2));
    }else if(unit==2){
        cal_s = price*(weight*1000);
        $("#old_sub_total_"+count).val(cal_s.toFixed(2));
    }
    grand_old_pro_total();
    calculateTotal();
}

$("body").on("keyup",".old_weight",function(){
    var count = $(this).data("count");
    OldSectionTotal(count);
    calculateTotal();
});

$("body").on("change", ".purity_ids", function(){
    var count = $(this).data("count");
    OldSectionTotal(count);
    calculateTotal();
});


</script>
<script>

    
    
    
    function grand_total(){
        var tot=0;
          $(".totals").each(function(){
            tot+=Number($(this).val());
          });
          
          
          $("#total_product_amount").text(tot.toFixed(2));
          $("#total_amount").val(tot);
          
          calculate();
          calculateTotal();
    }
    
    
    function grand_old_pro_total(){
        var tot=0;
          $(".old_total").each(function(){
            tot+=Number($(this).val());
          });
          $("#old_product_amount_span").text(tot.toFixed(2));
          $("#old_product_amount").val(tot.toFixed(2));
          calculate();
          calculateTotal();
    }
    
    
    function CrossStoneNet(){
        
        
        <?php foreach($price_data as $purity){
            $purity_id = $purity['purity_id'];
        ?>
        
          var net_wt=0;
          $(".net_wt_<?=$purity_id?>").each(function(){
            net_wt+=Number($(this).val());
          });
          
          var wastage_amount = 0;
          $(".wastage_amount_<?=$purity_id?>").each(function(){
            wastage_amount+=Number($(this).val());
          });
          
          var stone_amt=0;
          $(".stone_amount_<?=$purity_id?>").each(function(){
            stone_amt+=Number($(this).val());
          });
          
          
          
          // Paid Amount Starts Here
        paid_amounts = 0;
        
        $('input.paid_amounts:checked').each(function(){
            console.log($(this).data('paid_amount'));
            paid_amounts += Number($(this).data('paid_amount'));
        });
        
          // Paid Amount Endsss Here
          // Scheme Weight - Here
          var scheme_weight = $("#scheme_weight_<?=$purity_id?>").val() || 0;
          var scheme_gold_rate = parseFloat($("#scheme_gold_rate_<?=$purity_id?>").val()) || 0;
          
          scheme_total = parseFloat(scheme_weight)*parseFloat(scheme_gold_rate);
          $("#scheme_total_2").val(paid_amounts.toFixed(2));
          
          
          
          
          // Non Scheme Weight - Here
          $("#non_scheme_weight_<?=$purity_id?>").val((net_wt-scheme_weight).toFixed(3));
          var non_scheme_gold_rate = $("#non_scheme_gold_rate_<?=$purity_id?>").val();
          var non_scheme_weight = $("#non_scheme_weight_<?=$purity_id?>").val();
          non_scheme_total = parseFloat(non_scheme_weight)*parseFloat(non_scheme_gold_rate);
          $("#non_scheme_total_<?=$purity_id?>").val(non_scheme_total.toFixed(2));
                                                
          
          
          
          
          $("#f_stone_<?=$purity_id?>").val(stone_amt.toFixed(3));
          $("#f_total_weight_<?=$purity_id?>").val(net_wt.toFixed(3));
          
          
          var scheme_gold_rate = $("#scheme_total_<?=$purity_id?>").val() || 0;
          var non_scheme_gold_rate = $("#non_scheme_total_<?=$purity_id?>").val() || 0;
          
          console.log("Scheme Gold Rate: "+scheme_gold_rate);
          console.log("Non Scheme Gold Rate: "+non_scheme_gold_rate);
          
          
          
          
          var gold_amount = parseFloat(scheme_gold_rate)+parseFloat(non_scheme_gold_rate);
          $("#f_gold_amount_<?=$purity_id?>").val(gold_amount.toFixed(2));
          $("#f_wastage_<?=$purity_id?>").val(wastage_amount.toFixed(2));
          
          
          var Final_Amount = parseFloat(gold_amount)+parseFloat(wastage_amount)+parseFloat(stone_amt);
          $("#f_grand_total_<?=$purity_id?>").val(Final_Amount.toFixed(2));
          
    
        <?php } ?>
                 
                                    
        

    }
    
    
    
    
    $(document).on('click', '.remove_product', function() {
        $(this).closest('.porow').remove();
        productCount();
        calculate();
        calculateTotal();
        CrossStoneNet();
        grand_total();
    });
    

    
        


function productCount(){
    var rowCount = $('#product_tbody tr').length;
    console.log("Product Row Count: "+rowCount);
    $('#total_product_items').text(rowCount);
    $('#payment_total_items').text(rowCount);
    calculate();
}


</script>



<script>
$(document).ready(function(){
   $('#form_details').on('submit', function(e){
                e.preventDefault();
                $('#loading-div').show();
                $('#content-div').hide();
                $("#submit").prop('disabled', true);
                var formData = new FormData(this);
                formData.append('type', 'create_sales');
                    $.ajax({
                            url: 'apis/sales.php',
                            type: 'post',
                            dataType: 'json',
                            data: formData,
                            cache: false,
                            contentType: false,
                            processData: false,
                            enctype: 'multipart/form-data',
                            success: function (response) {
                                console.log(response);
                                if(response['status']==200){
                                     /*alert(response['message']);*/
                                     console.log(response['sales_id']);
                                     console.log(response['type']);
                                     if(response['type']==1){
                                         window.location.href="pos-payment-add.php?id="+response['sales_id'];
                                         $('#success-div').show();
                                     }else{
                                         $('#quotation-success-div').show();
                                     }
                                     /*$('#form_details').trigger("reset");*/
                                     
                                     $("#submit").prop('disabled', false);
                                     
                                     $('#loading-div').hide();
                                }else{
                                    toastr.error(response['message']);
                                    $("#submit").prop('disabled', false);
                                    $('#loading-div').hide();
                                    $('#content-div').show();
                                }
                            }
                        });
    });
});
</script>



<script>
function product_selection(id, quantity){ 
    $('.search-box input[type="text"]').val('');
    $(".result").empty();
    $('.search-box input[type="text"]').focus();
    console.log("Product Selection ID: "+id);
    var product_present = 0;
    $('.product_ids').each(function(){
        if(id==$(this).val()){
            product_present = 1;
        }
    });
    
    if(product_present==0){
    $.ajax({
        url: 'api/product-search-append-api.php',
        type: 'post',
        dataType: 'text',
        data:{pro_id:id, quantity:quantity },
        success: function (response) {
            console.log('Product ID: '+id);
            $(".select22").select2();
            console.log('Response:  '+response);
            $('#product_tbody').append(response);
            $('.search-box input[type="text"]').val('');
            $(".result").empty();
            $('.search-box input[type="text"]').focus();
            productCount();
            calculate();
            discountType();
            CrossStoneNet();
            grand_total();
        }
    });
    }else{
        toastr.error("Product Already Added");
    }
}

$(document).ready(function(){
    var n='';
    n=parseFloat(n) || 0;

    
    $('.search-box input[type="text"]').on("keyup input", function(){
        console.log('Keyup Value='+n);
        var search_text = $(this).val();
        var resultDropdown = $(this).siblings(".result");
        var location_id = $("#location_id").val();
        if(search_text.length>1){
            $.get("api/product-search-api.php", 
                {search_text: search_text, location_id:location_id}).done(function(data){
                resultDropdown.html(data);
            });
        } else{
            resultDropdown.empty();
        }
    });
});



</script>

<script>
function calculate(){
        
var sub_total = parseFloat($('#total_amount').val()) || 0;
var payable=0;
var tax=$('#tax').val(); // Tax Percentage (GST %)
var tax_type=$('#tax_type').val(); // Tax Type (Inclusive, Exclusive)


if(tax==0){
    $('#tax_after_total').val(sub_total.toFixed(2));
    $('#grand_amount').val(sub_total.toFixed(2));
    $('#total_amount').val(sub_total.toFixed(2));
  $('#div-cgst').hide();
  $('#div-sgst').hide();
  $("#cgst_tax_amount").html(0);
  $("#sgst_tax_amount").html(0);
  $("#total_tax").html(0);
  
  //New One
  $('#cgst_tax_per_input').val(0);
  $('#total_tax_input').val(0);
  
  $('#cgst_tax_per').text(0);
  $('#sgst_tax_per').text(0);
  $('#gst_tax_per').text(0);
  
  
}else{

if(tax_type==1){ //Exclusive

    $('#tax_before_total').text(sub_total.toFixed(2));
    $('#tax_after_total').val((parseFloat(sub_total)+(parseFloat(sub_total)*tax/100)).toFixed(2));
    $('#grand_amount').val((parseFloat(sub_total)+(parseFloat(sub_total)*tax/100)).toFixed(2));
    
    $('#total_tax').text((sub_total*tax/100).toFixed(2));
    $('#total_tax_input').val((sub_total*tax/100).toFixed(2));
    payable = sub_total+((sub_total*tax/100).toFixed(2));
    $('#gst_tax_per').text(tax);
    $('#sgst_tax_per').text((tax/2).toFixed(2));
    $('#cgst_tax_per').text((tax/2).toFixed(2));
    $('#cgst_tax_amount').text(((sub_total*tax/100)/2).toFixed(2));
    $('#cgst_tax_per_input').val(parseFloat((sub_total*tax/100)/2).toFixed(2));
    $('#sgst_tax_amount').text(((sub_total*tax/100)/2).toFixed(2));
}else{
  
  $('#tax_before_total').text((parseFloat(sub_total)-(parseFloat(sub_total)*parseFloat(tax))/(parseFloat(100)+parseFloat(tax))).toFixed(2));
  $('#tax_after_total').val((parseFloat(sub_total)).toFixed(2));
  $('#grand_amount').val((parseFloat(sub_total)).toFixed(2));
  
  $('#total_tax').text(((parseFloat(sub_total)*parseFloat(tax))/(parseFloat(100)+parseFloat(tax))).toFixed(2));
  $('#total_tax_input').val(((parseFloat(sub_total)*parseFloat(tax))/(parseFloat(100)+parseFloat(tax))).toFixed(2));
  payable = sub_total;
  $('#gst_tax_per').text(tax);
  $('#sgst_tax_per').text(tax/2);
  $('#cgst_tax_per').text(tax/2);
  
  
  var tax_sub=((parseFloat(sub_total)*parseFloat(tax))/(parseFloat(100)+parseFloat(tax)))/2;
  
  $('#cgst_tax_amount').text(parseFloat(tax_sub).toFixed(2));
  $('#cgst_tax_per_input').val(parseFloat(tax_sub).toFixed(2));
  $('#sgst_tax_amount').text(parseFloat(tax_sub).toFixed(2));
}  
 
  $('#div-cgst').show();
  $('#div-sgst').show();
  
  
  

}

var gst_type=$('#gst_type').val();

console.log('GST Type: '+gst_type);
  if(gst_type==1){
      $('#gst_type_span').text('GST');
       $('#div-cgst').show();
        $('#div-sgst').show();
  }else{
    $('#gst_type_span').text('IGST');
    $('#div-cgst').hide();
    $('#div-sgst').hide();
  }
  

}


function discountType(){
    var discount_type = $("#discount_type").val();
    
    if(discount_type == 1){
        $("#discount_span").text("(%)");
    } else if(discount_type == 2){
        $("#discount_span").text("Amount");
    }
    calculateTotal();
}

function calculateTotal() {
    var totalAmount = parseFloat($("#tax_after_total").val()) || 0;
    var discount = parseFloat($("#discount").val()) || 0;
    var old_product_amount =  parseFloat($("#old_product_amount").val()) || 0;
    
    
    var discount_type = $("#discount_type").val();
    
    
    if (discount_type == 1) {
        var discountAmount = (totalAmount * discount / 100);
        totalAmount -= discountAmount+old_product_amount;
        $("#discount_amount_span").text(discountAmount.toFixed(2));
        $("#discount_amount").val(discountAmount);
    } else if (discount_type == 2) {
        totalAmount -= discount+old_product_amount;
        $("#discount_amount_span").text(discount.toFixed(2));
        $("#discount_amount").val(discount);
    }
    
    var paid_amount = parseFloat($("#paid_amount").val()) || 0;
    GrandTotal = parseFloat(totalAmount)-parseFloat(paid_amount);
    
    
    $("#grand_amount").val(GrandTotal.toFixed(2));
}

$(document).ready(function(){
        $('#old_gold_section').hide();
        $('#old_gold_button').click(function(){
            $("#old_gold_section").slideToggle();
            return false;
        });
});
</script>
<script>
    $(document).ready(function() {
        $("form").bind("keypress", function(e) {
            if (e.keyCode == 13) {
                return false;
            }
        });
    });
</script>
<script>
$(document).ready(function(){
    
    SalesFunction();
    get_customer_details();
    
$(".finnal_enter").keypress(function(event) {
        if(event.keyCode == 13) { 
        textboxes = $("input.finnal_enter");
        debugger;
        currentBoxNumber = textboxes.index(this);
        if (textboxes[currentBoxNumber + 1] != null) {
            nextBox = textboxes[currentBoxNumber + 1]
            nextBox.focus();
            nextBox.select();
            event.preventDefault();
            return false 
            }
        }
    });
    
    
    
});



$(document).on('change', '#customer_id', function() {
        get_customer_details();
});




function get_customer_details(){
        var customer_id = $("#customer_id").val();
        console.log("Customer ID: "+customer_id);
        $("#payment_scheme_details").empty();
        $("#weight_scheme_details").empty();
        $("#advance_details").empty();
        
        if(customer_id!=""){
            
            $.ajax({
    		    url: 'api/get-customer-details.php',
                type: 'post',
                data: {get_details:'get_payment_scheme_details',customer_id:customer_id},
                success: function (response) {
                    // console.log(response);
                    $("#payment_scheme_details").append(response);
    			},
    			error:function(err){
    			    toastr.error(err);
    			}
    		});
    		
    		$.ajax({
    		    url: 'api/get-customer-details.php',
                type: 'post',
                data: {get_details:'get_weight_scheme_details',customer_id:customer_id},
                success: function (response) {
                    // console.log(response);
                    $("#weight_scheme_details").append(response);
    			},
    			error:function(err){
    			    toastr.error(err);
    			}
    		});
    		
    		$.ajax({
    		    url: 'api/get-customer-details.php',
                type: 'post',
                data: {get_details:'get_customer_advance_details',customer_id:customer_id},
                success: function (response) {
                    // console.log(response);
                    $("#advance_details").append(response);
    			},
    			error:function(err){
    			    toastr.error(err);
    			}
    		});
    		
    		$.ajax({
    		    url: 'api/get-customer-details.php',
                type: 'post',
                dataType: 'json',
                data: {get_details:'get_customer_details',customer_id:customer_id},
                success: function (response) {
                    console.log(response);
                    console.log(response['scheme_amount']);
                    /*$("#scheme_amount").val(response['scheme_amount']);
                    $("#advance_amount").val(response['advance_amount']);*/
    			}
    		});
    		    

        }
    }
    
function SalesFunction(){
    var sales_type = $("#sales_type").val();
    if(sales_type==1){
        $("#sales_section").show();
        $("#scheme_section").hide();
        $("#advance_section").hide();
    }else if(sales_type==2){
        $("#sales_section").hide();
        $("#scheme_section").show();
        $("#advance_section").hide();
    }else if(sales_type==3){
        $("#sales_section").hide();
        $("#scheme_section").hide();
        $("#advance_section").show();
    }
}



function SchemeWeight(weight, amount, type, purity_id){
    
    product_present = 0;
    $('.product_ids').each(function(){
        if($(this).val()){
            product_present = 1;
        }
    });
    
    if(product_present==0){
        toastr.error("Please Select Any One Product!");
        $('input[type="checkbox"]:checked').prop('checked', false);
        return false;
    }else{
    
        var tot=0;
        var paid=0;
        $('input[type="checkbox"]:checked').each(function(){
            console.log($(this).val()); 
            tot+=Number($(this).val());
            paid+=Number($(this).data('paid_amount'));
        });
        
        $("#scheme_weight_"+purity_id).val(tot.toFixed(3));
        $("#scheme_gold_rate_"+purity_id).val(amount.toFixed(3));
        var paid_amount = parseFloat($("#paid_amount").val()) || 0;
        var scheme_amount = parseFloat(amount);
        $("#paid_amount").val((paid).toFixed(2));
        CrossStoneNet();
        productCount();
        calculate();
        grand_total();
        calculateTotal();
        console.log("Final TOTAL: ");
        calculateTotal();
        
        if(tot > 0){
            if(weight == 0){
                $("#payment_scheme_used").val(1);
            }
        
            if(weight != '' && weight != 0){
                $("#weight_scheme_used").val(1);
            }
        }else{
            
            if(weight == 0){
                $("#payment_scheme_used").val(0);
            }
        
            if(weight != '' && weight != 0){
                $("#weight_scheme_used").val(0);
            }
            
        }
    }
    
}



function Advanceweight(weight, amount, type, purity_id){
    
    product_present = 0;
    $('.product_ids').each(function(){
        if($(this).val()){
            product_present = 1;
        }
    });
    
    if(product_present==0){
        toastr.error("Please Select Any One Product!");
        $('input[type="checkbox"]:checked').prop('checked', false);
        return false;
    }else{
        
        var tot=0;
        var advance_paid=0;
        $('input[type="checkbox"]:checked').each(function(){
            console.log($(this).val()); 
            tot+=Number($(this).val());
            advance_paid+=Number($(this).data('paid_amount'));
        });
        
        
        $("#scheme_weight_"+purity_id).val(tot.toFixed(3));
        $("#scheme_gold_rate_"+purity_id).val(amount.toFixed(3));
        var paid_amount = parseFloat($("#paid_amount").val()) || 0;
        var advance_amount = parseFloat(amount);
        $("#paid_amount").val((advance_paid).toFixed(2));
        CrossStoneNet();
        productCount();
        calculate();
        grand_total();
        calculateTotal();
        console.log("Final TOTAL: ");
        calculateTotal();
        
        if(tot >0){
            $("#advance_used").val(1);
        }else{
            $("#advance_used").val(0);  
        }
    }
    
}


let paymentRowIds = [];
let weightRowIds = [];
let advanceRowIds = [];


// Function to handle checkbox clicks
function handlePaymentids(checkbox, rowId) {
    if (checkbox.checked) {
        // Add row ID to the array if it's not already in the array
        if (!paymentRowIds.includes(rowId)) {
            paymentRowIds.push(rowId);
        }
    } else {
        // Remove the row ID from the array if unchecked
        const index = paymentRowIds.indexOf(rowId);
        if (index > -1) {
            paymentRowIds.splice(index, 1);
        }
    }
    
    // Update the hidden input field with the new array values (comma-separated)
    document.getElementById("payment_scheme_ids").value = paymentRowIds.join(',');

    // For debugging, you can log the array to see the current selected IDs
    console.log(paymentRowIds);
}


// Function to handle checkbox clicks
function handleWeightids(checkbox, rowId) {
    if (checkbox.checked) {
        // Add row ID to the array if it's not already in the array
        if (!weightRowIds.includes(rowId)) {
            weightRowIds.push(rowId);
        }
    } else {
        // Remove the row ID from the array if unchecked
        const index = weightRowIds.indexOf(rowId);
        if (index > -1) {
            weightRowIds.splice(index, 1);
        }
    }
    
    // Update the hidden input field with the new array values (comma-separated)
    document.getElementById("weight_scheme_ids").value = weightRowIds.join(',');


    // For debugging, you can log the array to see the current selected IDs
    console.log(weightRowIds);
}


// Function to handle checkbox clicks
function handleAdvanceids(checkbox, rowId) {
    if (checkbox.checked) {
        // Add row ID to the array if it's not already in the array
        if (!advanceRowIds.includes(rowId)) {
            advanceRowIds.push(rowId);
        }
    } else {
        // Remove the row ID from the array if unchecked
        const index = advanceRowIds.indexOf(rowId);
        if (index > -1) {
            advanceRowIds.splice(index, 1);
        }
    }
    
    // Update the hidden input field with the new array values (comma-separated)
    document.getElementById("advance_ids").value = advanceRowIds.join(',');

    // For debugging, you can log the array to see the current selected IDs
    console.log(advanceRowIds);
}


</script>

<style>
    .checkboxx{
        position: absolute;
        top: -20%;
        left: -20%;
        display: block;
        width: 140%;
        height: 140%;
        margin: 0px;
        padding: 0px;
        background: rgb(255, 255, 255);
        border: 0px;
        opacity: 0;
    }
</style>


<style>

    .search-box{
        width: auto;
        display: inline-block;
        font-size: 14px;
    }
    .search-box {
        padding: 5px 10px;
        border: 1px solid #CCCCCC;
        font-size: 14px;
    }
    .result{
        /*position: absolute; */       
        z-index: 999;
        top: 100%;
        left: 0;
    }
    .search-box, .result{
        width: 100%;
        box-sizing: border-box;
    }
    /* Formatting result items */
    .result div{
        margin: 0;
        background: white;
        padding: 7px 10px;
        border: 1px solid #CCCCCC;
        border-top: none;
        cursor: pointer;
    }
    .result div:hover{
        background: blue;
        color: white;
    }
</style>

