<x-filament-panels::page>
    <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>

            <!-- 学习统计概览 -->
            @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>
</x-filament-panels::page>
