<?php $this->load->view("common/header"); ?>
<!-- end: HEAD -->
<?php 
$tmp_id=0;
$tmp_th_e=0;
$tmp_sr_id=0;
$vcl_srvs_inv_header=0;
$service_sub_tot=0;
$parts_sub_tot=0;
$technician_sub_tot=0;
$grand_total_sub_tot=0;
$loan_title='';
//$loan_id=8;
?>

<!-- end: HEAD -->
<!-- start: BODY -->
<body>
<!-- start: HEADER -->
<div class="navbar navbar-inverse navbar-fixed-top"> 
  <!-- start: TOP NAVIGATION CONTAINER -->
  <div class="container">
    <div class="navbar-header"> 
      <!-- start: RESPONSIVE MENU TOGGLER -->
      <button data-target=".navbar-collapse" data-toggle="collapse" class="navbar-toggle" type="button"> <span class="clip-list-2"></span> </button>
      <!-- end: RESPONSIVE MENU TOGGLER --> 
      <!-- start: LOGO -->
      <?php $this->load->view("common/logo"); ?>
      <!-- end: LOGO --> 
    </div>
    <div class="navbar-tools"> 
      <!-- start: TOP NAVIGATION MENU -->
      <?php $this->load->view("common/notifications.php"); ?>
      <!-- end: TOP NAVIGATION MENU --> 
    </div>
  </div>
  <!-- end: TOP NAVIGATION CONTAINER --> 
</div>
<!-- end: HEADER --> 
<!-- start: MAIN CONTAINER -->
<div class="main-container">
<div class="navbar-content"> 
  <!-- start: SIDEBAR -->
  <?php $this->load->view("common/navigation"); ?>
  <!-- end: SIDEBAR --> 
