<?php
$sid=$this->uri->segment('3');
$acedemic_session=$this->session->userdata('acedemic_session');
$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"];
    }
}
?>
<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_transport_fee_monthss{
    width:100%;
}
label{
    font-weight:600;
}


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

.w-25{
    width:25%;
    float:left;
    padding:10px;
}
</style>
<div class="container-fluid mt-3 mb-3 bg-white">
    <div class="row">
        <div class="col-sm-12 pt-3 pb-3">
            <h3 class="main-heading">Transport Fee Invoices</h3>
            <div class="mt-3 mb-3 table-responsive" id="get_student_transport_fee_invoices">
                
            </div>
                <form method="post" class="form-outer transport_fee_form">
                    <div>
                        <div class="form-header text-center font-weight-bolder">Pay Transport Fee</div>
                        <hr>
                        <div class="form-group w-100" id="" style="display:inline-block;">
                            <label class="font-weight-bold">Fee Months</label>
                            <div id="list_transport_fee_monthss" class="w-100"></div>
                        </div>
                        
                        <div class="form-group w-25">
                            <label>Route</label>
                            <input type="text" name="route" class="form-control" value="<?php echo $route_title; ?>" readonly>
                            <input type="hidden" name="route_id" value="<?php echo $route_id; ?>" id="route_id">
                        </div>
            
                        <div class="form-group w-25">
                            <label class="">Fee Months</label>
                            <input type="text" name="fee_months" id="transport_fee_months" class="form-control" placeholder="Fee Months" readonly>
                        </div>
                        <div class="form-group w-25">
                            <label class="">Estimate Fee</label>
                            <input type="number" class="form-control " name="transport_etimate_fee" id="transport_estimate_fee" placeholder="Estimate Fee" readonly >
                        </div>
            
                        <div class="form-group w-25">
                            <label>Prev Bal.</label>
                            <input type="number" class="form-control" name="transport_prev_bal" id="transport_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="transport_prev_extra_fee" id="transport_prev_extra_fee" placeholder="Extra Fee" readonly>
                        </div>
            
                        <div class="form-group w-25">
                            <label class="">Discount</label>
                            <input type="number" value="0" class="form-control transport_discount" name="transport_discount" id="transport_discount" placeholder="Discount" >
                        </div>
            
                        <div class="form-group w-25">
                            <label>Total</label>
                            <input type="number" class="form-control" name="transport_total_fee_amount" id="transport_total_fee_amount" placeholder="Total Fee" readonly>
                        </div>
            
                        <div class="form-group w-25">
                            <label>Received Fee</label>
                            <input type="number" class="form-control transport_received_fee" name="transport_received_fee" id="transport_received_fee" placeholder="Received Fee" value="0" >
                        </div>
            
                        <div class="form-group w-25">
                            <label>Balance</label>
                            <input type="number" class="form-control" name="transport_bal_fee" id="transport_bal_fee" placeholder="Balance Fee" readonly>
                        </div>    
            
            
                        <div class="form-group w-25">
                            <label>Extra</label>
                            <input type="number" class="form-control" name="transport_extra_fee" id="transport_extra_fee" placeholder="Extra Fee" readonly>
                        </div>
            
                        <div class="form-group w-25">
                            <label>Fee Date</label>
                            <input type="date" name="fee_date" class="form-control" id="transport_fee_date" value="<?php echo date('Y-m-d');?>" >
                        </div>
            
                        <div class="form-group w-25" >
                            <label>Payment Mode</label>    
                            <select name="payment_mode" id="transport_fee_mode" class="form-control transport_fee_mode">
                                <option value="">Select Pay.Mode</option>    
                                <option value="CASH" selected>Cash</option>
                                <option value="Bank Transfer">Bank Transfer</option>
                                <option value="Cheque">By Cheque</option>
                                <option value="PAYTM">Paytm</option>
                                <option value="UPI">UPI</option>
                            </select>
                        </div>
                        <div class="form-group w-25">
                            <label>Fee Note</label>
                            <input type="text" class="form-control transport_fee_note" placeholder="Fee Note" id="transport_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="Transaction Id / Check or Draft No." id="transport_transaction_info">
                        </div>
                        <div class="w_100" style="display:inline-block;">
                            <div class="form-group w-100">
                                <button class="btn btn-sm btn-success" id="transport_pay_fee" type="button">Pay Fee</button>    
                            </div>
                
                        </div>
                    </div>
                </form>   
                <div class="mt-3 mb-3">
                    <?php // $this->load->view('erp/transport/quick-links'); ?>
                </div>
            
        </div>
    </div>
</div>
        
