<? global $vis_language; 
   $invoice_status = array( 0 => $vis_language['TXTUNPAID'], 1 => $vis_language['TXTPAID'], 2 => $vis_language['TXTCANCELED'], 3 => $vis_language['TXTINPROCESS'] );
   $payment_method = array( 0 => $vis_language['TXTOFFLINEPAYMENT'], 1 => $vis_language['TXTPAYPALEMAIL'], 2 => $vis_language['TXTAUTHORIZE']);
?>
<header class="header b-b b-light bg-w clearfix">
                 <section class="row">
                  <div class="col-md-12">
                  	<h3 class="m-b-xs text-black">#<? echo $invoice_info['invoice_hash']; ?></h3>
                  </div>
                  <!-- .breadcrumb -->
                  <ul class="breadcrumb bg-empty no-border m-b-xxs">
                    <li><a class="link" href="#/home/quick_view/<? echo $vis_language['TXTHOME']; ?>"><? echo $vis_language['TXTHOME']; ?></a></li>
					<li><a class="link" href="#/billing/billing_menu/"<? echo $vis_language['TXTBILLING'];?>><? echo $vis_language['TXTBILLING']; ?></a></li>
					<li><a class="link" href="#/billing/get_invoices/"<? echo $vis_language['TXTINVOICES'];?>><? echo $vis_language['TXTINVOICES']; ?></a></li>
                    <li class="active"><? echo $invoice_info['invoice_hash']; ?></li>
                  </ul>
                  <!-- / .breadcrumb -->
                  </section>
            </header>
            <section class="scrollable m-t-lg">
			<form class="form-horizontal" target="vis_hidden_frame" name="vis_invoice_form" id="vis_invoice_form_<? echo $invoice_info['invoice_id']; ?>" method="post" action="index.php">
                <div class="row">
                    <div class="wrapper">
					
                       <div class="col-sm-4">
                         <div class="inline m-l">
                           <div class="h4 m-t m-b-xs font-bold text-lt"><? echo $invoice_info['first_name']." ".$invoice_info['last_name']; ?></div>
                           <div class="text-muted"><a class="link" href="#/client/client_details/<? echo $invoice_info['first_name']." ".$invoice_info['last_name']; ?>/<? echo $invoice_info['client_id']; ?>"><? echo $invoice_info['email']; ?></a></div>
                           <p><strong><? echo $vis_language['TXTINVOICEDATE']; ?>:</strong><? echo format_datetime($invoice_info['entry_date']); ?></p>
                           <p class="form-inline"><strong><? echo $vis_language['TXTDUEDATE']; ?>: </strong><input  class="input-sm form-control" size="20"  type="text" placeholder="DD-MM-YYYY" id="vis_duedate" name="vis_duedate" value="<? echo format_datetime($invoice_info['due_date']);  ?>" >
						   </p>
                           <p><strong><? echo $vis_language['TXTTOTALDUE'].' ('.$domain['currency'].')'; ?>: </strong><? echo ($invoice_info['total']); ?></p>
                           <p><strong><? echo $vis_language['TXTBALANCE'].' ('.$domain['currency'].')'; ?>: </strong> <span class="<? if($invoice_info['balance'] > 0) echo "vh-danger font-bold"; else echo "vh-success font-bold" ?>"><? echo $invoice_info['balance']; ?></span></p>
                          </div>
                       </div> <!-- /.col-sm-4 -->
                       <div class="col-sm-8">
                         <div class="col-xs-8">
                           <div class="padder text-center">
							   <? if($invoice_info['invoice_status'] == 1): ?>
									<h3 class="vh-success font-bold"><? echo $invoice_status[$invoice_info['invoice_status']]; ?></h3>
							   <? elseif($invoice_info['invoice_status'] == 0): ?>
									<h3 class="vh-danger font-bold"><? echo $invoice_status[$invoice_info['invoice_status']]; ?></h3>
                               <? else: ?>
									<h3 class="font-bold"><? echo $invoice_status[$invoice_info['invoice_status']]; ?></h3>
                               <? endif; ?>
                               <? if($invoice_info['invoice_status'] == 1): ?>
									<p><span class="text-muted"><? echo $vis_language['TXTPAIDDATE']; ?>: </span><? echo format_datetime($invoice_info['paid_date']); ?></p>
							   <? else: ?>
									<p><span class="text-muted"><? echo $vis_language['TXTDUEDATE']; ?>: </span><? echo format_datetime($invoice_info['due_date']); ?></p>
							   <? endif; ?>
                               <div class="btn-group" id="invoice_email_template">
                                  <select class="form-control input-sm selectpicker" name="email_template" id="email_template">
							      <option value="0"><? echo $vis_language['TXTSELECTMAILTEMPLATE']; ?></option>
							      <? for($i=0;$i<count($etemplates);$i++)
							      echo '<option value="'.$etemplates[$i]['etemplate_id'].'"'.($template_map[0]==$etemplates[$i]['etemplate_id']?' selected="selected"':'').'>'.$etemplates[$i]['etemplate_name'].'</option>'; ?>
                                  </select>
                               </div>
                               <div class="btn-group">
                                 <button type="submit" class="btn btn-sm btn-block vh-success-bg" onclick="send_invoice_mail()"><? echo $vis_language['TXTSENDEMAIL']; ?></button>
                               </div>
                           </div>
                         </div> <!--/ .col-xs-8 -->
                         <div class="col-xs-4">
                            <div class="pull-right padder-v">
								<button type="button" class="btn btn-sm btn-block btn-default" data-toggle="ajaxModal" data-modal-arguments="vis_module=billing&vis_operation=get_add_payment&vis_static_area=vis_invoice_form&vis_client_id=<? echo $invoice_info['client_id']; ?>&vis_invoice_id=<? echo $invoice_info['invoice_id']; ?>" ><?= $vis_language['TXTADDINVOICEPAYMENT']; ?></button>
							<?  if($invoice_info['invoice_status']==0 || $invoice_info['invoice_status']==3) {?>
								<a href="index.php?vis_module=billing&vis_operation=export_invoice&vis_client_id=<? echo $invoice_info['client_id']; ?>&vis_invoice_id=<? echo $invoice_info['invoice_id']; ?>" class="btn btn-sm btn-block btn-default" target="_blank"><? echo $vis_language['TXTPDF']; ?></a>
								<button type="button" class="btn btn-sm btn-block btn-default" id="apply_credit" onclick="applyCredit(<? echo $client['credit']; ?>,<? echo $invoice_info['client_id']; ?>,<? echo $invoice_info['invoice_id']; ?>)"><?= $vis_language['TXTAPPLYCREDIT']; ?></button>
								<button type="button" class="btn btn-sm btn-block btn-default" onclick="$(this).visExecution('load_operations',{'vis_module':'billing', 'vis_operation':'update_invoice_status', 'vis_client_id':<? echo $invoice_info['client_id'];?>, 'vis_invoice_id':<? echo $invoice_info['invoice_id'];?>, 'vis_update_id':2})"><?= $vis_language['TXTMARKCANCEL']; ?></button>
							<? } elseif($invoice_info['invoice_status']==1) {?>
								<a href="index.php?vis_module=billing&vis_operation=export_invoice&vis_client_id=<? echo $invoice_info['client_id']; ?>&vis_invoice_id=<? echo $invoice_info['invoice_id']; ?>" class="btn btn-sm btn-block btn-default" target="_blank"><? echo $vis_language['TXTPDF']; ?></a>
								<button type="button" class="btn btn-sm btn-block btn-default" onclick="$(this).visExecution('load_operations',{'vis_module':'billing','vis_operation':'update_invoice_status', 'vis_client_id':<? echo $invoice_info['client_id'];?>, 'vis_invoice_id':<? echo $invoice_info['invoice_id'];?>, 'vis_update_id':0})"><?= $vis_language['TXTMARKUNPAID']; ?></button>
								<button type="button" class="btn btn-sm btn-block btn-default" onclick="$(this).visExecution('load_operations',{'vis_module':'billing', 'vis_operation':'update_invoice_status', 'vis_client_id':<? echo $invoice_info['client_id'];?>, 'vis_invoice_id':<? echo $invoice_info['invoice_id'];?>, 'vis_update_id':2})"><?= $vis_language['TXTMARKCANCEL']; ?></button>
							<? } elseif($invoice_info['invoice_status']==2) {?>
								<a href="index.php?vis_module=billing&vis_operation=export_invoice&vis_client_id=<? echo $invoice_info['client_id']; ?>&vis_invoice_id=<? echo $invoice_info['invoice_id']; ?>" class="btn btn-sm btn-block btn-default" target="_blank"><? echo $vis_language['TXTPDF']; ?></a>
								<button type="button" class="btn btn-sm btn-block btn-default" onclick="$(this).visExecution('load_operations',{'vis_module':'billing','vis_operation':'update_invoice_status', 'vis_client_id':<? echo $invoice_info['client_id'];?>, 'vis_invoice_id':<? echo $invoice_info['invoice_id'];?>, 'vis_update_id':0})"><?= $vis_language['TXTMARKUNPAID']; ?></button>
							<? } ?>
                            </div>
                         </div> <!--/ .col-xs-4 -->
                       </div> <!-- /.col-sm-8 -->
                    </div> <!--/ .wrapper -->
                </div> <!-- /.row -->

            	<div class="row form-group">
                  <div class="wrapper">
                	<div class="col-md-12">
                          <section class="panel m-b-n">
                            <header class="panel-heading">
                              <h5 class="font-bold"><? echo $vis_language['TXTINVOICEITEMS'];?></h5>
                            </header>
                            <div class="panel-body">
                              <div class="row form-group">
                                <div class="form-group m-b-none">
									<div class="col-sm-4">
									<label class="control-label m-l-sm font-bold"><? echo $vis_language['TXTDESCRIPTIONS']; ?></label></div>
									<div class="col-sm-2">
									<label class="control-label m-l-sm font-bold"><? echo $vis_language['TXTAMOUNT'].' ('.$domain['currency'].')'; ?></label></div>
									<div class="col-sm-5 text-center">
									<label class="control-label m-l-sm font-bold"><? echo $vis_language['TXTTAXED']; ?></label></div>
									<div class="col-sm-1">
									<label class="control-label m-l-sm font-bold"></label></div>
								
								</div>
									
							    </div>
										<div id="vis_new_invoice_form_area_<?= $invoice_info['invoice_id']; ?>">
										  <input type="hidden" name="vis_ticket_id" value="<? echo $invoice_info['ticket_id']; ?>" />
										  <input type="hidden" name="vis_client_id" value="<? echo $invoice_info['client_id']; ?>" />
										  <input type="hidden" name="vis_desc_count" id="vis_desc_count_<? echo $invoice_info['invoice_id']; ?>" value="1" />
										  <input type="hidden" name="vis_desc_tot_count" id="vis_desc_tot_count_<? echo $invoice_info['invoice_id']; ?>" value="1" />
										  <input type="hidden" name="vis_tax_count" id="vis_tax_count" value="<? echo count($tax_list); ?>" />
										  <input type="hidden" name="vis_status" value="<? echo $invoice_info['invoice_status']; ?>" /><?
											 if(isset($invoice_info['invoice_id']) && $invoice_info['invoice_id']!=0) echo '<input type="hidden" name="vis_invoice_id" value="'.$invoice_info['invoice_id'].'" />'; ?>
										</div>
                                    <div>
                                      <div class="row form-group">
                                        <div class="col-sm-11"></div>
                                        <div class="col-sm-1 text-right"><a href="javascript:void(0)" type="button" class="btn vh-success-bg btn-xs" onclick="add_invoice_area('', 0, '')"><? echo $vis_language['TXTADDITEM'];?></a></div>
                                      </div>
                                      <div class="row form-group">
                                        <div class="col-sm-10 text-right" colspan="2"><strong><? echo $vis_language['TXTSUBTOTAL'];?></strong></div>
                                        <div class="col-sm-1 text-center"><? echo $invoice_info['subtotal']; ?></div>
										<div>&nbsp;</div>
                                      </div>
                                      <div class="row form-group">
                                        <div class="col-sm-10 text-right no-border" colspan="2"><strong><? echo ($tax_details['compound_tax']==1)?'Compounded Tax ':' Simple tax '; $vis_language['TXTTAXX'];?></strong></div>
                                        <div class="col-sm-1 text-center"><? echo $invoice_info['tax']; ?></div>
										<div>&nbsp;</div>
                                      </div>
                                      <div class="row form-group">
                                        <div class="col-sm-10 text-right no-border" colspan="2"><strong><? echo $vis_language['TXTCREDIT'];?></strong></div>
                                        <div class="col-sm-1 text-center"><? echo $invoice_info['invoiceCredit']; ?></div>
                                        <div><? if($invoice_info['invoiceCredit'] > 0) {?><button type="button" class="btn vh-success-bg btn-xs" data-toggle="ajaxModal" data-modal-arguments="vis_module=billing&vis_operation=get_remove_credit&vis_static_area=vis_domain_delete_form&vis_client_id=<? echo $invoice_info['client_id']; ?>&vis_invoice_id=<? echo $invoice_info['invoice_id']; ?>" ><?= $vis_language['TXTREMOVECREDIT']; ?></button><? } ?></div>
                                      </div>
                                       <div class="row form-group">
                                        <div class="col-sm-10 text-right no-border" colspan="2"><strong><? echo $vis_language['TXTTOTALDUE'].' ('.$domain['currency'].')';?></strong></div>
                                        <div class="col-sm-1 text-center"><? echo ($invoice_info['total']-$invoice_info['invoiceCredit']); ?></div>
										<div>&nbsp;</div>
                                      </div>
                                    </div>
                                 </div>
                                  <footer class="panel-footer text-right bg-light lter">
                                <div class="form-group m-t-xs">
                                  <button type="submit" class="btn btn-default" onclick="$('#vis_workpanel_div').visExecution('load_module','invoices/<?echo $invoice_info['invoice_id']?>',{'vis_module':'billing','vis_operation':'edit_invoice', 'vis_invoice_id':<?echo $invoice_info['invoice_id']?>});"><? echo $vis_language['TXTCANCEL'];?></button>
                                  <button type="submit" class="btn btn-primary" onclick="$(this).visExecution('submit_form',{'vis_module':'billing', 'vis_operation':'update_invoice', 'vis_form':'vis_invoice_form_<? echo $invoice_info['invoice_id']; ?>'});"><? echo $vis_language['TXTUPDATE'];?></button>
                                </div>
                            </footer>
                                </div>
                              </div>
                            </div>
                          </section>
						 <? if(is_array($transaction_info)): ?>
                          <section class="panel b-t">
                            <header class="panel-heading">
                              <h5 class="font-bold"><? echo $vis_language['TXTTRANSACTIONS'];?></h5>
                            </header>
                            <div class="panel-body">
                              <div class="row">
                                <div class="table-responsive">
                                  <table class="table m-t-n m-b-n b-t b-light">
                                    <thead>
                                      <tr>
                                        <th><? echo $vis_language['TXTTRANSACTIONID'];?></th>
                                        <th><? echo $vis_language['TXTDESCRIPTION'];?></th>
                                        <th><? echo $vis_language['TXTTRANSACTIONDATE'];?></th>
                                        <th><? echo $vis_language['TXTPAYMENTMETHOD'];?></th>
                                        <th><? echo $vis_language['TXTAMOUNT'].' ('.$domain['currency'].')';;?></th>
                                        <th width="20"></th>
                                      </tr>
                                    </thead>
                                    <tbody>
									<? foreach($transaction_info as $transactionItem): ?>
                                      <tr>
                                        <td>#<? echo $transactionItem['transaction_id']; ?></td>
                                        <td><? echo $transactionItem['content']; ?></td>
                                        <td><? echo format_datetime($transactionItem['entry_date']); ?></td>
                                        <td><? echo $payment_method[$transactionItem['gateway_id']]; ?></td>
                                        <td><? echo $transactionItem['amount']; ?></td>
                                        <td>
											<button data-dismiss="modal" class="close" type="button" title="<? echo $vis_language['TXTDELTRANSACTION']; ?>" data-toggle="ajaxModal" data-modal-arguments="vis_module=billing&vis_operation=get_delete_transaction&vis_transaction_id=<? echo $transactionItem['balance_id'];?>&vis_static_area=vis_invoice_form&vis_client_id=<? echo $invoice_info['client_id'];?>&vis_invoice_id=<? echo $invoice_info['invoice_id'];?>" >×</button>
