<div>
    @php
        $currentUser = auth()->user();
        $isTeacher = $currentUser?->isTeacher() ?? false;
        $teacherId = $isTeacher ? $currentUser->teacher?->teacher_id : null;
        $teachers = \App\Models\Teacher::with('user')->get()->mapWithKeys(fn($t) => [$t->teacher_id => $t->user?->full_name ?? $t->name]);

        // 根据角色统计
        $studentQuery = \App\Models\Student::query();
        if ($isTeacher && $teacherId) {
            $studentQuery->where('teacher_id', $teacherId);
        }
        $studentCount = $studentQuery->count();
        $gradeCount = (clone $studentQuery)->distinct('grade')->count('grade');
    @endphp

    <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
        {{-- 主表单区域 --}}
        <div class="lg:col-span-2">
            <div class="card bg-base-100 shadow-xl">
                <div class="card-body">
                    <h2 class="card-title text-xl mb-2">学生信息</h2>
                    <p class="text-base-content/60 text-sm mb-6">请填写学生的基本信息，带 * 的为必填项</p>

                    <form wire:submit="create">
                        <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                            {{-- 姓名 --}}
                            <div class="form-control w-full">
                                <label class="label">
                                    <span class="label-text font-medium">姓名 <span class="text-error">*</span></span>
                                </label>
                                <input type="text"
                                       wire:model="data.name"
                                       placeholder="请输入学生姓名"
                                       class="input input-bordered w-full focus:input-primary"
                                       required />
                                @error('data.name')
                                    <label class="label"><span class="label-text-alt text-error">{{ $message }}</span></label>
                                @enderror
                            </div>

                            {{-- 年级 --}}
                            <div class="form-control w-full">
                                <label class="label">
                                    <span class="label-text font-medium">年级 <span class="text-error">*</span></span>
                                </label>
                                <input type="text"
                                       wire:model="data.grade"
                                       placeholder="例如：高一、高二"
                                       class="input input-bordered w-full focus:input-primary"
                                       required />
                                @error('data.grade')
                                    <label class="label"><span class="label-text-alt text-error">{{ $message }}</span></label>
                                @enderror
                            </div>

                            {{-- 班级 --}}
                            <div class="form-control w-full">
                                <label class="label">
                                    <span class="label-text font-medium">班级</span>
                                    <span class="label-text-alt text-base-content/50">选填</span>
                                </label>
                                <input type="text"
                                       wire:model="data.class_name"
                                       placeholder="例如：1班、2班"
                                       class="input input-bordered w-full focus:input-primary" />
                            </div>

                            {{-- 指导老师 --}}
                            @if(!$isTeacher)
                            <div class="form-control w-full">
                                <label class="label">
                                    <span class="label-text font-medium">指导老师 <span class="text-error">*</span></span>
                                </label>
                                <select wire:model="data.teacher_id"
                                        class="select select-bordered w-full focus:select-primary"
                                        required>
                                    <option value="">请选择指导老师</option>
                                    @foreach($teachers as $id => $name)
                                        <option value="{{ $id }}">{{ $name }}</option>
                                    @endforeach
                                </select>
                                @error('data.teacher_id')
                                    <label class="label"><span class="label-text-alt text-error">{{ $message }}</span></label>
                                @enderror
                            </div>
                            @endif

                            {{-- 备注 --}}
                            <div class="form-control w-full md:col-span-2">
                                <label class="label">
                                    <span class="label-text font-medium">备注</span>
                                    <span class="label-text-alt text-base-content/50">选填</span>
                                </label>
                                <textarea wire:model="data.remark"
                                          class="textarea textarea-bordered w-full focus:textarea-primary"
                                          placeholder="可以填写学生的特殊情况、学习偏好等"
                                          rows="3"></textarea>
                            </div>
                        </div>

                        {{-- 操作按钮 --}}
                        <div class="divider"></div>
                        <div class="flex justify-end gap-3">
                            <a href="{{ \App\Filament\Resources\StudentResource::getUrl('index') }}" class="btn btn-ghost">
                                取消
                            </a>
                            <button type="submit" class="btn btn-primary gap-2">
                                <span wire:loading.remove wire:target="create">
                                    <x-heroicon-o-plus class="w-5 h-5" />
                                    创建学生
                                </span>
                                <span wire:loading wire:target="create" class="flex items-center gap-2">
                                    <span class="loading loading-spinner loading-sm"></span>
                                    创建中...
                                </span>
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>

        {{-- 侧边栏 --}}
        <div class="lg:col-span-1 space-y-6">
            {{-- 提示卡片 --}}
            <div class="card bg-base-100 shadow-xl">
                <div class="card-body">
                    <div class="flex items-center gap-3 mb-4">
                        <div class="w-10 h-10 rounded-lg bg-info/20 flex items-center justify-center">
                            <x-heroicon-o-light-bulb class="w-5 h-5 text-info" />
                        </div>
                        <h3 class="card-title text-base">创建提示</h3>
                    </div>
                    <ul class="space-y-3 text-sm">
                        <li class="flex items-start gap-2">
                            <x-heroicon-o-check-circle class="w-5 h-5 text-success flex-shrink-0" />
                            <span>学生ID将自动生成</span>
                        </li>
                        <li class="flex items-start gap-2">
                            <x-heroicon-o-check-circle class="w-5 h-5 text-success flex-shrink-0" />
                            <span>年级格式：高一、高二、初一等</span>
                        </li>
                        <li class="flex items-start gap-2">
                            <x-heroicon-o-check-circle class="w-5 h-5 text-success flex-shrink-0" />
                            <span>班级信息可后续补充</span>
                        </li>
                    </ul>
                </div>
            </div>

            {{-- 统计卡片 --}}
            <div class="card bg-base-100 shadow-xl">
                <div class="card-body">
                    <div class="flex items-center gap-3 mb-4">
                        <div class="w-10 h-10 rounded-lg bg-secondary/20 flex items-center justify-center">
                            <x-heroicon-o-chart-bar class="w-5 h-5 text-secondary" />
                        </div>
                        <h3 class="card-title text-base">{{ $isTeacher ? '我的学生' : '全部统计' }}</h3>
                    </div>
                    <div class="stats stats-vertical shadow w-full">
                        <div class="stat">
                            <div class="stat-title">学生总数</div>
                            <div class="stat-value text-primary">{{ $studentCount }}</div>
                        </div>
                        <div class="stat">
                            <div class="stat-title">年级数</div>
                            <div class="stat-value text-secondary">{{ $gradeCount }}</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
