<?php

declare(strict_types=1);

namespace App\Filament\Resources\DberpPurchaseRequests\Pages;

use App\Business\Purchase\PurchaseRequestService;
use App\Filament\Resources\DberpPurchaseRequests\DberpPurchaseRequestResource;
use App\Filament\Resources\Pages\CreateRecord;
use Filament\Support\Enums\Width;
use Illuminate\Database\Eloquent\Model;

class CreateDberpPurchaseRequest extends CreateRecord
{
    protected static string $resource = DberpPurchaseRequestResource::class;

    protected Width|string|null $maxContentWidth = Width::Full;

    /**
     * 资源存在 view 页时，Filament 默认创建后会跳查看页；采购明细在编辑页维护更顺手。
     */
    protected function getRedirectUrl(): string
    {
        $record = $this->getRecord();
        $resource = static::getResource();

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

        return parent::getRedirectUrl();
    }

    protected function handleRecordCreation(array $data): Model
    {
        return app(PurchaseRequestService::class)->createRequest($data);
    }
}