</div>
<!-- start: PAGE -->
<div class="main-content"> 
  <!-- start: PANEL CONFIGURATION MODAL FORM -->
  <div class="modal fade" id="panel-config" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true"> &times; </button>
          <h4 class="modal-title">Panel Configuration</h4>
        </div>
        <div class="modal-body"> Here will be a configuration form </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal"> Close </button>
          <button type="button" class="btn btn-primary"> Save changes </button>
        </div>
      </div>
      <!-- /.modal-content --> 
    </div>
    <!-- /.modal-dialog --> 
  </div>
  <!-- /.modal --> 
  <!-- end: SPANEL CONFIGURATION MODAL FORM -->
  <div class="container"> 
    <!-- start: PAGE HEADER -->
    <div class="row">
      <div class="col-sm-12"> 
        <!-- start: PAGE TITLE & BREADCRUMB -->
        <ol class="breadcrumb">
          <li> <a href="<?php echo base_url('dashboard'); ?>"> Dashboard </a> </li>
          <li> <a href="<?php echo base_url('loan'); ?>"> Loans </a> </li>
          <li class="active">Mofify Loan></li>
          <li class="search-box">
            <form class="sidebar-search">
              <div class="form-group">
                <input type="text" placeholder="Start Searching...">
                <button class="submit"> <i class="clip-search-3"></i> </button>
              </div>
            </form>
          </li>
        </ol>
     
        
        <!-- end: PAGE TITLE & BREADCRUMB --> 
      </div>
    </div>
    <!-- start grid --> 
    
    <!-- sart tab -->
    <?php $config = array('role' =>'form', 'class'=>'form-horizontal','id'=>'loan_item_save', 'name'=>'loan_item_save');
	echo form_open_multipart("#",$config);?>
    <!-- start: INLINE TABS PANEL -->
    <div class="panel panel-default">
      <div class="panel-heading"> <i class="fa fa-reorder"></i>
        <div> Loan Information </div>
        <div class="panel-tools"> <a class="btn btn-xs btn-link panel-collapse collapses" href="#"> </a> <a class="btn btn-xs btn-link panel-refresh" href="#"> <i class="fa fa-refresh"></i> </a> <a class="btn btn-xs btn-link panel-expand" href="#"> <i class="fa fa-resize-full"></i> </a> <a class="btn btn-xs btn-link panel-close" href="#"> <i class="fa fa-times"></i> </a> </div>
      </div>
      <div class="panel-body">
        <div class="clearfix"> </div>
        <!--start header details details-->
        <div class="col-md-12">
          <div class="panel panel-default">
            
            <div class="panel-heading" style="font-weight: 500;"> Schedule of payments </div>
            <div class="panel-body">
              
              <div class="col-md-12">
              
               <div class="col-md-2">                 
                    <label>Loan Date *</label>
                    <?php $nowdate=date("Y-m-d H:i:s");?>
                    <div class="input-group">
                      <input id="loan_date" type="text" autocomplete="off" data-date-format="dd-mm-yyyy" data-date-viewmode="years" class="form-control date-picker" name="loan_date" value="<?php if(isset($hdr_data->loan_date)) echo date('d-m-Y', strtotime($hdr_data->loan_date));?>">
                      <span class="input-group-addon"> <i class="fa fa-calendar"></i> </span>                      
                  </div>
                </div>
                
                
                <div class="col-md-2">
                  <div class="form-group">
                    <label for="loan_payment_term">Payment Term * </label>
                    <select  id="loan_payment_term" class="form-control search-select" name="loan_payment_term" >
                      <?php 						
						foreach ($payment_term_list as $row)
							{                            
                             $sel='';
							if($hdr_data->loan_payment_term==$row['id'])
							{
							$sel='selected="selected"';
							}
							else
							{
								$sel='';
							}
							?>
                      <option <?php echo $sel;?>  value="<?php echo $row['id'];?>"><?php echo $row['payment_term_name'];?></option>
                      <?php }  ?>
                    </select>
                   </div>
                </div>
              
                <div class="col-sm-3">
                  <div class="form-group">
                    <label>Route  * </label>
                    <select id="route_id" class="form-control search-select" name="route_id" >
                      <option value="">- select route - </option>
                      <?php foreach ($routes_list as $row)
					{
						$sel='';
					if($hdr_data->route_id==$row['route_id'])
					{
					$sel='selected="selected"';
					}?>
                      <option <?php echo $sel; ?>  value="<?php echo $row['route_id']; ?>"> <?php echo $row['route_name']; ?> </option>
                      <?php }?>
                    </select>
                  </div>
                </div>
                
                <div class="col-md-4">
                    <div class="form-group">
                      <label for="cus_id">Customer * </label>
                      <select  id="cus_id" class="form-control search-select" name="cus_id" onChange="load_customer_old_recod(this.value)">
                        <option value="">-Select Customer-</option>
                        <?php	foreach ($customer_list as $row)
						{ 
						$sel='';
						if($hdr_data->cus_id==$row['cus_id'])
						{
						$sel=' selected="selected"';
						}else
						{
						$sel='';
						}?>
                        <option <?php echo $sel;?> value="<?php echo $row['cus_id'];?>"><?php echo $row['cus_name']; if($row['cus_nic']) echo " / ".$row['cus_nic'];?> </option>
                        <?php }  ?>
                      </select>
                      <a id="modal_ajax_customers_btn" style="cursor:pointer;"><i style="float: left;position: absolute;margin-left: 7px;margin-top: 5px;" class="fa fa-2x fa-plus-circle addIcon" id="addIcon"></i></a>
                  </div>
                </div>
                <div class="col-md-2">
                  <div class="form-group">
                    <label for="loan_interest">Interest (%) * </label>                   
                    <input style="text-align:right;" type="number" id="loan_interest" name="loan_interest" class="form-control" value="<?php if(isset($hdr_data->loan_interest)) echo $hdr_data->loan_interest;?>" data-d-group="2" onChange="calculate_payble_abount()">                   
                  </div>
                </div>
                
                <div class="col-md-2">
                  <div class="form-group">
                    <label for="bkng_head_count">No. of Term * </label>                  
                    <input style="text-align:right;" type="number" id="loan_no_of_terms" name="loan_no_of_terms" class="form-control" value="<?php if(isset($hdr_data->loan_no_of_terms)) echo $hdr_data->loan_no_of_terms;?>" data-d-group="2" onChange="calculate_payble_abount()">               
                  </div>
                </div>
                
                <div class="col-md-2" style="display:none">
                  <div class="form-group">
                    <label for="loan_interest_type">Interest Type </label>
                    <select  id="loan_interest_type" class="form-control search-select" name="loan_interest_type" >
                      <option value="1">Standard Type</option>
                    </select>
                  </div>
                </div>
                
                <div class="col-md-4">                 
                    <div class="form-group">
                      <label for="g_cus_id">Garenter </label>
                      <select  id="g_cus_id" class="form-control search-select" name="g_cus_id">
                        <option value="">-Select Garenter-</option>
                        <?php	foreach ($customer_list as $row)
						{
							$sel='';
						if($hdr_data->g_cus_id==$row['cus_id'])
						{
						$sel=' selected="selected"';
						}?>
                        <option <?php echo $sel;?> value="<?php echo $row['cus_id'];?>"><?php echo $row['cus_name']; if($row['cus_phone']) echo " / ".$row['cus_phone'];?> </option>
                        <?php }  ?>
                      </select>
                      <a id="modal_ajax_customers_btn_garanter" style="cursor:pointer;"><i style="float: left;position: absolute;margin-left: 7px;margin-top: 5px;" class="fa fa-2x fa-plus-circle addIcon" id="addIcon"></i></a> </div>
                  </div>
                </div>
            
              
              <div class="col-md-3">
                <div class="col-md-12" >
                  <div class="form-group">
                    <label for="menu_id">Total Amount * </label>
                    <input style="text-align:right;" type="number" id="loan_tot_amount" name="loan_tot_amount" class="form-control" value="<?php if(isset($hdr_data->loan_tot_amount)) echo $hdr_data->loan_tot_amount;?>" data-d-group="2" onChange="calculate_payble_abount()">
                  </div>
                </div>
              </div>
              <div class="col-md-3" >
                <div class="form-group">
                  <label for="loan_term_amount">Term Amount * </label>
                  <input style="text-align:right;" type="text" value="<?php if(isset($hdr_data->loan_term_amount)) echo $hdr_data->loan_term_amount;?>" class="form-control" name="loan_term_amount" id="loan_term_amount" readonly>
                </div>
              </div>
              <div class="col-md-2">
                <div class="form-group"> <br>
                  <input type="checkbox" name="reduce_from_cheque" value="1" <?php if(isset($hdr_data->reduce_fron_ceque)&&$hdr_data->reduce_fron_ceque==1){ echo "checked";} ?>   id="reduce_from_cheque">
                  Reduce from Cheqe Date<br>
                  <input type="checkbox" name="panalty_enable" value="1" <?php if(isset($hdr_data->panalty_enable)&&$hdr_data->panalty_enable==0){ }else{echo "checked";} ?> id="panalty_enable">
                  Panelty Enable<br>
                </div>
              </div>
              
              <div class="col-md-2">
                <div class="form-group">
                  Panalty % <input type="number" name="panalty_presentage" value="<?php if(isset($hdr_data->panalty_presentage)) echo $hdr_data->panalty_presentage;?>"  id="panalty_presentage">
                 
                  Due % <input type="number" name="due_presentage" value="<?php if(isset($hdr_data->due_presentage)) echo $hdr_data->due_presentage;?>"  id="due_presentage">
                </div>
              </div>
              <div class="col-md-12" style="margin-right:2%;">
                <div class="col-md-12" style="margin-right:2%;">
                  <label for="note">Note </label>
                  <textarea id ="note" name="note" class="form-control"><?php if(isset($hdr_data->note)) echo $hdr_data->note;?></textarea>
                </div>
              </div>
              <div class="col-md-12" style="margin-right:2%;">
                <br>
              </div>
              <div class="col-md-6" style="margin-right:2%;">
                <div class="col-md-8" style="margin-right:2%; " >
                  <div class="form-group" style="border: thin solid black;">
                    <div class="col-md-12"> <br>
                      <p><strong><u> Summary </u> </strong> </p>
                      <p> Payable Total Amount : <span id="payable_total_amount_txt"> </span> </p>
                      <p> No of Terms : <span id="no_of_terms_txt"> </span> </p>
                      <p> Payable Term Amount : <span id="payable_term_amount_txt"> </span> </p>
                      <br>
                    </div>
                  </div>
                </div>
              </div>
              
              
              
              <div class="col-md-3 pull-right" style="margin-right:2%;">
              <div class="form-group">
               
                <input name="service_type" type="hidden" value="<?php //echo $service_type;?>">
            
             
                <a href="#" class="btn btn-primary show-tab pull-right" id="loan_inv_hdr_update"> Update Loan Details <i class="fa fa-save"></i> </a>
             
                
                <button type="reset" class="btn btn-danger show-tab pull-right" id="reset_form"> Cancel</button>
               </div>
            </div>           
              
            </div>
            
             
         	<input type="hidden" id="loan_payable_total_amount" name="loan_payable_total_amount" value="" readonly/>
            <input type="hidden" id="loan_id" name="loan_id" value="<?php echo $loan_id ?>" readonly/>
            <input type='hidden' id="loan_ref" name="loan_ref" value="<?php //echo $loan_ref ?>" readonly/>
            <input type='hidden' id="total_interest" name="total_interest" value="" readonly/>
            <input type='hidden' id="term_intrerest" name="term_intrerest" value="" readonly/>
            <input  id="warehouse_id" name="warehouse_id" type="hidden" value="<?php echo $this->session->userdata('ss_warehouse_id');?>">
               
              </div>
            </div>
           
            
            <div class="col-md-12" style="margin-right:2%;">
              <div class="form-group"><br>
                Customer History
                <div class="panel-body">
                  <div id="error"></div>
                  <table class="table table-bordered table-condensed table-hover table-striped dataTable" id="products_table">
                    <thead>
                      <tr>
                        <th></th>
                        <th>Reference<br>
                          No.</th>
                        <th>Customer</th>
                        <th>Loan Date <br/>
                          (yyyy-mm-dd)</th>
                        <th>Payment<br>
                          Term</th>
                        <th>No. of<br>
                          Months</th>
                        <th>Term<br>
                          Amount </th>
                        <th>Monthlly <br>
                          Interest </th>
                        <th>Status</th>
                        <th>Total Amount</th>
                        <th>Paid</th>
                        <th>Balance</th>
                        <th>Action</th>
                      </tr>
                    </thead>
                    <tfoot>
                      <tr>
                        <th></th>
                        <th>Reference No.</th>
                        <th>Customer</th>
                        <th>Loan Date</th>
                        <th>Payment <br>
                          Term</th>
                        <th>No. of<br>
                          Months</th>
                        <th>Term <br>
                          Amount </th>
                        <th>Monthlly <br>
                          Interest </th>
                        <th>Status</th>
                        <th>Paid</th>
                        <th>Total Amount</th>
                        <th>Balance</th>
                        <th>Action</th>
                      </tr>
                      </tfoot>
                  </table>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    
    <!--end header details details-->
    </form>
    <!-- end tab -->
    
   
  </div>
  <!-- end: PAGE --> 
