<style>

.pay-fee-form{
    width:100%;
    box-shadow:0px 0px 5px silver;
    padding:10px 15px;
}

.form-outer-1{
    width:100%;
    display:flex;
    flex-direction:row;
    flex-wrap:wrap;
    box-shadow:0px 0px 5px silver;
}

.update_subject_stream_form{
    width:100%;
    display:flex;
    flex-direction:row;
    flex-wrap:wrap;
    box-shadow:0px 0px 5px silver;
    
}
.form-group{
    margin-top:0px !important;
    margin-bottom:0px !important;
}
.main-heading-1{
    text-align:center;
    padding:12px 0px;
}

.w_50{
    width:50%;
        float:left;
        padding:10px;
}
    #form1{
        margin-bottom:20px;
        display: inline-block;
        border-radius:5px;
    }
    /*.wht{padding:10px;box-shadow:0 0 5px silver;height:auto;height:200px;}*/
    /*.wht-1{padding:10px;box-shadow:0 0 5px silver;height:auto;height:100px;}*/
    /*.wht, .wht-1{padding:10px;}*/
    
    .w_100{
        width:100%;
        padding:10px;
    }
    .wft{
        width:25%;
        float:left;
        padding:10px;
        
    }
    .fee-sec{
    
        /*padding:15px;*/
        /*height:100%;*/
        width:100%;
        /*position:relative;*/
        display:flex;
        flex-direction:row;
        flex-wrap:wrap;
    }
    .w-25{
        width:25%;
        float:left !importat;
    }
    @media only screen and (max-width:767px){
        .wft{width:100%;float:left;padding:10px;}   
        .w-25{
            width:100%;
        }
    }


    /* 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);
    }



</style>

<style>
body{background:#EAEDED;}
.fee-portal{background:#fff;}
h3{font-weight:600; font-size:18px;color:#FF5030;border-bottom:2px solid #FF5030;}
a:hover{text-decoration:none;}
a{color:black;}
</style>
<?php
$sid=$this->uri->segment('3');
$acedemic_session=$this->session->userdata('acedemic_session');
                    
$prev_ac_session="";
$ac_sess_table='academic_session_details';
$get_active_session_info=$this->NewFeeModel->get_item_data($acedemic_session,$ac_sess_table);
if($get_active_session_info){
    foreach($get_active_session_info as $ac_sess_data){
        $prev_ac_session=$ac_sess_data['prev_ac_session'];
    }
}
?>
<div class="container-fluid fee-portal bg-white mt-3 mb-3">
    <div class="row">
        <div class="col-sm-12 pt-3">
    
            <ul class="breadcrumb">
                
                <li class="breadcrumb-item"><a href="<?php echo base_url(); ?>Welcome">Home</a></li> 
                <li class="breadcrumb-item"><a href="<?php echo base_url(); ?>Fee/all_students">All Student</a></li> 
                <li class="breadcrumb-item">Pay Fee</li> 
                <li class="breadcrumb-item"><?php echo $this->session->userdata('ac_year'); ?></li> 
            </ul> 

    
            <div class="">
                
                <?php
                    
                    $student_course='';
                    $student_course_section='';
                    $new_admission="";
                    $course_id='';
                    // $acedemic_session=2;
                    
                    $get_students_current_course_data=$this->NewFeeModel->get_studdents_current_course_data($sid,$acedemic_session);
                    if($get_students_current_course_data){
                        foreach($get_students_current_course_data as $student_data){
                            $course_id=$student_data['course_id'];
                            $student_course=$student_data['class'];
                            $student_course_section=$student_data['section'];
                            $new_admission=$student_data["new_admission"];
                        }
                    }

                    $table='student';
                    $get_student_data=$this->NewFeeModel->get_item_data($sid,$table);
                    if($get_student_data){
                        foreach($get_student_data as $data){
                            // $student_course=$data['class'];

                ?>
                <form class="form-outer-1">
                    <div class="form-group wft">
                        <label>Student Name</label>
                        <input type="hidden" id="sch_no" value="<?php echo $data['sch_no'];?>">
                        <input type="hidden" id="sid" value="<?php echo $data['id'];?>">
                        <input type="text" class="form-control" value="<?php echo $data['student_name'];?>" readonly>
                    </div>
                    <div class="form-group wft">
                        <label>Father's Name</label>
                        <input type="text" class="form-control" value="<?php echo $data['father_name'];?>" readonly>
                    </div>
                    <div class="form-group wft">
                        <label>Contact Number</label>
                        <input type="text" class="form-control" value="<?php echo $data['contact_no'];?>" readonly>
                    </div>
                    <div class="form-group wft">
                        <label>Class</label>
                        <?php
                        
                        $com_class_info="";
                        $courses_table='courses';
                        $com_class_info .=$student_course;
                        if($student_course_section !=""){
                            $com_class_info .= "-".$student_course_section; 
                        }
                        $get_course_stream=$this->NewFeeModel->get_item_data($course_id,$courses_table);
                        if($get_course_stream){
                            foreach($get_course_stream as $subject_stream){
                                if($subject_stream['subject_stream'] !='' ){
                                    $com_class_info .= " - ".$subject_stream['subject_stream']; 
                                }
                            }
                        }
                        ?>
                        <input type="text" class="form-control" value="<?php echo $com_class_info; ?>" readonly>
                    </div>
                </form>
                    <?php
                        }
                    }
                    ?>
                
            </div>
        </div>
    </div>
<!--</div>-->
<?php
if($acedemic_session <=0){
    

    $check_opening_balance=$this->OpeningBalanceModel->check_opening_balancce($sid);
    if($check_opening_balance){
        foreach($check_opening_balance as $opening_balance){
?>
<!--<div class="container-fluid bg-white">-->
    <div class="row">
        <div class="col-sm-12 pt-3 pb-3">
            <div class="mt-3 mb-3">
                <h2 class="text-center main-heading">Opening Balance</h2> 
            </div>
            <hr>
            
            <div class="table-responsive">
                <table class="table table-bordered">
                    <tr>
                        <th>2020-2021 Opening Balance Amount :</th>
                        <td><?php echo $opening_balance['opening_fee_amount'];?></td>
                    </tr>
                    <tr>    
                        <th>Opening Balance Note :</th>
                        <td><?php echo $opening_balance['fee_note'];?></td>
                    </tr>
                    <tr>
                        <td>
                            <a href="<?php echo base_url();?>Fee/pay_opening_balance/<?php echo $sid;?>" class="btn btn-sm btn-primary">Pay Opening Balance</a> 
                        </td>
                        <td>
                            <a href="<?php echo base_url();?>Fee/edit_opening_balance/<?php echo $sid;?>" class="btn btn-sm btn-info">Edit Opening Balance</a>
                        </td>
                    </tr>
                </table>
            </div>
        </div>
    </div>
<!--</div>-->
<?php
    $this->load->view('erp/new-fee/opening-fee-invoices');
        }
    }else{
?>
    <div class="row">
        <div class="col-sm-12 pb-3 pt-3">
            <form method="post">
                <div clss="form-group">
                    <label>Fee Amount </label>
                    <input type="hidden" name="sid" class="form-control" value="<?php echo $sid;?>">
                    <input type="number" class="form-control" name="opening_fee_amount" placeholder="Opening Fee Amount" value="">
                </div>    
                <div class="form-group">
                    <label>Fee Note</label>
                    <input type="text" class="form-control" name="fee_note" placeholder="Fee Note" value="">
                </div>
    
                <div class="form-group">
                    <label>Fee for acedemic session</label>
                    <select class="form-control" name="fee_for_ac_sess" required>
                        <option value="">-- select acedemic session --</option>
                        <?php
                        $acedemic_session_table='academic_session_details';
                        $get_all_ac_session_data=$this->WelcomeModel->get_data($acedemic_session_table);
                        if($get_all_ac_session_data){
                            foreach($get_all_ac_session_data as $ac_session_data){
                        ?>
                        <option value="<?php echo $ac_session_data['id'];?>"><?php echo $ac_session_data['year_long'];?></option>
                        <?php
                            }
                        }
                        ?>
            
                    </select>
                </div>
    
                <div class="form-group">
                    <input type="submit" name="opening_balance_submit" class="btn btn-sm btn-success" value="Add opening balance">
                </div>

            </form>    
            <?php
                    }
            ?>
        </div>
    </div>
    <?php
    }
    ?>
    
    <div class="row">
        <div class="col-sm-12 ">
  
            <div class="pb-3 table-responsive prev-year-balance-table" >
               
                
            </div>
                <?php
                $courses_table='courses';
                $get_courses=$this->NewFeeModel->get_data($courses_table);
                    // if($student_course == 9 || $student_course == 10 || $student_course == 11 || $student_course ==12){
                ?>

                <div class="update_subject_stream">
                    
                    <form method="post" class="update_subject_stream_form">
                        <h3 class="w_100">Update Course Data</h3>
                        
                        <?php
                        if($success1){
                        ?>
                        <div class="alert alert-success w_100"><?php echo $success1;?></div>
                        <?php
                        }
                        if($error1){
                        ?>
                        <div class="alert alert-danger w_100"><?php echo $error1;?></div>
                        <?php
                        }
        
                        ?>
                        <div class="form-group w_50">
                            <label>Course</label>
                            <input type="hidden" name="sid" value="<?php echo $sid;?>">
                            <input type="hidden" name="course" value="<?php echo $student_course; ?>">
                            <select class="form-control" name="course_id">
                                <option value="">-- select course --</option>
                                <?php 
                                if($get_courses){
                                    foreach($get_courses as $courses){
                                        $course=$courses['course'];
                                        // if($course == 9 || $course == 10 || $course == 11 || $course == 12){
                                ?>
                                <option value="<?php echo $courses['id'];?>" <?php if($courses['id'] == $course_id){echo "selected";}?>><?php echo $courses['course']; echo " "; echo $courses['subject_stream'];?></option>
                                <?php
                                        // }
                                    }
                                }
                                ?>
                            </select>
                        </div>
                        <div class="form-group w_50">
                            <label class="text-white " style="display:inline-block;width:100%;">Course</label>
                            <input type="submit" name="update_course_id" class="btn btn-success" value="Update Course">
                        </div>
                    </form>
                </div>

                <?php
                // }
                ?>
                <div class="table-responsive mt-3  mb-3" id="">
                    <table class="table table-bordered table-striped">
                        <thead>
                            <tr>
                                <th>Invoice No. </th>
                                <th>Sch No</th>
                                <th>Bill Book No</th>
                                <th>Est. Fee</th>
                                <th>Discount</th>
                                <th>Received</th>
                                <th>Extra</th>
                                <th>Balance</th>
                                <th>Fee  Months</th>
                                <th>No of Months</th>
                                <th>Fee Type</th>
                                <th>Date</th>
                                <!--<th>Invoices</th>-->
                                <th>Invoices</th>
                                <th>Edit</th>
                                <?php 
                                if($this->session->userdata('delete_fee_invoice') ==1){
                                ?>
                                <th class="text-danger text-center">
                                <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-trash-fill" viewBox="0 0 16 16">
                                    <path d="M2.5 1a1 1 0 0 0-1 1v1a1 1 0 0 0 1 1H3v9a2 2 0 0 0 2 2h6a2 2 0 0 0 2-2V4h.5a1 1 0 0 0 1-1V2a1 1 0 0 0-1-1H10a1 1 0 0 0-1-1H7a1 1 0 0 0-1 1H2.5zm3 4a.5.5 0 0 1 .5.5v7a.5.5 0 0 1-1 0v-7a.5.5 0 0 1 .5-.5zM8 5a.5.5 0 0 1 .5.5v7a.5.5 0 0 1-1 0v-7A.5.5 0 0 1 8 5zm3 .5v7a.5.5 0 0 1-1 0v-7a.5.5 0 0 1 1 0z"/>
                                </svg>
                                </th>
                                <?php
                                }
                                ?>
                            </tr>    
                        </thead>    
                        <tbody id="list_invoice">
                            
                        </tbody>
                        <tfoot id="ListBalanceFeeInvoice">
                            
                        </tfoot>
                    </table>    
                </div>


              
                
                <?php 
                
                if($get_active_session_info){
                    foreach($get_active_session_info as $ac_sess_info){
                ?>
                <h3 class="pb-3">Monthly fee for the session <?php echo $ac_sess_info['year_long'];?> &nbsp;
                    <span id="pay_fee_display_btn" class="btn btn-sm btn-danger">Pay Fee</span>
                    <!--<span id="pay_balance_fee_display_btn" class="btn btn-sm btn-info text-white" val="">Pay Balance Fee () </span>-->
                    <span class="btn btn-sm btn-warning"><a href="<?php echo base_url();?>Fee/all_students">Pay New</a></span>
                </h3>
            <?php
                }
            }
            ?>

            <div class=" mt-3 mb-3">
                <form id="form1" class="pay-fee-form" method="POST" action="#">
                    <h4 class="main-heading-1">Pay Fee</h4><hr>
                    <div class="fee-sec">
                        
                            <div class="form-group w_100" id="list-months">
                                <label>Fee For Months</label>
                                <div class="" id="list_monthss">
    
                                </div>
                            </div>
                            
                            


                            <div class="table-responsive w_100">
                                <table class="table table-bordered table-striped">
                                    <thead>

                                    </thead>
                                    <tbody id="fee_month_data">
    
                                    </tbody>
                                </table>
                            </div>  
                            
                            <div class="form-group w_100">
                                <label style="width:100%;">Fee Months</label>
                                <input type="hidden" value="Monthly" id="fee_type" name="fee_type">
                                <!--<input type="text" class="form-control" id="fee_months" readonly>-->
                                <div id="fee-monthss" style="text-transform:uppercase;">
                                    <div id="fee_months">
                                    
                                    </div>
                                </div>
                            </div>
                            
                            
                            
                            <!--<div class="form-group wft">-->
                            <!--    <label>Previous Opening Balance(20-21)</label>-->
                            <!--    <input type="number" name="previous_opening_balance" class="form-control" placeholder="" value="0" readonly>-->
                            <!--</div>-->
                            
                            <!--<div class="form-group wft">-->
                            <!--    <label>Opening Balance</label>-->
                            <!--    <input type="number" name="opening_balance" class="form-control" placeholder="" value="0" readonly>-->
                            <!--</div>-->

                            <div class="form-group wft">
                                <label>Previous Balance</label>
                                <input type="number" class="form-control previous_balance" id="previous_balance" placeholder="Previous Balance" readonly>
                            </div>
                            
                            <div class="form-group wft">
                                <label>Previous Extra Amount</label>
                                <input type="number" class="form-control" placeholder="" id="previous_extra_amount" readonly>
                            </div>
                            
                            <div class="form-group wft">
                                <label>Estimate Fee<span id="sum_amount"></span></label>
                                <input type="number" val="" id="est_amount" readonly class="form-control" >
                            </div>

                            
                            <div class="form-group wft">
                                <label>Discount Type</label>
                                <select name="discount_type" id="discount_type" class="form-control discount_type" >
                                    <!--onchange='myFunction()'-->
                                    <option value="" >Select Discount Type.</option>
                                    <option value="0" <?php echo "selected";?>>No Discount</option>
                                    <option value="inrs">Discount in Rs.</option>
                                    <option value="inps">Discount in Percent.</option>
                                </select>
                            </div>
                            <div class="form-group wft">
                                <label>Discount</label>
                                <input type="number" name="discount" placeholder="value" id="discount_value" class="form-control discount_value" > 
                                <!--onclick='myFunction()'-->
                            </div>



                            <div class="form-group wft">
                                <label>Total Amount</label>
                                <input type="number" class="form-control total" id="total" readonly>
                            </div>

                            <div class="form-group wft">
                                <label>Received Amount</label>    
                                <input type="number" class="form-control received_amount" id="received_amount" placeholder="Received Amount" >
                                
                                <!--onclick='myFunction()'-->
                            </div>



                            <div class="form-group wft">
                                <label>Balance Amount</label>
                                <input type="number" class="form-control new_balance_amount" id="new_balance_amount" placeholder="Balance Amount" readonly >
                            </div>

                            <div class="form-group wft">
                                <label>Extra Amount</label>
                                <input type="number" class="form-control new_extra_amount" id="new_extra_amount" placeholder="Extra Amount" readonly>
                            </div>
                            
                            <div class="form-group wft" >
                                <label>Payment Mode</label>    
                                <select name="paymentmode" id="fee_mode" class="form-control">
                                    <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 wft">
                                <label>Trans. Id / Check / Draft No.</label>
                                <input type="text" class="form-control mb-1" placeholder="TransactionId/Check or Draft No." id="transaction_info" onclick='myFunction()'>
                            </div>
                            
                            
                            <div class="form-group wft">
                                <label>Fee Note</label>
                                <input type="text" class="form-control hide_submit_btn" placeholder="Fee Note" id="fee_note">
                            </div>
                            
                            
                            <div class="form-group wft">
                                <label>Bill Book No.</label>
                                <input type="text" class="form-control hide_submit_btn" value="" id="bill-book-no" placeholder="Bill Book No" >
                            </div>

                            <div class="form-group wft">
                                <label>Fee Date</label>
                                <?php 
                                $tid=$this->session->userdata('tid');
                                $staff_table='school_staff';
                                $fee_edit_permission='';
                                $get_stff_info=$this->NewFeeModel->get_item_data($tid,$staff_table);
                                // if($get_stff_info){
                                //     foreach($get_stff_info as $staff_info){
                                //         $fee_edit_permission=$staff_info['fee_edit_permission'];
                                //     }
                                // }
                                ?>
                                <input type="date" class="form-control hide_submit_btn" id="fee_date" onclick='myFunction()' value="<?php echo date('Y-m-d'); ?>" <?php if($this->session->userdata('back_date_fee') == 0){ echo "readonly"; }else{}  ?>>
                            </div>


                            
                            
                            <div class="w_100" >
                                <!--<button type="button" id="getValue" class="btn btn-info btn-sm">Calculate Fee</button>-->
                                <button type="button" class="btn btn-sm btn-success" id="fee_submit">Submit</button>    
                            </div>
                            
                        
                    </div>            
                </form>
            </div>

        </div>
    </div>
</div>

<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<script>
    $(document).ready(function(){
        opening_balance_data();
        function opening_balance_data(){
            // 
            var sid = '<?php echo $sid;?>';
            var acedemic_session='<?php echo $prev_ac_session; ?>';
            var fee_type='Monthly';
            
            $.ajax({

                url: '<?php echo base_url(); ?>Fee/academic_year_fee_details/',
                type: 'POST',
                data: {acedemic_session:acedemic_session,sid:sid,fee_type:fee_type},
                success: function(response){
                    // $("#previous_balance").html(data);
                    var success_data = JSON.parse(response);
                    var error=success_data.error;
                    var estimate_fee=success_data.estimate_fee;
                    let received=success_data.received;
                    let discount=success_data.discount;
                    let balance=parseInt(success_data.balance);
                    let extra=parseInt(success_data.extra);
                    var due_months=success_data.due_months;
                    
                    if(balance>0 || extra >0){
                        var opening_balance_data=' <table class="table table-bordered table-striped mb-3"><thead><tr><th class="text-center text-danger" colspan="2">Previous Academic Year Balance</th></tr><tr><th>Balance Amount</th><td class="text-danger">'+balance+'</td></tr><tr><th>Months</th><td class="text-danger">'+due_months+'</td></tr><tr><th>Extra Fee</th><td>'+extra+'</td></tr></thead></table>';
                        $(".prev-year-balance-table").html(opening_balance_data);
                    }else{
                        // alert(success_data);
                    }
                    
                    // $("#previous_extra_amount").val(extra);
                    // $("#previous_balance").val(balance);
                }
            });
        }
        
        $(document).on("keyup",".discount_type",function(){
            calculate_fee_data();    
        });
        
        $(document).on("keyup",".discount_value",function(){
            calculate_fee_data();    
        });
        
        $(document).on("keyup",".total",function(){
            calculate_fee_data();    
        });
        
        $(document).on("keyup",".received_amount",function(){
            calculate_fee_data();    
        });
        
        $(document).on("click",".fee",function(){
            //  $('.fee').on('click',function(){
             calculate_fee_data();
             
            if(this.checked){
                // calculate_fee_data();
                // $('.checkbox').each(function(){
                //     this.checked = true;
                // });
            }else{
                // $('.checkbox').each(function(){
                //     this.checked = false;
                // });
                // calculate_fee_data();
            }
        });
        
        
        $("#form1").hide(); 
        // hide_submit_btn();
        function hide_submit_btn(){
            // $("#fee_submit").hide();     
        }
    
   
        $("#pay_balance_fee_display_btn").click(function(){
        //   alert("Ok");
            var sid_url = <?php echo $sid;?>;
            var acedemic_session =<?php echo $acedemic_session; ?>;
            $.ajax({
                url: '<?php echo base_url() ?>Fee/get_balance_fee_invoices/',
                type: 'POST', 
                data: {sid_url:sid_url,acedemic_session:acedemic_session},
                success: function(data){
                    // $("#list_invoice").html(data);
                    alert(data);
                }
            });
    
        });
    
        $("#bill-book-no").click(function(){
            // hide_submit_btn();
            
            calculate_fee_data();
        });


        $("input[class='fee']:checked"), function(){
            // $("#fee_submit").hide(); 
            // calculate_fee_data();
            // alert( "Handler called." );
            calculate_fee_data();
        };



        $("#pay_fee_display_btn").click(function () {
            $("#form1").show(); 
            get_balance_and_extra_Amount();
        });    
 
        loadallfeeinvoices();
        function loadallfeeinvoices(){
            var sid_url = <?php echo $sid;?>;
            var acedemic_session =<?php echo $acedemic_session; ?>;
            $.ajax({
                url: '<?php echo base_url() ?>Fee/getallfeeinvoices/',
                type: 'POST', 
                data: {sid_url:sid_url,acedemic_session:acedemic_session},
                success: function(data){
                    $("#list_invoice").html(data);
                }
            });
        }


        getmonths();
        function getmonths(){
            var sid = <?php echo $sid;?>;
            var course='<?php echo $course_id; ?>';
            var acedemic_session=<?php echo $acedemic_session; ?>;
            $.ajax({
                url: '<?php echo base_url() ?>Fee/get_months/',
                type: 'POST',
                data: {sid:sid,acedemic_session:acedemic_session,course:course},
                success: function(data){
                    // alert(data);
                    $("#list_monthss").html(data);
                }
            });
        }


    get_balance_and_extra_Amount();
    function get_balance_and_extra_Amount(){
    
        var sid = <?php echo $sid;?>;
        var acedemic_session=<?php echo $acedemic_session; ?>;
        var fee_type='Monthly';
        var course_id='<?php echo $course_id; ?>';
        var new_admission='<?php echo $new_admission; ?>';
        
        
        // alert(sid);
        // $.ajax({
        
        //     url: '<?php echo base_url(); ?>Fee/get_balance_amount/',
        //     type: 'POST',
        //     data: {acedemic_session:acedemic_session,sid:sid,fee_type:fee_type},
        //     success: function(data){
        //         $("#previous_balance").val(data);
        //         // alert(data);
        //     }
        // }); 

        // $.ajax({

        //     url: '<?php echo base_url(); ?>Fee/get_extra_amount/',
        //     type: 'POST',
        //     data: {acedemic_session:acedemic_session,sid:sid,fee_type:fee_type},
        //     success: function(data){
        //         $("#previous_extra_amount").val(data);
        //     }
        // }); 

        
        $.ajax({

            url: '<?php echo base_url(); ?>Fee/get_balance_amount_new/',
            type: 'POST',
            data: {acedemic_session:acedemic_session,sid:sid,fee_type:fee_type,course_id:course_id,new_admission:new_admission},
            success: function(response){
                // $("#previous_balance").html(data);
                var success_data = JSON.parse(response);
                var error=success_data.error;
                var estimate_fee=success_data.estimate_fee;
                let received=success_data.received;
                let discount=success_data.discount;
                let balance=parseInt(success_data.balance);
                let extra=success_data.extra;
                let last_invoice_id=success_data.last_invoice_id;
                $("#previous_extra_amount").val(extra);
                $("#previous_balance").val(balance);
                if(balance > 0){
                    var balanceinv_data='<tr><th colspan="13">Balance : '+balance+' <a href="<?php echo base_url(); ?>Fee/pay_balance_fee/'+last_invoice_id+'" class="btn btn-sm btn-outline-warning" target="_blank">Pay Balance Fee</a></th></tr>';
                    $("#ListBalanceFeeInvoice").html(balanceinv_data);
                }
            }
        });
        
        
        


    }


    
    
    function calculate_fee_data(){
        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;


        $.each($("input[class='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 course='<?php echo $course_id; ?>';
        var acedemic_session=<?php echo $acedemic_session; ?>;
        var sid='<?php echo $sid; ?>';
        $.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,sid:sid},
            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 est_amount = 0;
        for (var i = 0; i < fee_amounts.length; i++) {
            est_amount += fee_amounts[i] << 0;
        }

        document.getElementById("fee_months").innerHTML = fee_months;
        $("#est_amount").val(est_amount);
        var discount_amount;
        var discount_type;
        var discount_value;

        var new_balance_amount;
        var new_extra_amount;
        var received_amount;

        var total;

        var previous_extra_amount =parseInt($("#previous_extra_amount").val());
        var previous_balance=parseInt($("#previous_balance").val());

        received_amount=$("#received_amount").val();
        new_balance_amount=$("#new_balance_amount").val();
        new_extra_amount=$("#new_extra_amount").val();


        discount_value = $("#discount_value").val();
        discount_type = $("#discount_type").val();
        // debugger;


        // alert(previous_extra_amount);

        // alert(previous_balance);

        if(discount_type == "inrs"){
    
            if(discount_value <= est_amount){
                discount_amount =  discount_value;    
            }else{
                swal("Please Enter a value less or equal to estimated fee");    
            }
        }else if(discount_type == "inps"){
            if(discount_value > 100){
                swal("Please Enter Discount  value less or max 100 %");       
            }else{
                discount_amount = (est_amount*discount_value)/100;  
            }    
        }else if(discount_type == "0"){
            discount_amount = 0;
        }else{
            discount_amount = 0;
            // swal("Please Select Discount Type");
        }
        total=  (est_amount) - (discount_amount) - (previous_extra_amount) + (previous_balance);
        $("#total").val(total);
        // alert (total);

        if(received_amount < total){
            new_balance_amount = total - received_amount;
        }else{
            new_balance_amount=0;
        }
        if(received_amount > total){
            new_extra_amount =received_amount -total;
        }else{
            new_extra_amount=0;    
        }
        $("#new_balance_amount").val(new_balance_amount);
        $("#new_extra_amount").val(new_extra_amount);




        $("#fee_submit").show();
    }
    
    $("#fee_submit").click(function () {
        var fee_amount=[];    
        var fee_month = [];
        var fee_amounts=[];    
        var fee_months = [];
        var fee_date;
        var bill_book_no;
        var month_name;
        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;
        var received_amount=$("#received_amount").val();
        var previous_extra_amount =parseInt($("#previous_extra_amount").val());

        var previous_balance=parseInt($("#previous_balance").val());

        var fee_note; var fee_mode; var transaction_info;
        // $.each($("#april:checked"), function(){
        // // fee_amounts.push($(this).val());
        // m4 =$(this).attr("month");
        // alert(m4);


        // });
        $.each($("input[class='fee']:checked"), function(){
            fee_amounts.push($(this).val());
            fee_months.push($(this).attr("month"));
            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;}
        });

        fee_month = fee_months.join(" , ") ;
        fee_amount = fee_amounts.join(" , ") ;
        var x = fee_month.toString();


        var est_amount = 0;
        var total_months_no=0;
        for (var i = 0; i < fee_amounts.length; i++) {
            est_amount += fee_amounts[i] << 0;
            total_months_no ++;
        }

        document.getElementById("fee_months").innerHTML = fee_months;
        $("#est_amount").val(est_amount);
        // alert(est_amount);
        var discount_amount;
        var discount_type;
        var discount_value;
        discount_value = $("#discount_value").val();
        discount_type = $("#discount_type").val();
        fee_date = $("#fee_date").val();
        bill_book_no =$("#bill-book-no").val();
        //discount and total calculation 
        var total;
        if(discount_type == "inrs"){
            if(discount_value <= est_amount){
                discount_amount =  discount_value;    
            }else{
                swal("Please Enter a value less or equal to estimated fee");    
            }
        }else if(discount_type == "inps"){
            if(discount_value > 100){
                swal("Please Enter Discount  value less or max 100 %");       
            }else{
                discount_amount = (est_amount*discount_value)/100;  
            }    
        }else if(discount_type == "0"){
            discount_amount = 0;
        }else{
            discount_amount = 0;
        }
        // total=  est_amount - discount_amount;    
        total=  (est_amount) - (discount_amount) - (previous_extra_amount) + (previous_balance);
    
    
    
    
        function discountValidation(){
            if(discount_type == "inrs"){
                if(discount_value <= est_amount){
                    discount_amount =  discount_value;   
                    return 1
                }else{
                    swal("Please Enter a value less or equal to estimated fee");
                    return 0;
                }
            }else if(discount_type == "inps"){
                if(discount_value > 100){
                    swal("Please Enter Discount  value less or max 100 %"); 
                    return 0;
                }else{
                    discount_amount = (est_amount*discount_value)/100;  
                    return 1
                }    
            }else if(discount_type == "0"){
                discount_amount = 0;
                return 1;
            }else{
                discount_amount = 0;
                return 1;
            }    
    
        }    
    
    
    
        function checkselectedMonth() {

            if(fee_months.length > 0){
                // alert("month is ok");
                return 1;
            }else{
                // alert("Select month");
                return 0;
            }

        }    
   
        function fee_date_validity() {
            var d = new Date();
            var current_day=d.getDay();
            var current_month=d.getMonth();

    
            // var fee_day=fee_date.getDay();
            // var fee_month_v=fee_date.getMonth() + 1; 

            if(fee_date =='' ){
                return 0;
            }else{
                return 1;
            }

        }     

    
        fee_note =$("#fee_note").val(); fee_mode = $("#fee_mode").val();
        transaction_info = $("#transaction_info").val();       
    
    
        function check_fee_mode() {

            if(fee_mode.length > 0){
                // alert("fee mode selected");
                return 1;
            }else{
                // alert("Select Fee Mode");
                return 0;
            }

        }        
    
    
        function check_fee_transaction_id() {

            if(fee_mode !="cash" && transaction_info==''){
                // alert("Enter transaction detail");
                return 0;
            }else{
                // alert("transaction detail ok Mode");
                return 1;
            }

        }
        
    
    
    
        var sid;
        var schno;
        var fee_type;

        // sid = $("#sid").val();
        sid=<?php echo $sid;?>;
        schno = $("#sch_no").val();
        fee_type = $("#fee_type").val();
    
 

        var fee_mode_validity;    
        fee_mode_validity =check_fee_mode();




        var trasaction_validity;
        trasaction_validity =check_fee_transaction_id();

        // var fee_months = [];    
        var no_of_months; 
        no_of_months = checkselectedMonth(); 
        var discount_validity;
        discount_validity  =discountValidation();

        fee_date_validity_no =fee_date_validity();
        // fee_date

        var y1;
        var y2;
        var table;

        // y1=2021;
        // y2=2022;

        var acedemic_session=<?php echo $acedemic_session;?>;

        <?php
        $get_session_data=$this->NewFeeModel->get_item_data($acedemic_session,$table);
        if($get_session_data){
            foreach($get_session_data as $get_data){
        ?>

        y1 ='<?php echo $get_data['y1']; ?>';
        y2 ='<?php echo $get_data['y2']; ?>';

        <?php
        }
        }
        ?>

        var balance_amount =0;
        var extra_amount =0;

        if(total > received_amount){
            balance_amount=total - received_amount;
        }else if(received_amount > total){
            extra_amount=received_amount - total;
        }
        if(received_amount ==''){
            swal("Enter Received Amount");
        }
        table = "student_fee";
        if(no_of_months > 0 & discount_validity >0 & fee_date_validity_no >0 & trasaction_validity >0 & fee_mode_validity >0 && received_amount !=''){
            $.ajax({
                url: '<?php echo base_url() ?>Fee/add_fee',
                type: 'POST',
                data: { sid:sid, schno:schno,total:total,fee_type:fee_type,x:x,y1:y1,y2:y2,table:table,total_months_no:total_months_no,discount_amount:discount_amount,est_amount:est_amount,status_1:status_1,status_2:status_2,status_3:status_3,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,fee_date:fee_date,fee_note:fee_note,fee_mode:fee_mode,transaction_info:transaction_info,bill_book_no:bill_book_no,acedemic_session:acedemic_session,received_amount:received_amount,balance_amount:balance_amount,extra_amount:extra_amount},
                success: function(data){
                    // alert(data);
                    $("#form1").hide();  
                    loadallfeeinvoices();
                    getmonths();
                    swal(data);
                    // swal("Fee Submitted Successfully");
                    // location.reload(); 

                }
            });


        }else if(no_of_months ==  0){
            swal("Select  Fee Month");
        }else if(discount_validity ==  0){
            swal("Select Valid Discount");
        }else if(fee_mode_validity ==0){
            swal("Select Fee Mode");    
        }else if(trasaction_validity ==0){
            swal("Enter Transaction id");    
        }else if(fee_date_validity_no == 0) {   
            swal("Select Fee Date");
        }
    });

});
</script>


<script>
    $(document).ready(function(){
        $(document).on("click","#item_to_trash",function(){
            // alert("Hi");     
            //   var item_id=$(this).
            var item_id = $(this).attr("item_id");
            // alert(item_id);
            $.ajax({
                url: '<?php echo base_url(); ?>Fee/delete_invoice',
                type: 'POST',
                data: {item_id:item_id},
                success: function(data){
                    alert(data);
                    location.reload();
                    
                    // $("#dump_data").html(data);
                }
            });
        });
    });
</script>