<?php
session_start();
include "../config/config.php";
$id = $_GET['id'];
?>
<style>
    thead{
        background: #dbcbcb;
    }
    table.table tbody tr:hover td {
        background: none !important;
    }
</style>

<?php


    $statement = $pdo->prepare("SELECT cs.*,
c.customer_name, c.mobile_no,
(SELECT COUNT(id) FROM `tbl_payments` p WHERE p.accounts=3 and p.scheme_id=cs.id) as payment_paid_month_counts
FROM `tbl_customer_scheme` cs
LEFT JOIN tbl_customers c ON c.id=cs.customer_id
LEFT JOIN tbl_schemes ON tbl_schemes.id=cs.scheme_id
WHERE cs.id=?");


    $statement->execute(array($id));
    $emp_data = $statement->fetchAll(PDO::FETCH_ASSOC);
    $totalData = $statement->rowCount();
    
    
    if($totalData==1){
        foreach($emp_data as $data){
            
            
            $customer_name = $data['customer_name'];
            $mobile_no = $data['mobile_no'];
            
            
        $customer_id = $data['customer_id'];
        $scheme_id = $data['scheme_id'];
        $scheme_amount = $data['scheme_amount'];
        $scheme_no = $data['scheme_no'];
        $scheme_added_date = $data['scheme_added_date'];
        $scheme_months = $data['scheme_months'];
        $payment_paid_month_counts = $data['payment_paid_month_counts'];
        $payment_balance_month_counts = $scheme_months-$payment_paid_month_counts;
            
            
        if($data['scheme_completed_date']=="0000-00-00"){
            $scheme_completed_date = "";
        }else{
            $scheme_completed_date = date('d-m-Y', strtotime($data['scheme_completed_date']));
        }
            

		 	

    
  	$grand_total = $scheme_months*$scheme_amount;
        	
        	
        	
    if($total_paid_amount==$grand_total){
        $nestedData ="<span class='btn btn-success btn-xs'>Payment Completed</span>";
    }else if($total_paid_amount!=0){
        $nestedData ="<span class='btn btn-warning btn-xs'>Payment Due</span>";
    }else if($total_paid_amount==0){
        $nestedData ="<span class='btn btn-danger btn-xs'>Payment Pending</span>";
    }else{
        $nestedData ="<span class='btn btn-danger btn-xs'>NULL</span>";
    }
        	
        	
        	
        	
        	
        	
        	
        	
        	
        }
    }
?>

<div class="modal-dialog" role="document" id="add_modal" style="width: 1250px;">
    <div class="modal-content" id="printableArea">
      <div class="modal-header">
            <h4 class="modal-title" id="exampleModalLabel">
                <b>Customer Scheme Details</b>
                <!--<a class="btn btn-primary" href='modal/purchase-add-modal.php?id=<?=$id?>' id='<?=$id?>' data-toggle='ajax-modal' title='Payment' style="margin-left: 50px;padding: 3px 10px 3px 10px;"><i class="fa fa-inr"></i>  Add Payments</a>-->
                <span class="btn btn-success" style="margin-left: 100px;padding: 3px 10px 3px 10px;">Scheme Total Amount: <b><?=number_format($grand_total,2)?></b> </span>
                <!--<span class="btn btn-danger" style="padding: 3px 10px 3px 10px;">Scheme Balance Amount: <b><?//=number_format($current_balance,2)?></b> </span>-->
            </h4>
            
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true"><i class="fa fa-times" style="margin-top: -23px; font-size: 28px;"></i></span>
            </button>
      </div>
      
           
           
      <div class="modal-body">
        <div class="row">
            
            
<div class="col-md-12" style="padding-top: 5px;">
 
<table class="table table-striped">
    
    
    <tr>
        <th>Customer Name</th>
        <td style="vertical-align: top;"><?=$customer_name?></td>
        <th>Customer Mobile No</th>
        <td style="vertical-align: top;"><?=$mobile_no?></td>
    </tr>
    
    <tr>
        <th>Scheme No</th>
        <td style="vertical-align: top;"><?=$scheme_no?></td>
        <th>Scheme Amount</th>
        <td style="vertical-align: top;"><?=$scheme_amount?></td>
    </tr>
    
    <tr>
        <th style="width: 20%;">Added Date</th>
        <td style="width: 30%; background: none !important;"><?=date('d-m-Y', strtotime($scheme_added_date))?></td>
        <th style="width: 20%;">Completed Date</th>
        <td style="width: 30%;"><?=$scheme_completed_date?></td>
    </tr>
    
    <tr>
        <th>Scheme Month</th>
        <td style="vertical-align: top;"><?=$scheme_months?></td>
        <th>Scheme Total Amount</th>
        <td style="vertical-align: top;"><?=number_format($scheme_months*$scheme_amount, 2)?></td>
    </tr>
    <tr>
        <th>Scheme Paid Month:</th>
        <td style="vertical-align: top;"><?=$payment_paid_month_counts?></td>
        <th>Scheme Balance Month:</th>
        <td style="vertical-align: top;"><?=$payment_balance_month_counts?></td>
    </tr>

    
   
</table>

</div>
 
<div class="col-md-12" style="padding-top: 5px;"> 
 
 

<table class="table table-striped">
  <thead>
    <tr>
      <th scope="col">S.No</th>
      <th scope="col">Month - Year</th>
      <th scope="col">Amount</th>
      <th scope="col">Payment Type</th>
      <th scope="col">Reference</th>
      <th scope="col">Notes</th>
      <th scope="col">Created At</th>
      <th scope="col">Actions</th>
    </tr>
  </thead>
  <tbody>
      
<?php
    $statement = $pdo->prepare("SELECT p.*, tbl_payment_type.name as payment_type_name
    FROM `tbl_payments` p 
    LEFT JOIN tbl_payment_type ON tbl_payment_type.id=p.payment_type
    WHERE p.accounts=? and p.scheme_id=? and p.customer_id=?");
    $statement->execute(array(3, $id, $customer_id));
    $data_results = $statement->fetchAll(PDO::FETCH_ASSOC);
    $totalData = $statement->rowCount();
    if($totalData>0){
        $i=1;
        foreach($data_results as $res){
            
            if($res['month']==1){
                $payment_month = "January";
            }else if($res['month']==2){
                $payment_month = "February";
            }else if($res['month']==3){
                $payment_month = "March";
            }else if($res['month']==4){
                $payment_month = "April";
            }else if($res['month']==5){
                $payment_month = "May";
            }else if($res['month']==6){
                $payment_month = "June";
            }else if($res['month']==7){
                $payment_month = "July";
            }else if($res['month']==8){
                $payment_month = "August";
            }else if($res['month']==9){
                $payment_month = "September";
            }else if($res['month']==10){
                $payment_month = "October";
            }else if($res['month']==11){
                $payment_month = "November";
            }else if($res['month']==12){
                $payment_month = "December";
            }
            
            
            
            $actions='';
            /*$actions.="<a style='padding: 7px;margin-right: 3px;' href='modal/purchase-edit-modal.php?id=".$data["id"]."' title='Edit Payment' data-toggle='ajax-modal' class='tip btn btn-warning btn-xs'>
		                    <i class='fa fa-edit'></i>
		                </a>";*/
            $actions.="<button style='padding: 7px;margin-right: 3px;'  title='Delete Payment' onClick='delete_Payments(".$res["id"].")' class='tip btn btn-danger btn-xs'>
		                    <i class='fa fa-trash-o'></i>
		                </button>";
            
?>
            
            <tr>
              <td style="text-align: center;"><?=$i++?></td>
              <td style="text-align: center;"><?=$payment_month?> - <?=$res['year']?></td>
              <td style="text-align: center; padding-right: 35px;">
                  <b><?=number_format($res['amount'],2)?></b>
              </td>
              <td style="text-align: center;">
                  <?=$res['payment_type_name']?>
              </td>
              <td style="text-align: center;">
                  <?=$res['reference_no']?>
              </td>
              <td style="text-align: center;">
                  <?=$res['notes']?>
              </td>
              <td style="text-align: center;">
                  <?=date('d-m-Y h:i A', strtotime($res['created_at']))?>
              </td>
              <td style="text-align: center;">
                  <?=$actions?>
              </td>
            </tr>
            
<?php   }
    }else{
        echo "<tr><td colspan='7' style='text-align: center;'>No Payments Found</td></tr>";
    }
?>
  </tbody>
</table>







      
           
            
        </div>
      </div>
      
      
      <div class="modal-footer">
        <button type="button" class="btn btn-danger pull-left" data-dismiss="modal"><i class="fa fa-times"></i> Close</button>
        <!--<button type="button" class="btn btn-success pull-right" onclick="printDiv('printableArea')"><i class="bi bi-printer-fill"></i> Print</button>-->
      </div>

    </div>
</div>
<script>
    function delete_Payments(id){
            var result = confirm("Confirm Want to Delete This Payments?");
            if (result) {
                    $.ajax({
                    type: 'POST',
                    url: 'api/delete.php',
                    data: {type:'hard_delete',delete_id:id, table: 'tbl_payments'},                  
                    success: function(response) {
                        console.log(response); 
                        $("#add_modal .close").click();
                        emp_datas();
                    }
                });
            }
    }
</script>