<?php
$id=$this->uri->segment('3');
$acedemic_session=$this->session->userdata('acedemic_session');

?>
<style>
/* The container */
.form-group-1{
  display: block;
  position: relative;
  float:left;
  padding-left: 35px;
  margin-bottom: 12px;
  cursor: pointer;
  font-size: 22px;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  width:25%;
}

/* Hide the browser's default checkbox */
.form-group-1 input {
  position: absolute;
  opacity: 0;
  cursor: pointer;
  height: 0;
  width: 0;
}

/* Create a custom checkbox */
.checkmark {
  position: absolute;
  top: 0;
  left: 0;
  height: 25px;
  width: 25px;
  background-color: #eee;
}

/* On mouse-over, add a grey background color */
 .form-group-1:hover input ~ .checkmark {
  background-color: #ccc;
}

/* When the checkbox is checked, add a blue background */
 .form-group-1 input:checked ~ .checkmark {
  background-color: #2196F3;
}

/* Create the checkmark/indicator (hidden when not checked) */
.checkmark:after {
  content: "";
  position: absolute;
  display: none;
}

/* Show the checkmark when checked */
 .form-group-1 input:checked ~ .checkmark:after {
  display: block;
}

/* Style the checkmark/indicator */
 .form-group-1 .checkmark:after {
  left: 9px;
  top: 5px;
  width: 5px;
  height: 10px;
  border: solid white;
  border-width: 0 3px 3px 0;
  -webkit-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  transform: rotate(45deg);
}

#list_monthss{width:100%;}
label{font-weight:600;}


.w-50{
    width:50%;
    float:left;
    padding:10px;
}

