<?php

namespace Tests\Feature;

use App\Models\Plan;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class AdminPlanIndexKeepFiltersOnLinksTest extends TestCase
{
    use RefreshDatabase;

    protected function loginAsPlatformAdmin(): void
    {
        $this->seed();

        $this->post('/admin/login', [
            'email' => 'platform.admin@demo.local',
            'password' => 'Platform@123456',
        ])->assertRedirect('/admin');
    }

    public function test_plans_index_tools_export_form_keeps_filters(): void
    {
        $this->loginAsPlatformAdmin();

        Plan::query()->create([
            'code' => 'plan_keep_filters_01',
            'name' => '筛选透传测试套餐',
            'billing_cycle' => 'monthly',
            'price' => 9.9,
            'list_price' => 9.9,
            'status' => 'active',
            'sort' => 1,
            'published_at' => now(),
        ]);

        $res = $this->get('/admin/plans?status=active&billing_cycle=monthly&published=published&keyword=' . urlencode('筛选'));
        $res->assertOk();

        // 导出表单 hidden input 透传筛选口径
        $res->assertSee('<form method="get" action="/admin/plans/export">', false);
        $res->assertSee('<input type="hidden" name="status" value="active">', false);
        $res->assertSee('<input type="hidden" name="published" value="published">', false);
        $res->assertSee('<input type="hidden" name="billing_cycle" value="monthly">', false);

        // keyword 可能包含 URL 编码字符，这里只校验 name 存在，避免脆弱断言
        $res->assertSee('name="keyword"', false);
    }
}
