@extends('layouts.app')

@section('title', 'ট্যাবুলেশন শিট')

@section('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="{{ 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">
                        ট্যাবুলেশন শিট 
                        @if(request('class_id'))
                            - {{ App\Models\Classes::find(request('class_id'))->class_name_bn ?? '' }}
                        @endif
                        @if(request('exam_name'))
                            - {{ request('exam_name') }}
                        @endif
                    </h5>
                </div>
                <div class="card-body">
                    @if(!isset($results) || $results->isEmpty())
                        <div class="alert alert-warning text-center">
                            <i class="fa fa-exclamation-triangle fa-2x"></i>
                            <h5>কোনো ফলাফল পাওয়া যায়নি</h5>
                        </div>
                    @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;
                                                }
                                            }
                                        @endphp
                                        @foreach($subjects as $code => $subject)
                                        <th colspan="2" class="text-center">{{ $subject }}</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>
                                    </tr>
                                    <tr>
                                        @foreach($subjects as $code => $subject)
                                        <th>প্রাপ্ত</th>
                                        <th>গ্রেড</th>
                                        @endforeach
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach($results as $studentId => $studentResults)
                                    @php
                                        $firstResult = $studentResults->first();
                                        $student = $firstResult->student;
                                        $totalMarks = 0;
                                        $totalPoints = 0;
                                        $subjectCount = 0;
                                    @endphp
                                    <tr>
                                        <td class="text-center">{{ $loop->iteration }}</td>
                                        <td class="text-center">{{ $student->roll_number ?? '' }}</td>
                                        <td>{{ $student->name_bn ?? '' }}</td>
                                        <td>{{ $student->student_id ?? '' }}</td>
                                        
                                        @foreach($subjects as $code => $subject)
                                            @php
                                                $result = $studentResults->where('subject_code', $code)->first();
                                                if($result) {
                                                    $totalMarks += $result->obtained_marks;
                                                    if($result->grade_point > 0) {
                                                        $totalPoints += $result->grade_point;
                                                        $subjectCount++;
                                                    }
                                                }
                                            @endphp
                                            <td class="text-center {{ $result && $result->status == 'fail' ? 'bg-light-danger' : '' }}">
                                                {{ $result ? number_format($result->obtained_marks, 2) : '-' }}
                                            </td>
                                            <td class="text-center">
                                                @if($result)
                                                    <span class="badge badge-{{ $result->grade == 'A+' ? 'success' : ($result->grade == 'F' ? 'danger' : 'info') }}">
                                                        {{ $result->grade }}
                                                    </span>
                                                @else
                                                    -
                                                @endif
                                            </td>
                                        @endforeach
                                        
                                        @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';
                                        @endphp
                                        
                                        <td class="text-center font-weight-bold">{{ number_format($totalMarks, 2) }}</td>
                                        <td class="text-center">{{ number_format($gpa, 2) }}</td>
                                        <td class="text-center">
                                            <span class="badge badge-{{ $grade == 'A+' ? 'success' : ($grade == 'F' ? 'danger' : 'info') }}">
                                                {{ $grade }}
                                            </span>
                                        </td>
                                    </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@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_sheet_{{ date('Y-m-d') }}.xls';
        link.href = url;
        link.click();
    }
</script>
@endsection