<style>
    #tbl1 tr th {
        font-weight: 600;
        font-size: 12px;
    }
</style>

<div class="wraper responsive-width">
    <main class="" id="main">

        <!----admin template section---->
        <div class="admin_tempblock">
            <div class="admin_tempsec">
                <div class="admin_sec">
                    <div class="">
                        <div class=" subject_l_full_width ">
                            <div class="subject_lsec">
                                <!-- <button style="font-size: 14px;padding: 6px 25px;" class="subject_addbtn ">Back</button> -->

                                <div class="subject_lhead"><?php echo $title; ?>
                                </div>
                                <a href="<?= base_url() . 'admin/export_mark_card?batch=' . $batch_id . '&course=' . $course_id . '&exam=' . $exam_id . '&section=' . $section_id . '' ?>" class="btn btn-outline-primary mb-2">Export Excel</a>

                                <?php  //dd($student_results);
                                // echo '<pre>';
                                // print_r($student_results);
                                ?>
                                <table id="tbl1" style='width:100%;' class="table table-bordered table-striped text-center">
                                    <thead>
                                        <tr>
                                            <th rowspan="2">SNo</th>
                                            <th rowspan="2">student Name</th>
                                            <th rowspan="2">Roll No </th>
                                            <?php if (count($course_subjects) > 0) {
                                                foreach ($course_subjects as $cs_value) {

                                            ?>
                                                    <th colspan="3"><?= $cs_value['subject_name'] ?></th>
                                            <?php
                                                }
                                            }
                                            ?>
                                            <th rowspan="2">Total Marks</th>
                                            <!-- <th rowspan="2">GPA</th>
                                            <th rowspan="2">Final Grade</th>
                                            <th rowspan="2">Remarks</th> -->
                                            <th rowspan="2">Percentage (%)</th>
                                        </tr>

                                        <tr>
                                            <?php if (count($course_subjects) > 0) {
                                                foreach ($course_subjects as $cs_value) {

                                            ?>
                                                    <th>Th. Mark </th>
                                                    <!-- <th>Th. Grade</th> -->
                                                    <th>Pr. Marks</th>
                                                    <!-- <th>Pr. Grade</th> -->
                                                    <th>
                                                        Total Mark
                                                    </th>
                                                    <!-- <th>Subject Grade</th> -->
                                            <?php
                                                }
                                            }
                                            ?>
                                        </tr>

                                    </thead>
                                    <tbody>
                                        <?php
                                        $total_subjects = (count($course_subjects) * 3);
                                        $sno = 1;
                                        if (count($student_results) > 0) {
                                            foreach ($student_results as $studkey => $student) {


                                        ?>
                                                <tr>
                                                    <th><?= $sno; ?></th>
                                                    <th><?= $student['student_name'] ?></th>
                                                    <th><?= $student['roll_no'] ?></th>

                                                    <?php
                                                    $theFinalresult = array();

                                                    // echo'<pre>;
                                                    // print_r($theFinalresult);;
                                                    foreach ($course_subjects as $csk => $csv) {
                                                        // echo'<pre>';
                                                        // print_r($student['subject_results'][$csv['subject_id']['total_full_marks']]);
                                                        if (isset($student['subject_results']) && count($student['subject_results']) > 0) {


                                                            if (isset($student['subject_results'][$csv['subject_id']]['sub_id']) && ($student['subject_results'][$csv['subject_id']]['sub_id'] == $csv['subject_id'])) {
                                                    ?>
                                                                <td><?php echo (isset($student['subject_results'][$csv['subject_id']]['th_obt_marks']) && !empty($student['subject_results'][$csv['subject_id']]['th_obt_marks'])) ? $student['subject_results'][$csv['subject_id']]['th_obt_marks'] : ''; ?></td>
                                                                <!-- <td><?php echo (isset($student['subject_results'][$csv['subject_id']]['th_obt_grade']) && !empty($student['subject_results'][$csv['subject_id']]['th_obt_grade'])) ? $student['subject_results'][$csv['subject_id']]['th_obt_grade'] : ''; ?></td> -->
                                                                <td><?php echo (isset($student['subject_results'][$csv['subject_id']]['pr_obt_marks']) && !empty($student['subject_results'][$csv['subject_id']]['pr_obt_marks']) && ($student['subject_results'][$csv['subject_id']]['pr_obt_marks']) != 0.00) ? $student['subject_results'][$csv['subject_id']]['pr_obt_marks'] : ''; ?></td>
                                                                <!-- <td><?php echo (isset($student['subject_results'][$csv['subject_id']]['th_obt_grade']) && !empty($student['subject_results'][$csv['subject_id']]['pr_obt_grade'])) ? $student['subject_results'][$csv['subject_id']]['pr_obt_grade'] : ''; ?></td> -->
                                                                <td><?php echo (isset($student['subject_results'][$csv['subject_id']]['total_obt_marks']) && !empty($student['subject_results'][$csv['subject_id']]['total_obt_marks'])) ? $student['subject_results'][$csv['subject_id']]['total_obt_marks'] : ''; ?></td>
                                                                <!-- <td><?php echo (isset($student['subject_results'][$csv['subject_id']]['total_obt_grade']) && !empty($student['subject_results'][$csv['subject_id']]['total_obt_grade'])) ? $student['subject_results'][$csv['subject_id']]['total_obt_grade'] : ''; ?></td> -->
                                                            <?php

                                                                $theFinalresult[] = array(
                                                                    'exam_total_obt_marks' => (isset($student['subject_results'][$csv['subject_id']]['exam_total_obt_marks']) && !empty($student['subject_results'][$csv['subject_id']]['exam_total_obt_marks'])) ? $student['subject_results'][$csv['subject_id']]['exam_total_obt_marks'] : '',
                                                                    'exam_gpa' => (isset($student['subject_results'][$csv['subject_id']]['gpa']) && !empty($student['subject_results'][$csv['subject_id']]['gpa'])) ? $student['subject_results'][$csv['subject_id']]['gpa'] : '',
                                                                    'remarks_gpa' => (isset($student['subject_results'][$csv['subject_id']]['remarks_gpa']) && !empty($student['subject_results'][$csv['subject_id']]['remarks_gpa'])) ? $student['subject_results'][$csv['subject_id']]['remarks_gpa'] : '',
                                                                    'exams_total_percentage' => (isset($student['subject_results'][$csv['subject_id']]['exams_total_percentage']) && !empty($student['subject_results'][$csv['subject_id']]['exams_total_percentage'])) ? $student['subject_results'][$csv['subject_id']]['exams_total_percentage'] : '',
                                                                    'exam_total_obt_grade' => (isset($student['subject_results'][$csv['subject_id']]['grade']) && !empty($student['subject_results'][$csv['subject_id']]['grade'])) ? $student['subject_results'][$csv['subject_id']]['grade'] : '',
                                                                );
                                                            } else {
                                                                echo " <td><b style='color:red;'>NA</b></td>
                                                <td><b style='color:red;'>NA</b></td>
                                                <td><b style='color:red;'>NA</b></td>";
                                                            }
                                                        } else { ?>
                                                            <td>-</td>
                                                            <td>-</td>
                                                            <td>-</td>
                                                        <?php
                                                        }
                                                        ?>

                                                    <?php }
                                                    if (count($theFinalresult) > 0) {
                                                    ?>

                                                        <td><?php echo (isset($theFinalresult[0]['exam_total_obt_marks']) && !empty($theFinalresult[0]['exam_total_obt_marks'])) ? $theFinalresult[0]['exam_total_obt_marks'] : ''; ?></td>
                                                        <!-- <td><?php echo (isset($theFinalresult[0]['exam_gpa']) && !empty($theFinalresult[0]['exam_gpa'])) ? $theFinalresult[0]['exam_gpa'] : ''; ?></td> -->
                                                        <!-- <td>
                                                            <?php
                                                            if (isset($theFinalresult[0]['remarks_gpa']) && !empty($theFinalresult[0]['remarks_gpa'])) {
                                                                foreach ($exam_grade_system as $g_row) {
                                                                    if ($theFinalresult[0]['remarks_gpa'] == $g_row['remarks']) {
                                                                        echo $g_row['grade'];
                                                                    }
                                                                }
                                                            } else {
                                                                echo '-';
                                                            }
                                                            echo (isset($theFinalresult[0]['exam_total_obt_grade']) && !empty($theFinalresult[0]['exam_total_obt_grade'])) ? $theFinalresult[0]['exam_total_obt_grade'] : '';
                                                            ?>
                                                        </td> -->
                                                        <!-- <td><?php echo (isset($theFinalresult[0]['remarks_gpa']) && !empty($theFinalresult[0]['remarks_gpa'])) ? $theFinalresult[0]['remarks_gpa'] : ''; ?></td> -->
                                                        <td><?php echo (isset($theFinalresult[0]['exams_total_percentage']) && !empty($theFinalresult[0]['exams_total_percentage'])) ? $theFinalresult[0]['exams_total_percentage'] : ''; ?></td>
                                                </tr>
                                    <?php
                                                    }


                                                    $sno++;
                                                }
                                            } ?>
                                    </tbody>

                                    <tfoot>

                                    </tfoot>


                                </table>

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

    </main>

    <?php
    $theExportHeader = 'The Markscard Reports';
    if (isset($school_info) && !empty($school_info)) {
        $theExportHeader = $school_info['school_name'];
    }
    ?>
</div>
<script>
    $(document).ready(function() {
        var jsTheExportHeader = '<?php echo $theExportHeader; ?>';
        $('#tbl1').DataTable({
            "lengthMenu": [
                [100, 125, 150, -1],
                [100, 125, 150, "All"]
            ],
            fixedColumns: {
                left: 2,
            },

            dom: 'lBfrtip',
            buttons: [{
                    extend: 'pdfHtml5',
                    title: 'Student List',
                    orientation: 'landscape',
                    pageSize: 'LEGAL',

                    exportOptions: {
                        columns: "thead th:not(.noExport)",
                        rows: function(indx, rowData, domElement) {
                            return $(domElement).css("display") != "none";
                        }
                    }
                },
                {
                    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');
                    }
                }
            ]
        });
    });
</script>