<div>

<div class="space-y-6">
    {{-- 统计卡片 --}}
    <div class="grid grid-cols-2 md:grid-cols-5 gap-4">
        <div class="stats shadow bg-base-100 border">
            <div class="stat py-3">
                <div class="stat-title text-xs">总记录</div>
                <div class="stat-value text-2xl text-primary">{{ $this->statistics['total'] }}</div>
            </div>
        </div>
        <div class="stats shadow bg-base-100 border">
            <div class="stat py-3">
                <div class="stat-title text-xs">待处理</div>
                <div class="stat-value text-2xl text-gray-500">{{ $this->statistics['pending'] }}</div>
            </div>
        </div>
        <div class="stats shadow bg-base-100 border">
            <div class="stat py-3">
                <div class="stat-title text-xs">处理中</div>
                <div class="stat-value text-2xl text-info">{{ $this->statistics['processing'] }}</div>
            </div>
        </div>
        <div class="stats shadow bg-base-100 border">
            <div class="stat py-3">
                <div class="stat-title text-xs">已完成</div>
                <div class="stat-value text-2xl text-success">{{ $this->statistics['completed'] }}</div>
            </div>
        </div>
        <div class="stats shadow bg-base-100 border">
            <div class="stat py-3">
                <div class="stat-title text-xs">失败</div>
                <div class="stat-value text-2xl text-error">{{ $this->statistics['failed'] }}</div>
            </div>
        </div>
    </div>

    {{-- 操作栏 --}}
    <div class="flex justify-between items-center">
        <div class="flex gap-2">
            <a href="{{ route('filament.admin.pages.upload-exam-paper') }}" class="btn btn-primary">
                <svg class="w-5 h-5 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"></path>
                </svg>
                上传卷子
            </a>
        </div>
    </div>

    {{-- 筛选区域 --}}
    <div class="card bg-base-100 shadow-sm border">
        <div class="card-body p-4">
            <div class="grid grid-cols-1 md:grid-cols-5 gap-4">
                {{-- 搜索 --}}
                <div class="form-control">
                    <input
                        type="text"
                        wire:model.live.debounce.300ms="search"
                        placeholder="搜索学生姓名..."
                        class="input input-bordered input-sm w-full"
                    >
                </div>

                {{-- 状态筛选 --}}
                <div class="form-control">
                    <select wire:model.live="filterStatus" class="select select-bordered select-sm w-full">
                        <option value="">全部状态</option>
                        <option value="pending">待处理</option>
                        <option value="processing">处理中</option>
                        <option value="completed">已完成</option>
                        <option value="failed">失败</option>
                    </select>
                </div>

                {{-- 年级筛选 --}}
                <div class="form-control">
                    <select wire:model.live="filterGrade" class="select select-bordered select-sm w-full">
                        <option value="">全部年级</option>
                        @foreach($this->grades as $grade)
                            <option value="{{ $grade }}">{{ $grade }}</option>
                        @endforeach
                    </select>
                </div>

                {{-- 班级筛选 --}}
                <div class="form-control">
                    <select wire:model.live="filterClass" class="select select-bordered select-sm w-full">
                        <option value="">全部班级</option>
                        @foreach($this->classes as $class)
                            <option value="{{ $class }}">{{ $class }}</option>
                        @endforeach
                    </select>
                </div>

                {{-- 重置按钮 --}}
                <div class="form-control">
                    <button wire:click="resetFilters" class="btn btn-ghost btn-sm w-full">
                        <svg class="w-4 h-4 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"></path>
                        </svg>
                        重置
                    </button>
                </div>
            </div>
        </div>
    </div>

    {{-- 数据表格 --}}
    <div class="card bg-base-100 shadow-lg border">
        <div class="card-body p-0">
            <div class="overflow-x-auto">
                <table class="table">
                    <thead>
                        <tr class="bg-base-200">
                            <th>学生</th>
                            <th>年级/班级</th>
                            <th>图片</th>
                            <th>状态</th>
                            <th>进度</th>
                            <th>置信度</th>
                            <th>创建时间</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($this->records as $record)
                            @php
                                $recordId = $record->id;
                                $showDetail = isset($showDetailId) && $showDetailId == $recordId;
                                $detailUrl = \App\Filament\Pages\OCRRecordView::getUrl(['recordId' => $recordId]);
                            @endphp
                            <tr class="hover" onclick="window.location.href='{{ $detailUrl }}'" style="cursor: pointer;">
                                <td>
                                    <span class="font-medium">{{ $record->student->name ?? '未知' }}</span>
                                </td>
                                <td class="text-sm text-gray-500">
                                    {{ $record->student->grade ?? '-' }} / {{ $record->student->class_name ?? '-' }}
                                </td>
                                <td>
                                    <div class="flex items-center gap-2">
                                        @if($record->image_path)
                                            <a href="{{ $detailUrl }}" onclick="event.stopPropagation();" class="avatar hover:opacity-80 transition-opacity">
                                                <div class="w-10 h-10 rounded">
                                                    <img src="{{ asset('storage/' . $record->image_path) }}" alt="预览" class="object-cover">
                                                </div>
                                            </a>
                                        @endif
                                        <span class="text-xs text-gray-500 max-w-[100px] truncate">
                                            {{ $record->image_filename }}
                                        </span>
                                    </div>
                                </td>
                                <td>
                                    @php
                                        $statusConfig = match($record->status) {
                                            'pending' => ['class' => 'badge-ghost', 'text' => '待处理'],
                                            'processing' => ['class' => 'badge-info', 'text' => '处理中'],
                                            'completed' => ['class' => 'badge-success', 'text' => '已完成'],
                                            'failed' => ['class' => 'badge-error', 'text' => '失败'],
                                            default => ['class' => 'badge-ghost', 'text' => $record->status],
                                        };
                                    @endphp
                                    <span class="badge {{ $statusConfig['class'] }} badge-sm">
                                        {{ $statusConfig['text'] }}
                                    </span>
                                </td>
                                <td>
                                    @if($record->total_questions > 0)
                                        <div class="flex items-center gap-2">
                                            <progress
                                                class="progress progress-primary w-16"
                                                value="{{ $record->processed_questions }}"
                                                max="{{ $record->total_questions }}"
                                            ></progress>
                                            <span class="text-xs">
                                                {{ $record->processed_questions }}/{{ $record->total_questions }}
                                            </span>
                                        </div>
                                    @else
                                        <span class="text-gray-400">-</span>
                                    @endif
                                </td>
                                <td>
                                    @if($record->confidence_avg)
                                        @php
                                            $confidence = $record->confidence_avg * 100;
                                            $colorClass = $confidence >= 70 ? 'text-success' : ($confidence >= 50 ? 'text-warning' : 'text-error');
                                        @endphp
                                        <span class="{{ $colorClass }} font-medium">
                                            {{ number_format($confidence, 1) }}%
                                        </span>
                                    @else
                                        <span class="text-gray-400">-</span>
                                    @endif
                                </td>
                                <td class="text-sm text-gray-500">
                                    {{ $record->created_at->format('m-d H:i') }}
                                </td>
                                <td onclick="event.stopPropagation();">
                                    @if($record->status === 'pending' || $record->status === 'failed')
                                        <button
                                            wire:click="startRecognition({{ $record->id }})"
                                            class="btn btn-primary btn-xs"
                                        >
                                            <svg class="w-3 h-3 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M14.752 11.168l-3.197-2.132A1 1 0 0010 9.87v4.263a1 1 0 001.555.832l3.197-2.132a1 1 0 000-1.664z"></path>
                                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path>
                                            </svg>
                                            识别
                                        </button>
                                    @elseif($record->status === 'completed')
                                        <span class="badge badge-success badge-xs">已完成</span>
                                    @elseif($record->status === 'processing')
                                        <span class="badge badge-info badge-xs">处理中</span>
                                    @else
                                        <span class="badge badge-ghost badge-xs">{{ $record->status }}</span>
                                    @endif
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="8" class="text-center py-8 text-gray-500">
                                    <svg class="w-12 h-12 mx-auto mb-3 opacity-50" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 13h6m-3-3v6m5 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"></path>
                                    </svg>
                                    <p>暂无记录</p>
                                </td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            {{-- 分页 --}}
            @if($this->records->hasPages())
                <div class="px-4 py-3 border-t">
                    {{ $this->records->links() }}
                </div>
            @endif
        </div>
    </div>
</div>

</div>
