<x-filament::page>
    <div class="space-y-4">
        <div class="flex flex-wrap items-center gap-3">
            <x-filament::input.wrapper class="w-64">
                <x-filament::input
                    wire:model.debounce.500ms="search"
                    placeholder="搜索卷子标题/编码"
                />
            </x-filament::input.wrapper>

            <x-filament::input.wrapper class="w-32">
                <x-filament::input.select wire:model="gradeFilter">
                    <option value="">年级</option>
                    @foreach($this->gradeOptions() as $value => $label)
                        <option value="{{ $value }}">{{ $label }}</option>
                    @endforeach
                </x-filament::input.select>
            </x-filament::input.wrapper>

            <x-filament::input.wrapper class="w-32">
                <x-filament::input.select wire:model="termFilter">
                    <option value="">学期</option>
                    @foreach($this->termOptions() as $value => $label)
                        <option value="{{ $value }}">{{ $label }}</option>
                    @endforeach
                </x-filament::input.select>
            </x-filament::input.wrapper>

            <x-filament::button color="gray" wire:click="autoInfer">
                自动推断
            </x-filament::button>
            <x-filament::button color="gray" wire:click="autoInferSelected">
                批量推断
            </x-filament::button>

            <x-filament::button color="primary" wire:click="savePaper">
                保存当前卷子
            </x-filament::button>
            <x-filament::button color="gray" wire:click="$set('dense', ! $wire.dense)">
                密度切换
            </x-filament::button>
        </div>

        <div class="grid grid-cols-12 gap-6">
            <div class="col-span-8 space-y-4">
                <x-filament::section>
                    <div class="text-sm text-slate-500 mb-2">卷子列表（勾选后可批量覆盖）</div>
                    <div class="mb-2 flex flex-wrap gap-2">
                        <x-filament::button color="gray" wire:click="selectAllVisible">全选当前列表</x-filament::button>
                        <x-filament::button color="gray" wire:click="clearSelection">清空选择</x-filament::button>
                    </div>
                    <div class="max-h-64 overflow-y-auto divide-y divide-gray-100">
                        @foreach($this->papers() as $paper)
                            <label class="flex items-start gap-3 {{ $dense ? 'py-1' : 'py-2' }}">
                                <input type="checkbox" wire:model="selectedIds" value="{{ $paper->id }}" class="mt-1 rounded border-gray-300">
                                <button type="button" wire:click="selectPaper({{ $paper->id }})" class="text-left flex-1">
                                    <div class="text-sm font-medium text-gray-900">{{ $paper->title ?? $paper->full_title ?? '未命名' }}</div>
                                    <div class="text-xs text-gray-500">{{ $paper->paper_code }} · 年级 {{ $paper->grade ?? '-' }} · 学期 {{ $paper->term ?? '-' }}</div>
                                </button>
                            </label>
                        @endforeach
                        @if($this->papers()->isEmpty())
                            <div class="py-6 text-center text-sm text-gray-500">暂无卷子数据</div>
                        @endif
                    </div>
                </x-filament::section>

                <x-filament::section>
                    <div class="text-sm text-slate-500 mb-2">原始 Markdown 预览</div>
                    <div class="prose prose-sm max-w-none bg-gray-50 p-4 rounded-lg min-h-[240px]">
                        @if($this->selectedPaper())
                            {!! \App\Services\MathFormulaProcessor::processFormulas($this->selectedPaper()?->raw_markdown ?? '') !!}
                        @else
                            <div class="text-sm text-gray-400">暂无选中卷子</div>
                        @endif
                    </div>
                </x-filament::section>
            </div>

            <div class="col-span-4 space-y-4">
                <x-filament::section heading="卷子信息">
                    <div class="space-y-3">
                        <x-filament::input.wrapper>
                            <x-filament::input wire:model="form.edition" placeholder="教材体系 edition" />
                        </x-filament::input.wrapper>

                        <x-filament::input.wrapper>
                            <x-filament::input.select wire:model="form.grade">
                                <option value="">年段 grade</option>
                                @foreach($this->gradeOptions() as $value => $label)
                                    <option value="{{ $value }}">{{ $label }}</option>
                                @endforeach
                            </x-filament::input.select>
                        </x-filament::input.wrapper>

                        <x-filament::input.wrapper>
                            <x-filament::input.select wire:model="form.term">
                                <option value="">学期 term</option>
                                @foreach($this->termOptions() as $value => $label)
                                    <option value="{{ $value }}">{{ $label }}</option>
                                @endforeach
                            </x-filament::input.select>
                        </x-filament::input.wrapper>

                        <x-filament::input.wrapper>
                            <x-filament::input wire:model="form.chapter" placeholder="章节 chapter" />
                        </x-filament::input.wrapper>

                        <x-filament::input.wrapper>
                            <x-filament::input.select wire:model="form.source_type">
                                <option value="">卷子类型</option>
                                @foreach($this->sourceTypeOptions() as $value => $label)
                                    <option value="{{ $value }}">{{ $label }}</option>
                                @endforeach
                            </x-filament::input.select>
                        </x-filament::input.wrapper>

                        <x-filament::input.wrapper>
                            <x-filament::input wire:model="form.source_year" placeholder="来源年份" />
                        </x-filament::input.wrapper>

                        <x-filament::input.wrapper>
                            <x-filament::input.select wire:model="form.textbook_id">
                                <option value="">选择教材</option>
                                @foreach($this->textbookOptions() as $id => $title)
                                    <option value="{{ $id }}">{{ $title }}</option>
                                @endforeach
                            </x-filament::input.select>
                        </x-filament::input.wrapper>

                        <x-filament::input.wrapper>
                            <x-filament::input wire:model="form.textbook_series" placeholder="教材系列" />
                        </x-filament::input.wrapper>

                        <x-filament::input.wrapper>
                            <x-filament::input wire:model="form.source_name" placeholder="来源名称" />
                        </x-filament::input.wrapper>

                        <x-filament::input.wrapper>
                            <x-filament::input wire:model="form.source_page" placeholder="页码范围" />
                        </x-filament::input.wrapper>

                        <x-filament::input.wrapper>
                            <x-filament::input wire:model="form.tags" placeholder="标签（逗号分隔）" />
                        </x-filament::input.wrapper>
                    </div>
                </x-filament::section>

                <x-filament::section heading="批量覆盖">
                    <div class="text-xs text-gray-500 mb-2">对勾选卷子批量应用非空字段</div>
                    <div class="space-y-2">
                        <x-filament::input.wrapper>
                            <x-filament::input wire:model="batch.edition" placeholder="教材体系 edition" />
                        </x-filament::input.wrapper>
                        <x-filament::input.wrapper>
                            <x-filament::input.select wire:model="batch.grade">
                                <option value="">年段 grade</option>
                                @foreach($this->gradeOptions() as $value => $label)
                                    <option value="{{ $value }}">{{ $label }}</option>
                                @endforeach
                            </x-filament::input.select>
                        </x-filament::input.wrapper>
                        <x-filament::input.wrapper>
                            <x-filament::input.select wire:model="batch.term">
                                <option value="">学期 term</option>
                                @foreach($this->termOptions() as $value => $label)
                                    <option value="{{ $value }}">{{ $label }}</option>
                                @endforeach
                            </x-filament::input.select>
                        </x-filament::input.wrapper>
                        <x-filament::input.wrapper>
                            <x-filament::input wire:model="batch.chapter" placeholder="章节 chapter" />
                        </x-filament::input.wrapper>
                        <x-filament::input.wrapper>
                            <x-filament::input.select wire:model="batch.source_type">
                                <option value="">卷子类型</option>
                                @foreach($this->sourceTypeOptions() as $value => $label)
                                    <option value="{{ $value }}">{{ $label }}</option>
                                @endforeach
                            </x-filament::input.select>
                        </x-filament::input.wrapper>
                        <x-filament::input.wrapper>
                            <x-filament::input wire:model="batch.source_name" placeholder="来源名称" />
                        </x-filament::input.wrapper>
                        <x-filament::input.wrapper>
                            <x-filament::input wire:model="batch.source_page" placeholder="页码范围" />
                        </x-filament::input.wrapper>
                        <x-filament::input.wrapper>
                            <x-filament::input wire:model="batch.tags" placeholder="标签（逗号分隔）" />
                        </x-filament::input.wrapper>
                        <x-filament::button color="warning" x-on:click.prevent="if(confirm('确认对勾选卷子批量覆盖？')) { $wire.applyBatch() }">
                            批量覆盖
                        </x-filament::button>
                    </div>
                </x-filament::section>
            </div>
        </div>
    </div>
</x-filament::page>