</div>

<!-- end: MAIN CONTAINER --> 
<!-- start: FOOTER -->
<div class="footer clearfix">
  <div class="footer-inner"> <?php echo date("Y");?> &copy; Financial Management System by sallelanka solutions. </div>
  <div class="footer-items"> <span class="go-top"><i class="clip-chevron-up"></i></span> </div>
</div>
<!-- end: FOOTER --> 
<!-- start: RIGHT SIDEBAR -->
<div id="page-sidebar"> <a class="sidebar-toggler sb-toggle" href="#"><i class="fa fa-indent"></i></a>
  <div class="sidebar-wrapper">
    <ul class="nav nav-tabs nav-justified" id="sidebar-tab">
      <li class="active"> <a href="#users" role="tab" data-toggle="tab"><i class="fa fa-users"></i></a> </li>
      <li> <a href="#favorites" role="tab" data-toggle="tab"><i class="fa fa-heart"></i></a> </li>
      <li> <a href="#settings" role="tab" data-toggle="tab"><i class="fa fa-gear"></i></a> </li>
    </ul>
  </div>
</div>

<!-- end: RIGHT SIDEBAR -->
<div id="event-management" class="modal fade" tabindex="-1" data-width="760" style="display: none;">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true"> &times; </button>
        <h4 class="modal-title">Event Management</h4>
      </div>
      <div class="modal-body"></div>
      <div class="modal-footer">
        <button type="button" data-dismiss="modal" class="btn btn-light-grey"> Close </button>
        <button type="button" class="btn btn-danger remove-event no-display"> <i class='fa fa-trash-o'></i> Delete Event </button>
        <button type='submit' class='btn btn-success save-event'> <i class='fa fa-check'></i> Save </button>
      </div>
    </div>
  </div>