<!--
											<a title="<? echo $vis_language['TXTDELTRANSACTION']?>"  href="javascript:void(0);"  data-toggle="ajaxModal" data-modal-arguments="vis_module=billing&vis_operation=get_delete_transaction&vis_transaction_id=<? echo $transactionItem['balance_id'];?>&vis_static_area=vis_invoice_form&vis_client_id=<? echo $invoice_info['client_id'];?>&vis_invoice_id=<? echo $invoice_info['invoice_id'];?>" title="<? echo $vis_language['TXTDELETE']; ?>"><i class="vh vh-cross vh-danger"></i></a>
-->
											</td>
                                      </tr>
                                    <? endforeach; ?>
                                    </tbody>
                                  </table>
                                  </form>
                                </div>
                              </div>
                            </div>
                          </section>
						 <? endif; ?>
                    </div>
                  </div>
                </div> <!-- /.row -->
			 </form>
            </section>
<script type="text/javascript">
 $('#vis_duedate').datetimepicker({
        format: "<?php echo VIS_DATETIMEPICKER_FORMAT;?>"
	});	
	$(".selectpicker").selectpicker();
	function send_invoice_mail()
	{
		var template = $('#email_template').val();
		$('#vis_global_msg_div').visExecution('load_operations',{'vis_module':'billing','vis_operation':'send_mail_for_invoice', 'vis_invoice_id':<?echo $invoice_info['invoice_id']?>,'vis_email_template': template});
	}
	function add_invoice_area(amount, tax, content)
	{
		var n=parseInt($("#vis_desc_count_<? echo $invoice_info['invoice_id']; ?>").val());
		var temp = '<div class="row form-group" id="vis_invoice'+n+'"><div class="col-sm-4"><textarea class="form-control" rows="1" data-minwords="6" data-required="true" name="vis_invoice'+n+'"></textarea></div><div class="col-sm-2"><input type="text" class="form-control" name="vis_amount'+n+'"></div><div class="checkbox i-checks  text-center col-sm-5"><? for($i=0;$i<count($tax_list);$i++){?><label class="checkbox-inline m-r-sm"><input name="vis_taxed<?= $i;?>_'+n+'" id="vis_taxed<?= $i;?>_'+n+'" value="1" type="checkbox" checked="checked" /><i></i><? echo $tax_list[$i]['tax_name']; ?></label><input name="vis_tax_val<?= $i;?>_'+n+'" id="vis_tax_val<?= $i;?>_'+n+'" value="<? echo $tax_list[$i]['tax_id']; ?>" type="hidden"/><? } ?><input name="vis_tax_enabled" id="vis_tax_enabled" value="<? echo $tax_details['tax_enabled']; ?>" type="hidden"/></div><div class="col-sm-1"><button data-dismiss="modal" class="close" type="button" title="<? echo $vis_language['TXTDELINVOICEITEM']; ?>" onclick="close_invoice('+n+')" >×</button></div></div>';
		$("#vis_new_invoice_form_area_<? echo $invoice_info['invoice_id']; ?>").append(temp);
        $("#vis_desc_count_<? echo $invoice_info['invoice_id']; ?>").val(n+1);
        $("#vis_desc_tot_count_<? echo $invoice_info['invoice_id']; ?>").val(n+1);
	}
	   function add_new_invoice_area(amount, content,transactionId, tax, seltax) {
		var n=parseInt($("#vis_desc_count_<? echo $invoice_info['invoice_id']; ?>").val());
		//var data = json_decode(seltax);
		var data = JSON.parse("[" + seltax + "]");
		var temp = '<div class="row form-group"><div class="col-sm-4"><textarea class="form-control" rows="1" data-minwords="6" data-required="true" name="vis_invoice'+n+'">'+(content.replace(/<br>/g, "\n"))+'</textarea></div><div class="col-sm-2"><input type="text" class="form-control" name="vis_amount'+n+'" value='+amount+'></div><div class="checkbox i-checks  text-center col-sm-5"><?if($tax_list!=-1)for($i=0;$i<count($tax_list);$i++){?><label class="checkbox-inline m-r-sm"><input name="vis_taxed<?= $i;?>_'+n+'" id="vis_taxed<?= $i;?>_'+n+'" value="1" type="checkbox"';
			 if(data.includes(<?php echo $tax_list[$i]['tax_id']; ?>)==true || tax==1) var test="checked='checked'"; else var test="";
			 temp +=''+test+'/><i></i><? echo $tax_list[$i]['tax_name']; ?></label><input name="vis_tax_val<?= $i;?>_'+n+'" id="vis_tax_val<?= $i;?>_'+n+'" value="<? echo $tax_list[$i]['tax_id']; ?>" type="hidden"/><?  }?></div><input name="vis_tax_enabled" id="vis_tax_enabled" value="<? echo (isset($tax_details['tax_enabled']))?$tax_details['tax_enabled']:1; ?>" type="hidden"/><div class="col-sm-1"><button data-dismiss="modal" class="close" type="button" title="<? echo $vis_language['TXTDELINVOICEITEM']; ?>" data-toggle="ajaxModal" data-modal-arguments="vis_module=billing&vis_operation=get_delete_invoice_item&vis_transaction_id='+transactionId+'&vis_invoice_id=<? echo $invoice_info['invoice_id']; ?>&vis_client_id=<? echo $invoice_info['client_id'];?>&vis_static_area=vis_invoice_form" >×</button></div></div>';
		$("#vis_new_invoice_form_area_<? echo $invoice_info['invoice_id']; ?>").append(temp);
        $("#vis_desc_count_<? echo $invoice_info['invoice_id']; ?>").val(n+1);
        $("#vis_desc_tot_count_<? echo $invoice_info['invoice_id']; ?>").val(n+1);
		} <?
      for($i=0;$i<$invoice_info['count'];$i++) {  ?>
       add_new_invoice_area("<? echo $invoice_details[$i]['amount']; ?>", "<? echo str_replace(array("\n", "\r"), array('<br>', ''), addslashes($invoice_details[$i]['description'])); ?>","<? echo $invoice_details[$i]['invoice_detail_id']; ?>","<? echo $invoice_details[$i]['tax']; ?>",<? echo json_encode($invoice_details[$i]['selected_tax']); ?>); <?
      }
      ?>
   function close_invoice(n)
   {
	   $('#vis_invoice'+n).remove();
	   if($("#vis_desc_tot_count_<? echo $invoice_info['invoice_id']; ?>").val()>1)
	   {
	   num=$("#vis_desc_tot_count_<? echo $invoice_info['invoice_id']; ?>").val()-1;
	   $("#vis_desc_tot_count_<? echo $invoice_info['invoice_id']; ?>").val(num);
	   }
   }
   function applyCredit(clientCredit,clientId,invoiceId)
   {
	    if(clientCredit > 0)
	    {
			$('#apply_credit').attr('data-toggle','ajaxModal');
			$('#apply_credit').attr('data-modal-arguments','vis_module=billing&vis_operation=add_balance&vis_static_area=vis_invoice_form&vis_client_id='+clientId+'&vis_invoice_id='+invoiceId);
		}
		else
		{
			$("#vis_global_msg_div").visInterface("load_global_message", "<?php echo $vis_language['MSGNOTSUFFICIENTCREDIT'] ?>");
		}
   }
  
</script>