.w-25{
    width:25%;
    float:left;
    padding:10px;
}
</style>
<?php 
$get_trans_fee_combined_data=$this->TransportModel->get_transport_inv_comb_data($id,$acedemic_session);
// var_dump($get_trans_fee_combined_data);
if($get_trans_fee_combined_data){
    foreach($get_trans_fee_combined_data as $fee_data){
        $sid=$fee_data['sid'];
        $route_id="";
        $route_title="";
        $get_student_transport_data=$this->TransportModel->get_student_transport_comb_info_for_ac_sesssion($sid,$acedemic_session);
        if($get_student_transport_data){
            foreach($get_student_transport_data as $rote_info){
                $route_id=$rote_info["route_no"];
                $route_title=$rote_info["route_title"];
            }
        }
        
        $fee_mode=$fee_data['fee_mode'];
        $esatimate_fee=$fee_data['esatimate_fee'];
        $prev_bal_added=$fee_data['prev_bal_added'];
        $extra_amount_used=$fee_data['extra_amount_used'];
        $discount=$fee_data['discount'];
        $total_fee=$esatimate_fee + $prev_bal_added - $extra_amount_used - $discount;
        
        
?>
<div class="container-fluid mt-3 mb-3 bg-white">
    <div class="row">
        <div class="col-sm-12 pt-3 pb-3">
            <ul class="breadcrumb">
                <li class="breadcrumb-item">
                    <a href="Welcome">Home</a>
                </li>
                <li class="breadcrumb-item">
                    <a href="Transport/students">Transport</a>
                </li>
                
                <li class="breadcrumb-item">
                    <a href="<?php echo base_url()."Transport/fee/".$sid; ?>"><?php echo $fee_data['student_name']; ?></a>
                </li>
            </ul>
            
            
            <div class="mt-3 mb-3 table-responsive" id="get_student_transport_fee_invoices"></div>
                <form method="post" class="form-outer">
                    <div>
                        <div class="form-header text-center font-weight-bolder">Update Transport Fee</div>
                        <hr>
                        <!--<div class="form-group w-100" id="">-->
                        <!--    <label class="font-weight-bold">Fee Months</label>-->
                        <!--    <div id="list_monthss" class="w-100"></div>-->
                        <!--</div>-->
                        <div class="form-group w-100">
                            <label>Route Title</label>
                            <input type="text" name="route_title" class="form-control" value="<?php echo $route_title; ?>" readonly>
                            <input type="hidden" name="route_id" id="route_id" value="<?php echo $route_id; ?>">
                            
                            <input type="hidden" name="fee_id" id="fee_id" value="<?php echo $fee_data['id']; ?>">
                            
                            <input type="hidden" name="sid" value="<?php echo $fee_data['sid']; ?>">
                        </div>
                        <div class="form-group w-25">
                            <label>Student Name</label>
                            <input type="text" name="student_name" class="form-control" value="<?php echo $fee_data['student_name'];?>" readonly>
                        </div>
                        
                        <div class="form-group w-25">
                            <label>Father's Name</label>
                            <input type="text" name="father_name" class="form-control" value="<?php echo $fee_data['father_name'];?>" readonly>
                        </div>
                        
                        <div class="form-group w-25">
                            <label>Class</label>
                            <input type="text" name="class" value="<?php echo $fee_data['class'].'-'.$fee_data['section']; ?>" class="form-control" readonly>
                        </div>
            
                        <div class="form-group w-25">
                            <label class="">Fee Months</label>
                            <input type="text" name="fee_months" id="fee_months" class="form-control" placeholder="Fee Months" value="<?php echo $fee_data['fee_months']; ?>" readonly>
                        </div>
                        <div class="form-group w-25">
                            <label class="">Estimate Fee</label>
                            <input type="number" value="<?php echo $fee_data['esatimate_fee']; ?>" class="form-control" name="etimate_fee" id="estimate_fee" placeholder="Estimate Fee" readonly >
                        </div>
            
                        <div class="form-group w-25">
                            <label>Prev Bal.</label>
                            <input type="number" value="<?php echo $fee_data['prev_bal_added']; ?>" class="form-control" name="prev_bal" id="prev_bal_fee" placeholder="Prev Bal." readonly>
                        </div>
            
                        <div class="form-group w-25">
                            <label>Extra Fee</label>
                            <input type="number" class="form-control" name="prev_extra_fee" id="prev_extra_fee" placeholder="Extra Fee" value="<?php echo $fee_data['extra_amount_used']; ?>" readonly>
                        </div>
            
                        <div class="form-group w-25">
                            <label class="">Discount</label>
                            <input type="number"  class="form-control" name="discount" id="discount" placeholder="Discount" onclick="myFunction()" value="<?php echo $fee_data['discount']; ?>">
                        </div>
                        
                        <div class="form-group w-25">
                            <label>Total</label>
                            <input type="number" class="form-control" name="total_fee_amount" id="total_fee_amount" placeholder="Total Fee" value="<?php echo $total_fee; ?>" readonly >
                        </div>
            
                        <div class="form-group w-25">
                            <label>Received Fee</label>
                            <input type="number" class="form-control" name="received_fee" id="received_fee" placeholder="Received Fee" onclick="myFunction()" value="<?php echo $fee_data['received_fee']; ?>">
                        </div>
            
                        <div class="form-group w-25">
                            <label>Balance</label>
                            <input type="number" class="form-control" name="bal_fee" id="bal_fee" placeholder="Balance Fee" value="<?php echo $fee_data['balance_fee']; ?>" readonly>
                        </div>    
            
            
                        <div class="form-group w-25">
                            <label>Extra</label>
                            <input type="number" class="form-control" name="extra_fee" id="extra_fee" placeholder="Extra Fee" value="<?php echo $fee_data['extra_fee']; ?>" readonly>
                        </div>
            
                        <div class="form-group w-25">
                            <label>Fee Date</label>
                            <input type="date" name="fee_date" class="form-control" id="fee_date" onclick="myFunction()" value="<?php echo $fee_data['fee_date']; ?>">
                        </div>
            
                        <div class="form-group w-25" >
                            <label>Payment Mode</label>    
                            <select name="paymentmode" id="fee_mode" class="form-control fee_mode">
                                <option value="">Select Pay.Mode</option>    
                                <option value="CASH" <?php if($fee_mode == "CASH"){ echo "selected"; } ?>>Cash</option>
                                <option value="Bank Transfer" <?php if($fee_mode == "Bank Transfer"){ echo "selected"; } ?>>Bank Transfer</option>
                                <option value="Cheque" <?php if($fee_mode == "Cheque"){ echo "selected"; } ?>>By Cheque</option>
                                <option value="PAYTM" <?php if($fee_mode == "PAYTM"){ echo "selected"; } ?>>Paytm</option>
                                <option value="UPI" <?php if($fee_mode == "UPI"){ echo "selected"; } ?>>UPI</option>
                            </select>
                        </div>
                        <div class="form-group w-25">
                            <label>Fee Note</label>
                            <input type="text" class="form-control hide_submit_btn fee_note" placeholder="Fee Note" id="fee_note" value="<?php echo $fee_data['fee_note']; ?>">
                        </div>



                        <div class="form-group w-25">
                            <label>Trans. Id / Check / Draft No.</label>
                            <input type="text" class="form-control mb-1" placeholder="TransactionId/Check or Draft No." id="transaction_info" value="<?php echo $fee_data['transaction_info']; ?>" onclick='myFunction()'>
                        </div>
                        <div class="w_100" style="display:inline-block;">
                    
                            <div class="form-group w-100">
                                <button class="btn btn-sm btn-warning" id="calculate_fee" type="button">Calculate Fee</button>
                                <button class="btn btn-sm btn-success" id="pay_fee" type="button">Update Fee</button>    
                            </div>
                
                        </div>
                    </div>
                </form>   
                <div class="mt-3 mb-3">
                    <?php  $this->load->view('erp/transport/quick-links'); ?>
                </div>
            
        </div>
    </div>
</div>
<?php
    }
}
?>

