<div class="ui-page">
    <div class="mx-auto flex max-w-7xl flex-col gap-6 px-4 py-8">
        @include('filament.partials.page-header', [
            'kicker' => '教材详情',
            'title' => $this->record->official_title ?? '教材详情',
            'subtitle' => '查看教材信息、目录结构与关联卷子',
            'actions' => new \Illuminate\Support\HtmlString(
                ($this->record?->id
                    ? '<a class="btn btn-primary" href="' . route('filament.admin.resources.textbooks.edit', ['record' => $this->record->id]) . '">编辑教材</a>'
                      . '<a class="btn btn-outline" href="' . route('filament.admin.resources.textbooks.edit', ['record' => $this->record->id]) . '#cover">上传封面</a>'
                    : ''
                )
                . '<a class="btn btn-secondary" href="' . route('filament.admin.pages.textbook-excel-import-page') . '?type=textbook_catalog">导入目录</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-4">
                        <div class="flex items-start gap-4">
                            @php
                                $cover = $this->record->cover_path ?? null;
                                $coverUrl = null;
                                if ($cover) {
                                    $coverUrl = \Illuminate\Support\Str::startsWith($cover, ['http://', 'https://', '/'])
                                        ? $cover
                                        : \Illuminate\Support\Facades\Storage::disk('public')->url($cover);
                                }
                            @endphp
                            @if($coverUrl)
                                <img src="{{ $coverUrl }}" alt="封面" class="h-28 w-20 rounded-lg border border-slate-200 object-cover" />
                            @else
                                <div class="flex h-28 w-20 items-center justify-center rounded-lg border border-dashed border-slate-200 bg-slate-50 text-xs text-slate-400">暂无封面</div>
                            @endif
                            <div class="space-y-2">
                                <div class="text-lg font-semibold text-slate-900">{{ $this->record->official_title ?? '未命名教材' }}</div>
                                <div class="text-sm text-slate-500">系列：{{ $this->record->series_name ?? '未归类系列' }}</div>
                                <div class="flex flex-wrap gap-2">
                                    <span class="ui-tag">学段：{{ $this->record->stage ?? '未标注' }}</span>
                                    <span class="ui-tag">年级：{{ $this->record->grade ? $this->record->grade . '年级' : '未标注' }}</span>
                                    <span class="ui-tag">学期：{{ $this->record->semester == 1 ? '上学期' : ($this->record->semester == 2 ? '下学期' : '未标注') }}</span>
                                </div>
                            </div>
                        </div>
                        <div class="grid grid-cols-2 gap-3 text-sm text-slate-600">
                            <div class="ui-badge-muted">ISBN：{{ $this->record->isbn ?? '未填写' }}</div>
                            <div class="ui-badge-muted">状态：{{ $this->record->status ?? '未知' }}</div>
                            <div class="ui-badge-muted">ID：{{ $this->record->id }}</div>
                        </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">
                        @if(empty($this->linkedPapers))
                            @include('filament.partials.empty-state', [
                                'title' => '暂无关联卷子',
                                'description' => '当前教材系列尚未关联到源卷子。',
                            ])
                        @else
                            <div class="mb-3 text-xs text-slate-500">
                                目录未绑定卷子：{{ $this->unlinkedPaperCount }} 套
                            </div>
                            <div class="space-y-3">
                                @foreach($this->linkedPapers as $paper)
                                    <div class="rounded-xl border border-slate-200 px-4 py-3">
                                        <div class="text-sm font-semibold text-slate-900">{{ $paper['title'] }}</div>
                                        <div class="mt-1 flex flex-wrap gap-2 text-xs text-slate-500">
                                            <span class="ui-tag">章节：{{ $paper['chapter'] ?? '未标注' }}</span>
                                            <span class="ui-tag">年级：{{ $paper['grade'] ?? '未标注' }}</span>
                                            <span class="ui-tag">学期：{{ $paper['term'] ?? '未标注' }}</span>
                                            <span class="ui-tag">来源：{{ $paper['source_type'] ?? '未知' }}</span>
                                        </div>
                                    </div>
                                @endforeach
                            </div>
                        @endif
                    </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">教材章节目录树</div>
                        </div>
                        <a class="btn btn-outline btn-sm" href="{{ route('filament.admin.resources.textbook-catalogs.index', ['tableFilters[textbook_id][value]' => $this->record->id]) }}">管理目录</a>
                    </div>
                    <div class="ui-card-body">
                        @if(empty($this->catalogTree))
                            @include('filament.partials.empty-state', [
                                'title' => '暂无目录',
                                'description' => '请先导入或维护教材目录结构。',
                                'action' => new \Illuminate\Support\HtmlString('<a class="btn btn-primary btn-sm" href="' . route('filament.admin.pages.textbook-excel-import-page') . '?type=textbook_catalog">导入目录</a>'),
                            ])
                        @else
                            @include('filament.partials.catalog-tree', ['nodes' => $this->catalogTree, 'coverage' => $this->catalogCoverage])
                        @endif
                    </div>
                </div>
            </div>
        </div>
    </div>
    @include('filament.partials.loading-overlay')
</div>
