<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
<style>
label.error {
    color : red;
}
.np-datepicker {
    border: 1px solid #ced4da;
}
.fee-installment-table .installment_amount input {
    border: 1px solid #ced4da;
    font-size: 13px;
}
</style>
<div class="wraper responsive-width">  
    <main class="" id="main">

        <div class="admin_tempblock">
            <div class="admin_tempsec">
                <div class="admin_sec">
                    <form id="installmentform" action="<?= base_url() ?>admin/insert_update_feeinstallment/<?= $fees_id ?>"  method ="POST">
                        <div class="subsec_sec">
                            <?php if($fees_id == 0) { ?>

                                <div class="subject_l subject_l_full_width" style="width:45%">
                                    <div class="subject_lsec">
                                        <div class="subject_lhead">Create Fee Installment</div>
                                        <div class="subject_lformarea">

                                            <div class="form-group">
                                                <label for="">Select Batch<span class="text-danger font-weight-bold">*</span></label>
                                                <?php //print_r($installment); ?>
                                                <select class="form-control" id="batch_id" name="batch_id" >
                                                    <option selected disabled>Select Batch</option>
                                                    <?php foreach($batch as $cur){ 
                                                        if($cur['id'] == $cid) { ?>
                                                          <option value="<?= $cur['id']; ?>" selected><?= $cur['b_name'] . ' : ' . batchdateFormatTo($cur['b_from'],$cur['b_end']); ?></option>
                                                      <?php   } else { ?>
                                                          <option value="<?= $cur['id']; ?>"> <?= $cur['b_name'] . ' : ' . batchdateFormatTo($cur['b_from'],$cur['b_end']); ?> </option>
                                                      <?php       } 
                                                  }
                                                  ?>
                                              </select>
                                          </div>

                                          <div class="form-group">
                                            <label for="">Select Course<span class="text-danger font-weight-bold">*</span></label>
                                            <select class="form-control" name="ficourse" id="ficourse">
                                                <option selected disbaled value="">Please select batch above</option>
                                            </select>
                                            <p id="ficourse_error" style="color:red;"></p>
                                            <p id="sname_error" style="color:red;"></p>
                                        </div>

                                        <div class="form-group">
                                            <label for="">Select Feetype<span class="text-danger font-weight-bold">*</span></label>
                                            <select class="form-control" name="ft_id" id="ft_id">
                                                <option selected disabled value="">Select Now</option>
                                                <option value="1">Please select course above</option>
                                            </select>
                                        </div>

                                        <div class="hostel-plan form-group" id="hostel_plan_container" style="display:none">
                                            <label for="">Select Hostel Plan<span class="text-danger font-weight-bold">*</span></label>
                                            <select class="form-control" onchange="get_hostel_fee(this)" name="hostel_plan" id="hostel_plan">
                                                <option selected disabled value="">Select Now</option>
                                            </select>
                                        </div>

                                        <div class="fee-fee_value_name form-group" id="fee_value_name_container" style="display:none">
                                            <label for="">Select Fee Name<span class="text-danger font-weight-bold">*</span></label>
                                            <select class="form-control" onchange="get_unique_fee(this)" name="fee_value_name" id="fee_value_name">
                                                <option selected disabled value="">Select Now</option>
                                            </select>
                                        </div>

                                        <div class="due-message mb-4" style="display:none">

                                        </div>

                                        <div class="form-group" style="display:none">
                                            <label for="">Set Due Date</label>
                                            <input type="date" name="fibatch_from" id="fibatch_from" class="form-control"  value="<?php ?>" placeholder="Enter batch start year">
                                            <p id="crbatch_from_error" style="color:red;"></p>
                                            <input type="hidden" name="course-fee_id" id="course_fee_id">
                                        </div>

                                        <div class="form-group">
                                            <label for="">Status<span class="text-danger font-weight-bold">*</span></label>
                                            <select name="is_active" class="form-control">
                                                <option selected disabled value="">Choose section status</option>
                                                <option value="yes">Yes</option>
                                                <option value="no">No</option>
                                            </select>
                                            <label id="is_active-error" class="error" for="is_active" style="display:none"></label>
                                        </div>

                                        <div class="admin_weightagehead">Installment</div>
                                        <div class="form-group">
                                            <label for="">Number of Installment<span class="text-danger font-weight-bold">*</span></label>
                                            <div class="form-group-inner">
                                                <select id="paymentoption" name="paymentoption[]"  class="selectpicker"  multiple>
                                                    <option disabled selected value="">Select the number of installment</option>
                                                    <?php foreach ($payment_types as $key => $value) {
                                                        echo '<option value="'.$value['number_of_payment'].'">'.$value['payment_type_name'].'</option>';
                                                    } ?>
                                                </select>
                                                <button type = "button" id = "emi" class="subject_addbtn add_btn_white" onclick = "get_fees()">Apply</button>
                                            </div>
                                            <label id="paymentoption-error" class="error" for="paymentoption"></label>

                                        </div>

                                        <table class="table  fee-installment-table dataTable table-responsive">
                                            <thead>
                                                <tr>
                                                    <th><span style="width:115px;display:block;">Payment method</span></th>
                                                    <th><span style="width:115px;display:block">Amount</span></th>
                                                    <th><span style="width:115px;display:block">Due dates</span></th>
                                                </tr>
                                            </thead>
                                            <tbody id="installment_section">
                                            </tbody>
                                        </table>
                                        <p id="afee_error" class="mb-2" style="color:red;"></p>
                                        <p id="success" style="color:green;"></p>
                                        <button type="submit" class="subject_addbtn" >Submit</button>
                                        <a href="<?= base_url() ?>admin/fee-installment"><button type="button" class="subject_addbtn bg-dark ml-2">Back</button></a>

                                    </div>
                                </div>

                            <?php } ?>

                            <?php if($fees_id > 0) { ?>

                                <div class="subject_l subject_l_full_width" style="width:45%">
                                    <div class="subject_lsec">
                                        <div class="subject_lhead">Edit Fee Installment</div>
                                        <div class="subject_lformarea">

                                            <div class="form-group">
                                                <label for="">Select Batch<span class="text-danger font-weight-bold">*</span></label>
                                                <?php //print_r($installment); ?>
                                                <select class="form-control" id="batch_id" name="batch_id" >
                                                    <?php foreach($batch as $cur){ 
                                                        if($cur['id'] == $batch_id) { ?>
                                                          <option value="<?= $cur['id']; ?>" selected><?= $cur['b_name'] . ' : ' .batchdateFormatTo($cur['b_from'],$cur['b_end']); ?></option>
                                                      <?php   } else { ?>
                                                          <option value="<?= $cur['id']; ?>"> <?= $cur['b_name'] . ' : ' .batchdateFormatTo($cur['b_from'],$cur['b_end']); ?> </option>
                                                      <?php       } 
                                                  }
                                                  ?>
                                              </select>
                                          </div>

                                          <div class="form-group">
                                            <label for="">Select Course<span class="text-danger font-weight-bold">*</span></label>
                                            <select class="form-control" name="ficourse" id="ficourse">
                                             <?php foreach($course as $cur){ 
                                                if($cur['id'] == $course_id) { ?>
                                                  <option value="<?= $cur['id']; ?>" selected="selected" ><?= $cur['course_name']; ?></option>
                                              <?php   } else { ?>
                                                  <option value="<?= $cur['id']; ?>"> <?= $cur['course_name']; ?> </option>
                                              <?php       } 
                                          }
                                          ?>
                                      </select>
                                      <p id="ficourse_error" style="color:red;"></p>
                                      <p id="sname_error" style="color:red;"></p>
                                  </div>

                                  <div class="form-group">
                                    <label for="">Select Feetype<span class="text-danger font-weight-bold">*</span></label>
                                    <select class="form-control" name="ft_id" id="ft_id">
                                        <?php foreach($fee_types as $cur){ 
                                            if($cur['id'] == $feetype_id) { ?>
                                              <option value="<?= $cur['id']; ?>" selected="selected" ><?= $cur['feetype_name']; ?></option>
                                          <?php   } else { ?>
                                              <option value="<?= $cur['id']; ?>"> <?= $cur['feetype_name']; ?> </option>
                                          <?php       } 
                                      }
                                      ?>
                                  </select>
                              </div>

                              <div class="hostel-plan form-group" id="hostel_plan_container" style="display:none">
                                <label for="">Select Hostel Plan<span class="text-danger font-weight-bold">*</span></label>
                                <select class="form-control" onchange="get_hostel_fee(this)" name="hostel_plan" id="hostel_plan">
                                    <option selected disabled value="">Select Now</option>
                                </select>
                            </div>

                            <div class="fee-fee_value_name form-group" id="fee_value_name_container" style="display:none">
                                <label for="">Select Fee Name<span class="text-danger font-weight-bold">*</span></label>
                                <select class="form-control" onchange="get_unique_fee(this)" name="fee_value_name" id="fee_value_name">
                                    <option selected disabled value="">Select Now</option>
                                </select>
                            </div>

                            <div class="due-message mb-4">
                                <span class="mb-4" style="font-size : 15px">Note:<p> The due-date for selected course fee is <b class="display_edit_due"></b></p></span>

                            </div>

                            <div class="form-group" style="display:none">
                                <label for="">Set Due Date</label>
                                <input type="date" name="fibatch_from" value="<?php echo date('Y-m-d', strtotime($due_date)) ?>" id="fibatch_from" class="form-control"  value="<?php ?>" placeholder="Enter batch start year">
                                <p id="crbatch_from_error" style="color:red;"></p>
                                <input type="hidden" name="course-fee_id" value="<?= $cf_id ?>" id="course_fee_id">
                            </div>

                            <div class="form-group">
                                <label for="">Status<span class="text-danger font-weight-bold">*</span></label>
                                <select name="is_active" class="form-control">
                                    <option selected value="<?php echo $installment[0]['is_active'] ?>"><?php echo ucfirst($installment[0]['is_active']) ?></option>
                                    <?php if($installment[0]['is_active'] == 'yes') { ?>
                                        <option value="no">No</option>
                                    <?php } else {?>
                                        <option value="yes">Yes</option>
                                    <?php } ?>

                                </select>
                                <label id="is_active-error" class="error" for="is_active" style="display:none"></label>
                            </div>

                            <div class="admin_weightagehead">Installment</div>
                            <div class="form-group">
                                <label for="">Number of Installment<span class="text-danger font-weight-bold">*</span></label>
                                <div class="form-group-inner">
                                    <select id="paymentoption" name="paymentoption[]"  class="selectpicker"  multiple>
                                        <option value="" >Select the number of installment</option>
                                        <?php foreach ($payment_types as $key => $value) {
                                            $matched = false;
                                            foreach ($pt_keys as $index => $ele) {
                                                if($ele == $value['number_of_payment']) {
                                                    $matched = true;
                                                } 
                                            }

                                            if($matched) {
                                                echo '<option selected value="'.$value['number_of_payment'].'">'.$value['payment_type_name'].'</option>';
                                            } else {
                                                echo '<option value="'.$value['number_of_payment'].'">'.$value['payment_type_name'].'</option>';
                                            }

                                        } ?>
                                    </select>
                                    <button type = "button" id = "emi" class="subject_addbtn add_btn_white" onclick = "get_fees()">Apply</button>
                                </div>
                                <label id="paymentoption-error" class="error" for="paymentoption"></label>

                            </div>

                            <table class="table fee-installment-table dataTable table-responsive">
                                <thead>
                                    <tr>
                                        <th><span style="width:115px;display:block;">Payment method</span></th>
                                        <th><span style="width:115px;display:block">Amount</span></th>
                                        <th><span style="width:115px;display:block">Due dates</span></th>
                                    </tr>
                                </thead>
                                <tbody id="installment_section">
                                    <?php 

                                    foreach ($due_info as $key => $value) { 

                                        $CI = &get_instance();
                                        $CI->load->model('Admin_model');
                                        $inst_info = $this->db->get_where('payment_types',array('id'=>$key))->row_array();
                                        ?>
                                        <tr>

                                            <td class="installment_pm">
                                                <input type="hidden" readOnly name="payment_method[]" class="mb-2" value="<?= $key ?>">
                                                <?= $inst_info['payment_type_name'] ?>
                                            </td>
                                            <td class="installment_amount">
                                                <?php 
                                                foreach ($value as $index => $ele) { ?>

                                                    <?php if($key == '1'){?>
                                                        <input type="text" name="amount[one]" class="form-control amount-input mb-2" value=<?= $ele->amount ?>>
                                                    <?php } ?>

                                                    <?php if($key == '2'){?>
                                                        <input type="text" name="amount[two][<?= $index ?>]" class="form-control amount-input mb-2" value=<?= $ele->amount ?>>
                                                    <?php } ?>

                                                    <?php if($key == '3'){?>
                                                        <input type="text" name="amount[four][<?= $index ?>]" class="form-control amount-input mb-2" value=<?= $ele->amount ?>>
                                                    <?php } ?>

                                                    <?php if($key == '4'){?>
                                                        <input type="text" name="amount[six][<?= $index ?>]" class="form-control amount-input mb-2" value=<?= $ele->amount ?>>
                                                    <?php } ?>
                                                    
                                                    
                                                <?php }
                                                ?>
                                            </td>
                                            <td class="installment_due">
                                                <?php 
                                                foreach ($value as $index => $ele) { 
                                                    $ele->due_date = date('Y-m-d', strtotime($ele->due_date))
                                                    ?>

                                                    <?php if($key == '1'){?>
                                                        <input type="text" readonly name="duedate[one]" class="form-control np-datepicker mb-2" value=<?= $ele->due_date ?>>
                                                    <?php } ?>

                                                    <?php if($key == '2'){?>
                                                        <input type="text" readonly name="duedate[two][<?= $index ?>]" class="form-control np-datepicker mb-2" value=<?= $ele->due_date ?>>
                                                    <?php } ?>

                                                    <?php if($key == '3'){?>
                                                        <input type="text" readonly name="duedate[four][<?= $index ?>]" class="form-control np-datepicker mb-2" value=<?= $ele->due_date ?>>
                                                    <?php } ?>

                                                    <?php if($key == '4'){?>
                                                        <input type="text" readonly name="duedate[six][<?= $index ?>]" class="form-control np-datepicker mb-2" value=<?= $ele->due_date ?>>
                                                    <?php } ?>
                                                    
                                                    
                                                <?php }
                                                ?>
                                            </td>
                                        </tr>
                                    <?php }

                                    ?>
                                </tbody>
                            </table>
                            <p id="afee_error" class="mb-2" style="color:red;"></p>
                            <p id="success" style="color:green;"></p>
                            <button type="submit" class="subject_addbtn" >Update</button>
                            <a href="<?= base_url() ?>admin/fee-installment"><button type="button" class="subject_addbtn bg-dark ml-2">Back</button></a>
                        </div>
                    </div>

                <?php } ?>

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

