<?php

declare(strict_types=1);

namespace App\Filament\Resources\Posts\Pages;

use App\Filament\Resources\Posts\PostResource;
use Filament\Actions\CreateAction;
use Filament\Resources\Pages\ListRecords;

class ListPosts extends ListRecords
{
    protected static string $resource = PostResource::class;

    public function bootedInteractsWithTable(): void
    {
        // 支持从分类管理跳转：?category_id=X 预置分类筛选
        // （在 booted 阶段设置，早于表单填充；SelectFilter 状态为 category_id.value 嵌套结构）
        if (blank($this->tableFilters) && ($categoryId = (int) request()->query('category_id', 0))) {
            $this->tableFilters = ['category_id' => ['value' => $categoryId]];
            $this->tableDeferredFilters = ['category_id' => ['value' => $categoryId]];
        }

        parent::bootedInteractsWithTable();
    }

    protected function getHeaderActions(): array
    {
        return [
            CreateAction::make(),
        ];
    }
}
