<?php

declare(strict_types=1);

namespace Plugins\Demo\HelloWorld\Filament\Pages;

use App\Models\Setting;
use Filament\Actions\Action;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Notifications\Notification;
use Filament\Pages\Page;
use Filament\Schemas\Schema;

class HelloWorldSettings extends Page
{
    use InteractsWithForms;

    protected static \UnitEnum|string|null $navigationGroup = '插件';

    protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-face-smile';

    protected static ?string $navigationLabel = 'Hello World 设置';

    protected static ?string $title = 'Hello World 设置';

    protected string $view = 'filament.pages.plugin-settings';

    public array $data = [];

    public function mount(): void
    {
        $this->form->fill([
            'hello_world_greeting' => Setting::get('hello_world_greeting', '你好'),
        ]);
    }

    public function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                \Filament\Schemas\Components\Section::make('问候语')
                    ->schema([
                        TextInput::make('hello_world_greeting')->label('默认问候语')->required(),
                    ]),
            ])
            ->statePath('data');
    }

    public function save(): void
    {
        foreach ($this->form->getState() as $key => $value) {
            Setting::set($key, (string) $value);
        }

        Notification::make()->title('已保存')->success()->send();
    }

    protected function getFormActions(): array
    {
        return [
            Action::make('save')->label('保存')->submit('save'),
        ];
    }
}