<!----admin template section end---->
</main>
<!--End right-top side-->  
</div>
<script defer src="<?php echo base_url(); ?>assets_admin/js/bootstrap-select.min.js"></script>

<script>
    var inst_array = Array();
    function reset_fees()
    {

        $('#installment_section').html('');
        $('#paymentoption').val('');

    }
</script>

<script>
    <?php if($fees_id == 0) { ?>
        var global_fees = {};
    <?php } else { ?>
        var global_fees = {
            amount : Number(<?php echo $due_amount_total ?>),
            due : moment('<?php echo date('Y-m-d', strtotime($due_date)) ?>').format('YYYY-MM-DD')
        };
    <?php } ?>
    
    var selectCount = document.getElementById("paymentoption");
    selectCount.addEventListener("change", numOfCount);

    // Get all the guest forms into an collection
    var selectForms = document.querySelectorAll(".selectForm"); 

    function numOfCount() {
    // Loop through the guest forms based on the number selected
    selectForms.forEach(function(v,i){
        if(i < selectCount.value) {
            v.classList.remove("hidden");
        }
    });
} 

function cleanup_install_val(){
    $('#ficourse').val('');
    $('#fibatch_from').val('');
    $('#paymentoption').val('');
    $('#installment_section').html('');
}

$("#batch_id").change(function () {
    var batch_id = this.value;

    $.ajax({
        type : 'POST',
        url : "<?php echo base_url(); ?>admin/ajax_get_installment_course_batch",
        data : {
            batch : batch_id
        },
        success:function(data){

            $('#ficourse').empty();
            $('#installment_section').empty();
            $('select[name="paymentoption[]"]').val('');
            $('#ft_id').val('');
            $('#ficourse').append(data);

        },
    });

});