</div>

<!-- end: RIGHT SIDEBAR --> 
<!-- start: MAIN JAVASCRIPTS -->
<?php $this->load->view("common/footer"); ?>

<!-- end: MAIN JAVASCRIPTS --> 
<script src="<?php echo asset_url(); ?>js/form-validation-loan_add.js"></script> 
<script src="<?php echo asset_url(); ?>js/loan_add.js"></script> 

<!-- end: JAVASCRIPTS REQUIRED FOR THIS PAGE ONLY --> 
<script>
jQuery(document).ready(function() {
	//Main.init();
	//FormElements.init();
	//$('#loan_date').val('29-07-2016');
	load_customer_old_recod(cus_id)				
	calculate_payble_abount();
	calculateExtraItemTotal();
	$("#loan_payment_term").on("change", function() {
        if ($(this).val() === "4") {
			
            $("#cheque_p_day_div").show();
			calculate_payble_abount();
        }
        else {
           $("#cheque_p_day_div").hide();
		   calculate_payble_abount();
        }
    });
});		
			
			
function load_interest_rate(id)
{	
 var x = document.getElementById("loan_interest");
x.remove(x.selectedIndex);  
   
	$.post("<?php echo  base_url(); ?>loan/get_all_interest_by_payment_term_id?R="+id, function(data) {

var sel = $("#loan_interest");
   
sel.empty();
$("#loan_interest").append('<option value="" selected> - select interest -</option>');
$.each(data, function(i, item){
$("#loan_interest").append('<option value="' + i + '">'+ item + '</option>');
})
},"json");	
}		
			
