<?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">New 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 echo date('d-m-Y'); ?>">
                      <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='';
							?>
                      <option  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)
					{?>
                      <option 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="loan_interest">Customer </label>                   
                            <input type="hidden"  name="cus_id" class="js-data-cus_id-ajax form-control" style="padding:0px 0px; border: none; box-shadow: none" placeholder="-Select Customer-"  onChange="load_customer_old_recod(this.value);"  /><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="" 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="" 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>                      
                       <input type="hidden"  name="g_cus_id" class="js-data-g_cus_id-ajax form-control" style="padding:0px 0px; border: none; box-shadow: none" placeholder="-Select Garenter-" /><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="" 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="" 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"  id="reduce_from_cheque">
                  Reduce from Cheqe Date<br>
                  <input type="checkbox" name="panalty_enable" value="1" 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="0.1"  id="panalty_presentage">
                 
                  Due % <input type="number" name="due_presentage" value="0.3"  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">
</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">
                <?php if (!$vcl_srvs_inv_header){ ?>
                <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"> Proceed <i class="fa fa-arrow-circle-right"></i> </a>
                <?php }?>
                <?php if ($vcl_srvs_inv_header){ ?>
                <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>
                <?php } ?>
                
                <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="" readonly/>
            <input type='hidden' id="loan_ref" name="loan_ref" value="" 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>Capital Amount</th>
                        <th>Paid( Loan )</th>
                        <th>Balance ( Loan )</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?v=1"></script> 

<!-- end: JAVASCRIPTS REQUIRED FOR THIS PAGE ONLY --> 
<script>
jQuery(document).ready(function() {
	//Main.init();
	//FormElements.init();
	//$('#loan_date').val('29-07-2016');				
	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();}
});


//-----------------------------------------------------
 $(".js-data-cus_id-ajax").select2({
        ajax: {
        url: "<?php echo base_url('customers/filter_customer') ?>",
                dataType: 'json',
                delay: 250,
                data: function (query) {
                if (!query)
                        query = '';
                return {
                search_string: query,
                        format: 'json'
                };
                },
                results: function (data) {
                return {
                results: $.map(data, function (item) {
                var text1 = item.cus_name + "  (" + item.cus_nic + ")";
                return {
                text: text1,
                        slug: item.cus_name,
                        id: item.cus_id
                };
                })
                };
                },
                cache: true
        }
        });
//-----------------------------------------------------	

//-----------------------------------------------------




 $(".js-data-g_cus_id-ajax").select2({
        ajax: {
        url: "<?php echo base_url('customers/filter_customer') ?>",
                dataType: 'json',
                delay: 250,
                data: function (query) {
                if (!query)
                        query = '';
                return {
                search_string: query,
                        format: 'json'
                };
                },
                results: function (data) {
                return {
                results: $.map(data, function (item) {
                var text1 = item.cus_name + "  (" + item.cus_nic + ")";
                return {
                text: text1,
                        slug: item.cus_name,
                        id: item.cus_id
                };
                })
                };
                },
                cache: true
        }
        });
//-----------------------------------------------------	
function test(){


	
}
       
			
</script>
</body>
<!-- end: BODY -->
</html>