<script>
// function myFunction(){
//         $("#pay_fee").hide();
//     }
$(document).ready(function(){
   
    getmonths();
    function getmonths(){
        var sid = <?php echo $sid;?>;
        var acedemic_session=<?php echo $acedemic_session; ?>;
        $.ajax({
            url: '<?php echo base_url();?>Transport/get_months/',
            type: 'POST',
            data: {sid:sid,acedemic_session:acedemic_session},
            success: function(data){
                // alert(data);
                $("#list_transport_fee_monthss").html(data);
            }
        });
    }
    
    get_prev_bal();
    function get_prev_bal(){
        var sid=<?php echo $sid;?>;
        var acedemic_session=<?php echo $acedemic_session;?>;
        $.ajax({
           url: '<?php echo base_url();?>Transport/get_prev_balance_fee',
           type: 'POST',
           data: {sid:sid,acedemic_session:acedemic_session},
           success: function(data){
                $("#transport_prev_bal_fee").val(data);
                // alert(data);
           }
        });
        
    }
    
    get_extra_fee();
    function get_extra_fee(){
        var sid=<?php echo $sid;?>;
        var acedemic_session=<?php echo $acedemic_session;?>;
        $.ajax({
           url: '<?php echo base_url();?>Transport/get_prev_extra_fee',
           type: 'POST',
           data: {sid:sid,acedemic_session:acedemic_session},
           success: function(data){
                $("#transport_prev_extra_fee").val(data);
                // alert(data);
           }
        });
        
    }
    
    get_student_transport_fee_invoices();
    function get_student_transport_fee_invoices(){
        var sid=<?php echo $sid;?>;
        var acedemic_session=<?php echo $acedemic_session;?>;
        $.ajax({
           url: '<?php echo base_url();?>Transport/get_student_transport_fee_invoices',
           type: 'POST',
           data: {sid:sid,acedemic_session:acedemic_session},
           success: function(data){
                $("#get_student_transport_fee_invoices").html(data);
                // alert(data);
           }
        });
    }
    
    
    $(document).on("keyup",".transport_discount",function(){
        calculate_transport_fee();
    });
    $(document).on("keyup",".transport_received_fee",function(){
        calculate_transport_fee();
    });
    // $("#calculate_fee").click(function(){
    // $.each($("input[class='transport_fee']:checked"), function(){
    //         calculate_transport_fee();
    // });
    
    // $("input[class='transport_fee']:checked"), function(){
 
    //     calculate_transport_fee();
    // };
    
    $(document).on("click",".transport_fee",function(){
        calculate_transport_fee();
             
        if(this.checked){
                
        }else{
                
        }
    });
    
    function calculate_transport_fee(){
        // get_balance_and_extra_Amount();

        var fee_amount=[];    
        var fee_month = [];
        var fee_amounts=[];    
        var fee_months = [];
        var status_4 =0;
        var status_5= 0;
        var status_6 =0;
        var status_7 =0;
        var status_8 =0;
        var status_9 =0;
        var status_10 =0;
        var status_11 =0;
        var status_12 =0;
        var status_1 =0;
        var status_2 =0;
        var status_3 =0;
        
        // alert(status_9);

        $.each($("input[class='transport_fee']:checked"), function(){
            // calculate_transport_fee();
            fee_amounts.push($(this).val());
            fee_months.push($(this).attr("month"));

            var month_name=$(this).attr("month");
            if(month_name == 'april'){status_4 = 1;}else if(month_name == 'may'){status_5 = 1;}else if(month_name == 'june'){status_6 = 1;}else if(month_name == 'july'){status_7 = 1;}else if(month_name == 'august'){status_8 = 1;}else if(month_name == 'september'){status_9 = 1;}else if(month_name == 'october'){status_10 = 1;}else if(month_name == 'november'){status_11 = 1;}else if(month_name == 'december'){status_12 = 1;}else if(month_name == 'january'){status_1 = 1;}else if(month_name == 'february'){status_2 = 1;}else if(month_name == 'march'){status_3 = 1;}
            
        });
        // alert(status_9);
        var acedemic_session=<?php echo $acedemic_session; ?>;

        // $.ajax({
        //     url: '<?php echo base_url() ?>Fee/get_monthly_structure/',
        //     type: 'POST',
        //     data: {acedemic_session:acedemic_session,course:course,status_4:status_4,status_5:status_5,status_6:status_6,status_7:status_7,status_8:status_8,status_9:status_9,status_10:status_10,status_11:status_11,status_12:status_12,status_1:status_1,status_2:status_2,status_3:status_3},
        //     success: function(data){
        //         // alert(data);
        //         $("#fee_month_data").html(data);
        //     }
        // });
        fee_month = fee_months.join(" , ") ;
        fee_amount = fee_amounts.join(" , ") ;
        var x = fee_month.toString();
        
        var all_fee_months=fee_month.toUpperCase();

        var est_amount = 0;
        for (var i = 0; i < fee_amounts.length; i++) {
            est_amount += fee_amounts[i] << 0;
        }
        
        var discount_amount=parseInt($("#transport_discount").val());
        var prev_extra_amount=parseInt($("#transport_prev_extra_fee").val());
        var prev_bal_amount=parseInt($("#transport_prev_bal_fee").val());
            
        var received_fee = parseInt($("#transport_received_fee").val());
        var bal_fee = 0;
        var extra_fee = 0;
        
        
        // var estimate_fee_amount=parseInt(est_amount);
            
        var total_fee_amount=(est_amount)-(prev_extra_amount) - (discount_amount) + (prev_bal_amount);
            ///total_fee_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;
        }
        
        // alert(total_fee_amount);
        // document.getElementById("fee_months").innerHTML = fee_months;
        $("#transport_fee_months").val(all_fee_months);
        $("#transport_estimate_fee").val(est_amount);
        $("#transport_total_fee_amount").val(total_fee_amount);
        $("#transport_bal_fee").val(bal_fee);
        $("#transport_extra_fee").val(extra_fee);
        $("#transport_pay_fee").show();
    // });
    }
    
    $("#transport_pay_fee").click(function(){
        var fee_amount=[];    
        var fee_month = [];
        var fee_amounts=[];    
        var fee_months = [];
        var status_4 =0;
        var status_5= 0;
        var status_6 =0;
        var status_7 =0;
        var status_8 =0;
        var status_9 =0;
        var status_10 =0;
        var status_11 =0;
        var status_12 =0;
        var status_1 =0;
        var status_2 =0;
        var status_3 =0;
        
        

        $.each($("input[class='transport_fee']:checked"), function(){
            fee_amounts.push($(this).val());
            fee_months.push($(this).attr("month"));

            var month_name=$(this).attr("month");
            if(month_name == 'april'){status_4 = 1;}else if(month_name == 'may'){status_5 = 1;}else if(month_name == 'june'){status_6 = 1;}else if(month_name == 'july'){status_7 = 1;}else if(month_name == 'august'){status_8 = 1;}else if(month_name == 'september'){status_9 = 1;}else if(month_name == 'october'){status_10 = 1;}else if(month_name == 'november'){status_11 = 1;}else if(month_name == 'december'){status_12 = 1;}else if(month_name == 'january'){status_1 = 1;}else if(month_name == 'february'){status_2 = 1;}else if(month_name == 'march'){status_3 = 1;}

        });
    
        var acedemic_session=<?php echo $acedemic_session; ?>;
        fee_month = fee_months.join(" , ") ;
        fee_amount = fee_amounts.join(" , ") ;
        var x = fee_month.toString();
        
        var all_fee_months=fee_month.toUpperCase();

        var est_amount = 0;
        for (var i = 0; i < fee_amounts.length; i++) {
            est_amount += fee_amounts[i] << 0;
        }
        
        var discount_amount=parseInt($("#transport_discount").val());
        var prev_extra_amount=parseInt($("#transport_prev_extra_fee").val());
        var prev_bal_amount=parseInt($("#transport_prev_bal_fee").val());
            
        var received_fee = parseInt($("#transport_received_fee").val());
        var fee_date=$("#transport_fee_date").val();
        
        var fee_mode =$("#transport_fee_mode").val();
        var fee_note =$("#transport_fee_note").val();
        var transaction_info =$("#transport_transaction_info").val();
        var route_id=$("#route_id").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 sid=<?php echo $sid;?>;
        var acedemic_session=<?php echo $acedemic_session;?>;
        if(total_fee_amount > 0 && received_fee > 0){
            $.ajax({
                url: '<?php echo base_url();?>Transport/pay_fee',
                type: 'POST',
                data: {
                    sid:sid,
                    acedemic_session:acedemic_session,
                    all_fee_months:all_fee_months,
                    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,
                    status_4:status_4,
                    status_5:status_5,
                    status_6:status_6,
                    status_7:status_7,
                    status_8:status_8,
                    status_9:status_9,
                    status_10:status_10,
                    status_11:status_11,
                    status_12:status_12,
                    status_1:status_1,
                    status_2:status_2,
                    status_3:status_3,
                    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")
        }
    });

        $(document).on("click",".delete-transport-invoice",function(){
            var invoice_id=$(this).attr("invoice_id");
            // alert(invoice_id);
            
            $.ajax({
                url: '<?php echo base_url(); ?>Transport/delete_invoice',
                type: 'POST',
                data: {invoice_id:invoice_id},
                success: function(response){
                    // alert(response);
                    var success_data = JSON.parse(response);
                    var error=success_data.error;
                    // alert(error);
                    if(error == false){
                        // var order_id=success_data.order_id;
                        var message=success_data.msg;
                        alert(message);
                        location.reload();
                        // $(".case-footer-btns").html(case_footer_data);
                        // $(".case-message-div").html("<p class='text-center text-success font-weight-bolder'>"+message+"</p>");
                    }else{
                        var message=success_data.msg;
                        alert(message);
                        location.reload();
                        // var case_footer_data='<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>';
                        // $(".case-footer-btns").html(case_footer_data);
                        // $(".case-message-div").html("<p class='text-center text-danger font-weight-bolder'>"+message+"</p>");
                    }
                },
                // error: function(jqXHR, textStatus, errorThrown) {
                //     console.log(textStatus, errorThrown);
                // }
            });
        });
});
</script>