<?php

namespace App\Filament\Resources;

use App\Filament\Resources\MenuPermissionResource\Pages;
use App\Models\MenuPermission;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Columns\ToggleColumn;
use Filament\Tables\Columns\BadgeColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Filters\TernaryFilter;
use Filament\Actions;
use Filament\Actions\EditAction;
use Filament\Actions\DeleteAction;
use Filament\Actions\BulkAction;
use BackedEnum;
use UnitEnum;

class MenuPermissionResource extends Resource
{
    protected static ?string $model = MenuPermission::class;

    protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-squares-2x2';
    protected static ?string $navigationLabel = '菜单权限管理';
    protected static string|UnitEnum|null $navigationGroup = '其他';
    protected static ?int $navigationSort = 100;

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('user_id')
                    ->label('用户ID')
                    ->searchable()
                    ->sortable(),
                TextColumn::make('menu_key')
                    ->label('菜单标识')
                    ->searchable()
                    ->sortable(),
                TextColumn::make('menu_label')
                    ->label('菜单名称')
                    ->searchable(),
                TextColumn::make('menu_group')
                    ->label('菜单分组')
                    ->badge()
                    ->color('gray'),
                TextColumn::make('menu_url')
                    ->label('菜单URL')
                    ->limit(50)
                    ->copyable(),
                ToggleColumn::make('is_visible')
                    ->label('是否可见')
                    ->sortable(),
                TextColumn::make('sort_order')
                    ->label('排序')
                    ->sortable(),
                TextColumn::make('created_at')
                    ->label('创建时间')
                    ->dateTime('Y-m-d H:i')
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                SelectFilter::make('menu_group')
                    ->label('菜单分组')
                    ->options([
                        '管理' => '管理',
                        '工具' => '工具',
                        '分析' => '分析',
                    ])
                    ->multiple(),
                TernaryFilter::make('is_visible')
                    ->label('可见状态')
                    ->placeholder('全部')
                    ->trueLabel('可见')
                    ->falseLabel('隐藏'),
            ])
            ->actions([
                EditAction::make(),
                DeleteAction::make(),
            ])
            ->bulkActions([
                Actions\BulkActionGroup::make([
                    Actions\DeleteBulkAction::make(),
                    BulkAction::make('toggle_visibility')
                        ->label('批量切换可见性')
                        ->icon('heroicon-m-eye')
                        ->action(function ($records) {
                            foreach ($records as $record) {
                                $record->update(['is_visible' => !$record->is_visible]);
                            }
                        }),
                ]),
            ])
            ->defaultSort('sort_order')
            ->paginated([10, 25, 50, 100])
            ->poll('30s');
    }

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

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