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

            <!-- 快速统计 -->
            <x-exam-analysis.quick-stats :recordData="$recordData" />

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

                @if($useChatGPT)
                    <div class="p-4">
                        <div class="space-y-3">
                            <div>
                                <label class="block text-sm font-medium text-gray-700 mb-1">
                                    试卷图片URL <span class="text-red-500">*</span>
                                </label>
                                <input
                                    type="url"
                                    wire:model="imageUrl"
                                    placeholder="请输入试卷图片URL"
                                    class="w-full px-3 py-2 text-sm border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
                                />
                            </div>

                            @if(!empty($imageUrl))
                                <div class="flex items-center gap-2">
                                    <button
                                        wire:click="analyzeWithChatGPT"
                                        wire:loading.attr="disabled"
                                        class="px-4 py-2 bg-blue-600 text-white text-sm font-medium rounded-md 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-1"
                                    >
                                        @if($isAnalyzing)
                                            <svg class="animate-spin 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>
                                            分析中...
                                        @else
                                            <svg class="w-4 h-4" 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-3 py-2 bg-gray-200 text-gray-700 text-sm font-medium rounded-md hover:bg-gray-300 focus:outline-none focus:ring-2 focus:ring-gray-400"
                                    >
                                        重置
                                    </button>
                                </div>

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

                            <!-- ChatGPT分析结果 -->
                            @if(!empty($chatGPTResult))
                                <div class="mt-3 p-3 bg-green-50 border border-green-200 rounded-md">
                                    <h3 class="text-sm font-semibold text-green-900 mb-1">✅ 分析完成</h3>
                                    <div class="text-xs 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>
                            @endif
                        </div>
                    </div>
                @else
                    <div class="p-4 text-center text-gray-500">
                        <p class="text-sm">启用ChatGPT直接从图片识别答案</p>
                    </div>
                @endif
            </div>

            <!-- 学习分析概览 -->
            <x-exam-analysis.learning-analysis :analysisData="$analysisData" />

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

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