<script>
    function myFunction(){
        $("#pay_fee").hide();
    }
    $(document).ready(function(){
        $("#calculate_fee").click(function(){
            var acedemic_session=<?php echo $acedemic_session; ?>;
            var est_amount=parseInt($("#estimate_fee").val());
            var discount_amount=parseInt($("#discount").val());
            var prev_extra_amount=parseInt($("#prev_extra_fee").val());
            var prev_bal_amount=parseInt($("#prev_bal_fee").val());
            var received_fee = parseInt($("#received_fee").val());
            var bal_fee = 0;
            var extra_fee = 0;
            var total_fee_amount=(est_amount)-(prev_extra_amount) - (discount_amount) + (prev_bal_amount);
        
            if(total_fee_amount > received_fee){
                bal_fee=total_fee_amount - received_fee;
            }else if(total_fee_amount < received_fee){
                extra_fee= received_fee - total_fee_amount;
            }
        
            
            $("#total_fee_amount").val(total_fee_amount);
            $("#bal_fee").val(bal_fee);
            $("#extra_fee").val(extra_fee);
            $("#pay_fee").show();
        });
        
        
        $("#pay_fee").click(function(){
            var route_id=$("#route_id").val();
            var fee_id=$("#fee_id").val();
            var est_amount=parseInt($("#estimate_fee").val());
            var discount_amount=parseInt($("#discount").val());
            var prev_extra_amount=parseInt($("#prev_extra_fee").val());
            var prev_bal_amount=parseInt($("#prev_bal_fee").val());
            var received_fee = parseInt($("#received_fee").val());
            var bal_fee = 0;
            var extra_fee = 0;
            var total_fee_amount=(est_amount)-(prev_extra_amount) - (discount_amount) + (prev_bal_amount);
        
            if(total_fee_amount > received_fee){
                bal_fee=total_fee_amount - received_fee;
            }else if(total_fee_amount < received_fee){
                extra_fee= received_fee - total_fee_amount;
            }
            
            var fee_date=$("#fee_date").val();
            var fee_mode=$("#fee_mode").val();
            var fee_note=$("#fee_note").val();
            var transaction_info=$("#transaction_info").val();
            if(total_fee_amount > 0 && received_fee > 0){
                $.ajax({
                    url: '<?php echo base_url();?>Transport/update_fee',
                    type: 'POST',
                    data: {
                        fee_id:fee_id,
                        est_amount:est_amount,
                        prev_extra_amount:prev_extra_amount,
                        prev_bal_amount:prev_bal_amount,
                        discount_amount:discount_amount,
                        received_fee:received_fee,
                        fee_date:fee_date,
                        bal_fee:bal_fee,
                        extra_fee:extra_fee,
                        fee_mode:fee_mode,
                        fee_note:fee_note,
                        transaction_info:transaction_info,
                        route_id:route_id
                    },
                    success: function(data){
                        alert(data);
                        location.reload();
                    }
                });            
            }else{
                alert("No Fee Found")
            }
        });
    });
</script>    