$("#ficourse").change(function () {
    var course_id = this.value;
    var batch_id = $('#batch_id').val()

    $.ajax({
        type : 'POST',
        url : "<?php echo base_url(); ?>admin/ajax_get_installment_course_feetype",
        data : {
            course : course_id,
            batch : batch_id
        },
        success:function(data){
            $('#ft_id').empty();
            $('#ft_id').append(data);
            $('#installment_section').empty();
            $('select[name="paymentoption[]"]').val('');
        },
    });

});

function resteValues() {
    $('.due-message').hide();
    $('.due-message').empty();
    global_fees = {};
    $('#hostel_plan_container').hide();
    $('#hostel_plan').val('');
    $('#course_fee_id').val('');
    $('#fibatch_from').val('');
    $('#fee_value_name_container').hide();
    $('#fee_value_name').val('');
}

$("#ft_id").change(function () {
    var ft_id = this.value;
    var hostel_id = 3;
    var course_id = $('#ficourse').val();
    var batch_id = $('#batch_id').val();
    
    resteValues();
    $('#installment_section').empty();
    $('select[name="paymentoption[]"]').val([]);
    $('.due-message').hide();
    $('.due-message').empty();

    $.ajax({
        type : 'POST',
        url : "<?php echo base_url(); ?>admin/ajax_get_installment_course_fee_date_id",
        data : {
            course : course_id,
            batch : batch_id,
            fee_id : ft_id
        },
        success:function(data){
            global_fees = {};
            res = JSON.parse(data);
            console.log(res);
            if(res.length < 2) {

                let x = moment(res[0].due_date).format('YYYY-MM-DD').split('-');
                let [Ryear,Rmonth,Rday] = x;

                Rmonth = Number(Rmonth)-1;
                Rmonth = NepaliFunctions.GetBsMonth(Number(Rmonth))

                global_fees.amount = res[0].amount;
                global_fees.due = moment(res[0].due_date).format('YYYY-MM-DD');

                $('.due-message').show();
                $('.due-message').html('<span class="mb-4" style="font-size : 15px">Note:<p> The due-date for selected course fee is <b>'+ Rday+'-'+Rmonth+'-'+Ryear + ' </b></p></span>');
                $('#course_fee_id').val(res[0].id);
                $('#fibatch_from').val(moment(res[0].due_date).format('YYYY-MM-DD'))
            } else {

                let url_link;

                if(ft_id == hostel_id) {
                    url_link = "<?php echo base_url(); ?>admin/ajax_get_course_fee_hostel_plan";
                } else {
                    url_link = "<?php echo base_url(); ?>admin/ajax_get_course_fee_values";
                }

                $.ajax({
                    type : 'POST',
                    url : url_link,
                    data : {
                        course : course_id,
                        batch : batch_id,
                        fee_id : ft_id
                    },
                    success:function(data){
                        if(ft_id == hostel_id) {
                            $('#hostel_plan').empty();
                            $('#hostel_plan').append(data);
                            $('#hostel_plan_container').show();
                        } else {
                            $('#fee_value_name').empty();
                            $('#fee_value_name_container').show();
                            $('#fee_value_name').append(data);
                        }
                    },
                });
            }
        },
    });
    // }
}); 

