<script type="text/javascript">

let suggests = @json($item_code_list);

function showAutoCompleteItemCodes(key_element)
{
  $("#item_code_"+key_element).multiAutocomplete(suggests);

  $(document).ready(function(){

  		$('.multiAutocomplete-suggestions').css('left','25px');
  		$('.multiAutocomplete-suggestions').css('width','100px');

  });
	
}


function addInvoiceRowOnetime(event)
	{
		event.preventDefault();
		var uniqid = $('#invoice_table tr').length;

		var myfun  = "create_PurchaseItem(event,"+uniqid+")";

     
	    var item_code = '<input required="" type="text" name= "item_code[]" class="form-control form-control-sm item_code " id="item_code_'+uniqid+'" placeholder="Code"/>';

        var description = '<input required="" class = "form-control form-control-sm cls_alpha_numeric" id="description_'+uniqid+'" placeholder="Description" type="text" name="description[]"/>';

        var unit_price = '<input required=""  class = "form-control form-control-sm cls_alpha_numeric" id="unit_price_'+uniqid+'" placeholder="Unit Price" onkeyup='+myfun+' type="text" name="unit_price[]"/>'

        var discount = '<input  class = "form-control form-control-sm cls_alpha_numeric" id="discount_'+uniqid+'"   placeholder="Discount"  onkeyup='+myfun+' type="text" name="discount[]"/>';

        var quantity ='<input required="" class ="form-control form-control-sm cls_alpha_numeric" id="quantity_'+uniqid+'" placeholder="Quantity"  onkeyup='+myfun+' type="text" name="quantity[]"/>'

        var gst_percentage = '<input class = "form-control form-control-sm cls_alpha_numeric" id="gst_percentage_'+uniqid+'" placeholder="GST %"   onkeyup='+myfun+' type="text" name="gst_percentage[]"/>';

        var gst_value = '<input class = "form-control form-control-sm cls_alpha_numeric" id="gst_value_'+uniqid+'" placeholder="GST Value"  type="text" name="gst_value[]" readonly/>';

        var amount = '<input class = "form-control form-control-sm cls_alpha_numeric" id="amount_'+uniqid+'" placeholder="Amount"  type="text" name="amount[]" readonly/>';

        var button = '<button class=" btn-success btn-sm" onclick="addInvoiceRowOnetime(event)"><i class="fa fa-plus"></i></button>';

        var view = '<tr><td>'+item_code+'</td><td>'+description+'</td><td>'+unit_price+'</td><td>'+discount+'</td><td>'+quantity+'</td><td>'+gst_percentage+'</td><td>'+gst_value+'</td><td>'+amount+'</td><td>'+button+'</td></tr>'; 

        $('#invoice_table').append(view); 

    // showAutoCompleteItemCodes('{{$uniqid}}');
  	showAutoCompleteItemCodes(uniqid);
        



		   
	}

    function getVendor(event,obj)
	{
		event.preventDefault();
		
		resetVendorInfo();

		var route = '/vendorinfo/'+$(obj).val();
		showLoader();
		$.get(route,function(data)
		{
    		if(data.id > 0)
    		{
    			$('.name').html(data.name);
    			$('.cnic').html(data.cnic);
    			$('.contact').html(data.contact);
    			$('.address').html(data.temp_address);

    		
    		}

    		removeLoader();
  		});
	}

	function resetVendorInfo()
	{
		$('.name').html("");
		$('.cnic').html("");
		$('.contact').html("");
		$('.address').html("");
	}

	function create_PurchaseItem(event,key_element)
	{
		event.preventDefault();

        var unit_price  		=  $('#unit_price_'+key_element).val();
        var discount 			=  $('#discount_'+key_element).val();
        var quantity  			=  $('#quantity_'+key_element).val();
        var gst_percentage  	=  $('#gst_percentage_'+key_element).val();
        var gst_value  			=  $('#gst_value_'+key_element).val();
        var discount 			=  $('#discount_'+key_element).val();

			var total_amount = unit_price * quantity;

			total_amount = total_amount - discount;

			var gst =  Math.round((gst_percentage * total_amount)/100);

			var total_gst_value  =  $('#gst_value_'+key_element).val(gst);

			total_amount = total_amount + gst;

			var amount =  $('#amount_'+key_element).val(total_amount);
	}

	// AJAX REQUEST TO STORE USER(EMPLOYEE) WHILE COMMISSION
	function storeInvoice(event,obj)
	{
		event.preventDefault();
		var obj = $('#form_add_invoice');
		var route = obj.attr('action');
	    var formdata = obj.serialize();

        showLoader();

		$.post(route, formdata, function(result)
		{
			if(result == true)
		    {

			    messageToaster('success','Invoice has created succssfully.','Success');

              location.reload(true);
			    

		    }
        removeLoader();


        },'json')
        .fail(function(data) {
			        messageToaster('error','Required form validation.','Failed');

		          ajax_show_error(data,obj,$('#md_add_voucher'));


		          removeLoader();

		  });
	}

	function updateInvoice(event,obj)
	{
		event.preventDefault();
		var obj = $('#form_update_invoice');
		var route = obj.attr('action');
	    var formdata = obj.serialize();

        showLoader();

		$.post(route, formdata, function(result)
		{
			if(result == true)
		    {

			    messageToaster('success','Invoice has updated succssfully.','Success');

              location.reload(true);


		    }
        removeLoader();


        },'json')
        .fail(function(data) {
			        messageToaster('error','Required form validation.','Failed');

		          ajax_show_error(data,obj,$('#md_add_voucher'));


		          removeLoader();

		  });
	}



</script>