<?php

namespace App\Filament\Pages;

use App\Models\EsgMain;
use Filament\Forms\Components\Actions\Action;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Tabs;
use Filament\Forms\Components\Tabs\Tab;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Form;
use Filament\Notifications\Notification;
use Filament\Pages\Page;
use SolutionForest\FilamentTranslateField\Forms\Component\Translate;

class EsgMainPage extends Page
{
    use InteractsWithForms;

    protected static ?string $navigationIcon = 'heroicon-o-cog-6-tooth';
    protected static ?string $navigationLabel = 'ESG 主要設定';
    protected static ?string $title = 'ESG 主要設定';
    protected static string $view = 'filament.pages.esg-main';
    protected static ?string $navigationGroup = 'ESG 上稿內容管理';
    protected static ?int $navigationSort = 99;

    public ?array $data = [];
    protected ?EsgMain $esgMain = null;

    public function mount(): void
    {
        $this->esgMain = EsgMain::getMain();
        $esgMainArray = $this->esgMain->safeToArray();
        \Log::info("輸出", $esgMainArray);
        $this->form->fill($esgMainArray);
    }

    public function form(Form $form): Form
    {
        return $form
            ->schema([
                Tabs::make('設定')->tabs([
                    Tab::make('基本資訊')->schema([
                        Translate::make()->schema(fn (string $locale) => [
                            TextInput::make('title')
                            ->label("標題"),
                            Textarea::make('description')
                        ])->locales(["zh_TW", "en"])
                        ->columnSpanFull(),
                        FileUpload::make('img_pc')->label("Banner (PC)")
                        ->disk("public")
                        ->directory("esg"),
                        FileUpload::make('img_mobile')->label("Banner (Mobile)")
                        ->disk("public")
                        ->directory("esg"),
                    ]),
                    Tab::make('SEO 設定')->schema([
                        Translate::make()->schema(fn (string $locale) => [
                            TextInput::make('meta_title'),
                            TextInput::make('meta_keyword'),
                            Textarea::make('meta_description'),
                        ])->locales(["zh_TW", "en"])
                        ->columnSpanFull(),
                        FileUpload::make('og_img')
                        ->disk("public")
                        ->directory("esg"),
                    ]),
                ])
            ])
            ->statePath('data');
    }

    public function save(): void
    {
        try {
            $data = $this->form->getState();

            $settings = EsgMain::getMain();
            $settings->update($data);

            Notification::make()
                ->title('設定已儲存')
                ->success()
                ->send();

        } catch (\Exception $e) {
            Notification::make()
                ->title('儲存失敗')
                ->body($e->getMessage())
                ->danger()
                ->send();
        }
    }

    protected function getFormActions(): array
    {
        return [
            Action::make('save')
                ->label('儲存設定')
                ->action('save')
                ->color('primary'),
        ];
    }

}
