<x-filament-panels::page>

<div class="min-h-screen bg-gray-50 p-8">
    {{-- 页面标题区域 --}}
    <div class="mb-8">
        <div class="bg-white rounded-xl shadow-sm p-6 border border-gray-200">
            <div class="flex items-center justify-between mb-6">
                <div>
                    <h1 class="text-3xl font-bold text-gray-900 flex items-center">
                        <svg class="w-8 h-8 mr-3 text-indigo-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.293.707V19a2 2 0 01-2 2z"></path>
                        </svg>
                        模拟判卷
                    </h1>
                    <p class="mt-2 text-sm text-gray-600 ml-11">
                        选择知识点和技能，生成题目进行练习，自动记录答题结果并更新学生掌握度
                    </p>
                </div>
            </div>

            {{-- 选择器区域 --}}
            <div class="bg-gray-50 rounded-lg p-4 border border-gray-200">
                <div class="flex items-end space-x-6">
                    <div class="flex-1">
                        <label for="teacher-select" class="block text-sm font-medium text-gray-700 mb-2">
                            <svg class="w-4 h-4 inline mr-1" 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>
                            选择老师
                        </label>
                        <select
                            id="teacher-select"
                            wire:model.live="teacherId"
                            class="block w-full rounded-lg border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 text-sm py-2.5 bg-gray-50"
                        >
                            <option value="">请选择老师</option>
                            @foreach ($teachers as $teacher)
                                <option value="{{ $teacher->teacher_id }}">
                                    {{ $teacher->name }} ({{ $teacher->subject }})
                                </option>
                            @endforeach
                        </select>
                    </div>

                    <div class="flex-1">
                        <label for="student-select" class="block text-sm font-medium text-gray-700 mb-2">
                            <svg class="w-4 h-4 inline mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4.354a4 4 0 110 5.292M15 21H3v-1a6 6 0 0112 0v1zm0 0h6v-1a6 6 0 00-9-5.197M13 7a4 4 0 11-8 0 4 4 0 018 0z"></path>
                            </svg>
                            选择学生
                        </label>
                        <select
                            id="student-select"
                            wire:model.live="studentId"
                            class="block w-full rounded-lg border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 text-sm py-2.5 bg-gray-50"
                            @disabled(empty($teacherId))
                        >
                            <option value="">请选择学生</option>
                            @foreach ($students as $student)
                                <option value="{{ $student->student_id }}">
                                    {{ $student->name }} - {{ $student->grade }}{{ $student->class_name }}
                                </option>
                            @endforeach
                        </select>
                    </div>
                </div>
            </div>
        </div>
    </div>

    {{-- 加载状态 --}}
    @if ($isLoading)
        <div class="mb-8">
            <div class="bg-white rounded-xl shadow-sm p-12 border border-gray-200">
                <div class="flex flex-col items-center justify-center">
                    <svg class="animate-spin h-12 w-12 text-indigo-600" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
                        <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
                        <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
                    </svg>
                    <p class="mt-4 text-sm text-gray-600">正在生成题目，请稍候...</p>
                </div>
            </div>
        </div>
    @else
        {{-- 练习题目模块 --}}
        <div class="mb-8">
            <div class="bg-white shadow-sm rounded-xl border border-gray-200">
                <div class="px-6 py-5 border-b border-gray-100">
                    <div class="flex items-center justify-between mb-4">
                        <h3 class="text-lg font-semibold text-gray-900 flex items-center">
                            <svg class="w-5 h-5 mr-2 text-indigo-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.293.707V19a2 2 0 01-2 2z"></path>
                            </svg>
                            模拟判卷练习
                        </h3>

                </div>
                <div class="p-6">
                        <div class="space-y-6">
                            {{-- 知识点和技能选择区域 --}}
                            <div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
                                {{-- 知识点选择 --}}
                                <div class="border border-gray-200 rounded-lg p-4">
                                    <h4 class="text-sm font-medium text-gray-900 mb-3 flex items-center">
                                        <svg class="w-4 h-4 mr-2 text-indigo-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 6.253v13m0-13C10.832 5.477 9.246 5 7.5 5S4.168 5.477 3 6.253v13C4.168 18.477 5.754 18 7.5 18s3.332.477 4.5 1.253m0-13C13.168 5.477 14.754 5 16.5 5c1.747 0 3.332.477 4.5 1.253v13C19.832 18.477 18.247 18 16.5 18c-1.746 0-3.332.477-4.5 1.253"></path>
                                        </svg>
                                        选择知识点
                                    </h4>
                                    <select
                                        wire:model.live="selectedKnowledgePoint"
                                        class="block w-full rounded-lg border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 text-sm bg-gray-50"
                                    >
                                        <option value="">随机知识点</option>
                                        @foreach ($availableKnowledgePoints as $kp)
                                            <option value="{{ $kp['id'] ?? $kp['code'] ?? $kp }}">{{ $kp['name'] ?? $kp['code'] ?? $kp }}</option>
                                        @endforeach
                                    </select>
                                </div>

                                {{-- 技能选择 --}}
                                <div class="border border-gray-200 rounded-lg p-4">
                                    <h4 class="text-sm font-medium text-gray-900 mb-3 flex items-center">
                                        <svg class="w-4 h-4 mr-2 text-green-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 10V3L4 14h7v7l9-11h-7z"></path>
                                        </svg>
                                        选择技能（可多选）
                                    </h4>
                                    <div class="space-y-2 max-h-32 overflow-y-auto">
                                        @foreach ($availableSkills as $skill)
                                            <label class="flex items-center">
                                                <input
                                                    type="checkbox"
                                                    wire:model.live="selectedSkills"
                                                    value="{{ $skill['id'] ?? $skill['code'] ?? $skill }}"
                                                    class="rounded border-gray-300 text-indigo-600 focus:ring-indigo-500"
                                                />
                                                <span class="ml-2 text-sm text-gray-700">{{ $skill['name'] ?? $skill['code'] ?? $skill }}</span>
                                            </label>
                                        @endforeach
                                    </div>
                                </div>
                            </div>

                            {{-- 题目数量和生成按钮 --}}
                            <div class="flex items-center space-x-4 p-4 bg-gray-50 rounded-lg">
                                <div class="flex items-center space-x-2">
                                    <label class="text-sm font-medium text-gray-700">题目数量:</label>
                                    <input
                                        type="number"
                                        wire:model.live="questionsPerSet"
                                        min="1"
                                        max="10"
                                        class="w-16 rounded-lg border-gray-300 text-sm text-center bg-gray-50"
                                    />
                                </div>
                                <button
                                    wire:click="generateBatchQuestions"
                                    wire:loading.attr="disabled"
                                    class="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-lg shadow-sm text-white bg-green-600 hover:bg-green-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-green-500"
                                >
                                    <svg wire:loading class="animate-spin -ml-1 mr-2 h-4 w-4 text-white" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
                                        <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
                                        <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
                                    </svg>
                                    生成题目组
                                </button>
                                <div class="text-sm text-gray-500">
                                    当前批次ID: {{ $currentBatchId ?: '未生成' }}
                                </div>
                            </div>

                            {{-- 题目列表 --}}
                            @if (!empty($exerciseQuestions))
                                <div class="border border-gray-200 rounded-lg p-4">
                                    <h4 class="text-sm font-medium text-gray-900 mb-4">题目列表 (请标记对错)</h4>
                                    <div class="space-y-4 max-h-96 overflow-y-auto">
                                        @foreach ($exerciseQuestions as $index => $question)
                                            <div class="border border-gray-200 rounded-lg p-4">
                                                <div class="flex items-start justify-between mb-3">
                                                    <div class="flex-1">
                                                        <div class="flex items-center mb-2">
                                                            <span class="inline-flex items-center justify-center w-6 h-6 rounded-full bg-indigo-100 text-indigo-800 text-xs font-medium mr-2">
                                                                {{ $index + 1 }}
                                                            </span>
                                                            <span class="text-sm text-gray-500">{{ $question['type'] ?? '数学题' }}</span>
                                                            <span class="mx-2 text-gray-300">|</span>
                                                            <span class="text-sm text-gray-500">难度: {{ $question['difficulty'] ?? 3 }}/5</span>
                                                        </div>
                                                        <h5 class="text-base font-medium text-gray-900">
                                                            {{ $question['content'] ?? '' }}
                                                        </h5>
                                                    </div>
                                                </div>

                                                <div class="grid grid-cols-1 lg:grid-cols-2 gap-4">
                                                    <div>
                                                        <label class="block text-sm font-medium text-gray-700 mb-1">学生答案（可选）</label>
                                                        <input
                                                            type="text"
                                                            wire:model.live="exerciseAnswers.{{ $index }}.user_answer"
                                                            placeholder="输入答案..."
                                                            class="block w-full rounded-lg border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 text-sm bg-gray-50"
                                                        />
                                                    </div>
                                                    <div>
                                                        <label class="block text-sm font-medium text-gray-700 mb-1">答题结果</label>
                                                        <div class="flex space-x-4">
                                                            <label class="flex items-center">
                                                                <input
                                                                    type="radio"
                                                                    wire:model.live="exerciseAnswers.{{ $index }}.is_correct"
                                                                    value="1"
                                                                    class="h-4 w-4 text-green-600 focus:ring-green-500 border-gray-300"
                                                                />
                                                                <span class="ml-2 text-sm text-green-700">正确</span>
                                                            </label>
                                                            <label class="flex items-center">
                                                                <input
                                                                    type="radio"
                                                                    wire:model.live="exerciseAnswers.{{ $index }}.is_correct"
                                                                    value="0"
                                                                    class="h-4 w-4 text-red-600 focus:ring-red-500 border-gray-300"
                                                                />
                                                                <span class="ml-2 text-sm text-red-700">错误</span>
                                                            </label>
                                                        </div>
                                                    </div>
                                                </div>

                                                <div class="mt-3 p-2 bg-green-50 border border-green-200 rounded">
                                                    <span class="text-xs text-green-800">正确答案: </span>
                                                    <span class="text-xs">
                                                        {{ $question['answer'] ?? 'N/A' }}
                                                    </span>
                                                </div>
                                            </div>
                                        @endforeach
                                    </div>

                                    {{-- 批量提交按钮 --}}
                                    <div class="mt-6 pt-4 border-t border-gray-200">
                                        <button
                                            wire:click="submitBatchAnswers"
                                            wire:loading.attr="disabled"
                                            class="w-full inline-flex items-center justify-center px-6 py-3 border border-transparent text-sm font-medium rounded-lg shadow-sm text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
                                        >
                                            <svg wire:loading class="animate-spin -ml-1 mr-2 h-4 w-4 text-white" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
                                                <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
                                                <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
                                            </svg>
                                            <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"></path>
                                            </svg>
                                            批量提交答案 ({{ count($exerciseQuestions) }} 题)
                                        </button>
                                    </div>
                                </div>
                            @else
                                {{-- 批量模式空状态 --}}
                                <div class="text-center py-12">
                                    <svg class="mx-auto h-16 w-16 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 11H5m14 0a2 2 0 012 2v6a2 2 0 01-2 2H5a2 2 0 01-2-2v-6a2 2 0 012-2m14 0V9a2 2 0 00-2-2M5 11V9a2 2 0 012-2m0 0V5a2 2 0 012-2h6a2 2 0 012 2v2M7 7h10"></path>
                                    </svg>
                                    <h3 class="mt-4 text-lg font-medium text-gray-900">批量练习模式</h3>
                                    <p class="mt-2 text-sm text-gray-500 max-w-md mx-auto">
                                        选择知识点和技能，设置题目数量，点击"生成题目组"开始批量答题练习
                                    </p>
                                </div>
                            @endif
                        </div>
                    </div>
                </div>
            </div>
        </div>

        {{-- 答题历史模块 --}}
        @if (!empty($studentId))
            <div class="mb-8">
                <div class="bg-white shadow-sm rounded-xl border border-gray-200">
                    <div class="px-6 py-5 border-b border-gray-100">
                        <h3 class="text-lg font-semibold text-gray-900 flex items-center">
                            <svg class="w-5 h-5 mr-2 text-purple-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"></path>
                            </svg>
                            答题历史
                        </h3>
                    </div>
                    <div class="p-6">
                        {{-- 每页显示数量选择 --}}
                        <div class="flex items-center justify-between mb-6">
                            <div class="flex items-center space-x-2">
                                <label class="text-sm font-medium text-gray-700">每页显示:</label>
                                <select
                                    wire:model.live="historyPerPage"
                                    class="rounded-lg border-gray-300 text-sm bg-gray-50"
                                >
                                    <option value="5">5 条</option>
                                    <option value="10">10 条</option>
                                    <option value="20">20 条</option>
                                    <option value="50">50 条</option>
                                </select>
                            </div>
                            <div class="text-sm text-gray-500">
                                共 {{ $historyTotal }} 条记录
                            </div>
                        </div>

                        {{-- 历史记录列表 --}}
                        @if (!empty($exerciseHistory))
                            <div class="space-y-4">
                                @foreach ($exerciseHistory as $history)
                                    <div class="border border-gray-200 rounded-lg p-4 hover:bg-gray-50 transition-colors">
                                        <div class="flex items-start justify-between mb-3">
                                            <div class="flex-1">
                                                <div class="flex items-center mb-2">
                                                    <span class="inline-flex items-center justify-center w-6 h-6 rounded-full bg-purple-100 text-purple-800 text-xs font-medium mr-2">
                                                        {{ $loop->iteration + ($historyCurrentPage - 1) * $historyPerPage }}
                                                    </span>
                                                    <span class="text-sm text-gray-500">批次: {{ $history['batch_id'] ?? 'N/A' }}</span>
                                                    <span class="mx-2 text-gray-300">|</span>
                                                    <span class="text-sm text-gray-500">
                                                        {{ $history['kp_code'] ?? 'N/A' }}
                                                    </span>
                                                </div>
                                                <h5 class="text-base font-medium text-gray-900 mb-2">
                                                    {{ Str::limit($history['question_content'] ?? 'N/A', 100) }}
                                                </h5>
                                                <div class="grid grid-cols-1 lg:grid-cols-2 gap-4 mt-3">
                                                    <div>
                                                        <span class="text-xs text-gray-500">学生答案:</span>
                                                        <div class="text-sm text-gray-900">
                                                            {{ $history['student_answer'] ?? '未填写' }}
                                                        </div>
                                                    </div>
                                                    <div>
                                                        <span class="text-xs text-gray-500">正确答案:</span>
                                                        <div class="text-sm text-gray-900">
                                                            {{ $history['correct_answer'] ?? 'N/A' }}
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="ml-4 text-right">
                                                @if ($history['is_correct'])
                                                    <span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-medium bg-green-100 text-green-800">
                                                        <svg class="w-3 h-3 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"></path>
                                                        </svg>
                                                        正确
                                                    </span>
                                                @else
                                                    <span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-medium bg-red-100 text-red-800">
                                                        <svg class="w-3 h-3 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path>
                                                        </svg>
                                                        错误
                                                    </span>
                                                @endif
                                                <div class="mt-2 text-xs text-gray-500">
                                                    {{ date('Y-m-d H:i', strtotime($history['created_at'])) }}
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                @endforeach
                            </div>

                            {{-- 分页导航 --}}
                            @if ($historyTotalPages > 1)
                                <div class="mt-6 pt-4 border-t border-gray-200 flex items-center justify-between">
                                    <div class="text-sm text-gray-700">
                                        显示第 {{ ($historyCurrentPage - 1) * $historyPerPage + 1 }} -
                                        {{ min($historyCurrentPage * $historyPerPage, $historyTotal) }} 条，
                                        共 {{ $historyTotal }} 条记录
                                    </div>
                                    <div class="flex items-center gap-2">
                                        <button
                                            wire:click="previousHistoryPage"
                                            @disabled($historyCurrentPage <= 1)
                                            class="px-3 py-1 border rounded-lg text-sm hover:bg-gray-50 disabled:opacity-50 disabled:cursor-not-allowed"
                                        >
                                            上一页
                                        </button>

                                        @foreach ($this->getHistoryPages() as $page)
                                            <button
                                                wire:click="gotoHistoryPage({{ $page }})"
                                                class="px-3 py-1 border rounded-lg text-sm {{ $page === $historyCurrentPage ? 'bg-indigo-600 text-white' : 'hover:bg-gray-50' }}"
                                            >
                                                {{ $page }}
                                            </button>
                                        @endforeach

                                        <button
                                            wire:click="nextHistoryPage"
                                            @disabled($historyCurrentPage >= $historyTotalPages)
                                            class="px-3 py-1 border rounded-lg text-sm hover:bg-gray-50 disabled:opacity-50 disabled:cursor-not-allowed"
                                        >
                                            下一页
                                        </button>
                                    </div>
                                </div>
                            @endif
                        @else
                            <div class="text-center py-12">
                                <svg class="mx-auto h-16 w-16 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"></path>
                                </svg>
                                <h3 class="mt-4 text-lg font-medium text-gray-900">暂无答题历史</h3>
                                <p class="mt-2 text-sm text-gray-500 max-w-md mx-auto">
                                    选择学生后，答题历史将显示在这里
                                </p>
                            </div>
                        @endif
                    </div>
                </div>
            </div>
        @endif
    @endif
</div>

{{-- 通知脚本 --}}
<script>
    document.addEventListener('notify', (event) => {
        const message = event.detail.message;
        const type = event.detail.type || 'info';
        alert(message);
    });
</script>

</x-filament-panels::page>
