<div>

<div class="space-y-6">
    {{-- 模式选择 --}}
    <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-4">
        <div class="flex gap-4">
            <button
                wire:click="$set('mode', 'select_paper')"
                class="px-4 py-2 rounded-md font-medium transition-colors {{ $mode === 'select_paper' ? 'bg-blue-600 text-white' : 'bg-gray-100 text-gray-700 hover:bg-gray-200' }}"
            >
                <svg class="w-5 h-5 inline-block mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.707.293V19a2 2 0 012-2H5a2 2 0 01-2 2v-14z"></path>
                </svg>
                选择已有试卷评分
            </button>
            <button
                wire:click="$set('mode', 'upload')"
                class="px-4 py-2 rounded-md font-medium transition-colors {{ $mode === 'upload' ? 'bg-blue-600 text-white' : 'bg-gray-100 text-gray-700 hover:bg-gray-200' }}"
            >
                <svg class="w-5 h-5 inline-block mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16v1a3 3 0 003 3h10a3 3 0 003 3v-1m-4-8l-4-4m0 0L8 8m4-4v12"></path>
                </svg>
                上传试卷照片
            </button>
        </div>
    </div>

    {{-- 上传模式 --}}
    @if($mode === 'upload')
        {{-- 选择老师和学生 --}}
        <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-6 mb-6">
            <h2 class="text-lg font-semibold mb-4 flex items-center">
                <svg class="w-5 h-5 mr-2 text-blue-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z"></path>
                </svg>
                选择老师和学生
            </h2>
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                {{-- 选择老师 --}}
                <div class="form-control w-full">
                    <label class="block text-sm font-medium text-gray-700 mb-2">
                        选择老师 <span class="text-red-500">*</span>
                        @if($isTeacher)
                            <span class="text-green-600 text-xs ml-2">(当前登录)</span>
                        @endif
                    </label>
                    <select
                        wire:model.live="teacherId"
                        @if($isTeacher) disabled @endif
                        class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 @if($isTeacher) bg-gray-100 @endif"
                    >
                        <option value="">请选择老师...</option>
                        @foreach($this->teachers as $teacher)
                            <option value="{{ $teacher->teacher_id }}">
                                {{ trim($teacher->name ?? $teacher->teacher_id) . ($teacher->subject ? " ({$teacher->subject})" : '') }}
                            </option>
                        @endforeach
                    </select>
                </div>

                {{-- 选择学生 --}}
                <div class="form-control w-full">
                    <label class="block text-sm font-medium text-gray-700 mb-2">选择学生 <span class="text-red-500">*</span></label>
                    <select
                        wire:model.live="studentId"
                        wire:loading.attr="disabled"
                        @if(empty($teacherId)) disabled @endif
                        class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 @if(empty($teacherId)) bg-gray-100 @endif"
                    >
                        <option value="">
                            @if(empty($teacherId))
                                请先选择老师
                            @else
                                请选择学生...
                            @endif
                        </option>
                        @foreach($this->students as $student)
                            <option value="{{ $student->student_id }}">
                                {{ trim($student->name ?? $student->student_id) . " ({$student->grade} - {$student->class_name})" }}
                            </option>
                        @endforeach
                    </select>
                </div>
            </div>
        </div>

        {{-- 图片上传和OCR识别组件 --}}
        @if(!empty($teacherId) && !empty($studentId))
            @livewire(\App\Livewire\UploadExam\UploadForm::class, ['teacherId' => $teacherId, 'studentId' => $studentId])
            @livewire(\App\Livewire\UploadExam\OCRResults::class)
        @endif
    @endif

    {{-- 选择试卷评分模式 --}}
    @if($mode === 'select_paper')
        <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-6">
            <h2 class="text-xl font-semibold mb-6 flex items-center">
                <svg class="w-6 h-6 mr-2 text-blue-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.707.293V19a2 2 0 012-2H5a2 2 0 01-2 2v-14z"></path>
                </svg>
                选择已有试卷评分
            </h2>

            {{-- 选择老师和学生 --}}
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                {{-- 选择老师 --}}
                <div class="form-control w-full">
                    <label class="block text-sm font-medium text-gray-700 mb-2">
                        选择老师 <span class="text-red-500">*</span>
                        @if($isTeacher)
                            <span class="text-green-600 text-xs ml-2">(当前登录)</span>
                        @endif
                    </label>
                    <select
                        wire:model.live="teacherId"
                        @if($isTeacher) disabled @endif
                        class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 @if($isTeacher) bg-gray-100 @endif"
                    >
                        <option value="">请选择老师...</option>
                        @foreach($this->teachers as $teacher)
                            <option value="{{ $teacher->teacher_id }}">
                                {{ trim($teacher->name ?? $teacher->teacher_id) . ($teacher->subject ? " ({$teacher->subject})" : '') }}
                            </option>
                        @endforeach
                    </select>
                </div>

                {{-- 选择学生 --}}
                <div class="form-control w-full">
                    <label class="block text-sm font-medium text-gray-700 mb-2">选择学生 <span class="text-red-500">*</span></label>
                    <select
                        wire:model.live="studentId"
                        wire:loading.attr="disabled"
                        @if(empty($teacherId)) disabled @endif
                        class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
                    >
                        <option value="">
                            @if(empty($teacherId))
                                请先选择老师
                            @else
                                请选择学生...
                            @endif
                        </option>
                        @foreach($this->students as $student)
                            <option value="{{ $student->student_id }}">
                                {{ trim($student->name ?? $student->student_id) . " ({$student->grade} - {$student->class_name})" }}
                            </option>
                        @endforeach
                    </select>
                </div>
            </div>

            {{-- 选择试卷 --}}
            @if(!empty($studentId))
                <div class="form-control w-full mt-6">
                    <label class="block text-sm font-medium text-gray-700 mb-2">选择试卷 <span class="text-red-500">*</span></label>
                    <select
                        wire:model.live="selectedPaperId"
                        class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
                    >
                        <option value="">请选择试卷...</option>
                        @foreach($this->studentPapers as $paper)
                            <option value="{{ $paper['paper_id'] }}">
                                {{ $paper['paper_name'] }} ({{ $paper['total_questions'] }}题 / {{ $paper['total_score'] }}分) - {{ $paper['created_at'] }}
                            </option>
                        @endforeach
                    </select>
                </div>
            @endif

            {{-- 评分面板组件 --}}
            @if(!empty($selectedPaperId))
                @livewire(\App\Livewire\UploadExam\GradingPanel::class, [
                    'teacherId' => $teacherId,
                    'studentId' => $studentId,
                    'selectedPaperId' => $selectedPaperId,
                    'questions' => $this->selectedPaperQuestions
                ])
            @endif
        </div>
    @endif

    {{-- 最近上传记录 --}}
    <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-6">
        <h2 class="text-lg font-semibold mb-4 flex items-center">
            <svg class="w-5 h-5 mr-2 text-blue-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6 2a9 9 0 11-18 0 9 9 0 011-18 0z"></path>
            </svg>
            最近试卷记录
        </h2>

        @if(count($this->recentRecords) > 0)
            <div class="overflow-x-auto">
                <table class="min-w-full divide-y divide-gray-200">
                    <thead class="bg-gray-50">
                        <tr>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">试卷名称</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">学生</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">题目数</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">状态</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">创建时间</th>
                        </tr>
                    </thead>
                    <tbody class="bg-white divide-y divide-gray-200">
                        @foreach($this->recentRecords as $record)
                            <tr class="hover:bg-gray-50">
                                <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-blue-600">
                                    @php
                                        $url = '';
                                        if (in_array($record['type'], ['graded_paper', 'generated'])) {
                                            $url = '/admin/exam-analysis?paperId=' . ($record['paper_id'] ?? '') . '&studentId=' . $record['student_id'];
                                        } elseif ($record['type'] === 'ocr_upload') {
                                            $url = '/admin/exam-analysis?recordId=' . ($record['record_id'] ?? '') . '&studentId=' . $record['student_id'];
                                        }
                                    @endphp
                                    @if($url)
                                        <a href="{{ $url }}" class="hover:underline">{{ $record['paper_name'] }}</a>
                                    @else
                                        {{ $record['paper_name'] }}
                                    @endif
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                    {{ $record['student_name'] }}
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                    {{ $record['total_questions'] }}
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap">
                                    <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full
                                        @if($record['is_completed'])
                                            bg-green-100 text-green-800
                                        @elseif($record['status'] === 'processing')
                                            bg-yellow-100 text-yellow-800
                                        @else
                                            bg-gray-100 text-gray-800
                                        @endif">
                                        {{ $record['status_text'] ?? $record['status'] }}
                                    </span>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                    {{ $record['created_at'] }}
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        @else
            <div class="text-center py-8 text-gray-500">
                <svg class="w-12 h-12 mx-auto mb-3 opacity-50" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 13h6m-3-9v9h6m0 6v6m-6-6h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.707.293V19a2 2 0 012-2H5a2 2 0 01-2 2v-14z"></path>
                </svg>
                <p>暂无上传记录</p>
            </div>
        @endif
    </div>

</div>