const get_unique_fee = function(ele) {

    var course_fee = ele.value;
    $('#installment_section').empty();
    $('select[name="paymentoption[]"]').val([]);
    $('.due-message').hide();
    $('.due-message').empty();

    $.ajax({
        type : 'POST',
        url : "<?php echo base_url(); ?>admin/ajax_get_complete_course_fee_id",
        data : {
            id : course_fee,
        },
        success:function(data){
            global_fees = {};
            res = JSON.parse(data);
            console.log(res);

            let x = moment(res[0].due_date).format('YYYY-MM-DD').split('-');
            let [Ryear,Rmonth,Rday] = x;
            
            Rmonth = Number(Rmonth)-1;
            Rmonth = NepaliFunctions.GetBsMonth(Number(Rmonth))
            


            global_fees.amount = res[0].amount;
            global_fees.due = moment(res[0].due_date).format('YYYY-MM-DD');
            
            $('.due-message').show();
            $('.due-message').html('<span class="mb-4" style="font-size : 15px">Note:<p> The due-date for selected course fee is <b>'+ Rday+'-'+Rmonth+'-'+Ryear + ' </b></p></span>');
            $('#course_fee_id').val(res[0].id);
            $('#fibatch_from').val(moment(res[0].due_date).format('YYYY-MM-DD'))
        },
    });

}

