<?php  

   session_start();
 //  error_reporting(0);
    include("../db.php");
    include("../all_query.php");
		
		
 
  if(isset($_SESSION['loan_crm_username']) && isset($_SESSION['loan_crm_password']))
  {
		 
     
	  $type = $_REQUEST['type'];
	 $account_holder_id = $_REQUEST['account_holder_id'];
	 $loan_ids = $_REQUEST['loan_ids'];
	
	 
	  $loan_idsArray = json_decode(stripslashes($_REQUEST['loan_ids']));
	   $loan_ids = "'" . implode ( "', '", $loan_idsArray ) . "'";
	  
	  
	  
	   $customerQ = mysqli_query($conn,"select *from `customer` where account_holder_id = '$account_holder_id' ");
	   $customerData = mysqli_fetch_assoc($customerQ);
	   
	   
	   
	    $leadQ=mysqli_query($conn,"select *  from `leads`  where  account_holder_id = '$account_holder_id' and account_id in ($loan_ids)  order by id desc ");
		 
		 $total_loan = mysqli_num_rows($leadQ);
		 
		 $total_loan_amount = 0;
		 $total_tos = 0;
		 $total_principal = 0;
		 $total_interest = 0;
		 $total_feesdue = 0;
		 $total_penalty = 0;
		 
		 
		 $dpd_bucket_names = '';
		 $dpd_bucketArray = array();
		 
		 
		while($leadData = mysqli_fetch_assoc($leadQ))
		{
			 $lead_data = $leadData['lead_data'];
			 $leadDataArray = json_decode(stripslashes($lead_data),TRUE); 
			 
			 
			 $dpd_bucketArray[] = $leadDataArray['dpdbucket'];
                       
					   
					   
			$total_loan_amount = $total_loan_amount + $leadData['loan_amount'];
			$total_tos = $total_tos + $leadData['tos'];
			
			$total_principal = $total_principal + $leadDataArray['principaldue'];
			$total_interest = $total_interest + $leadDataArray['interestdue'];
			$total_feesdue = $total_feesdue + $leadDataArray['feesdue'];
			$total_penalty = $total_penalty + $leadDataArray['penaltydue'];
			
			
		}
		
		
		$dpd_bucket_names =  "'" . implode ( "', '", $dpd_bucketArray ) . "'";
		
		
		//get highest weightage 
		
		$dpd_bucketQ  = mysqli_query($conn,"select *from `dpd_bucket` where  name in ($dpd_bucket_names)  order by weightage desc  limit 1 ");
		$dpdBucketData = mysqli_fetch_assoc($dpd_bucketQ);
		 $max_per  = $dpdBucketData['max_per'];
		 $dpd_bucket = $dpdBucketData['name'];
	  
	  
	  
	  
	  
	  
	  ?>
	  
	   <div class="card-body" style="padding:0;zoom:85%;">
	   
	   <form action="" id="settlement_form" method="post" >
	   
	   
	   <input type="hidden" name="account_holder_id" value="<?php echo $account_holder_id; ?>"  />
	   <input type="hidden" name="loan_ids" value="<?php echo $loan_ids; ?>"  />
	   <input type="hidden" name="settlement_type" value="<?php echo $type; ?>"  />
         
		   <li class="mb-3">
              <span class="fw-bold me-2">Total Selected Loan:</span>
              <span><?php echo count($loan_idsArray); ?></span>
            </li>
			
			
		   <li class="mb-3">
              <span class="fw-bold me-2">DPD Bucket:</span>
              <span><?php echo $dpd_bucket; ?></span>
            </li>
       
			 <br/>
			 
		 
		 <div class="row g-4 mb-4">
		 
		  <div class="col-sm-6 col-xl-3" style="margin:2px;">
			<div class="card">
			  <div class="card-body" style="    background: #f3f3fe;"  >
				<div class="d-flex align-items-start justify-content-between">
				  <div class="content-left">
				  
					<div class="d-flex align-items-end mt-2">
					  <h4 class="mb-0 me-2"><?php echo number_format($total_loan_amount); ?></h4>
					  
					</div>
					<small>Total Loan Amt</small>
				  </div>
				 
				</div>
			  </div>
			</div>
		  </div>
		  
		  
		    <div class="col-sm-6 col-xl-3" style="margin:2px;" >
			<div class="card">
			  <div class="card-body" style="    background: #f3f3fe;" >
				<div class="d-flex align-items-start justify-content-between">
				  <div class="content-left">
				  
					<div class="d-flex align-items-end mt-2">
					  <h4 class="mb-0 me-2"><?php echo number_format($total_tos); ?></h4>
					  
					</div>
					<small>Total Outstating</small>
				  </div>
				 
				</div>
			  </div>
			</div>
		  </div>
		  
		  
		  
		   <div class="col-sm-6 col-xl-3" style="margin:2px;" >
			<div class="card">
			  <div class="card-body" style="    background: #f3f3fe;" >
				<div class="d-flex align-items-start justify-content-between">
				  <div class="content-left">
				  
					<div class="d-flex align-items-end mt-2">
					  <h4 class="mb-0 me-2"><?php echo number_format($total_principal); ?></h4>
					  
					</div>
					<small>Total Principal</small>
				  </div>
				 
				</div>
			  </div>
			</div>
		  </div>
		  
		 </div>

		 <div class="row g-4 mb-4">
			 
		   <div class="col-sm-6 col-xl-3" style="margin:2px;">
			<div class="card">
			  <div class="card-body" style="background: #fff7f7;" >
				<div class="d-flex align-items-start justify-content-between">
				  <div class="content-left">
				  
					<div class="d-flex align-items-end mt-2">
					  <h4 class="mb-0 me-2"><?php echo number_format($total_interest); ?></h4>
					  
					</div>
					<small>Total Interest</small>
				  </div>
				 <?php if($type=='Waiver') { ?><input class="form-check-input" onchange="calConsiderAmt();"  name="waiver_select" value="total_interest" checked type="checkbox"   /> <?php } ?>
				</div>
			  </div>
			</div>
		  </div>
		  
		  
		     <div class="col-sm-6 col-xl-3" style="margin:2px;">
			<div class="card">
			  <div class="card-body" style="background: #fff7f7;" >
				<div class="d-flex align-items-start justify-content-between">
				  <div class="content-left">
				  
					<div class="d-flex align-items-end mt-2">
					  <h4 class="mb-0 me-2"><?php echo number_format($total_feesdue); ?></h4>
					  
					</div>
					<small>Total Feesdue</small>
				  </div><?php if($type=='Waiver') { ?><input class="form-check-input" onchange="calConsiderAmt();"  name="waiver_select" value="total_feesdue" checked type="checkbox"   /> <?php } ?>
				 
				</div>
			  </div>
			</div>
		  </div>
		  
		  
		  
		   <div class="col-sm-6 col-xl-3" style="margin:2px;">
			<div class="card">
			  <div class="card-body" style="background: #fff7f7;">
				<div class="d-flex align-items-start justify-content-between">
				  <div class="content-left">
				  
					<div class="d-flex align-items-end mt-2">
					  <h4 class="mb-0 me-2"><?php echo number_format($total_penalty); ?></h4>
					  
					</div>
					<small>Total Penalty</small>
				  </div><?php if($type=='Waiver') { ?><input class="form-check-input" onchange="calConsiderAmt();"  name="waiver_select" value="total_penalty" checked type="checkbox"   /> <?php } ?>
				 
				</div>
			  </div>
			</div>
		  </div>
		  
		  
		  
   
	</div>
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
        <div class="info-container" style="padding:10px;">
          <ul class="list-unstyled" style="    text-align: left;">
           
          
			
			
		   <li class="mb-3" style="    display: flex;"> 
			   
		  <input type="hidden" id="total_outstanding" name="total_outstanding" value="<?php echo $total_tos; ?>" />
		  <input type="hidden" id="total_loan_amount" name="total_loan_amount"   value="<?php echo $total_loan_amount; ?>" />
		  <input type="hidden" id="total_principal" name="total_principal"  value="<?php echo $total_principal; ?>" />
		  <input type="hidden" id="total_interest" name="total_interest"  value="<?php echo $total_interest; ?>" />
		  <input type="hidden" id="total_feesdue" name="total_feesdue"   value="<?php echo $total_feesdue; ?>" />
		  <input type="hidden" id="total_penalty" name="total_penalty"   value="<?php echo $total_penalty; ?>" />
		  
		  
		  <input type="hidden" id="consider_amount" name="consider_amount" value="<?php echo $total_tos; ?>" />
		  
		  
		  <input type="hidden" id="consider_type" name="consider_type" value="<?php echo $total_outstanding; ?>" />
		  <input type="hidden" id="max_per" name="max_per" value="<?php echo $max_per; ?>" />
		  <input type="hidden" id="dpd_bucket" name="dpd_bucket" value="<?php echo $dpd_bucket; ?>" />
			   
			   
		
		  
		  <div class="input-group" style="margin-left: -4px; <?php if($type=='Waiver') { ?>display:none; <?php } ?>" >
          <span style="width: 120px; font-size: 14px;"  class="input-group-text fw-bold"><?php echo $type; ?>(%):</span>
          <input type="number" id="dis_per" max="<?php echo $max_per; ?>" name="dis_per" onKeyup="calDisPer();" class="form-control"  >
          <span class="input-group-text">%</span>
          </div>
		   
		   
		   
		   
		   <div class="input-group" style="margin-left: -4px;" >
          <span style="width: 120px; font-size: 14px;"class="input-group-text fw-bold"><?php if($type=='Waiver') { echo "Payable Amt"; } else { echo "Settlement Amt"; } ?>:</span>
          <input type="number"    <?php if($type=='Waiver') {  echo "readonly"; } ?>   id="sett_amount" name="sett_amount"  onKeyup="calEMISettlement();" value="<?php if($type=='Waiver') {  echo $total_tos; } ?>"  class="form-control"  required >
          </div>
		  
		 
		  
		   
           </li>
		   
		   
		   <li class="mb-3"> 
			   
		   <div class="input-group" style="margin-left: -4px;" >
           <span style="width: 120px; font-size: 14px;"   class="input-group-text fw-bold"> No.EMI:</span>
          	<div style="width:45%;">
			<select onchange="calEMISettlement();"   name="select_emi_no" id="select_emi_no"     class="select2 form-select" data-allow-clear="true"   required  >
			  <option value="">Select EMI</option>
			 <?php $catQ= mysqli_query($conn,"select *from `no_of_emi`   ");
			  while($catData=mysqli_fetch_assoc($catQ))
		       {  ?>
				 <option  value="<?php echo $catData['name']; ?>"><?php echo $catData['name']; ?></option>
				 <?php
			   }
            	 ?>
			  </select>
			   
			</div>
			
			&nbsp;&nbsp;
			  
			  <select onchange="calEMISettlement();"   name="emi_type" id="emi_type"     class="select2 form-select" data-allow-clear="true"   required  >
			    <option selected value="standard">Standard</option>
			    <option   value="equal">Equal EMI</option>
			      
			  </select>
			
            </div>
			
			 
            </li>
		   
		   
		   
		   
		   
		   
		   
		   
		   
		   
			
		 
			
			 <li class="mb-3"> 
		   
			<div id="settlemnt_emi_details_here" ></div>
		   
			</li>
			
			
			
			
			
			 <li class="mb-3" style="    display: flex;">
			 
		    <div class="input-group" style="margin-left: -4px;" >
            <span style="width: 100px; font-size: 14px;"  class="input-group-text fw-bold">Start date:</span>
             <input type="date" id="s_start_date" min="<?php echo date('Y-m-d'); ?>" value="<?php echo date('Y-m-d'); ?>" name="s_start_date" class="form-control"  >
             
           </div>
		   
			<div class="input-group" style="margin-left: -4px;" >
            <span style="width: 120px; font-size: 14px;"  class="input-group-text fw-bold">End date:</span>
             <input type="date" id="s_end_date" name="s_end_date" class="form-control" required  >
             
           </div>
			
			</li>
			
			
			 <li class="mb-3"> 
			 
			 <div class="input-group" style="margin-left: -4px;" >
             <span style="width: 120px; font-size: 14px;"  class="input-group-text fw-bold">Expires On:</span>
             <input  style="width: 150px;"  type="date" id="expires_date" name="expires_date" class="form-control"  required >
             
             </div>
		   
		    
			  </li>
			  
			  
			   <li class="mb-3"> 
			 
			 <div class="input-group" style="margin-left: -4px;" >
             <span style="width: 120px; font-size: 14px;"  class="input-group-text fw-bold">Remark:</span>
             <textarea    type="date"  name="remark" class="form-control"  ></textarea>
             
             </div>
		   
		    
			  </li>
			  
			<li class="mb-3" style="display:flex;"> 
			 <button class="btn btn-success">Save</button>&nbsp;
			 <button  type="button" class="  btn btn-danger"  data-bs-dismiss="offcanvas" aria-label="Close" >Cancel</button>
			 </li>
			
			 
            
          </ul>
           
        </div>
		
		
		</form>
		
		
		
      </div>
	  
	  
	  
	  
	  <?php
	  
	  
	  
	  
	  
	  
	  
	  
	  
    		 
			
			
 }
		
		
		?>