$(document).ready(function() {
  $("#invoiceserialno").css("width","800px");

  $("#invoice_home").on("click", ".invoice_serialno", function(){
       
    var invoice_id=$(this).data('id');
    
    $.ajaxSetup({
  
      headers: 
      {

        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')

      }
 
    }); 

    $.ajax({

      url:url_getserialno,
 
      method:"POST",

      data:
      {invoice_id:invoice_id},
      success:function(data)
      {
        $("#serialno_trackinvoiceno").val(data.invoice_no);
		
        var productrowscount=(data.products).length;
        var productrows="";
        var count=1;
        
        for(var i=0;i<productrowscount;i++)
        {
          productrows=productrows+
          '<tr>'+
            '<td class="txt-center"><input type="checkbox"  style="width:15px;" name="serialcheck" id="serialno_check" class="serialno_check" value="'+count+'" data-id="'+data.products[i].id+'" '+ (data.products[i].serial_no == null ? "" : "disabled" )+'></td>'+
            '<td class="txt-center">'+count+'</td>'+
            '<td class="txt-justify srproduct_name-'+count+'">'+data.products[i].name+'</td>'+
            '<td class="txt-center"><input type="text" class="form-control serialno_space serialno_space-'+count+'" name="serialno" value="'+(data.products[i].serial_no == "null" ? "" : data.products[i].serial_no )+'" id="serialno_space" multiple="multiple" style="width:440px;height:40px;border:none;" readonly /></td>'+
          '</tr>';
          count++;
        }
        
        $('#serial_tablebody').html(productrows);
        console.log('here called 3-3');
        $("#invoice_serialno").modal('show');
      }

    });

  });
 
  $(document).on("click", "input:checkbox[name=serialcheck]", function(){
    var product_id=$(this).val();
    
    if($(this).prop("checked") == true)
    {
      $('.serialno_space-'+product_id).prop('readonly', false);
    }
    else if($(this).prop("checked") == false)
    {
      $('.serialno_space-'+product_id).prop('readonly', true);
      $('.serialno_space-'+product_id).val("");
    }
    
  });

  $(document).on("click", ".serialno_submit", function(){
    var id=[];
    var serial_no=[];
    var product_id=[];
    var invoice_no=$('#serialno_trackinvoiceno').val();
    var generated_by=$('#serialno_generater').val();
    
    $('input:checkbox[name=serialcheck]:checked').each(function(i){
      id[i]=$(this).val();
      product_id[i]=$(this).data('id');
    });
    
    var checkboxcont=(id).length;
    if(checkboxcont){
		
		for(var i=0;i<checkboxcont;i++)
		{
		 serial_no[i]=$('.serialno_space-'+id[i]).val();

		  if(serial_no[i]=="")
		  {
			var product_name=$('.srproduct_name-'+id[i]).html();
			alert("Please Enter Serial Number for the product:-"+product_name+"or Deselect the Checkbox");
			return false;
		  }
		  
		}
		console.log(serial_no);
      saveserialno_detail_i(product_id,serial_no,invoice_no,generated_by);
	}else{
		$("#invoice_serialno").modal('hide');
	}
  });

});

function saveserialno_detail_i(id,data,invoice,generater)
{
  $.ajaxSetup({
    headers: 
    {
      'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    } 
  });
console.log("i'm 3-3 here too");
  $.ajax({ 
    
    url:url_setserialno,
  
    method:"POST",
   
    data:{
      id:id,
      data:data,
      invoice:invoice,
      generater:generater
    },
  
    success:function(result)
    {
      $("#invoice_serialno").modal('hide');
      if(result==true)
      {
        var success="";

        success=success+
      
        '<p style="text-align:center;"><b>Serial number Addeed Sucess Fully!!!</b></p>';
      
          $('#invoice_success').append(success);
          $('#invoice_success').removeClass("hide");
      
          setTimeout(function(){
            location.reload();
          }, 200);
      }
      else
      {
        var success="";

        success=success+
      
        '<p style="text-align:center;"><b>Serial number Not Addeed SucessFully Please Try again!!!</b></p>';
      
          $('#invoice_success').append(success);
          $('#invoice_success').removeClass("hide");
      
          setTimeout(function(){
            location.reload();
          }, 200);
      }  
    }
  });
}