<div>
    @php
        $teacher = $this->record->loadMissing('students');
        $user = $teacher->user ?? \App\Models\User::find($teacher->user_id);
        $subjectOptions = \App\Filament\Resources\TeacherResource::subjectOptions();
        $subjectLabel = $subjectOptions[$teacher->subject] ?? $teacher->subject ?? '未设置';
        $studentsCount = $teacher->students->count();
    @endphp

    <div class="grid grid-cols-1 xl:grid-cols-3 gap-6">
        {{-- 主表单区域 --}}
        <div class="xl:col-span-2 space-y-6">
            <div class="card bg-base-100 shadow-xl">
                <div class="card-body">
                    <div class="flex flex-col lg:flex-row lg:items-center lg:justify-between gap-4">
                        <div>
                            <p class="text-sm text-base-content/60">教师资料</p>
                            <h2 class="card-title text-2xl">编辑教师信息</h2>
                            <p class="text-base-content/60 text-sm mt-1">保持登录与基本资料一致，保存后立即生效</p>
                        </div>
                        <div class="flex flex-wrap gap-2">
                            <a href="{{ \App\Filament\Resources\TeacherResource::getUrl('view', ['record' => $teacher]) }}"
                               class="btn btn-ghost btn-sm gap-2">
                                <x-heroicon-o-arrow-left class="w-4 h-4" />
                                返回详情
                            </a>
                            <a href="/admin/students?tableFilters[teacher_id][value]={{ $teacher->teacher_id }}"
                               class="btn btn-outline btn-primary btn-sm gap-2">
                                <x-heroicon-o-users class="w-4 h-4" />
                                查看学生
                            </a>
                        </div>
                    </div>

                    {{-- 快速信息 --}}
                    <div class="grid grid-cols-1 md:grid-cols-3 gap-4 mt-6">
                        <div class="bg-base-200/60 rounded-xl p-4 flex items-center gap-3">
                            <div class="w-10 h-10 rounded-lg bg-primary/20 flex items-center justify-center">
                                <x-heroicon-o-identification class="w-5 h-5 text-primary" />
                            </div>
                            <div>
                                <p class="text-sm text-base-content/60">教师ID</p>
                                <p class="font-semibold font-mono">{{ $teacher->teacher_id }}</p>
                            </div>
                        </div>
                        <div class="bg-base-200/60 rounded-xl p-4 flex items-center gap-3">
                            <div class="w-10 h-10 rounded-lg bg-secondary/20 flex items-center justify-center">
                                <x-heroicon-o-academic-cap class="w-5 h-5 text-secondary" />
                            </div>
                            <div>
                                <p class="text-sm text-base-content/60">教授科目</p>
                                <p class="font-semibold">{{ $subjectLabel }}</p>
                            </div>
                        </div>
                        <div class="bg-base-200/60 rounded-xl p-4 flex items-center gap-3">
                            <div class="w-10 h-10 rounded-lg bg-info/20 flex items-center justify-center">
                                <x-heroicon-o-chart-bar class="w-5 h-5 text-info" />
                            </div>
                            <div>
                                <p class="text-sm text-base-content/60">管理学生</p>
                                <p class="font-semibold">{{ $studentsCount }} 人</p>
                            </div>
                        </div>
                    </div>

                    {{-- 表单 --}}
                    <form wire:submit="save" class="mt-6 space-y-6">
                        <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                            <div class="form-control">
                                <label class="label">
                                    <span class="label-text font-medium">教师ID</span>
                                    <span class="label-text-alt text-base-content/60">系统生成</span>
                                </label>
                                <input type="text"
                                       wire:model="data.teacher_id"
                                       class="input input-bordered bg-base-200"
                                       disabled />
                            </div>

                            <div class="form-control">
                                <label class="label">
                                    <span class="label-text font-medium">手机号（登录名）</span>
                                    <span class="label-text-alt text-base-content/60">不可修改</span>
                                </label>
                                <input type="text"
                                       wire:model="data.user.username"
                                       class="input input-bordered bg-base-200"
                                       disabled />
                            </div>
                        </div>

                        <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                            <div class="form-control">
                                <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 focus:input-primary" />
                                @error('data.name')
                                    <label class="label"><span class="label-text-alt text-error">{{ $message }}</span></label>
                                @enderror
                            </div>

                            <div class="form-control">
                                <label class="label">
                                    <span class="label-text font-medium">教授科目 <span class="text-error">*</span></span>
                                </label>
                                <select wire:model="data.subject"
                                        class="select select-bordered focus:select-primary">
                                    <option value="">请选择科目</option>
                                    @foreach($subjectOptions as $value => $label)
                                        <option value="{{ $value }}">{{ $label }}</option>
                                    @endforeach
                                </select>
                                @error('data.subject')
                                    <label class="label"><span class="label-text-alt text-error">{{ $message }}</span></label>
                                @enderror
                            </div>
                        </div>

                        <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                            <div class="form-control md:col-span-2">
                                <label class="label">
                                    <span class="label-text font-medium">登录密码</span>
                                    <span class="label-text-alt text-base-content/60">留空则不修改</span>
                                </label>
                                <input type="password"
                                       wire:model.defer="data.user.password_hash"
                                       placeholder="输入新密码"
                                       class="input input-bordered focus:input-primary" />
                                @error('data.user.password_hash')
                                    <label class="label"><span class="label-text-alt text-error">{{ $message }}</span></label>
                                @enderror
                            </div>
                        </div>

                        <div class="form-control">
                            <label class="label">
                                <span class="label-text font-medium">备注</span>
                                <span class="label-text-alt text-base-content/60">选填</span>
                            </label>
                            <textarea wire:model.defer="data.remark"
                                      class="textarea textarea-bordered focus:textarea-primary"
                                      placeholder="记录教学侧重点、班主任情况等"
                                      rows="3"></textarea>
                            @error('data.remark')
                                <label class="label"><span class="label-text-alt text-error">{{ $message }}</span></label>
                            @enderror
                        </div>

                        <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4 pt-2 border-t border-base-200">
                            <div class="text-sm text-base-content/70 flex items-center gap-2">
                                <x-heroicon-o-shield-check class="w-4 h-4" />
                                未填写密码时将保留原密码，保存后立即同步到登录账号。
                            </div>
                            <div class="flex flex-wrap 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" class="flex items-center gap-2">
                                        <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="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-24">
                            <span class="text-3xl">{{ mb_substr($teacher->name, 0, 1) }}</span>
                        </div>
                    </div>
                    <h3 class="text-xl font-bold">{{ $teacher->name }}</h3>
                    <p class="text-base-content/60">{{ $subjectLabel }}</p>
                    <div class="badge badge-outline mt-2">{{ $teacher->teacher_id }}</div>
                    <div class="mt-3 space-y-1 text-sm text-base-content/70">
                        <p>账号：{{ $user?->username ?? '未设置' }}</p>
                        <p>最后登录：{{ $user?->last_login?->format('Y-m-d H:i:s') ?? '从未登录' }}</p>
                    </div>
                </div>
            </div>

            <div class="card bg-base-100 shadow-xl">
                <div class="card-body">
                    <div class="flex items-center gap-3 mb-3">
                        <div class="w-10 h-10 rounded-lg bg-warning/20 flex items-center justify-center">
                            <x-heroicon-o-light-bulb 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>
                        <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-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-primary/20 flex items-center justify-center">
                            <x-heroicon-o-bolt class="w-5 h-5 text-primary" />
                        </div>
                        <h3 class="card-title text-base">快捷导航</h3>
                    </div>
                    <div class="flex flex-col gap-2">
                        <a href="{{ \App\Filament\Resources\TeacherResource::getUrl('index') }}"
                           class="btn btn-ghost btn-sm gap-2 justify-start">
                            <x-heroicon-o-list-bullet class="w-4 h-4" />
                            返回教师列表
                        </a>
                        <a href="{{ \App\Filament\Resources\TeacherResource::getUrl('view', ['record' => $teacher]) }}"
                           class="btn btn-outline btn-sm gap-2 justify-start">
                            <x-heroicon-o-eye class="w-4 h-4" />
                            查看教师详情
                        </a>
                        <a href="/admin/students?tableFilters[teacher_id][value]={{ $teacher->teacher_id }}"
                           class="btn btn-primary btn-sm gap-2 justify-start">
                            <x-heroicon-o-users class="w-4 h-4" />
                            跳转学生列表
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
