| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- <?php
- namespace App\Filament\Resources\TextbookResource\Pages;
- use App\Filament\Resources\TextbookResource;
- use App\Services\TextbookApiService;
- use App\Models\Textbook;
- use Filament\Forms;
- use Filament\Actions;
- use Filament\Resources\Pages\Page;
- use Illuminate\Http\Request;
- use Livewire\WithFileUploads;
- class EditTextbook extends Page implements Forms\Contracts\HasForms
- {
- use Forms\Concerns\InteractsWithForms;
- use WithFileUploads;
- protected static string $resource = TextbookResource::class;
- public array $data = [];
- public ?int $recordId = null;
- public function mount(Request $request): void
- {
- // 从路由参数获取教材ID,避免Livewire的隐式绑定
- $this->recordId = (int) $request->route('record');
- if (!$this->recordId) {
- abort(404);
- }
- // 从API获取教材数据
- $apiService = app(TextbookApiService::class);
- $textbookData = $apiService->getTextbook($this->recordId);
- if (!$textbookData) {
- abort(404);
- }
- // 初始化表单数据
- $this->data = $textbookData;
- $this->form->fill($this->data);
- }
- public function save(): void
- {
- // 验证数据
- $this->validate([
- 'data.series_id' => 'required|integer',
- 'data.stage' => 'required|string',
- 'data.grade' => 'nullable|integer',
- 'data.semester' => 'nullable|integer',
- 'data.official_title' => 'required|string|max:255',
- 'data.isbn' => 'nullable|string|max:255',
- 'data.status' => 'required|string|in:draft,published,archived',
- ]);
- // 只传递标量字段到API,跳过关系字段和嵌套对象
- $allowedFields = [
- 'series_id', 'stage', 'grade', 'semester', 'naming_scheme',
- 'track', 'module_type', 'volume_no', 'legacy_code',
- 'curriculum_standard_year', 'curriculum_revision_year',
- 'approval_authority', 'approval_year', 'edition_label',
- 'official_title', 'aliases', 'isbn',
- 'cover_path', 'status', 'sort_order', 'meta'
- ];
- $updateData = [];
- foreach ($allowedFields as $field) {
- if (isset($this->data[$field]) && !is_array($this->data[$field]) && !is_object($this->data[$field])) {
- $updateData[$field] = $this->data[$field];
- }
- }
- // 调用API更新
- $apiService = app(TextbookApiService::class);
- $updatedData = $apiService->updateTextbook($this->recordId, $updateData);
- // 更新本地数据
- $this->data = array_merge($this->data, $updatedData);
- // 显示成功消息
- \Filament\Notifications\Notification::make()
- ->title('教材更新成功')
- ->success()
- ->send();
- }
- protected function getHeaderActions(): array
- {
- return [
- Actions\Action::make('back')
- ->label('返回列表')
- ->url(static::$resource::getUrl('index'))
- ->color('gray'),
- ];
- }
- public function getTitle(): string
- {
- return '编辑教材';
- }
- public function getView(): string
- {
- return 'filament.resources.textbook-resource.edit';
- }
- public function form(\Filament\Forms\Form $form): \Filament\Forms\Form
- {
- return $form
- ->schema([
- Forms\Components\Section::make('基本信息')
- ->schema([
- Forms\Components\Select::make('data.series_id')
- ->label('教材系列')
- ->options(function () {
- $apiService = app(TextbookApiService::class);
- $series = $apiService->getTextbookSeries();
- $options = [];
- foreach ($series['data'] as $s) {
- $options[$s['id']] = $s['name'];
- }
- return $options;
- })
- ->required()
- ->searchable(),
- Forms\Components\Select::make('data.stage')
- ->label('学段')
- ->options([
- 'primary' => '小学',
- 'junior' => '初中',
- 'senior' => '高中',
- ])
- ->required()
- ->reactive(),
- Forms\Components\TextInput::make('data.grade')
- ->label('年级')
- ->numeric()
- ->helperText('例如:7表示七年级'),
- Forms\Components\Select::make('data.semester')
- ->label('学期')
- ->options([
- 1 => '上学期',
- 2 => '下学期',
- ])
- ->helperText('选择学期'),
- Forms\Components\TextInput::make('data.official_title')
- ->label('教材名称')
- ->required()
- ->maxLength(255),
- Forms\Components\TextInput::make('data.isbn')
- ->label('ISBN')
- ->maxLength(255),
- Forms\Components\Select::make('data.status')
- ->label('状态')
- ->options([
- 'draft' => '草稿',
- 'published' => '已发布',
- 'archived' => '已归档',
- ])
- ->required(),
- ])
- ->columns(2),
- Forms\Components\Section::make('版本与系列')
- ->schema([
- Forms\Components\Select::make('data.naming_scheme')
- ->label('命名体系')
- ->options([
- 'new' => '新体系',
- 'old' => '旧体系',
- ]),
- Forms\Components\Select::make('data.track')
- ->label('方向')
- ->options([
- 'science' => '理科',
- 'liberal_arts' => '文科',
- ]),
- Forms\Components\Select::make('data.module_type')
- ->label('模块类型')
- ->options([
- 'compulsory' => '必修',
- 'selective_compulsory' => '选择性必修',
- 'elective' => '选修',
- ]),
- Forms\Components\TextInput::make('data.volume_no')
- ->label('册次')
- ->numeric(),
- Forms\Components\TextInput::make('data.legacy_code')
- ->label('旧版代号'),
- Forms\Components\TextInput::make('data.edition_label')
- ->label('版本标识'),
- ])
- ->columns(2),
- Forms\Components\Section::make('封面上传')
- ->schema([
- Forms\Components\FileUpload::make('data.cover_path')
- ->label('封面图片')
- ->image()
- ->directory('textbook-covers')
- ->helperText('建议尺寸 600x800,JPG/PNG'),
- ])
- ->extraAttributes(['id' => 'cover']),
- ])
- ->statePath('data');
- }
- }
|