@extends('layouts.app')

@section('title', 'ট্যাবুলেশন শিট - {{ $class->class_name_bn }}')

@section('content')
<div class="container-fluid">
    <!-- Header -->
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header bg-primary text-white">
                    <h5 class="mb-0"><i class="fa fa-table"></i> ট্যাবুলেশন শিট: {{ $class->class_name_bn }} - {{ $request->exam_type }} {{ $request->exam_year }}</h5>
                </div>
                <div class="card-body">
                    <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>
                                    @foreach($subjects as $subject)
                                    <th colspan="2" class="text-center">{{ $subject['name'] }}</th>
                                    @endforeach
                                    <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>
                                    <th rowspan="2" style="vertical-align: middle;">মেরিট</th>
                                </tr>
                                <tr>
                                    @foreach($subjects as $subject)
                                    <th>প্রাপ্ত</th>
                                    <th>গ্রেড</th>
                                    @endforeach
                                </tr>
                            </thead>
                            <tbody>
                                @php
                                    $studentTotals = [];
                                    $studentGPAs = [];
                                @endphp
                                
                                @foreach($students as $student)
                                @php
                                    $totalObtained = 0;
                                    $totalSubjects = 0;
                                @endphp
                                <tr>
                                    <td>{{ $student->roll_number }}</td>
                                    <td>{{ $student->name_bn }}</td>
                                    <td>{{ $student->student_id }}</td>
                                    
                                    @foreach($subjects as $subject)
                                    @php
                                        $result = $results[$student->id][$subject['code']] ?? null;
                                        if ($result) {
                                            $totalObtained += $result->obtained_marks;
                                            $totalSubjects++;
                                        }
                                    @endphp
                                    <td class="{{ $result && $result->status == 'fail' ? 'bg-light-danger' : '' }}">
                                        {{ $result ? number_format($result->obtained_marks, 2) : '-' }}
                                    </td>
                                    <td>
                                        @if($result)
                                            <span class="badge badge-{{ $result->grade == 'A+' ? 'success' : ($result->grade == 'F' ? 'danger' : 'info') }}">
                                                {{ $result->grade }}
                                            </span>
                                        @else
                                            -
                                        @endif
                                    </td>
                                    @endforeach
                                    
                                    @php
                                        $gpa = $totalSubjects > 0 ? round($totalObtained / $totalSubjects / 20, 2) : 0;
                                        $studentTotals[$student->id] = $totalObtained;
                                        $studentGPAs[$student->id] = $gpa;
                                    @endphp
                                    
                                    <td class="font-weight-bold">{{ number_format($totalObtained, 2) }}</td>
                                    <td>{{ number_format($gpa, 2) }}</td>
                                    <td>
                                        @php
                                            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';
                                        @endphp
                                        <span class="badge badge-{{ $grade == 'A+' ? 'success' : ($grade == 'F' ? 'danger' : 'info') }}">
                                            {{ $grade }}
                                        </span>
                                    </td>
                                    <td>-</td>
                                </tr>
                                @endforeach
                            </tbody>
                            
                            <!-- Calculate ranks -->
                            @php
                                arsort($studentTotals);
                                $rank = 1;
                                $ranks = [];
                                foreach ($studentTotals as $studentId => $total) {
                                    $ranks[$studentId] = $rank++;
                                }
                            @endphp
                            
                            <!-- Update ranks in table -->
                            @foreach($students as $student)
                            @php
                                $rowId = 'rank-' . $student->id;
                            @endphp
                            <script>
                                $(document).ready(function() {
                                    $('tr:contains("{{ $student->student_id }}") td:last').text('{{ $ranks[$student->id] ?? '' }}');
                                });
                            </script>
                            @endforeach
                        </table>
                    </div>

                    <!-- Action Buttons -->
                    <div class="row mt-4">
                        <div class="col-md-12 text-center">
                            <button type="button" class="btn btn-success" onclick="exportToExcel()">
                                <i class="fa fa-file-excel"></i> এক্সেল ডাউনলোড
                            </button>
                            <button type="button" class="btn btn-info" onclick="window.print()">
                                <i class="fa fa-print"></i> প্রিন্ট
                            </button>
                            <a href="{{ route('results.index') }}" class="btn btn-secondary">
                                <i class="fa fa-arrow-left"></i> ফিরে যান
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

@section('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_{{ $class->class_name }}_{{ $request->exam_type }}_{{ $request->exam_year }}.xls';
        link.href = url;
        link.click();
    }
</script>
@endsection
@endsection