<div class="ui-page">
    <div class="mx-auto flex max-w-7xl flex-col gap-6 px-4 py-8" x-data="{ expandAll: true }">
        @include('filament.partials.page-header', [
            'kicker' => '卷子详情',
            'title' => $this->record->title ?? '源卷子详情',
            'subtitle' => '查看卷子元信息与题型区块',
            'actions' => new \Illuminate\Support\HtmlString('<a class="btn btn-outline" href="' . route('filament.admin.resources.source-papers.index') . '">返回列表</a>'),
        ])

        <div class="grid grid-cols-1 gap-6 lg:grid-cols-3">
            <div class="space-y-6">
                <div class="ui-card">
                    <div class="ui-card-header">
                        <div class="ui-section-title">卷子元数据</div>
                    </div>
                    <div class="ui-card-body space-y-3 text-sm text-slate-600">
                        <div class="ui-badge-muted">来源文件：{{ $this->record->file?->original_filename ?? '未知' }}</div>
                        <div class="ui-badge-muted">来源类型：{{ $this->record->source_type ?? '未标注' }}</div>
                        <div class="ui-badge-muted">章节：{{ $this->record->chapter ?? '未标注' }}</div>
                        <div class="ui-badge-muted">年级：{{ $this->record->grade ?? '未标注' }}</div>
                        <div class="ui-badge-muted">学期：{{ $this->record->term ?? '未标注' }}</div>
                        <div class="ui-badge-muted">教材系列：{{ $this->record->textbook_series ?? '未标注' }}</div>
                    </div>
                </div>

                <div class="ui-card">
                    <div class="ui-card-header">
                        <div>
                            <div class="ui-section-title">快速操作</div>
                            <div class="ui-subtitle">批量展开或收起区块</div>
                        </div>
                    </div>
                    <div class="ui-card-body flex gap-2">
                        <button class="btn btn-secondary btn-sm" @click="expandAll = true">展开全部</button>
                        <button class="btn btn-ghost btn-sm" @click="expandAll = false">收起全部</button>
                    </div>
                </div>
            </div>

            <div class="lg:col-span-2">
                <div class="ui-card">
                    <div class="ui-card-header">
                        <div>
                            <div class="ui-section-title">题型区块列表</div>
                            <div class="ui-subtitle">paper_parts 结构预览</div>
                        </div>
                    </div>
                    <div class="ui-card-body space-y-4">
                        @forelse($this->paperParts as $part)
                            <div class="rounded-2xl border border-slate-200 bg-white" x-data="{ open: true }">
                                <div class="flex flex-wrap items-center justify-between gap-3 px-4 py-3">
                                    <div>
                                        <div class="text-sm font-semibold text-slate-900">{{ $part['title'] }}</div>
                                        <div class="mt-1 flex flex-wrap gap-2 text-xs text-slate-500">
                                            <span class="ui-tag">题型：{{ $part['type'] }}</span>
                                            <span class="ui-tag">题量：{{ $part['question_count'] }}</span>
                                            <span class="ui-tag">候选题：{{ $part['candidate_count'] }}</span>
                                            @if($part['has_error'])
                                                <span class="ui-tag text-rose-600 border-rose-200 bg-rose-50">解析异常</span>
                                            @endif
                                        </div>
                                    </div>
                                    <div class="flex items-center gap-2">
                                        <a class="btn btn-outline btn-xs" href="{{ route('filament.admin.resources.paper-parts.view', ['record' => $part['id']]) }}">查看题目</a>
                                        <button class="btn btn-ghost btn-xs" @click="open = !open" x-text="open ? '收起' : '展开'"></button>
                                    </div>
                                </div>
                                <div class="border-t border-slate-200 px-4 py-3 text-sm text-slate-500" x-show="expandAll && open">
                                    <div class="line-clamp-3">{{ \Illuminate\Support\Str::limit($part['raw_markdown'] ?? '', 240) }}</div>
                                </div>
                            </div>
                        @empty
                            @include('filament.partials.empty-state', [
                                'title' => '暂无题型区块',
                                'description' => '请确认 Markdown 解析是否完成。',
                            ])
                        @endforelse
                    </div>
                </div>
            </div>
        </div>
    </div>
    @include('filament.partials.loading-overlay')
</div>
