<div>
    <div class="space-y-6">
        @if($loading)
            <x-exam-analysis.loading message="正在分析试卷数据..." />
        @else
            <!-- 页面头部 -->
            <x-exam-analysis.header :recordData="$recordData" title="📊 试卷分析报告" />

            <!-- 试卷基本信息 -->
            <div class="bg-white rounded-lg shadow-sm border border-gray-200">
                <div class="p-6 border-b border-gray-200">
                    <h2 class="text-lg font-semibold text-gray-900">📝 试卷信息</h2>
                </div>
                <div class="p-6">
                    <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
                        <div>
                            <label class="block text-sm font-medium text-gray-500 mb-1">试卷名称</label>
                            <p class="text-lg font-medium text-gray-900">{{ $recordData['paper_name'] ?? 'N/A' }}</p>
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-500 mb-1">试卷编号</label>
                            <p class="text-lg font-mono text-gray-900">{{ $recordData['paper_id'] ?? 'N/A' }}</p>
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-500 mb-1">题目数量</label>
                            <p class="text-lg font-medium text-gray-900">{{ $recordData['total_questions'] ?? 0 }} 题</p>
                        </div>
                    </div>
                </div>
            </div>

            <!-- ChatGPT智能识别 -->
            <div class="bg-white rounded-lg shadow-sm border border-gray-200">
                <div class="p-6 border-b border-gray-200">
                    <div class="flex items-center justify-between">
                        <h2 class="text-lg font-semibold text-gray-900">🤖 ChatGPT智能识别</h2>
                        <div class="flex items-center gap-2">
                            <input type="checkbox" wire:model="useChatGPT" id="useChatGPT" class="rounded border-gray-300 text-blue-600 focus:ring-blue-500">
                            <label for="useChatGPT" class="text-sm text-gray-700">启用ChatGPT识别</label>
                        </div>
                    </div>
                </div>

                @if($useChatGPT)
                    <div class="p-6">
                        <div class="space-y-4">
                            <div>
                                <label class="block text-sm font-medium text-gray-700 mb-2">
                                    试卷图片URL <span class="text-red-500">*</span>
                                </label>
                                <input
                                    type="url"
                                    wire:model="imageUrl"
                                    placeholder="请输入试卷图片的完整URL地址"
                                    class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
                                />
                                <p class="text-xs text-gray-500 mt-1">
                                    支持JPG、PNG格式图片，建议图片清晰、光线充足
                                </p>
                            </div>

                            @if(!empty($imageUrl))
                                <div class="flex items-center gap-4">
                                    <button
                                        wire:click="analyzeWithChatGPT"
                                        wire:loading.attr="disabled"
                                        class="px-6 py-3 bg-blue-600 text-white font-medium rounded-lg hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500 disabled:opacity-50 disabled:cursor-not-allowed flex items-center gap-2"
                                    >
                                        @if($isAnalyzing)
                                            <svg class="animate-spin h-5 w-5 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>
                                            分析中...
                                        @else
                                            <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9.663 17h4.673M12 3v1m6.364 1.636l-.707.707M21 12h-1M4 12H3m3.343-5.657l-.707-.707m2.828 9.9a5 5 0 117.072 0l-.548.547A3.374 3.374 0 0014 18.469V19a2 2 0 11-4 0v-.531c0-.895-.356-1.754-.988-2.386l-.548-.547z"></path>
                                            </svg>
                                            开始智能识别
                                        @endif
                                    </button>

                                    <button
                                        wire:click="resetChatGPTForm"
                                        class="px-4 py-2 bg-gray-200 text-gray-700 font-medium rounded-lg hover:bg-gray-300 focus:outline-none focus:ring-2 focus:ring-gray-400"
                                    >
                                        重置
                                    </button>
                                </div>

                                <!-- 图片预览 -->
                                <div class="mt-4">
                                    <p class="text-sm font-medium text-gray-700 mb-2">图片预览</p>
                                    <div class="border border-gray-300 rounded-lg overflow-hidden max-w-2xl">
                                        <img src="{{ $imageUrl }}" alt="试卷图片" class="w-full h-auto" onerror="this.style.display='none'">
                                    </div>
                                </div>
                            @endif

                            <!-- ChatGPT分析结果 -->
                            @if(!empty($chatGPTResult))
                                <div class="mt-6 p-4 bg-green-50 border border-green-200 rounded-lg">
                                    <h3 class="text-sm font-semibold text-green-900 mb-2">✅ ChatGPT分析完成</h3>
                                    <div class="text-sm text-green-800">
                                        <p>已成功分析 {{ count($chatGPTResult['questions'] ?? []) }} 道题目</p>
                                        <p>总体得分：{{ $chatGPTResult['score_obtained'] ?? 0 }} / {{ $chatGPTResult['total_score'] ?? 0 }}</p>
                                        <p>整体掌握度：{{ number_format(($chatGPTResult['summary']['overall_mastery'] ?? 0) * 100, 1) }}%</p>
                                    </div>
                                </div>

                                <!-- ChatGPT分析详情 -->
                                @if(isset($chatGPTResult['questions']))
                                    <div class="mt-4">
                                        <h4 class="text-sm font-semibold text-gray-700 mb-3">题目分析详情</h4>
                                        <div class="space-y-3 max-h-96 overflow-y-auto">
                                            @foreach($chatGPTResult['questions'] as $q)
                                                <div class="border rounded-lg p-3 {{ $q['is_correct'] ? 'border-green-200 bg-green-50' : 'border-red-200 bg-red-50' }}">
                                                    <div class="flex items-start justify-between mb-2">
                                                        <span class="text-sm font-medium text-gray-900">第{{ $q['q'] }}题</span>
                                                        <span class="text-xs px-2 py-1 rounded-full {{ $q['is_correct'] ? 'bg-green-100 text-green-800' : 'bg-red-100 text-red-800' }}">
                                                            {{ $q['is_correct'] ? '正确' : '错误' }}
                                                        </span>
                                                    </div>
                                                    <div class="text-xs text-gray-600 space-y-1">
                                                        <p><span class="font-medium">学生答案：</span>{{ $q['student_answer'] ?? '未识别到答案' }}</p>
                                                        <p><span class="font-medium">正确答案：</span>{{ $q['correct_answer'] ?? 'N/A' }}</p>
                                                        @if(isset($q['knowledge_points'][0]))
                                                            <p><span class="font-medium">知识点：</span>{{ $q['knowledge_points'][0]['name'] ?? $q['knowledge_points'][0]['id'] }}</p>
                                                            <p><span class="font-medium">掌握度：</span>{{ number_format($q['knowledge_points'][0]['mastery'] ?? 0, 1) }}%</p>
                                                        @endif
                                                    </div>
                                                </div>
                                            @endforeach
                                        </div>
                                    </div>
                                @endif

                                <!-- 学习建议 -->
                                @if(isset($chatGPTResult['summary']['suggested_study_path']))
                                    <div class="mt-4 p-4 bg-blue-50 border border-blue-200 rounded-lg">
                                        <h4 class="text-sm font-semibold text-blue-900 mb-2">📚 个性化学习建议</h4>
                                        <div class="text-sm text-blue-800 space-y-1">
                                            @foreach($chatGPTResult['summary']['suggested_study_path'] as $path)
                                                <p>• {{ $path }}</p>
                                            @endforeach
                                        </div>
                                    </div>
                                @endif
                            @endif
                        </div>
                    </div>
                @else
                    <div class="p-6 text-center text-gray-500">
                        <svg class="w-12 h-12 mx-auto text-gray-300 mb-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9.663 17h4.673M12 3v1m6.364 1.636l-.707.707M21 12h-1M4 12H3m3.343-5.657l-.707-.707m2.828 9.9a5 5 0 117.072 0l-.548.547A3.374 3.374 0 0014 18.469V19a2 2 0 11-4 0v-.531c0-.895-.356-1.754-.988-2.386l-.548-.547z"></path>
                        </svg>
                        <p class="text-sm">启用ChatGPT识别功能，直接从图片中识别学生答案并智能分析</p>
                    </div>
                @endif
            </div>

            <!-- 学习统计概览 -->
            @if(!empty($analysisData))
            <div class="bg-white rounded-lg shadow-sm border border-gray-200">
                <div class="p-6 border-b border-gray-200">
                    <h2 class="text-lg font-semibold text-gray-900">📊 学习统计概览</h2>
                </div>
                <div class="p-6">
                    <div class="grid grid-cols-1 md:grid-cols-4 gap-6">
                        <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-6">
                            <div class="flex items-center">
                                <div class="p-3 rounded-full bg-blue-100 text-blue-600">
                                    <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 19v-6a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2a2 2 0 002-2zm0 0V9a2 2 0 012-2h2a2 2 0 012 2v10m-6 0a2 2 0 002 2h2a2 2 0 002-2m0 0V5a2 2 0 012-2h2a2 2 0 012 2v14a2 2 0 01-2 2h-2a2 2 0 01-2-2z"></path>
                                    </svg>
                                </div>
                                <div class="ml-4">
                                    <p class="text-sm text-gray-500">整体掌握度</p>
                                    <p class="text-2xl font-bold text-gray-900">{{ number_format(($analysisData['overall_mastery'] ?? 0) * 100, 1) }}%</p>
                                </div>
                            </div>
                        </div>

                        <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-6">
                            <div class="flex items-center">
                                <div class="p-3 rounded-full bg-green-100 text-green-600">
                                    <svg class="w-6 h-6" 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>
                                </div>
                                <div class="ml-4">
                                    <p class="text-sm text-gray-500">知识点数量</p>
                                    <p class="text-2xl font-bold text-gray-900">{{ $analysisData['total_knowledge_points'] ?? 0 }}</p>
                                </div>
                            </div>
                        </div>

                        <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-6">
                            <div class="flex items-center">
                                <div class="p-3 rounded-full bg-yellow-100 text-yellow-600">
                                    <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-2.5L13.732 4c-.77-.833-1.964-.833-2.732 0L3.732 16.5c-.77.833.192 2.5 1.732 2.5z"></path>
                                    </svg>
                                </div>
                                <div class="ml-4">
                                    <p class="text-sm text-gray-500">薄弱知识点</p>
                                    <p class="text-2xl font-bold text-gray-900">{{ count($analysisData['weak_areas'] ?? []) }}</p>
                                </div>
                            </div>
                        </div>

                        <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-6">
                            <div class="flex items-center">
                                <div class="p-3 rounded-full bg-purple-100 text-purple-600">
                                    <svg class="w-6 h-6" 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>
                                </div>
                                <div class="ml-4">
                                    <p class="text-sm text-gray-500">总答题数</p>
                                    <p class="text-2xl font-bold text-gray-900">{{ $recordData['total_questions'] ?? 0 }}</p>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- 知识点掌握情况 -->
                    @if(isset($analysisData['knowledge_points']) && !empty($analysisData['knowledge_points']))
                    <div class="mt-6">
                        <h3 class="text-sm font-semibold text-gray-700 mb-3">知识点掌握情况</h3>
                        <div class="space-y-3">
                            @foreach($analysisData['knowledge_points'] as $kp)
                            <div class="border rounded-lg p-4
                                {{ ($kp['mastery_level'] ?? 0) < 0.6 ? 'border-red-200 bg-red-50' :
                                   (($kp['mastery_level'] ?? 0) < 0.8 ? 'border-yellow-200 bg-yellow-50' :
                                   'border-green-200 bg-green-50') }}">
                                <div class="flex items-center justify-between mb-2">
                                    <h4 class="font-medium text-gray-900">{{ $kp['kp_code'] ?? 'Unknown' }}</h4>
                                    <span class="text-sm font-bold
                                        {{ ($kp['mastery_level'] ?? 0) < 0.6 ? 'text-red-600' :
                                           (($kp['mastery_level'] ?? 0) < 0.8 ? 'text-yellow-600' : 'text-green-600') }}">
                                        {{ number_format(($kp['mastery_level'] ?? 0) * 100, 1) }}%
                                    </span>
                                </div>
                                <div class="w-full bg-gray-200 rounded-full h-2 mb-2">
                                    <div class="h-2 rounded-full
                                        {{ ($kp['mastery_level'] ?? 0) < 0.6 ? 'bg-red-400' :
                                           (($kp['mastery_level'] ?? 0) < 0.8 ? 'bg-yellow-400' : 'bg-green-400') }}"
                                         style="width: {{ ($kp['mastery_level'] ?? 0) * 100 }}%"></div>
                                </div>
                                <p class="text-xs text-gray-600">
                                    正确率: {{ number_format(($kp['accuracy_rate'] ?? 0) * 100, 1) }}% |
                                    练习次数: {{ $kp['total_attempts'] ?? 0 }}
                                </p>
                            </div>
                            @endforeach
                        </div>
                    </div>
                    @endif
                </div>
            </div>
            @endif

            <!-- 题目详情分析 -->
            <x-exam-analysis.question-details :questions="$recordData['questions'] ?? []" />

            <!-- 学习建议 -->
            @if(isset($analysisData['recommendations']))
                <x-exam-analysis.recommendations :recommendations="$analysisData['recommendations']" />
            @endif
        @endif
    </div>
</div>
