$(document).ready(function(){
	selectDataInput();
		var count = $("#counter").val();
	if(count == null){count=1;}
	$(document).on('click', '#addRows', function() { 
		count++;
		var selectValue = $('#select').val();
		var selectText = $("#select option:selected").text();
		/*
				var selectValueSplit = selectValue.split(",");
				var selectItemname = selectValueSplit[0];
				var selectItemid = selectValueSplit[1];
				var selectItemcount = selectValueSplit[2];
				var selectItemprice = selectValueSplit[3];
			*/
				var invType = $("#type").val();

				var productName =  document.querySelector('#select').value;
				var value = $("#select").val();
				var productId =  $('#s [value="' + value + '"]').data('id');
				var maxCount =  $('#s [value="' + value + '"]').data('count');
				var priceForm = $('#priceForm').val();
				var quantityForm = $('#quantityForm').val();
		if(selectValue != 0){
							for(i=1;i<=count;i++){
					if( $('#productName_'+i+'').val() == productName){
									var createRow = 1 ;
									var quantity = $('#quantity_'+i+'').val();
									var q = (+quantity + +quantityForm )  ;
									$('#quantity_'+i+'').val(q);

					}
				} 
				
		$('#extras').show();
		$('#extrastotal').show();
			
		if(createRow !== 1){
		$('#extras').show();
		$('#extrastotal').show();
		var htmlRows = '';
		htmlRows += '<tr id="item">';
		htmlRows += '<input type="hidden" name="productcode[]"class="form-control"  id="productcode_'+count+'"  value="'+productId+'"  autocomplete="off" required>';	
		htmlRows += '<td><input type="text" name="productName[]"class="form-control"  id="productName_'+count+'"  value="'+productName+'" readonly required ></td>';
		htmlRows += '<td><input type="number" step="any" name="quantity[]" id="quantity_'+count+'" class="form-control quantity" value="'+quantityForm+'" min="1" required ></td>';   
		htmlRows += '<td><input type="number" step="any" name="price[]" id="price_'+count+'" value="'+priceForm+'"  class="form-control price"  required></td>';		 
		htmlRows += '<td><input type="number" step="any" name="total[]" id="total_'+count+'" class="form-control total" value="'+priceForm*quantityForm+'" readonly required></td>';          
		htmlRows += '<td><button class="btn btn-danger delete" id="removeRows" type="button">حذف</button></td>';          
		htmlRows += '</tr>';
		}
		$("#select").val("").trigger( "change" ); 
		$('#invoiceItem').append(htmlRows);
		$("#priceForm").val(""); 
		$("#sellPriceForm").val(""); 
		$("#quantityForm").val("1"); 
		$("#discountForm").val(""); 
		calculateTotal();
		selectDataInput();


			}else{
				selectDataInput()
			swal({
				 title:"الرجاء إختيار صنف",
				 type:"warning",
				 showConfirmButton:false,
				 timer:800
			});	
			
			}		
			 
			}); 
	$(document).on('click', '#removeRows', function(){
		
			$(this).closest('tr').remove();
		
		$('#checkAll').prop('checked', false);
		calculateTotal();
	});		
	$(document).on('keyup', "[id^=quantity_]", function(){
		calculateTotal();
	});	
	$(document).on('keyup', "[id^=price_]", function(){
		calculateTotal();
	});	
	$(document).on('keyup', "#taxRate", function(){		
		calculateTotal();
	});	
	$(document).on('keyup', "#amountPaid", function(){
		var amountPaid = $(this).val();
		var totalAftertax = $('#totalAftertax').val();	
		if(amountPaid && totalAftertax) {
			totalAftertax = totalAftertax-amountPaid;			
			$('#amountDue').val(totalAftertax);
		} else {
			$('#amountDue').val(totalAftertax);
		}	
	});	

});	
 
function calculateTotal(){
	var totalAmount = 0; 
	$("[id^='price_']").each(function() {
		var id = $(this).attr('id');
		id = id.replace("price_",'');
		var price = $('#price_'+id).val();
		var quantity  = $('#quantity_'+id).val();
		if(!quantity) {
			quantity = 1;
		}
		var total = price*quantity;
		$('#total_'+id).val(parseFloat(total));
		totalAmount += total;			
	});
	$('#subTotal').val(parseFloat(totalAmount));	
	var taxRate = $("#taxRate").val();
	var subTotal = $('#subTotal').val();	
	if(subTotal) {
		var taxAmount = subTotal*taxRate/100;
		$('#taxAmount').val(taxAmount);
		subTotal = parseFloat(subTotal)+parseFloat(taxAmount);
		$('#totalAftertax').val(subTotal);		
		var amountPaid = $('#amountPaid').val();
		var totalAftertax = $('#totalAftertax').val();	
		if(amountPaid && totalAftertax) {
			totalAftertax = totalAftertax-amountPaid;			
			$('#amountDue').val(totalAftertax);
		} else {		
			$('#amountDue').val(subTotal);
		}
	}
}
function selectDataInput(){

		var varInpBox = document.getElementById('select');  
		varInpBox.focus;
		varInpBox.select();
		varInpBox.setActive;
		}
 