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

  $(document).on("click", ".custom-serial-no", function(){
       
    var product_id=$(this).data('id');
	

    console.log("this is called");
	console.log(product_id);
    $.ajaxSetup({
  
      headers: 
      {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
      }
 
    }); 

    $.ajax({

      url:url_getserialno_master,
 
      method:"get",

      data:
      {product_id:product_id},
      success:function(data)
      {
        
        var productrowscount=(data.products).length;
        var productrows="";
      
          productrows=productrows+
          '<tr>'+
            
            '<td class="txt-center">'+count+'</td>'+
            '<td class="txt-justify srproduct_name-'+count+'">'+data.products.name+'</td>'+
            '<td class="txt-center"><input type="text" class="form-control serialno_space serialno_space-'+count+'" name="serialno" value="'+(data.products.serial_no == "null" ? "" : data.products.serial_no )+'" id="serialno_space" style="width:440px;height:40px;border:none;"/></td>'+
          '</tr>';
   
        
        $('#serial_tablebody_master').html(productrows);
		$(".master_serialno_submit").attr("data-pid",product_id);
        $("#invoice_serialno_master").modal('show');
      }

    });

  });
 
  

  $(document).on("click", ".master_serialno_submit", function(){
    var serial_no;
    var generated_by=$('#serialno_generater').val();
    var product_id=$(this).data('pid');
	serial_no=$('.serialno_space').val();

		  if(serial_no=="")
		  {
			var product_name=$('.srproduct_name').html();
			alert("Please Enter Serial Number for the product:-"+product_name);
			return false;
		  }
		
      saveserialno_detail(product_id,serial_no,generated_by);
	
  });

});

function saveserialno_detail(id,data,generater)
{
  $.ajaxSetup({
    headers: 
    {
      'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    } 
  });
console.log(id+"hghg");
  $.ajax({ 
    
    url:url_setserialno_master,
    method:"POST",   
    data:{
      id:id,
      data:data,
      generater:generater
    },
  
    success:function(result)
    {
      $("#invoice_serialno_master").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);
      }  
    }
  });
}