const get_hostel_fee = function(ele) {
    var course_id = $('#ficourse').val();
    var batch_id = $('#batch_id').val();
    var ft_id = $('#ft_id').val();
    var plan = ele.value;

    $('#installment_section').empty();
    $('select[name="paymentoption[]"]').val([]);
    $('.due-message').hide();
    $('.due-message').empty();

    $.ajax({
        type : 'POST',
        url : "<?php echo base_url(); ?>admin/ajax_get_installment_course_fee_date_id",
        data : {
            course : course_id,
            batch : batch_id,
            fee_id : ft_id,
            plan_id : plan
        },
        success:function(data){
            console.log(data);
            global_fees = {};
            res = JSON.parse(data);
            console.log(res);

            global_fees.amount = res[0].amount;
            global_fees.due = moment(res[0].due_date).format('YYYY-MM-DD');

            $('.due-message').show();
            $('.due-message').html('<span class="mb-4" style="font-size : 15px">Note:<p> The due-date for selected course fee is <b>'+ moment(res[0].due_date).format('DD-MMM-YYYY') + ' </b></p></span>');
            $('#course_fee_id').val(res[0].id);
            $('#fibatch_from').val(moment(res[0].due_date).format('YYYY-MM-DD'))
        },
    });
}


function get_fees(){

    var course_fee = $('#course_fee_id').val();
    var start = $('#fibatch_from').val();
    var inst = $('#paymentoption').val();

    if($("#installmentform").valid()) {
     $('#sname_error').html('');
     $.ajax({
        type : 'post',
        url : '<?php echo base_url(); ?>Admin/get_duedates',
        data : { course_fee : course_fee, inst : inst,start:start },
        success:function(data){
            console.log(data);
            data = JSON.parse(data);
            $('#installment_section').html(data.html);
            var mainInput = $(".np-datepicker");
            let NDate = NepaliFunctions.GetCurrentBsDate();
            let todayDate = `${NDate.year}-${NDate.month}-${NDate.day}`;
            // alert(todayDate);
            mainInput.nepaliDatePicker({
                disableBefore: todayDate,
                language : 'english',
            });
        }
    });
 }
}
</script>

