<?php

namespace App\Filament\Pages\Company\Actions;

use App\Filament\Support\CheerfulNotification;
use Filament\Actions\Action;
use Filament\Forms\Components\Textarea;
use Filament\Support\Enums\Width;

class SaveCompanyAction extends Action
{
    public static function getDefaultName(): ?string
    {
        return 'save';
    }

    protected function setUp(): void
    {
        parent::setUp();

        $this->label('Simpan')
            ->modalHeading(fn () => CheerfulNotification::getByKey('company.save.title'))
            ->modalDescription(fn () => CheerfulNotification::getByKey('company.save.description'))
            ->modal(fn (): bool => (bool) ($this->getLivewire()?->needsCompanyReview()))
            ->schema(fn (): array => ($this->getLivewire()?->needsCompanyReview())
                ? [
                    Textarea::make('change_reason')
                        ->label('Alasan')
                        ->placeholder('Jelaskan alasan perubahan data ini...')
                        ->autocomplete(false)
                        ->autofocus()
                        ->required(),
                ]
                : [])
            ->action(function (array $data = []): void {
                $this->getLivewire()?->handleCompanySave($data);
            })
            ->modalWidth(Width::Large);
    }
}
