<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Arr;
use Tests\TestCase;

class AdminPlatformOrderReceiptSummaryPaidNoReceiptLinkShouldPreserveContextTest 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_paid_no_receipt_summary_link_should_preserve_context(): void
    {
        $this->loginAsPlatformAdmin();

        $res = $this->get('/admin/platform-orders?' . Arr::query([
            'merchant_id' => 2,
            'plan_id' => 3,
            'keyword' => 'alpha',
            'back' => '/admin/site-subscriptions',
            'page' => 7,
        ]));
        $res->assertOk();

        $html = (string) $res->getContent();
        $matched = preg_match('/data-role="po-summary-link-no-receipt-orders"[^>]*href="([^"]+)"/u', $html, $m);
        $this->assertSame(1, $matched, '未找到平台订单摘要卡“已付无回执订单”链接');

        $href = html_entity_decode($m[1] ?? '');
        $parts = parse_url($href);
        parse_str($parts['query'] ?? '', $q);

        $this->assertSame('2', (string) ($q['merchant_id'] ?? ''));
        $this->assertSame('3', (string) ($q['plan_id'] ?? ''));
        $this->assertSame('alpha', (string) ($q['keyword'] ?? ''));
        $this->assertSame('/admin/site-subscriptions', (string) ($q['back'] ?? ''));
        $this->assertSame('paid', (string) ($q['payment_status'] ?? ''));
        $this->assertSame('none', (string) ($q['receipt_status'] ?? ''));
        $this->assertArrayNotHasKey('page', $q);
    }
}