<script>
    $(document).ready(function() {
        if($('.np-datepicker').length > 0) {
            let NnDate = NepaliFunctions.GetCurrentBsDate();
            let todayDateN = `${NnDate.year}-${NnDate.month}-${NnDate.day}`;

            $('.np-datepicker').nepaliDatePicker({
                language : 'english',
                disableBefore : todayDateN
            });

            let x = moment(global_fees.due).format('YYYY-MM-DD').split('-');
            let [Ryear,Rmonth,Rday] = x;

            Rmonth = Number(Rmonth)-1;
            Rmonth = NepaliFunctions.GetBsMonth(Number(Rmonth))

            $('.display_edit_due').text(Rday+'-'+Rmonth+'-'+Ryear);
        }
        $("#installmentform").validate({
            ignore: ":hidden",
            rules: {

                batch_id : "required",
                ficourse : "required",
                fibatch_from : "required",
                ft_id : "required",
                'paymentoption[]' : 'required',
                'amount[one]' : 'required',
                <?php for($i = 0; $i <= 8; $i++){ ?>
                    'amount[two][<?= $i ?>]' : 'required',
                    'amount[four][<?= $i ?>]' : 'required',
                    'amount[six][<?= $i ?>]' : 'required',
                <?php } ?>
                
                is_active : 'required',
                fee_value_name : 'required',
                hostel_plan : 'required',
                
            },
            messages: {

                batch_id :  'Please select a batch',
                ficourse :  'Please select a course',
                fibatch_from : 'Please select start date of installments',
                'paymentoption[]' : 'Please select one payment option',
                ft_id : "Please select a fee type",
                hostel_plan : "Please select a hostel plan",
                fee_value_name : "Please select a fee name"
                
            },

            submitHandler: function(form,e) {
                e.preventDefault();
                let amountErr;
                let flag = false;
                let Allrows = $('#installment_section tr');
                $('.installment-err').remove();
                $.each(Allrows, function(index,ele) {
                    let tdTotal = 0;
                    let amountInput = $(this).find('.amount-input');
                    console.log(amountInput);
                    $.each(amountInput, function(index,ele) {
                        console.log(ele);
                        tdTotal += Number($(this).val());
                    });

                    $('.installment-err').hide();
                    if(tdTotal != global_fees.amount) {
                        $(this).before('<tr class="installment-err"><td colspan="3"><label class="error">Entered amount does not match with total fees amount: '+global_fees.amount+'</label></td></tr>')
                        amountErr = false;
                        return false;
                    } else {
                        amountErr = true;
                    }
                    
                });

                
                $.ajax({
                    url : '<?php echo base_url() ?>' + 'admin/ajax_validate_course_installment',
                    data : {
                        value : $('#course_fee_id').val(),
                        id : <?php echo $fees_id ?>
                    },
                    type : 'POST',
                    async : false,
                    success: function(data){
                        $('#afee_error').empty();
                        if(data == 'success'){
                            flag = true
                        } else {
                            $('#afee_error').append('Selected batch, course and fee type already have an installment.')
                            flag = false;
                        }

                    }
                });


                if(flag && amountErr) {
                    form.submit();
                }
            }
        });

        $('#tbl').DataTable( {
            "lengthMenu": [[5,10, 25, 50, -1], [5,10, 25, 50, "All"]]
        } );

        <?php if($fees_id > 0) { ?>

            <?php if($feetype_id == 3) { ?>

                var hostel_plan_id = '<?php echo $hostel_plan_id ?>';

                if((hostel_plan_id)) {
                    $('#hostel_plan_container').show();
                    var ft_id = $('#ft_id').val();
                    var course_id = $('#ficourse').val();
                    var batch_id = $('#batch_id').val();

                    $.ajax({
                        type : 'POST',
                        url : "<?php echo base_url(); ?>admin/ajax_get_course_fee_hostel_plan",
                        data : {
                            course : course_id,
                            batch : batch_id,
                            fee_id : ft_id
                        },
                        success:function(data){
                            console.log(data);
                            $('#hostel_plan').empty();
                            $('#hostel_plan').append(data);
                            $("#hostel_plan").val(hostel_plan_id);
                        },
                    });

                }

            <?php } else { ?>

                var ft_id = $('#ft_id').val();
                var course_id = $('#ficourse').val();
                var batch_id = $('#batch_id').val();

                $.ajax({
                    type : 'POST',
                    url : "<?php echo base_url(); ?>admin/ajax_get_course_fee_values_count_data",
                    data : {
                        course : course_id,
                        batch : batch_id,
                        fee_id : ft_id
                    },
                    success:function(response){
                        response = JSON.parse(response);
                        console.log(response);

                        if(response.count > 1) {
                            $('#fee_value_name_container').show();
                            $('#fee_value_name').empty();
                            $('#fee_value_name').append(response.data);
                            $('#fee_value_name').val('<?php echo $cf_id ?>');
                        }

                    },
                });

            <?php } ?>

            

        <?php } ?>

    })

$('#paymentoption').change(function(event) {
    $("#paymentoption").multiselect('refresh');
});
</script>