/*  $(document).ready(function() {
 
	 call_list(1,1);
	 
});
function callSearch_btn(){
	var sss=$('#start_date').val();
	var eee=$('#end_date').val(); 
	if(sss!="" && eee!=""){
		call_list(sss, eee);
		setTimeout(function(){
		$('#start_date').val(sss);
		$('#end_date').val(eee);},2000);
	}else if(sss!="" && eee==""){		
		call_list(sss, 1);
		setTimeout(function(){
		$('#start_date').val(sss);},2000);
	}else if(sss=="" && eee!=""){
		call_list(1, eee);
		setTimeout(function(){
		$('#end_date').val(eee);
		},2000);
	}
	
}

function call_list(s_date, e_date){
	 var tableDiv='<table id="invoice-datatable-responsive" class="invoice_datatable display" width="100%"></table>';
	$('#doc_list').html(tableDiv);
	var TableDataSet='';
	jQuery.ajax({
		url: baseurl + 'Invoice/invoiceList',
		data: {'s_date' : s_date , 'e_date' : e_date},
		type: 'post', 
		dataType: 'json',			 
		success: function (html) {			
			var table = $('#invoice-datatable-responsive').DataTable( {
				data: html.data,
				columns: [
					{ title: "Invoice Date" },
					{ title: "Invoice No." },
					{ title: "Client Name" },
					{ title: "Invoice amount" },
					{ title: "Paid status" },
					{ title: "Payment Date" },
					{ title: "Amount Paid" },
					{ title: "Balance payment" },
					{ title: "Action" }
				]
			} );	
	 
		},
		complete: function (data) {
			setTimeout(function(){
				var div='<div class="search_div_table">';
				div +='<input placeholder="Start Date" type="text" id="start_date" class="date_txt sDate"/><i class="fa fa-calendar sDate" aria-hidden="true"></i>';
				div +='<input placeholder="End Date" type="text" id="end_date" class="date_txt eDate"/><i class="fa fa-calendar eDate" aria-hidden="true"></i><input type="button" onclick="callSearch_btn()" class="btn btn-primary" value="Search" /></div>';
				$('.dataTables_filter').append(div);
				$('.sDate').datepicker({ dateFormat: 'yy-mm-dd' });
				$('.eDate').datepicker({ dateFormat: 'yy-mm-dd' });
			},1000);
		}
		 
	});
}

function callClient_url(){	 
	var txt='There is no data for generating Invoice. Please before add Client. ';
	txt += ' <a class="btn btn-primary" href="'+baseurl+'tlc/client">OK</a>';
	$('#client_info').html(txt);
}
</script>
 
 <div class="modal fade" id="invoice_list_msg_div">
    <div class="modal-dialog">
        <div class="modal-content panel-primary">
             <div class="modal-header panel-heading">                 
                <h1 class="modal-title" id="invoice_list_h1"></h1>
            </div>
			<div class="modal-body edit-document-control">
				<div id="invoice_listMSG">
				</div>
			</div>
        </div>
    </div>
</div>
<script>
	function listDIv(){
		$('#invoice_list_h1').text('');
		$('#invoice_listMSG').html('<p style="text-align:center"><img src="'+baseurl+'assets/images/loader.gif"/></p>');
	}
	jQuery(document).on('click', '.invoice_send_mailBTN', function () {
		listDIv();
		$('#invoice_list_h1').text($(this).data('title'));
	    
	    var id=$(this).data('utc');
		if(id!=0 || id!=""){
			jQuery.ajax({
				url: baseurl + 'Invoice/invoice_mailSend',
				type: 'post',
				data: {uTc : id}, 
				dataType: 'html',				 
				success: function (html) {
					if(html==true){ 
						$('#invoice_listMSG').html('<h4 style="color:green">Invoice Send Successfully!</h4>');
					}else{
						$('#invoice_listMSG').html('<h4 style="color:red">Error! Please try again.<h4>');
					}
					
				},
				error: function (xhr, ajaxOptions, thrownError) {
					console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
				}
			});
		}  
	});
	
	jQuery(document).on('click', '.invoicelist_share_mailBTN', function () {
		listDIv();
		$('#invoice_list_h1').text($(this).data('title'));
		var utc=$(this).data('utc');
	    $('#invoice_listMSG').html('<div id="mailShare_div"><div class="form-group"><b>Enter Emails with ( , )</b></br><input placeholder="Enter Emails" class="form-control" type="email" id="share_emails_list" /></div></div><p id="email_errorMSg"></p><input type="button" data-sahreutc="'+utc+'" class="btn btn-primary shareEmials_list" value="Share" />');
		//$('#invoice_listMSG').html('<div id="mailShare_div" class="col-sm-11"><div class="form-group"><input class="form-control" type="email" name="share_email_list" /></div></div><div class="col-sm-1"><a class="btn btn-primary fa fa-plus"></a></div><div class="col-sm-12"><input type="button" class="btn btn-primary" value="Share" /></div>');
	  
	   
	});
	
	 
	jQuery(document).on('blur', '#share_emails_list', function () {
		var Email = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var email_id=$(this).val();	 		
		var emailData=email_id.split(',');	
		 for(var i=0;i<emailData.length;i++){
			var str=emailData[i];
			if (!str.match(Email)){
				$('#email_errorMSg').text('Please enter a valid email address-' + emailData[i]).css('color','red');
				 
				 return false;
			}else{
				$('#email_errorMSg').text('').css('color','');
				 
			}
		} 
	});
	
	jQuery(document).on('click', '.invoice_print_mailBTN', function () {
		$('#invoice_list_msg_div').css('display','none');
		var id=$(this).data('utc');
		window.open(baseurl+'download_pdf/invoice/'+id);
	});
	
	jQuery(document).on('click', '.shareEmials_list', function () {
		if($('#email_errorMSg').text()==""){
			var utcid=$(this).data('sahreutc');
			//alert(id);
			var email=$('#share_emails_list').val();
			if(utcid!=0 || utcid!="" || email!=""){
				jQuery.ajax({
					url: baseurl + 'Invoice/invoice_mail_listShare',
					type: 'post',
					data: {utcid : utcid, emails : email}, 
					dataType: 'json',				 
					success: function (html) {
						if(html!=""){ 
							$('#invoice_listMSG').html(html);
						}else{
							$('#invoice_listMSG').html('<h4 style="color:red">Error! Please try again.<h4>');
						}						
					},
					error: function (xhr, ajaxOptions, thrownError) {
						console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
					}
				});
			}  
		}else{alert('Before Remove error!')}
	});
	
	jQuery(document).on('click', '.edit_invoice_mailBtn', function () {
 
		var utcid=$(this).data('utc');
		if(utcid!=0 || utcid!=""){
			jQuery.ajax({
				url: baseurl + 'Invoice/getexit_invoice',
				type: 'post',
				data: {utcid : utcid}, 
				dataType: 'json',				 
				success: function (html) {
					var data=html;
					$('#edit_invoice_client_name').val(data.client_name);
					$('#edit_invoice_UTC').val(data.uTc);
					$('#edit_invoice_NO').val(data.InvoiceNo);
					$('#edit_invoice_Date').val(data.invoiceDate);
					var desc=$.parseJSON(data.Description);
					var amount=$.parseJSON(data.Amount);
					var Description='';
					if(desc.length>amount.length){
						var count=desc.length
					}else{
						var count=amount.length
					}
					for(var i=0;i<count;i++){ 		 
					 Description +='<div id="removeDesc_'+i+'" style="display:flex;padding-bottom:10px"><span onclick="edit_remove_desc('+i+')" class="fa fa-close" style="position: absolute; right: 15px; margin-top: 9px; color: red; cursor: pointer;"></span>';
					  Description +='<div class="col-sm-9"><input class="form-control edit_invoice_Description" placeholder="Description *" name="edit_invoice_Description[]" value="'+desc[i]+'" type="text"></div>';
						Description +='<div class="col-sm-3"><input class="form-control edit_invoice_price" placeholder="Price *" name="edit_invoice_price[]" value="'+amount[i]+'" type="text"></div></div>';
					}
					$('#editINvoiceDiv').html(Description);
					var tax='';
					for (var i = 0; i <= 30; i++) {	
						if(i==data.ServiceTax){
							tax += '<option selected="selected" value="'+i+'">'+i+'</option>';
						}else{
							tax +='<option value="'+i+'">'+i+'</option>';
						}
					}
					$('#edit_invoice_client_tax').html(tax);
					$('#edit_invoice_subTotal').val(data.NetAmount);
					$('#edit_invoice_other').val(data.OtherCharges);
					edit_all_input_value();
					 
				},
				error: function (xhr, ajaxOptions, thrownError) {
					console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
				}
			});
		}  		 
	});
	
	jQuery(document).on('click', '#editINVOICEBTN', function () {
		var x = Math.floor((Math.random() * 100) + (Math.random() * 10));
		var html='<div id="removeDesc_'+x+'" style="display:flex;padding-bottom:10px"><span onclick="edit_remove_desc('+x+')" class="fa fa-close" style="position: absolute; right: 15px; margin-top: 9px; color: red; cursor: pointer;"></span><div class="col-sm-9">';
		html +='<input class="form-control edit_invoice_Description" placeholder="Description *" name="edit_invoice_Description[]" value="" type="text">';
		html +='</div><div class="col-sm-3"><input class="form-control edit_invoice_price" placeholder="Price *" name="edit_invoice_price[]" value="" type="text"></div></div>';
		$('#editINvoiceDiv').append(html);
	});
	function edit_remove_desc(x){
		$('#removeDesc_'+x).remove();
		edit_all_input_value();
	}
	
	jQuery(document).on('change', '.edit_invoice_price', function () {
		edit_all_input_value();
	});
	
	function edit_all_input_value(){	
		var d=0;
		var subtotal=0;var tax_amount=0;
		var data=$('input[name="edit_invoice_price[]"]').serializeArray();
		for(var i=0; i<data.length;i++){
			var c=data[i].value;
			if(c!=""){
				d += parseInt(data[i].value);
			}		  
		}
		$('#edit_invoice_subTotal').val(d);

		var other=$('#edit_invoice_other').val();

		if(other==""){
			subtotal=parseInt(d);      
		}else{
			subtotal=parseInt(d)+parseInt(other);
		}
		 
		var tax=$('#edit_invoice_client_tax').val();
	 
		if(tax!=0){
			tax_amount=parseInt(subtotal)*parseInt(tax)/100;
		}	
		$('#edit_settax_price').val(tax_amount);

		$('#edit_invoice_Total').val(tax_amount+subtotal);
	}
	 
jQuery(document).on('click', '#update_invoice_details', function () {
	
	if (jQuery('form#update_invoice_generation').valid() === false) {
		return false;
	} else {
		
		if($('#edit_invoice_subTotal').val()==""){
			
			$('#edit_invoice_msg').text('Please fill all mandatory details*').css('color','red');
			setTimeout(function(){
				$('#edit_invoice_msg').text('');
			}, 3000);
			return false;
		}else{	
			var auto_rem=0;		
			if(document.getElementById("edit_auto_reminder").checked){
				auto_rem=1;
			} 
			 
			var invoice_price=$('input[name="edit_invoice_price[]"]').serializeArray();
			var invoiceArray=[];
			for(var i=0; i<invoice_price.length;i++){
				invoiceArray[i]= invoice_price[i].value;		  
			}
			
			var invoiceDescription=$('input[name="edit_invoice_Description[]"]').serializeArray();
			var descriptionArray=[];
			for(var i=0; i<invoiceDescription.length;i++){
				descriptionArray[i]= invoiceDescription[i].value;		  
			}
			var U_T_C=jQuery('#edit_invoice_UTC').val();
			var formData = new FormData(); 
			formData.append('invoice_utc',U_T_C); 
			formData.append('amount', JSON.stringify(invoiceArray));
			formData.append('description', JSON.stringify(descriptionArray));				
			formData.append('netamount', jQuery('#edit_invoice_subTotal').val());
			formData.append('othercharges', jQuery('#edit_invoice_other').val());
			formData.append('invoice_tax', jQuery('#edit_invoice_client_tax :selected').val());
			formData.append('totalamount', jQuery("#edit_invoice_Total").val());			
			formData.append('auto_reminder', auto_rem);			
		   
			jQuery.ajax({
				url: baseurl + 'Invoice/update_Invoice',
				type: 'post',
				data: formData,
				processData: false,
				contentType: false,
				dataType: 'json',
				beforeSend: function () {
					 jQuery('.rems').remove();
					 jQuery('#footerModalDiv').html('<img width="70" src="'+baseurl+'assets/images/ajax-loading.gif">');
				},
				complete: function () {
					 
					jQuery('#edit-invoice-details').find('input[type=text]').each(function () {
						jQuery(this).val('');
					});
					jQuery('#edit-invoice-details').modal('hide');
				},
				success: function (html) {						 
					if(html){
						$('.edit_success_msg').html('<div class="alert alert-success">Update Invoice Successfully.</div>');	 
						setTimeout(function(){
							location.reload(true);
						},3000);
					}else{
						$('.edit_success_msg').html('<div class="alert alert-success">Error! Please try again.</div>');	 
						setTimeout(function(){
							location.reload(true);
						},3000);
					}					 
			   },
				error: function (xhr, ajaxOptions, thrownError) {
					console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
				}
			});
		}
	}
});
	jQuery(document).on('click', '.delete_invoice_mailBTN', function () {
		 listDIv();
		$('#invoice_list_h1').text($(this).data('title'));	    
	    var id=$(this).data('utc');
		if(id!=0 || id!=""){
			jQuery.ajax({
				url: baseurl + 'Invoice/delete_invoice',
				type: 'post',
				data: {invoice_utc : id}, 
				dataType: 'html',				 
				success: function (html) {
					if(html==true){ 
						$('#invoice_listMSG').html('<h4 style="color:green">Delete Successfully!</h4>');
						setTimeout(function(){
							location.reload(true);
						},3000);
					}else{
						$('#invoice_listMSG').html('<h4 style="color:red">Error! Please try again.<h4>');
					}
					
				},
				error: function (xhr, ajaxOptions, thrownError) {
					console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
				}
			});
		}  
	}); */