@extends('layouts.app')

@section('title', $exam->exam_name)

@section('content')
<div class="container-fluid">
    <!-- Exam Info Card -->
    <div class="row">
        <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-info-circle"></i> পরীক্ষার তথ্য: {{ $exam->exam_name }}</h5>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-3">
                            <strong>পরীক্ষার ধরন:</strong> {{ \App\Models\Exam::getExamTypes()[$exam->exam_type] ?? $exam->exam_type }}
                        </div>
                        <div class="col-md-2">
                            <strong>শ্রেণি:</strong> {{ $exam->class->class_name_bn ?? '' }}
                        </div>
                        <div class="col-md-3">
                            <strong>সময়সীমা:</strong> {{ $exam->start_date->format('d-m-Y') }} থেকে {{ $exam->end_date->format('d-m-Y') }}
                        </div>
                        <div class="col-md-2">
                            <strong>মোট বিষয়:</strong> {{ $routines->count() }}
                        </div>
                        <div class="col-md-2 text-right">
                            <a href="{{ route('exams.routine.print', $exam->id) }}" class="btn btn-sm btn-info" target="_blank">
                                <i class="fa fa-print"></i> রুটিন প্রিন্ট
                            </a>
                        </div>
                    </div>
                    @if($exam->description)
                    <div class="row mt-2">
                        <div class="col-12">
                            <strong>বিবরণ:</strong> {{ $exam->description }}
                        </div>
                    </div>
                    @endif
                </div>
            </div>
        </div>
    </div>

    <!-- Action Buttons -->
    <div class="row mt-3">
        <div class="col-12">
            <button type="button" class="btn btn-success" data-toggle="modal" data-target="#addRoutineModal">
                <i class="fa fa-plus-circle"></i> রুটিন যোগ করুন
            </button>
            <a href="{{ route('exams.seat-plan', $exam->id) }}" class="btn btn-warning">
                <i class="fa fa-chair"></i> সিট প্ল্যান
            </a>
            <a href="{{ route('exams.admit-cards.generate', $exam->id) }}" class="btn btn-info" onclick="return confirm('প্রবেশপত্র জেনারেট করবেন?')">
                <i class="fa fa-id-card"></i> প্রবেশপত্র জেনারেট
            </a>
            <a href="{{ route('exams.duty-chart', $exam->id) }}" class="btn btn-primary">
                <i class="fa fa-users"></i> ডিউটি চার্ট
            </a>
            <a href="{{ route('exams.analytics', $exam->id) }}" class="btn btn-secondary">
                <i class="fa fa-chart-bar"></i> বিশ্লেষণ
            </a>
        </div>
    </div>

    <!-- Exam Routine Table -->
    <div class="row mt-4">
        <div class="col-12">
            <div class="card">
                <div class="card-header bg-success text-white">
                    <h5 class="mb-0"><i class="fa fa-calendar-alt"></i> পরীক্ষার রুটিন</h5>
                </div>
                <div class="card-body">
                    @if($routines->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">
                            <thead class="bg-secondary text-white">
                                <tr>
                                    <th>তারিখ</th>
                                    <th>দিন</th>
                                    <th>বিষয়</th>
                                    <th>সময়</th>
                                    <th>পূর্ণমান</th>
                                    <th>পাস মার্ক</th>
                                    <th>রুম</th>
                                    <th>পরীক্ষক</th>
                                    <th width="100">একশন</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($routines as $routine)
                                <tr>
                                    <td>{{ $routine->exam_date->format('d-m-Y') }}</td>
                                    <td>
                                        @php
                                            $days = ['শনিবার', 'রবিবার', 'সোমবার', 'মঙ্গলবার', 'বুধবার', 'বৃহস্পতিবার', 'শুক্রবার'];
                                            echo $days[$routine->exam_date->dayOfWeek] ?? '';
                                        @endphp
                                    </td>
                                    <td class="font-weight-bold">{{ $routine->subject }}</td>
                                    <td>{{ date('h:i A', strtotime($routine->start_time)) }} - {{ date('h:i A', strtotime($routine->end_time)) }}</td>
                                    <td class="text-center">{{ $routine->total_marks }}</td>
                                    <td class="text-center">{{ $routine->pass_marks }}</td>
                                    <td>{{ $routine->room_number }} @if($routine->building)({{ $routine->building }})@endif</td>
                                    <td>{{ $routine->invigilator->name_bn ?? '' }}</td>
                                    <td>
                                        <a href="{{ route('exams.seat-plan.view', [$exam->id, $routine->id]) }}" class="btn btn-sm btn-info" title="সিট প্ল্যান">
                                            <i class="fa fa-chair"></i>
                                        </a>
                                        <button class="btn btn-sm btn-danger delete-routine" data-id="{{ $routine->id }}">
                                            <i class="fa fa-trash"></i>
                                        </button>
                                    </td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Add Routine Modal -->
<div class="modal fade" id="addRoutineModal" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header bg-success text-white">
                <h5 class="modal-title">পরীক্ষার রুটিন যোগ করুন</h5>
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>
            <form action="{{ route('exams.routine.add', $exam->id) }}" method="POST">
                @csrf
                <div class="modal-body">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>বিষয় <span class="text-danger">*</span></label>
                                <input type="text" name="subject" class="form-control" list="subjects" required>
                                <datalist id="subjects">
                                    @foreach($subjects as $subject)
                                    <option value="{{ $subject['name'] }}" data-code="{{ $subject['code'] }}">
                                    @endforeach
                                </datalist>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>বিষয় কোড</label>
                                <input type="text" name="subject_code" class="form-control" id="subject_code">
                            </div>
                        </div>
                    </div>
                    
                    <div class="row">
                        <div class="col-md-4">
                            <div class="form-group">
                                <label>পরীক্ষার তারিখ <span class="text-danger">*</span></label>
                                <input type="date" name="exam_date" class="form-control" 
                                       min="{{ $exam->start_date }}" max="{{ $exam->end_date }}" required>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                <label>শুরুর সময় <span class="text-danger">*</span></label>
                                <input type="time" name="start_time" class="form-control" required>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                <label>শেষের সময় <span class="text-danger">*</span></label>
                                <input type="time" name="end_time" class="form-control" required>
                            </div>
                        </div>
                    </div>
                    
                    <div class="row">
                        <div class="col-md-3">
                            <div class="form-group">
                                <label>পূর্ণমান <span class="text-danger">*</span></label>
                                <input type="number" name="total_marks" class="form-control" value="100" min="10" max="200" required>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="form-group">
                                <label>পাস মার্ক <span class="text-danger">*</span></label>
                                <input type="number" name="pass_marks" class="form-control" value="33" min="0" required>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="form-group">
                                <label>রুম নম্বর <span class="text-danger">*</span></label>
                                <input type="text" name="room_number" class="form-control" required>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="form-group">
                                <label>বিল্ডিং</label>
                                <input type="text" name="building" class="form-control" placeholder="মূল ভবন">
                            </div>
                        </div>
                    </div>
                    
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>পরীক্ষক <span class="text-danger">*</span></label>
                                <select name="invigilator_id" class="form-control" required>
                                    <option value="">শিক্ষক নির্বাচন করুন</option>
                                    @foreach($teachers as $teacher)
                                    <option value="{{ $teacher->id }}">{{ $teacher->name_bn }}</option>
                                    @endforeach
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>অতিরিক্ত তথ্য</label>
                                <input type="text" name="additional_notes" class="form-control" placeholder="প্রয়োজনীয় নোট">
                            </div>
                        </div>
                    </div>
                </div>
                
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">বাতিল</button>
                    <button type="submit" class="btn btn-success">রুটিন যোগ করুন</button>
                </div>
            </form>
        </div>
    </div>
</div>

@section('scripts')
<script>
    // Auto fill subject code
    $('input[name="subject"]').on('input', function() {
        var subject = $(this).val();
        $('#subjects option').each(function() {
            if ($(this).val() == subject) {
                $('#subject_code').val($(this).data('code'));
            }
        });
    });
    
    // Delete routine
    $('.delete-routine').click(function() {
        if (!confirm('এই রুটিন মুছে ফেলতে চান?')) return;
        
        var id = $(this).data('id');
        $.ajax({
            url: '{{ route("exams.routine.delete", "") }}/' + id,
            type: 'DELETE',
            data: { _token: '{{ csrf_token() }}' },
            success: function(response) {
                if (response.success) {
                    location.reload();
                }
            }
        });
    });
</script>
@endsection
@endsection