<? global $vis_language; ?>
<div class="modal-dialog">
   <div class="modal-content">
      <div class="modal-header">
         <button type="button" class="close" data-dismiss="modal">&times;</button>
         <h4 class="modal-title"><? echo$vis_language['TXTADDINVOICE']; ?></h4>
      </div>
      <div class="modal-body fixedheight scrollable">
         <div class="row">
            <div class="col-sm-12">
               <form role="form" class="form-horizontal" name="vis_invoice_form" id="vis_invoice_form" target="vis_hidden_frame">
				  <?php if($client['credit']> 0) {?>
                  <div class="form-group">
                     <label class="col-sm-3 control-label txt_nowrap"><? echo $vis_language['TXTCREDIT'].' ('.$currency.')'; ?></label>
                     <div class="col-sm-2"><label class="control-label"> <? echo $client['credit']; ?></label></div>
                     <div class="col-sm-6 checkbox i-checks m-r-lg pull-left p-d" ><label><input type="checkbox"  value="1" id="vis_apply_credit" name="vis_apply_credit"><i></i><?php echo $vis_language['TXTAPPLYAMOUNTCREDIT']; ?></label></div>
                  </div>
                  <?php } if($time_track_details!=-1){?>
                    <div class="form-group">
                  <div class="col-sm-3"><label class="control-label"> <? echo "Include time tracking"; ?></label></div>
                  <div class="col-sm-8 checkbox i-checks m-r-lg pull-right p-d"><label><input type="checkbox"  value="1" id="vis_apply_time_track" name="vis_apply_time_track" onclick="include_timetrack()"><i></i></label></div>
                  </div><? } ?>
                  <div id="vis_new_invoice_form_area"></div> 
                  <div style="text-align:right;"><a class="link" href="javascript:void(0);" onclick="add_new_invoice_area('', 0, '')"><i style="font-size:10px;" class="vh vh-plus"></i><?= $vis_language['TXTADDITEM']; ?></a></div>
                  <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_parent_id" value="<? echo $client['parent_id']; ?>" />
                  <input type="hidden" name="vis_desc_count" id="vis_desc_count" value="<? echo count($invoice_info['count']); ?>" />
                  <input type="hidden" name="vis_desc_tot_count" id="vis_desc_tot_count" value="<? echo count($invoice_info['count'])-1; ?>" />
                  <input type="hidden" name="vis_tax_count" id="vis_tax_count" value="<? echo count($tax_list); ?>" />
                  <?
                     if(isset($invoice_info['invoice_id']) && $invoice_info['invoice_id']!=0) echo '<input type="hidden" name="vis_invoice_id" value="'.$invoice_info['invoice_id'].'" />'; ?> 
               </form>
            </div>
         </div>
      </div>
      <div class="modal-footer">
           <a class="btn btn-default" onclick="reset_timer_<?= $invoice_info['ticket_id'];?>();" data-dismiss="modal"><? echo $vis_language['TXTCANCEL']; ?></a>
           <a class="btn btn-primary" onclick="reset_timer_<?= $invoice_info['ticket_id'];?>();" id="btn_add_invoice"><? echo $vis_language['TXTADD']; ?></a>
        </div>
   </div>
