<?php

namespace App\Filament\Resources\TextbookResource\Pages;

use App\Filament\Resources\TextbookResource;
use Filament\Resources\Pages\CreateRecord;
use Illuminate\Database\Eloquent\Model;

class CreateTextbook extends CreateRecord
{
    protected static string $resource = TextbookResource::class;

    /**
     * 禁用面包屑，避免重复导航
     */
    public function getBreadcrumbs(): array
    {
        return [];
    }

    /**
     * 处理记录创建
     */
    protected function handleRecordCreation(array $data): Model
    {
        // 获取TextbookResource实例
        $resource = static::getResource();

        // 调用API创建
        $apiService = $resource::getApiService();
        $createdData = $apiService->createTextbook($data);

        // 创建模型实例
        $record = new static::$model($createdData);
        $record->exists = true;
        $record->id = $createdData['id'] ?? null;

        return $record;
    }
}
