<?php $__env->startSection('title', 'ট্যাবুলেশন শিট'); ?>

<?php $__env->startSection('content'); ?>
<div class="container-fluid">
    <!-- Header -->
    <div class="row mb-3">
        <div class="col-12">
            <div class="card card-primary">
                <div class="card-header bg-primary text-white">
                    <h5 class="mb-0"><i class="fa fa-table"></i> ট্যাবুলেশন শিট</h5>
                </div>
                <div class="card-body">
                    <a href="<?php echo e(route('results.index')); ?>" class="btn btn-secondary">
                        <i class="fa fa-arrow-left"></i> ফিরে যান
                    </a>
                    <a href="#" class="btn btn-success" onclick="window.print()">
                        <i class="fa fa-print"></i> প্রিন্ট
                    </a>
                    <a href="#" class="btn btn-info" onclick="exportToExcel()">
                        <i class="fa fa-file-excel"></i> এক্সেল ডাউনলোড
                    </a>
                </div>
            </div>
        </div>
    </div>

    <!-- Tabulation Sheet -->
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header bg-info text-white">
                    <h5 class="mb-0">
                        ট্যাবুলেশন শিট 
                        <?php if(request('class_id')): ?>
                            - <?php echo e(App\Models\Classes::find(request('class_id'))->class_name_bn ?? ''); ?>

                        <?php endif; ?>
                        <?php if(request('exam_name')): ?>
                            - <?php echo e(request('exam_name')); ?>

                        <?php endif; ?>
                    </h5>
                </div>
                <div class="card-body">
                    <?php if(!isset($results) || $results->isEmpty()): ?>
                        <div class="alert alert-warning text-center">
                            <i class="fa fa-exclamation-triangle fa-2x"></i>
                            <h5>কোনো ফলাফল পাওয়া যায়নি</h5>
                        </div>
                    <?php else: ?>
                        <div class="table-responsive">
                            <table class="table table-bordered table-striped table-hover" id="tabulationTable">
                                <thead class="bg-secondary text-white">
                                    <tr>
                                        <th rowspan="2" style="vertical-align: middle;">ক্রমিক</th>
                                        <th rowspan="2" style="vertical-align: middle;">রোল</th>
                                        <th rowspan="2" style="vertical-align: middle;">ছাত্রের নাম</th>
                                        <th rowspan="2" style="vertical-align: middle;">আইডি</th>
                                        <?php
                                            $subjects = [];
                                            foreach($results as $studentId => $studentResults) {
                                                foreach($studentResults as $result) {
                                                    $subjects[$result->subject_code] = $result->subject;
                                                }
                                            }
                                        ?>
                                        <?php $__currentLoopData = $subjects; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $code => $subject): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <th colspan="2" class="text-center"><?php echo e($subject); ?></th>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                        <th rowspan="2" style="vertical-align: middle;">মোট</th>
                                        <th rowspan="2" style="vertical-align: middle;">GPA</th>
                                        <th rowspan="2" style="vertical-align: middle;">গ্রেড</th>
                                    </tr>
                                    <tr>
                                        <?php $__currentLoopData = $subjects; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $code => $subject): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <th>প্রাপ্ত</th>
                                        <th>গ্রেড</th>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php $__currentLoopData = $results; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $studentId => $studentResults): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <?php
                                        $firstResult = $studentResults->first();
                                        $student = $firstResult->student;
                                        $totalMarks = 0;
                                        $totalPoints = 0;
                                        $subjectCount = 0;
                                    ?>
                                    <tr>
                                        <td class="text-center"><?php echo e($loop->iteration); ?></td>
                                        <td class="text-center"><?php echo e($student->roll_number ?? ''); ?></td>
                                        <td><?php echo e($student->name_bn ?? ''); ?></td>
                                        <td><?php echo e($student->student_id ?? ''); ?></td>
                                        
                                        <?php $__currentLoopData = $subjects; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $code => $subject): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            <?php
                                                $result = $studentResults->where('subject_code', $code)->first();
                                                if($result) {
                                                    $totalMarks += $result->obtained_marks;
                                                    if($result->grade_point > 0) {
                                                        $totalPoints += $result->grade_point;
                                                        $subjectCount++;
                                                    }
                                                }
                                            ?>
                                            <td class="text-center <?php echo e($result && $result->status == 'fail' ? 'bg-light-danger' : ''); ?>">
                                                <?php echo e($result ? number_format($result->obtained_marks, 2) : '-'); ?>

                                            </td>
                                            <td class="text-center">
                                                <?php if($result): ?>
                                                    <span class="badge badge-<?php echo e($result->grade == 'A+' ? 'success' : ($result->grade == 'F' ? 'danger' : 'info')); ?>">
                                                        <?php echo e($result->grade); ?>

                                                    </span>
                                                <?php else: ?>
                                                    -
                                                <?php endif; ?>
                                            </td>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                        
                                        <?php
                                            $gpa = $subjectCount > 0 ? round($totalPoints / $subjectCount, 2) : 0;
                                            if ($gpa >= 5) $grade = 'A+';
                                            elseif ($gpa >= 4) $grade = 'A';
                                            elseif ($gpa >= 3.5) $grade = 'A-';
                                            elseif ($gpa >= 3) $grade = 'B';
                                            elseif ($gpa >= 2) $grade = 'C';
                                            elseif ($gpa >= 1) $grade = 'D';
                                            else $grade = 'F';
                                        ?>
                                        
                                        <td class="text-center font-weight-bold"><?php echo e(number_format($totalMarks, 2)); ?></td>
                                        <td class="text-center"><?php echo e(number_format($gpa, 2)); ?></td>
                                        <td class="text-center">
                                            <span class="badge badge-<?php echo e($grade == 'A+' ? 'success' : ($grade == 'F' ? 'danger' : 'info')); ?>">
                                                <?php echo e($grade); ?>

                                            </span>
                                        </td>
                                    </tr>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </tbody>
                            </table>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('scripts'); ?>
<script>
    function exportToExcel() {
        var table = document.getElementById('tabulationTable');
        var html = table.outerHTML;
        var url = 'data:application/vnd.ms-excel,' + escape(html);
        var link = document.createElement('a');
        link.download = 'tabulation_sheet_<?php echo e(date('Y-m-d')); ?>.xls';
        link.href = url;
        link.click();
    }
</script>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH /home/naowarin/school.naowarinternational.my.id/resources/views/results/tabulation-sheet.blade.php ENDPATH**/ ?>