<x-filament::page>
    @php($stats = $this->stats())
    <div class="space-y-4">
        <div class="grid grid-cols-4 gap-4">
            <x-filament::section>
                <div class="text-xs text-gray-500">待审核</div>
                <div class="text-2xl font-semibold text-gray-900">{{ $stats['pending'] ?? 0 }}</div>
            </x-filament::section>
            <x-filament::section>
                <div class="text-xs text-gray-500">已审核</div>
                <div class="text-2xl font-semibold text-gray-900">{{ $stats['reviewed'] ?? 0 }}</div>
            </x-filament::section>
            <x-filament::section>
                <div class="text-xs text-gray-500">已通过</div>
                <div class="text-2xl font-semibold text-gray-900">{{ $stats['accepted'] ?? 0 }}</div>
            </x-filament::section>
            <x-filament::section>
                <div class="text-xs text-gray-500">总题量</div>
                <div class="text-2xl font-semibold text-gray-900">{{ $stats['all'] ?? 0 }}</div>
            </x-filament::section>
        </div>

        <div class="flex flex-wrap items-center gap-3">
            <x-filament::input.wrapper class="w-44">
                <x-filament::input.select wire:model="groupBy">
                    <option value="paper">按卷子分组</option>
                    <option value="part">按区块分组</option>
                    <option value="type">按题型分组</option>
                </x-filament::input.select>
            </x-filament::input.wrapper>
            <x-filament::button color="gray" wire:click="selectAllVisible">全选列表</x-filament::button>
            <x-filament::button color="gray" wire:click="clearSelection">清空选择</x-filament::button>
            <x-filament::button color="gray" wire:click="jumpToNextIssue">跳转下一个问题</x-filament::button>
        </div>

        <div class="grid grid-cols-12 gap-6">
            <div class="col-span-4">
                <x-filament::section>
                    <div class="text-sm text-gray-500 mb-2">题目列表</div>
                    <div class="max-h-[520px] overflow-y-auto divide-y divide-gray-100">
                        @foreach($this->groupedCandidates() as $group => $items)
                            <div class="px-3 py-2 text-xs font-semibold text-slate-500 bg-slate-50">{{ $group }}</div>
                            @foreach($items as $candidate)
                                @php($meta = $candidate->meta ?? [])
                                <label class="flex gap-2 py-2">
                                    <input type="checkbox" wire:model="selectedIds" value="{{ $candidate->id }}" class="mt-1 rounded border-gray-300">
                                    <button type="button" wire:click="selectCandidate({{ $candidate->id }})" class="text-left flex-1">
                                        <div class="text-sm text-gray-900">{{ \Illuminate\Support\Str::limit($candidate->stem ?? $candidate->raw_markdown, 80) }}</div>
                                        <div class="text-xs text-gray-500 flex flex-wrap gap-2 items-center">
                                            <span>{{ $candidate->sourcePaper?->title }} · {{ $candidate->part?->title }}</span>
                                            @if(($candidate->ai_confidence ?? 0) < 0.6)
                                                <span class="px-2 py-0.5 rounded bg-amber-50 text-amber-700">低置信度</span>
                                            @endif
                                            @if(!$candidate->stem)
                                                <span class="px-2 py-0.5 rounded bg-rose-50 text-rose-700">缺题干</span>
                                            @endif
                                            @if(empty($meta['difficulty']))
                                                <span class="px-2 py-0.5 rounded bg-rose-50 text-rose-700">缺难度</span>
                                            @endif
                                            @if(empty($meta['kp_codes'] ?? []))
                                                <span class="px-2 py-0.5 rounded bg-rose-50 text-rose-700">缺知识点</span>
                                            @endif
                                            @if(empty($meta['answer']))
                                                <span class="px-2 py-0.5 rounded bg-rose-50 text-rose-700">缺答案</span>
                                            @endif
                                        </div>
                                    </button>
                                </label>
                            @endforeach
                        @endforeach
                    </div>
                </x-filament::section>
            </div>

            <div class="col-span-5">
                <x-filament::section>
                    <div class="text-sm text-gray-500 mb-2">题目内容</div>
                    <div class="prose prose-sm max-w-none bg-gray-50 p-4 rounded-lg min-h-[420px]">
                        {!! \App\Services\MathFormulaProcessor::processFormulas($this->currentCandidate()?->stem ?? $this->currentCandidate()?->raw_markdown ?? '') !!}
                    </div>
                </x-filament::section>
            </div>

            <div class="col-span-3 space-y-4">
                <x-filament::section heading="审核动作">
                    <div class="space-y-2">
                        <x-filament::button color="success" wire:click="approve">
                            标记为有效题目
                        </x-filament::button>
                        <x-filament::button color="danger" wire:click="reject">
                            标记为无效
                        </x-filament::button>
                    </div>
                </x-filament::section>

                <x-filament::section heading="批量审核">
                    <div class="space-y-2">
                        <x-filament::button color="success" wire:click="bulkApprove">
                            批量通过
                        </x-filament::button>
                        <x-filament::button color="danger" x-on:click.prevent="if(confirm('确认批量拒绝选中题目？')) { $wire.bulkReject() }">
                            批量拒绝
                        </x-filament::button>
                    </div>
                </x-filament::section>

                <x-filament::section heading="AI 辅助">
                    <div class="space-y-2">
                        <x-filament::button color="gray" wire:click="aiMatchKp">推荐知识点</x-filament::button>
                        <x-filament::button color="gray" wire:click="aiGenerateSolution">生成解析</x-filament::button>
                        <x-filament::button color="primary" wire:click="aiAssist">智能补全</x-filament::button>
                    </div>
                </x-filament::section>

                <x-filament::section heading="风险提示">
                    @php($meta = $this->currentCandidate()?->meta ?? [])
                    <div class="space-y-2 text-sm text-slate-600">
                        <div>AI 置信度：{{ $this->currentCandidate()?->ai_confidence ? number_format($this->currentCandidate()->ai_confidence * 100, 1) . '%' : '—' }}</div>
                        <div>缺失字段：</div>
                        <div class="flex flex-wrap gap-2">
                            @if(empty($meta['difficulty']))
                                <span class="px-2 py-0.5 rounded bg-rose-50 text-rose-700">难度</span>
                            @endif
                            @if(empty($meta['kp_codes'] ?? []))
                                <span class="px-2 py-0.5 rounded bg-rose-50 text-rose-700">知识点</span>
                            @endif
                            @if(empty($meta['answer']))
                                <span class="px-2 py-0.5 rounded bg-rose-50 text-rose-700">答案</span>
                            @endif
                            @if(empty($meta['solution']))
                                <span class="px-2 py-0.5 rounded bg-rose-50 text-rose-700">解析</span>
                            @endif
                            @if(!empty($meta['difficulty']) && !empty($meta['kp_codes'] ?? []) && !empty($meta['answer']) && !empty($meta['solution']))
                                <span class="px-2 py-0.5 rounded bg-emerald-50 text-emerald-700">无</span>
                            @endif
                        </div>
                    </div>
                </x-filament::section>
            </div>
        </div>
    </div>
</x-filament::page>
