@extends('admin.layouts.app')

@section('title', '站点管理')
@section('page_title', '站点管理')

@section('content')
@php
    // back 安全护栏：用于可能从其它页面跳入站点管理时的回退
    $incomingBack = (string) request()->query('back', '');
    $safeBackForLinks = \App\Support\BackUrl::sanitizeForLinks($incomingBack);

    // 当前页自身（去掉 back），用于生成 back 回跳，避免 back 嵌套膨胀
    $selfWithoutBack = \App\Support\BackUrl::selfWithoutBack();

    $merchantIndexUrlWithBack = \App\Support\BackUrl::withBack('/admin/merchants', $safeBackForLinks);

    $makePlatformOrdersUrl = function (int $merchantId, array $overrides = []) use ($selfWithoutBack) {
        $q = array_merge(['merchant_id' => $merchantId], $overrides);
        $url = '/admin/platform-orders?' . \Illuminate\Support\Arr::query($q);

        return \App\Support\BackUrl::withBack($url, $selfWithoutBack);
    };

    $makeSubscriptionsUrl = function (int $merchantId, array $overrides = []) use ($selfWithoutBack) {
        $q = array_merge(['merchant_id' => $merchantId], $overrides);
        $url = '/admin/site-subscriptions?' . \Illuminate\Support\Arr::query($q);

        return \App\Support\BackUrl::withBack($url, $selfWithoutBack);
    };
@endphp

<div class="page-header mb-20" data-page="admin.merchants.index">
    <div class="page-header-main">
        <div>
            <div class="page-header-title">站点管理</div>
            <div class="page-header-subtitle">总台视角的站点管理入口：开通、查看和维护 SaaS 站点主体，并联动订阅/平台订单做收费闭环治理。</div>
        </div>
        <div class="page-header-actions">
            @if($safeBackForLinks !== '')
                <a href="{!! $safeBackForLinks !!}" class="btn btn-secondary btn-sm">返回上一页（保留上下文）</a>
            @endif
        </div>
    </div>
</div>

<details class="card mb-20 collapsible filters-card" data-role="collapsible" data-storage-key="admin.merchants.filters" open>
    <summary class="collapsible-summary filters-summary">
        <div class="flex-between items-center">
            <div>
                <h3 class="mb-0">站点管理</h3>
                <div class="muted muted-xs">总台视角的站点管理入口：开通、查看和维护 SaaS 站点主体。</div>
            </div>
            <span class="muted muted-xs">点击收起/展开</span>
        </div>
        <div class="muted muted-xs mt-6">当前站点列表已接入缓存：{{ $cacheMeta['store'] }} / TTL {{ $cacheMeta['ttl'] }}。</div>

    </summary>

    <div class="collapsible-body filters-body">
        <h3 class="mt-0">新增站点</h3>
        <form method="post" action="/admin/merchants" class="filters-grid">
            @csrf
            <div class="grid-3">
                <input name="name" placeholder="站点名称" value="{{ old('name') }}">
                <input name="slug" placeholder="站点标识 slug" value="{{ old('slug') }}">
                <input name="plan" placeholder="套餐，如 pro/basic" value="{{ old('plan', 'basic') }}">
                <input name="status" placeholder="状态" value="{{ old('status', 'active') }}">
                <input name="contact_name" placeholder="联系人" value="{{ old('contact_name') }}">
                <input name="contact_phone" placeholder="联系电话" value="{{ old('contact_phone') }}">
            </div>
            <div class="mt-12"><input name="contact_email" placeholder="联系邮箱" class="w-full" value="{{ old('contact_email') }}"></div>
            <div class="mt-12"><button type="submit" class="btn btn-sm">创建站点</button></div>
        </form>
    </div>
</details>

<div class="card list-card">
    <div class="list-card-header">
        <div>
            <h3 class="list-card-title">站点列表</h3>
        </div>
    </div>
    <div class="list-card-body">
        <table class="list-card-table">
            <thead><tr><th>ID</th><th>站点名称</th><th>Slug</th><th>套餐</th><th>状态</th><th>联系人</th><th>操作</th></tr></thead>
            <tbody>
            @forelse($merchants as $merchant)
                <tr>
                    <td>{{ $merchant->id }}</td>
                    <td>{{ $merchant->name }}</td>
                    <td>{{ $merchant->slug }}</td>
                    <td>{{ $merchant->plan }}</td>
                    <td>{{ $merchant->status }}</td>
                    <td>{{ $merchant->contact_name }} / {{ $merchant->contact_phone }}</td>
                    <td>
                        <div class="actions gap-10">
                            <a href="/site-admin/login" target="_blank" rel="noopener">进入站点后台</a>
                            <a class="muted" href="{!! $makeSubscriptionsUrl((int) $merchant->id) !!}">订阅</a>
                            <a class="muted" href="{!! $makePlatformOrdersUrl((int) $merchant->id) !!}">平台订单</a>
                            <a class="muted" href="{!! $makePlatformOrdersUrl((int) $merchant->id, ['renewal_missing_subscription' => '1']) !!}">续费缺订阅</a>
                            <a class="muted" href="{!! $makePlatformOrdersUrl((int) $merchant->id, ['payment_status' => 'paid', 'receipt_status' => 'none']) !!}">已付无回执</a>
                        </div>
                        <div class="muted muted-xs">当前阶段请使用该站点管理员账号登录</div>
                    </td>
                </tr>
            @empty
                <tr><td colspan="7" class="muted table-empty">暂无站点</td></tr>
            @endforelse
            </tbody>
        </table>

        {{ $merchants->links('pagination.admin') }}
    </div>
</div>
@endsection