</div>
<script>
	$(".selectpicker").selectpicker();
   function add_new_invoice_area(amount, tax, content) {
        var n=parseInt($("#vis_desc_count").val());
        if(!(!isNaN(parseFloat(n)) && isFinite(n))) return false;
        var temp='<div id="vis_invoice_subitem_'+n+'"><div class="form-group"><label class="col-sm-3 control-label"><? echo $vis_language['TXTAMOUNT'].' ('.$currency.')'; ?></label><div class="col-sm-8"><input type="text" class="form-control input-sm" name="vis_amount'+n+'" id="vis_amount'+n+'" value="'+amount+'" /></div></div><?  if( $tax_details['tax_enabled']==1){if($tax_list!=-1) {?><div class="form-group"><label class="col-sm-3 control-label"></label><div class="col-sm-8"><? for($i=0;$i<count($tax_list);$i++){?> <div class="checkbox i-checks m-r-lg pull-left p-d-sm"><label><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"/></div><?}?><input name="vis_tax_enabled" id="vis_tax_enabled" value="<? echo $tax_details['tax_enabled']; ?>" type="hidden"/></div><?}}?>';
        if(n>=1) temp+='<a href="javascript:void(0)" onclick="javascript:close_invoice('+n+');"><i class="vh vh-cross text-muted"></i></a>';
        else temp+='</div>';
        temp+='<div class="form-group"><label class="col-sm-3 control-label"><? echo $vis_language['TXTDESCRIPTION']; ?></label><div class="col-sm-8"><textarea class="form-control input-sm" name="vis_invoice'+n+'" id="vis_invoice'+n+'" >'+content+'</textarea></div></div></div>';
        //temp+='</td></tr><tr><td><?= $vis_language['TXTDESCRIPTION']; ?></td><td colspan="2"><textarea name="vis_invoice'+n+'" id="vis_invoice'+n+'" class="vis_input" >'+(content.replace(/<br>/g, "\n"))+'</textarea></td></tr><tr><td colspan="3"><div class="vis_seperator_horizontal"></div></td></tr></table>';
        $("#vis_new_invoice_form_area").append(temp);
        $("#vis_desc_count").val(n+1);
         $("#vis_desc_tot_count_<? echo $invoice_info['invoice_id']; ?>").val(n+1);
    } 
    var content="<?php echo $invoice_details[0]['description']; ?>";
	add_new_invoice_area("<?php echo $invoice_details[0]['amount']; ?>", "0", content); 
	function include_timetrack(){
			var checkbox = document.getElementById("vis_apply_time_track");
			if(checkbox.checked){
			document.getElementById("vis_new_invoice_form_area").innerHTML = "";
		<?for($i=0;$i<count($time_track_details);$i++) {?>
			var n=parseInt($("#vis_desc_count").val());
			var amount="<? echo $time_track_details[$i]['amount'];?>";
			var content="<? echo str_replace(array("\n", "\r"), array('<br>', ''), addslashes($time_track_details[$i]['description'])); ?>";
			var temp = '<div id="vis_invoice_subitem_'+n+'"><div class="form-group"><label class="col-sm-3 control-label"><? echo $vis_language['TXTAMOUNT'].' ('.$currency.')'; ?></label><div class="col-sm-8"><input type="text" class="form-control" name="vis_amount'+n+'" value='+amount+'></div></div><? if( $tax_details['tax_enabled']==1){ if($tax_list!=-1) {?><div class="form-group"><label class="col-sm-3 control-label"></label><div class="col-sm-8"><? for($i=0;$i<count($tax_list);$i++){?> <div class="checkbox i-checks m-r-lg pull-left p-d-sm"><label><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></div><?}?><input name="vis_tax_enabled" id="vis_tax_enabled" value="<? echo $tax_details['tax_enabled']; ?>" type="hidden"/></div><?} } if($i>0){?><a href="javascript:void(0)" onclick="javascript:close_invoice('+n+');"><i class="vh vh-cross text-muted"></i></a><? } ?></div>';
			temp+='<div class="form-group"><label class="col-sm-3 control-label"><? echo $vis_language['TXTDESCRIPTION']; ?></label><div class="col-sm-8"><textarea class="form-control input-sm" name="vis_invoice'+n+'" id="vis_invoice'+n+'" >'+content+'</textarea></div></div></div>';
			$("#vis_new_invoice_form_area").append(temp);
			$("#vis_desc_count").val(n+1);
            $("#vis_desc_tot_count_<? echo $invoice_info['invoice_id']; ?>").val(n+1);
    <?  }?>
	}
	else {
			document.getElementById("vis_new_invoice_form_area").innerHTML = "";
			add_new_invoice_area("<?php echo $invoice_details[0]['amount']; ?>", "0",""); 
		 }
	}
    function close_invoice(n)
    {
   $('#vis_invoice_subitem_'+n).remove();
   if($("#vis_desc_tot_count").val()>1)
   {
   num=$("#vis_desc_tot_count").val()-1;
   $("#vis_desc_tot_count").val(num);
   }
   }
	$("#btn_add_invoice").bind("click", function(){
		var str="";
		if($("#ticket_details").length) str = "ticket_details";
		$(this).visExecution('submit_form',{'vis_module':'billing', 'vis_operation':'<? echo $invoice_info['vis_opeartion']; ?>', 'vis_form':'vis_invoice_form' ,'vis_client_id':'<? echo $invoice_info['client_id'];?>', 'vis_org_operation': str});
	});
</script>
