<style>
    table.dataTable tfoot th {
        font-weight: 600 !important;
    }

    .form-group .bootstrap-select>.dropdown-toggle {
        padding: 9px 9px !important;
        font-size: 12px !important;
        border: 1px solid #c8c8c8 !important;
        background: #fff !important;
    }

    .error {
        color: red;
        font-size: 12px;
    }

    .custom_tble th:nth-child(1),
    .custom_tble th:nth-child(2),
    .custom_tble td:nth-child(1),
    .custom_tble td:nth-child(2) {
        background: #fff;
        z-index: 1;
    }

    .table_div {
        width: 100%;
    }

    .dataTables_scrollHead,
    .dataTables_scrollBody,
    .dataTables_scrollFoot {
        width: 100% !important;
    }

    th,
    td {
        white-space: nowrap;
    }
</style>
<div class="wraper responsive-width">

    <main class="long-tbl-main" id="main">
        <!-- admin template section -->
        <div class="admin_tempblock">
            <div class="admin_tempsec">
                <div class="admin_sec">

                    <div class="subsec_sec" style="display:block">
                        <div class="subject_lsec dashboard_graph report_sec_graph">

                            <div class="report_sec_cover">
                                <div class="report_sec_left">
                                    <?php
                                    $currency_symbol =  $school_info['currency_symbol'];
                                    $theExportHeader = '';
                                    $today_date_time_array = explode(' ', $today_date);
                                    // $today_date_array = $today_date_time_array[0];
                                    $today_date_array = explode('-', $today_date_time_array[0]);
                                    $current_year = $today_date_array[0];
                                    if (isset($school_info) && !empty($school_info)) {
                                        $theExportHeader = $school_info['school_name'] . '\n'  . '\n Annual Report Details - ' . $current_year;
                                    }

                                    ?>
                                </div>


                                <div class="subject_lhead">Report Details</div>
                                <?php
                                $SearchBatch = (isset($search_batch) && !empty($search_batch)) ? $search_batch : $school_info['batch_id'];
                                $SearchCourse = (isset($search_course_id) && !empty($search_course_id)) ? $search_course_id : '';
                                $SearchSection = (isset($search_section) && !empty($search_section)) ? $search_section : '';
                                $search_feetype = (isset($search_feetype) && !empty($search_feetype)) ? $search_feetype : array();
                                $search_start_paid_date = (isset($search_start_paid_date) && !empty($search_start_paid_date)) ? date('Y-m-d', strtotime($search_start_paid_date)) : '';
                                $search_end_paid_date = (isset($search_end_paid_date) && !empty($search_end_paid_date)) ? date('Y-m-d', strtotime($search_end_paid_date))  : '';
                                ?>
                                <form id="searchFilters" action="<?= base_url(); ?>admin/getAnnualReportDetails" method="POST">

                                    <div class="row">
                                        <div class="col-md-12">
                                            <div class="row">
                                                <div class="col-md-3">
                                                    <div class="subject-filter">
                                                        <label>Select Batch</label>
                                                        <select id="f_batch" name="f_batch" class=" form-control">
                                                            <option value="">Select Batch</option>
                                                            <?php if (isset($batches) || !empty($batches)) {
                                                                foreach ($batches as $row) {
                                                                    $b_selected = ($row['id'] == $SearchBatch) ? 'selected' : '';
                                                            ?>

                                                                    <option value="<?php echo $row['id'] ?>" <?= $b_selected; ?>>
                                                                        <?php echo $row['b_name']; ?></option>

                                                            <?php    }
                                                            } ?>
                                                        </select>
                                                        <div class="text-danger cierr"><?php echo form_error('f_batch'); ?></div>
                                                    </div>
                                                </div>
                                                <div class="col-md-3">
                                                    <div class="subject-filter">
                                                        <label>Select Course</label>
                                                        <select id="f_course" name="f_course" class=" form-control">
                                                            <option value="">Select Course</option>
                                                            <?php if (isset($course) || !empty($course)) {
                                                                foreach ($course as $row) {
                                                                    $cselected = ($row['id'] == $SearchCourse) ? 'selected' : '';
                                                            ?>

                                                                    <option value="<?php echo $row['id'] ?>" <?= $cselected; ?>>
                                                                        <?php echo $row['course_name']; ?></option>

                                                            <?php    }
                                                            } ?>
                                                        </select>
                                                    </div>
                                                </div>
                                                <div class="col-md-3">
                                                    <div class=" form-group subject-filter">
                                                        <label>Select Section</label>
                                                        <?php
                                                        $SearchSection = (!empty($SearchSection)) ? $SearchSection : array();
                                                        if (!empty($SearchSection) && (isset($sections) && !empty($sections))) { ?>
                                                            <select id="f_section" id="f_section" name="f_section[]" class=" form-control selectpicker" multiple>
                                                                <option value="" disabled>Select Section</option>
                                                                <?php
                                                                foreach ($sections as $fs_value) {

                                                                    // $seselected=($fs_value['section_id'] == $SearchSection) ? 'selected' :'';
                                                                ?>
                                                                    <option value="<?= $fs_value['section_id']; ?>" <?php echo (in_array($fs_value['section_id'], $SearchSection)) ? "selected" : ""; ?>><?= $fs_value['section_name'] ?></option>

                                                                <?php
                                                                } ?>
                                                            </select>
                                                        <?php } else { ?>
                                                            <select id="f_section" id="f_section" name="f_section[]" class="form-control selectpicker" multiple>
                                                                <option value="">Select Section</option>

                                                            </select>
                                                        <?php } ?>
                                                    </div>
                                                </div>
                                                <div class="col-md-3">
                                                    <div class="form-group subject-filter">
                                                        <label>Select FeeType</label>

                                                        <select id="f_feetype" name="f_feetype[]" class=" form-control selectpicker" multiple required>
                                                            <option value="" disabled>Select FeeType</option>
                                                            <?php

                                                            $fees_types = (isset($fees_types) && !empty($fees_types)) ? $fees_types : array();
                                                            if ($fees_types != '') {
                                                                foreach ($fees_types as $f_value) {
                                                                    // $f_selected = ($f_value['feetype_name'] == $search_feetype) ? 'selected' : '';
                                                            ?>
                                                                    <option value="<?php echo $f_value['id'] ?>" <?php echo (in_array($f_value['id'], $search_feetype)) ? 'selected' : ''; ?>><?php echo $f_value['feetype_name'] ?> </option>
                                                            <?php }
                                                            } ?>
                                                        </select>
                                                    </div>
                                                </div>
                                            </div>

                                            <div class="row">
                                                <div class="col-md-3">
                                                    <div class="date-filter">
                                                        <div class="date-fil-row">
                                                            <label>Select Start Date</label>
                                                            <input type="text" name="f_start_paid_date" class="form-control w-100 nepal-date" value="<?php echo $search_start_paid_date; ?>" placeholder='dd-mm-yyyy'>
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="col-md-3">
                                                    <div class="date-filter">
                                                        <div class="date-fil-row">
                                                            <label>Select End Date</label>
                                                            <input type="text" name="f_end_paid_date" class="form-control w-100 nepal-date" value="<?php echo $search_end_paid_date; ?>" placeholder='dd-mm-yyyy'>
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="col-md-3">
                                                    <div class="date-filter pt-4"> <input type="submit" name="filtersubmit" id="filter_reset1" class="btn btn-success w-100" value="Filter"></div>
                                                </div>
                                                <div class="col-md-3">
                                                    <div class="date-filter pt-4"><a href="<?php echo base_url() ?>admin/getAnnualReportDetails">
                                                            <div class="date-filter"> <button type="button" name="reset_filter" id="reset_filter" value="reset_filter" class="subject_addbtn subject_addbtn_nobg"><i class="fas fa-redo"></i>Reset</button>
                                                        </a></div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>

                                </form>

                            </div>
                        </div>


                        <!-- new divs added -->
                        <div class="table_div">
                            <!-- new divs added -->
                            <!-- changed ID -->
                            <table id="tbl1" class="custom_tble">
                                <thead>
                                    <tr>
                                        <th>S.No</th>
                                        <th>Roll No</th>
                                        <th>Student Name</th>
                                        <th>Section</th>
                                        <?php
                                        if (isset($selected_fee_list) && !empty($selected_fee_list)) {
                                            foreach ($selected_fee_list as $sf_value) {
                                                // print_r($sf_value);
                                        ?>
                                                <th><?php echo $sf_value['fee_values']; ?></th>
                                        <?php
                                            }
                                        }
                                        ?>


                                        <th>Diduction Amount</th>
                                        <th>Fine Amount</th>
                                        <th>Total Assigned Amount</th>
                                        <th>Paid Amount</th>
                                        <th>Due Amount</th>





                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $tdCnt = 1;
                                    if (isset($studentlist) && !empty($studentlist)) {
                                        // dd($studentlist);
                                        // echo '<pre>';
                                        // print_r($studentlist);

                                        $sno = 1;
                                        $colTotalFeeAmt = array();
                                        foreach ($studentlist as $student) {
                                            // echo '<pre>';
                                            // var_dump($student);
                                            $tdCnt = 4;



                                            $totalPaid = 0;
                                    ?>
                                            <tr>
                                                <td><?php echo $sno; ?></td>
                                                <td><?php echo $student['roll_no'] ?></td>
                                                <td class="text-left">
                                                    <div class="w-100">
                                                        <?php
                                                        echo ucwords($student['name']); ?>
                                                    </div>
                                                </td>
                                                <td><?php echo $student['section_name'] ?></td>
                                                <?php
                                                if (isset($selected_fee_list) && !empty($selected_fee_list)) {
                                                    $rowtotalAmt = 0;
                                                    $rowpaidAmt = 0;
                                                    $rowdidAmt = 0;
                                                    $rowdueAmt = 0;
                                                    $rowFineAmt = 0;
                                                    $totalFeeAmt = array();
                                                    foreach ($selected_fee_list as $sf_key => $sf_value) {
                                                        // print_r($sf_value);



                                                        if (isset($student['fees'][$sf_value['id']])) {

                                                            if ($student['fees'][$sf_value['id']] != 0) {


                                                                array_push($totalFeeAmt, $student['fees'][$sf_value['id']]['paid_amount']);
                                                                // echo '<pre>';
                                                                // dd($student['fees']);
                                                                $rowpaidAmt = ($rowpaidAmt + ($student['fees'][$sf_value['id']]['paid_amount']));
                                                                $rowdidAmt = ($rowdidAmt + ($student['fees'][$sf_value['id']]['deducted_amount']));
                                                                $rowdueAmt = ($rowdueAmt + ($student['fees'][$sf_value['id']]['pending_amount']));
                                                                $rowFineAmt = ($rowFineAmt + ($student['fees'][$sf_value['id']]['fine_amount']));

                                                                $rowtotalAmt = (($rowpaidAmt + $rowdueAmt));

                                                                $paidAmount = $student['fees'][$sf_value['id']]['paid_amount'];
                                                            } else {
                                                                array_push($totalFeeAmt, 0);

                                                                $paidAmount = 0;
                                                            }
                                                        } else {
                                                            array_push($totalFeeAmt, 0);

                                                            $paidAmount = 'N.A';
                                                        }

                                                ?>
                                                        <td><?php echo $paidAmount; ?></td>
                                                <?php
                                                        $tdCnt++;
                                                    }
                                                }

                                                array_push($colTotalFeeAmt, $totalFeeAmt);
                                                ?>


                                                <td><?php echo $rowdidAmt;
                                                    $tdCnt++; ?></td>
                                                <td><?php echo $rowFineAmt;
                                                    $tdCnt++; ?></td>
                                                <td><?php echo $rowtotalAmt;
                                                    $tdCnt++; ?></td>
                                                <td><?php echo $rowpaidAmt;
                                                    $tdCnt++; ?></td>
                                                <td><?php echo $rowdueAmt;
                                                    $tdCnt++; ?></td>



                                            </tr>

                                    <?php $sno++;
                                        }
                                    } ?>

                                <tfoot>
                                    <tr>

                                        <th></th>
                                        <th></th>
                                        <th></th>
                                        <!-- <th></th> -->

                                        <th>In <?php echo $currency_symbol; ?></th>

                                        <?php


                                        // print_r(count($colTotalFeeAmt));

                                        if (isset($selected_fee_list) && !empty($selected_fee_list)) {
                                            foreach ($selected_fee_list as $sfl_key => $sfl_value) {
                                                // $t_fee_amount = 0;

                                                // foreach ($colTotalFeeAmt as $ctfa_key => $ctfa_value) {

                                                //     if ($ctfa_key == $sfl_key) {
                                                //         $t_fee_amount =
                                                //             $t_fee_amount + $ctfa_value[$ctfa_key];
                                                //         echo '<pre>';
                                                //         print_r($ctfa_value[$ctfa_key]);
                                                //     } else {
                                                //         $t_fee_amount = 0;
                                                //     }

                                        ?>
                                                <th style='font-size: 13px; text-align: center;'> </th>
                                        <?php
                                            }
                                        }
                                        ?>

                                        <th class="text-center"></th>
                                        <th class="text-center"></th>
                                        <th class="text-center"></th>
                                        <th class="text-center"></th>
                                        <th class="text-center"></th>
                                    </tr>
                                </tfoot>

                                </tbody>
                            </table>

                        </div>

                    </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>
    $(document).ready(function() {
        $("#searchFilters").validate({
            rules: {
                f_batch: 'required',
                f_course: 'required'
            },
            submitHandler: function(form, e) {
                e.preventDefault();
                form.submit();
            }
        });
    });

    $('#f_course').change(function() {
        var course = $('#f_course option:selected').val();
        // alert(course);
        var divData = '';
        if (course != '' && course != 0) {

            $.ajax({
                url: "<?php echo base_url(); ?>Admin/get_section_by_course",
                method: "POST",
                data: {
                    course_id: course
                },
                dataType: "json",
                success: function(response) {
                    // console.log(response);
                    // section options code and start from here 

                    divData += "<option value='' disabled> Select Section </option>";
                    if (response.sections != '') {
                        $.each(response.sections, function(i, sectiondata) {
                            divData += "<option value='" + sectiondata.section_id + "'>" + sectiondata.section_name + "</option>";
                        });
                    }
                    // $('#course_error_msg').text();
                    else {
                        divData += "<option value=''> -- No Section's Found --</option>";

                    }
                    $('#f_section').empty().append(divData).selectpicker("refresh");

                    // section options code here End from here 

                }
            });
        } else {
            divData = "<option value=''> -- Select Section -- </option>";
            $('#f_section').empty().append(divData);
        }
    });


    $(document).ready(function() {
        var jsTheExportHeader = '<?php echo $theExportHeader; ?>';
        var currency_symbol = "<?php echo $currency_symbol; ?>";
        var theTdCnt = '<?php echo $tdCnt; ?>';
        var theTdStr = new Array();
        for (let n = 0; n < theTdCnt; n++) {
            theTdStr.push(n);
        }

        $('.user-drpdown').click(function() {
            $('.drpdown-items').toggle();
        });


        console.log(theTdStr);

        //table js
        $('#tbl1').DataTable({
            scrollY: screen.availHeight * 0.65,
            scrollX: true,
            scrollCollapse: true,
            paging: false,
            fixedColumns: {
                left: 4,
                right: 2
            },
            "lengthMenu": [
                [100, -1],
                [100, "All"]
            ],
            "dom": 'lBfrtip',
            "buttons": [{
                    extend: 'pdfHtml5',
                    // orientation: 'landscape',
                    exportOptions: {
                        columns: theTdStr,

                    },
                    orientation: 'landscape',
                    pageSize: 'A4'
                    // messageTop: 'The information in this table is copyright to Sirius Cybernetics Corp.'
                },
                {
                    extend: 'csvHtml5',
                    exportOptions: {
                        columns: theTdStr
                    },
                    messageTop: 'The information in this table is copyright to Sirius Cybernetics Corp.'
                },
                {
                    extend: 'excelHtml5',
                    header: true,
                    footer: true,
                    title: jsTheExportHeader,

                    exportOptions: {
                        columns: "thead th:not(.noExport)",
                        rows: function(indx, rowData, domElement) {
                            return $(domElement).css("display") != "none";
                        }
                    },
                    customize: function(xlsx) {
                        var sheet = xlsx.xl.worksheets['sheet1.xml'];

                        $('row c[r^="C"]', sheet).attr('s', '2');
                    }
                }
            ],
            "footerCallback": function(row, data, start, end, display) {
                //var theDynTdCnt = (theTdCnt - 7);
                for (let fi = 4; fi < theTdCnt; fi++) {
                    var api = this.api(),
                        data;

                    // Remove the formatting to get integer data for summation
                    var intVal = function(i) {
                        return typeof i === 'string' ?
                            i.replace(/[\$,]/g, '') * 1 :
                            typeof i === 'number' ?
                            i : 0;
                    };

                    // Total over all pages
                    total = api
                        .column(fi)
                        .data()
                        .reduce(function(a, b) {
                            return intVal(a) + intVal(b);
                        }, 0);

                    // Total over this page
                    pageTotal = api
                        .column(fi, {
                            page: 'current'
                        })
                        .data()
                        .reduce(function(a, b) {
                            return intVal(a) + intVal(b);
                        }, 0);

                    $(api.column(fi).footer()).html(
                        //currency_symbol + ' ' + pageTotal + ' ( ' + currency_symbol + ' ' + total + ' total )'
                        pageTotal
                    );
                }


            }
        });


        //table js end
        //multiple select js start
        $('#f_section').selectpicker();
        $('#f_feetype').selectpicker();
        //multiple select js end
    });

    $(document).ready(function() {
        var table = $('#example').DataTable({
            scrollY: "300px",
            scrollX: true,
            scrollCollapse: true,
            paging: false,
            fixedColumns: {
                left: 1,
                right: 1
            }
        });
    });
</script>

</body>

</html>