//save invoice header
$( "#loan_inv_hdr" ).click(function() {
	 $("#loan_inv_hdr").prop("disabled", true);
		 $("#loan_inv_hdr").val('Please wait..');
		 
   var fields = $("#loan_item_save").serialize();
   
   $.post( "<?php echo base_url();?>loan/loan_header_save", fields)
	  .done(function( data ) {
		 var obj = jQuery.parseJSON(data);
		 
			if(obj.error==0){
				alert("Recode successfully saved");
				
				if(obj.is_cheque_req==1)
				{
				sendUrl='<?php echo base_url();?>loan/add_cheque_for_loan/'+obj.loan_id;					
				}else
				{
				sendUrl='<?php echo base_url();?>loan/loan_details/'+obj.loan_id;	
				}
				window.location.href = sendUrl;				
				//location.reload();
			}
			if(obj.error==1){
				$("#loan_inv_hdr").prop("disabled", false);
		 		$("#loan_inv_hdr").val('Next');
				 bootbox.alert(obj.msg, function () {                        
                    });
					$("#loan_inv_hdr").prop("disabled", false);
		 			$("#loan_inv_hdr").val('Next');
					//$table_name			
			}
	 });
});		
			
			
function load_customer_old_recod(cus_id)
{
 	$('#products_table').DataTable({
	"ajax": "<?php echo base_url('loan/get_list_loan_by_customer?cus_id=')?>"+cus_id,
	"bDestroy": true,
	"iDisplayLength": 50,
	"order": [[ 0, "desc" ]]
	});	
			
}		

//update invoice header
$( "#loan_inv_hdr_update" ).click(function() {
	 $("#loan_inv_hdr").prop("disabled", true);
		 $("#loan_inv_hdr").val('Please wait..');
		 
   var fields = $("#loan_item_save").serialize();
   
   $.post( "<?php echo base_url();?>loan/loan_header_update", fields)
	  .done(function( data ) {
		 var obj = jQuery.parseJSON(data);
		 
			if(obj.error==0){
				alert("Recode successfully saved");
				
				if(obj.is_cheque_req==1)
				{
				sendUrl='<?php echo base_url();?>loan/add_cheque_for_loan/'+obj.loan_id;					
				}else
				{
				sendUrl='<?php echo base_url();?>loan/loan_details/'+obj.loan_id;	
				}
				window.location.href = sendUrl;				
				//location.reload();
			}
			if(obj.error==1){
				$("#loan_inv_hdr").prop("disabled", false);
		 		$("#loan_inv_hdr").val('Next');
				 bootbox.alert(obj.msg, function () {                        
                    });
					$("#loan_inv_hdr").prop("disabled", false);
		 			$("#loan_inv_hdr").val('Next');
					//$table_name				
			}
	 });
});

$("#reduce_from_cheque").click(function() {
   if(document.getElementById('reduce_from_cheque').checked){
     // $("#cheque_p_day_div").hide();
		   calculate_payble_abount();}

  else
     { //$("#cheque_p_day_div").show();
		   calculate_payble_abount();}
});
			
</script>
</body>
<!-- end: BODY -->
</html>