<div>
    @php
        $student = $this->record;
        $currentUser = auth()->user();
        $isTeacher = $currentUser?->isTeacher() ?? false;
        $teachers = \App\Models\Teacher::with('user')->get()->mapWithKeys(fn($t) => [$t->teacher_id => $t->user?->full_name ?? $t->name]);
    @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="save">
                        <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                            {{-- 学生ID（只读） --}}
                            <div class="form-control w-full">
                                <label class="label">
                                    <span class="label-text font-medium">学生ID</span>
                                </label>
                                <input type="text"
                                       value="{{ $student->student_id }}"
                                       class="input input-bordered w-full bg-base-200"
                                       disabled />
                            </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.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 md:col-span-2">
                                <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-between">
                            <a href="{{ \App\Filament\Resources\StudentResource::getUrl('view', ['record' => $student]) }}"
                               class="btn btn-ghost gap-2">
                                <x-heroicon-o-arrow-left class="w-4 h-4" />
                                返回详情
                            </a>
                            <div class="flex gap-3">
                                <button type="button" wire:click="$refresh" class="btn btn-ghost">
                                    重置
                                </button>
                                <button type="submit" class="btn btn-primary gap-2">
                                    <span wire:loading.remove wire:target="save">
                                        <x-heroicon-o-check class="w-5 h-5" />
                                        保存修改
                                    </span>
                                    <span wire:loading wire:target="save" class="flex items-center gap-2">
                                        <span class="loading loading-spinner loading-sm"></span>
                                        保存中...
                                    </span>
                                </button>
                            </div>
                        </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 items-center text-center">
                    <div class="avatar placeholder mb-4">
                        <div class="bg-gradient-to-br from-primary to-secondary text-primary-content rounded-full w-20">
                            <span class="text-2xl">{{ mb_substr($student->name, 0, 1) }}</span>
                        </div>
                    </div>
                    <h3 class="text-lg font-bold">{{ $student->name }}</h3>
                    <p class="text-base-content/60 text-sm">{{ $student->grade }} {{ $student->class_name }}</p>
                    <div class="badge badge-outline badge-sm mt-2">{{ $student->student_id }}</div>
                </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-warning/20 flex items-center justify-center">
                            <x-heroicon-o-exclamation-triangle class="w-5 h-5 text-warning" />
                        </div>
                        <h3 class="card-title text-base">注意事项</h3>
                    </div>
                    <ul class="space-y-2 text-sm text-base-content/70">
                        <li class="flex items-start gap-2">
                            <x-heroicon-o-information-circle class="w-4 h-4 flex-shrink-0 mt-0.5" />
                            <span>学生ID不可修改</span>
                        </li>
                        <li class="flex items-start gap-2">
                            <x-heroicon-o-information-circle class="w-4 h-4 flex-shrink-0 mt-0.5" />
                            <span>修改后点击保存生效</span>
                        </li>
                    </ul>
                </div>
            </div>

            {{-- 危险操作 --}}
            <div class="card bg-error/5 border border-error/20 shadow-xl">
                <div class="card-body">
                    <div class="flex items-center gap-3 mb-4">
                        <div class="w-10 h-10 rounded-lg bg-error/20 flex items-center justify-center">
                            <x-heroicon-o-trash class="w-5 h-5 text-error" />
                        </div>
                        <h3 class="card-title text-base text-error">危险操作</h3>
                    </div>
                    <p class="text-sm text-base-content/70 mb-4">删除学生将同时删除所有相关的学习记录，此操作不可恢复。</p>
                    <button type="button"
                            wire:click="$dispatch('open-modal', { id: 'delete-confirmation' })"
                            class="btn btn-error btn-outline btn-sm w-full">
                        删除学生
                    </button>
                </div>
            </div>
        </div>
    </div>

    {{-- 删除确认模态框 --}}
    <x-filament::modal id="delete-confirmation" width="md">
        <x-slot name="heading">确认删除</x-slot>
        <x-slot name="description">
            确定要删除学生 <strong>{{ $student->name }}</strong> 吗？此操作无法撤销。
        </x-slot>
        <x-slot name="footerActions">
            <x-filament::button color="gray" x-on:click="$dispatch('close-modal', { id: 'delete-confirmation' })">
                取消
            </x-filament::button>
            <x-filament::button color="danger" wire:click="delete">
                确认删除
            </x-filament::button>
        </x-slot>
    </x-filament::modal>
</div>
