<?php

namespace App\Filament\Resources;

use AbdulmajeedJamaan\FilamentTranslatableTabs\TranslatableTabs;
use App\Filament\Resources\BannerResource\Pages;
use App\Service\DeepLService;
use App\Filament\Resources\BannerResource\RelationManagers;
use App\Models\Banner;
use Filament\Forms;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Group;
use Filament\Forms\Components\Radio;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Columns\ImageColumn;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use SolutionForest\FilamentTranslateField\Forms\Component\Translate;

class BannerResource extends Resource
{
    protected static ?string $model = Banner::class;
    protected static ?int $navigationSort = 1;
    protected static ?string $navigationIcon = 'heroicon-o-photo';
    protected static ?string $navigationGroup = '其他設定';
    // protected static ?string $navigationGroup = '上稿內容管理';
    protected static ?string $navigationLabel = "首頁 Banner 管理";
    protected static ?string $modelLabel = "首頁 Banner 管理";

    // 預設排序
    protected static function booted()
    {
        static::addGlobalScope('ordered', function ($query) {
            $query->orderBy('order')->orderBy('id');
        });
    }

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                //
                Section::make("")->schema([
                    Group::make()->schema([
                        Translate::make()->schema(fn (string $locale) => [
                            Textarea::make('title')->required($locale == 'zh_TW')->label("大字標題"),
                            Textarea::make('content')->required($locale == 'zh_TW')->label("小字標題")
                        ])
                        ->locales(["zh_TW", "en"])
                        ->actions([
                            app(DeepLService::class)->createTranslationAction("Main", ["title", "content"])
                        ])->columnSpanFull(),
                        Group::make()->schema([
                            FileUpload::make("img_url")->label("圖片")->directory("banners")->columnSpan(1),
                            FileUpload::make("mobile_img")->label("手機板圖片")->directory("banners")->columnSpan(1),
                            Translate::make()->schema(fn (string $locale) => [
                                TextInput::make('img_alt')->required($locale == 'zh_TW')->label("圖片註釋")
                            ])
                            ->locales(["zh_TW", "en"])
                            ->actions([
                                app(DeepLService::class)->createTranslationAction("Main", ["img_alt"])
                            ])->columnSpan(1),
                        ])->columnSpanFull(),
                        Radio::make("visible")->label("顯示/不顯示")->options([1 => "顯示", 0 => "不顯示"])->inline()->default(1)->columnSpan(2)
                    ])->columns(4)->columnSpanFull(),
                ])
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                //
                ImageColumn::make('img_url')->label("圖片")
            ])
            ->filters([
                //
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ])->reorderable('order');
    }

    public static function getRelations(): array
    {
        return [
            //
        ];
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListBanners::route('/'),
            'create' => Pages\CreateBanner::route('/create'),
            'edit' => Pages\EditBanner::route('/{record}/edit'),
        ];
    }
}
