<?php

declare(strict_types=1);

namespace App\Filament\Resources\DberpProducts\Pages;

use App\Filament\Resources\DberpProducts\DberpProductResource;
use App\Filament\Resources\Pages\CreateRecord;
use App\Models\DberpProduct;

class CreateDberpProduct extends CreateRecord
{
    protected static string $resource = DberpProductResource::class;

    /** 启用规格时跳转到编辑页，避免查看页下规格关联为只读无法维护。 */
    protected function getRedirectUrl(): string
    {
        $record = $this->getRecord();

        $resource = static::getResource();

        if (
            $record instanceof DberpProduct
            && (bool) $record->spec_enabled
            && $resource::hasPage('edit')
            && $resource::canEdit($record)
        ) {
            return $this->getResourceUrl('edit', $this->getRedirectUrlParameters());
        }

        return parent::getRedirectUrl();
    }
}
