<?php

declare(strict_types=1);

namespace App\Filament\Resources\Categories\Tables;

use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Illuminate\Support\Str;

class CategoriesTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->modifyQueryUsing(fn ($query) => $query->withCount('posts'))
            ->columns([
                TextColumn::make('name')
                    ->label(__('category.name'))
                    ->searchable()
                    ->url(fn ($record) => route('filament.admin.resources.posts.index').'?category_id='.$record->id)
                    ->description(fn ($record) => $record->description ? Str::limit($record->description, 40) : null),
                TextColumn::make('slug')
                    ->label(__('category.slug'))
                    ->searchable(),
                TextColumn::make('display_order')
                    ->label(__('category.display_order'))
                    ->numeric()
                    ->sortable(),
                TextColumn::make('posts_count')
                    ->label('文章数')
                    ->numeric()
                    ->sortable(),
                TextColumn::make('created_at')
                    ->label(__('category.created_at'))
                    ->dateTime('Y-m-d')
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                TextColumn::make('updated_at')
                    ->label(__('category.updated_at'))
                    ->dateTime('Y-m-d')
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                //
            ])
            ->recordActions([
                EditAction::make(),
            ])
            ->recordUrl(null)
            ->recordAction(null)
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ]);
    }
}
