@extends('site_admin.layouts.app')

@section('title', '站点商家')
@section('page_title', '站点商家')

@section('content')
@php
    $productWorkbenchUrl = '/site-admin/products?sort=stock_desc&status=published';
    $lowStockWorkbenchUrl = '/site-admin/products?sort=stock_asc&status=published';
    $latestProductWorkbenchUrl = '/site-admin/products?sort=latest';
    $orderWorkbenchUrl = '/site-admin/orders?sort=pay_amount_desc&payment_status=paid';
    $pendingOrderWorkbenchUrl = '/site-admin/orders?sort=latest&payment_status=unpaid';
    $failedOrderWorkbenchUrl = '/site-admin/orders?sort=latest&payment_status=failed';
@endphp
<div class="card card-spaced">
    <p class="muted mt-0">当前阶段继续复用 merchant 承接站点关系，但页面已先补最小运营查看能力，方便后续从“承接记录”平滑过渡到真实站点治理。</p>
    @php
        $merchantExportQuery = http_build_query(array_filter([
            'keyword' => $filters['keyword'] ?? '',
            'status' => $filters['status'] ?? '',
            'plan' => $filters['plan'] ?? '',
            'sort' => $filters['sort'] ?? 'latest',
        ], fn ($value) => $value !== null && $value !== ''));
    @endphp
    <div class="mb-12">
        <a href="{{ '/site-admin/merchants/export' . ($merchantExportQuery ? ('?' . $merchantExportQuery) : '') }}">导出当前筛选结果 CSV</a>
    </div>
    <h3>筛选条件</h3>
    <form method="get" action="/site-admin/merchants">
        <div class="grid-4 form-grid grid-align-end">
            <input type="text" name="keyword" placeholder="站点名称 / slug / 联系人 / 手机 / 邮箱" value="{{ $filters['keyword'] ?? '' }}">
            <select name="status">
                <option value="">全部状态</option>
                @foreach(($filterOptions['statuses'] ?? []) as $status)
                    <option value="{{ $status }}" @selected(($filters['status'] ?? '') === $status)>{{ $statusLabels[$status] ?? $status }}</option>
                @endforeach
            </select>
            <select name="plan">
                <option value="">全部套餐</option>
                @foreach(($filterOptions['plans'] ?? []) as $plan)
                    <option value="{{ $plan }}" @selected(($filters['plan'] ?? '') === $plan)>{{ $planLabels[$plan] ?? $plan }}</option>
                @endforeach
            </select>
            <select name="sort">
                @foreach(($filterOptions['sortOptions'] ?? []) as $value => $label)
                    <option value="{{ $value }}" @selected(($filters['sort'] ?? 'latest') === $value)>{{ $label }}</option>
                @endforeach
            </select>
        </div>
        <div class="actions mt-12">
            <button type="submit">应用筛选</button>
            <a href="/site-admin/merchants">重置</a>
        </div>
    </form>
</div>

<div class="card card-dark card-spaced">
    <h3 class="mt-0">当前筛选摘要</h3>
    <div class="grid-4">
        @foreach(($activeFilterSummary ?? []) as $label => $value)
            <div class="summary-box">
                <div class="muted">{{ $label }}</div>
                <strong class="text-md">{{ $value }}</strong>
            </div>
        @endforeach
    </div>
</div>

<div class="card card-spaced">
    <h3 class="mt-0">站点运营摘要</h3>
    <div class="grid-4">
        <div class="stat-box">
            <div class="muted">承接站点数</div>
            <strong class="num-md">{{ $summaryStats['site_count'] ?? 0 }}</strong>
        </div>
        <div class="stat-box">
            <div class="muted">启用中站点</div>
            <strong class="num-md">{{ $summaryStats['active_site_count'] ?? 0 }}</strong>
        </div>
        <div class="stat-box">
            <div class="muted">站点管理员数</div>
            <strong class="num-md">{{ $summaryStats['admin_count'] ?? 0 }}</strong>
        </div>
        <div class="stat-box">
            <div class="muted">站点用户数</div>
            <strong class="num-md">{{ $summaryStats['user_count'] ?? 0 }}</strong>
        </div>
        <div class="stat-box">
            <div class="muted">站点商品数</div>
            <strong class="num-md">{{ $summaryStats['product_count'] ?? 0 }}</strong>
        </div>
        <div class="stat-box">
            <div class="muted">站点订单数</div>
            <strong class="num-md">{{ $summaryStats['order_count'] ?? 0 }}</strong>
        </div>
        <div class="stat-box">
            <div class="muted">商品分类数</div>
            <strong class="num-md">{{ $summaryStats['category_count'] ?? 0 }}</strong>
        </div>
    </div>
</div>

<div class="card card-dark card-spaced">
    <h3 class="mt-0">运营关注项</h3>
    <div class="grid-3">
        <div class="focus-box">
            <div class="muted">商品侧</div>
            <strong class="text-md">
                @if(($summaryStats['product_count'] ?? 0) <= 0)
                    当前站点暂无商品，建议优先补齐基础商品数据。
                @elseif(($summaryStats['product_count'] ?? 0) < 3)
                    当前站点商品仍较少，建议优先查看最近新增与基础信息是否完整。
                @else
                    当前站点已有较稳定商品沉淀，建议优先巡检低库存与高库存商品结构。
                @endif
            </strong>
            <div class="muted mt-8">建议动作</div>
            <div class="inline-links mt-6">
                @if(($summaryStats['product_count'] ?? 0) <= 0)
                    <a href="{{ $latestProductWorkbenchUrl }}">先看商品空白情况</a>
                @elseif(($summaryStats['product_count'] ?? 0) < 3)
                    <a href="{{ $latestProductWorkbenchUrl }}">去看最近新增商品</a>
                    <a href="{{ $productWorkbenchUrl }}">去看已上架商品</a>
                @else
                    <a href="{{ $lowStockWorkbenchUrl }}">去看低库存商品</a>
                    <a href="{{ $productWorkbenchUrl }}">去看高库存商品</a>
                @endif
            </div>
        </div>
        <div class="focus-box">
            <div class="muted">订单侧</div>
            <strong class="text-md">
                @if(($summaryStats['order_count'] ?? 0) <= 0)
                    当前站点暂无订单，建议先确认交易链路与下单链路是否已完成联调。
                @elseif(($summaryStats['order_count'] ?? 0) < 5)
                    当前站点已有少量订单沉淀，建议先查看待支付与最近订单走势。
                @else
                    当前站点订单已形成基础规模，建议优先关注待支付、支付失败与高金额订单。
                @endif
            </strong>
            <div class="muted mt-8">建议动作</div>
            <div class="inline-links mt-6">
                @if(($summaryStats['order_count'] ?? 0) <= 0)
                    <a href="{{ $orderWorkbenchUrl }}">先看订单整体情况</a>
                @elseif(($summaryStats['order_count'] ?? 0) < 5)
                    <a href="{{ $pendingOrderWorkbenchUrl }}">去看待支付订单</a>
                    <a href="{{ $orderWorkbenchUrl }}">去看已支付订单</a>
                @else
                    <a href="{{ $pendingOrderWorkbenchUrl }}">去看待支付订单</a>
                    <a href="{{ $failedOrderWorkbenchUrl }}">去看支付失败订单</a>
                @endif
            </div>
        </div>
        <div class="focus-box">
            <div class="muted">联系信息</div>
            <strong class="text-md">
                @if(! $site->contact_name && ! $site->contact_phone && ! $site->contact_email)
                    联系信息仍为空，建议尽快补齐联系人、手机与邮箱，避免后续运营跟进断点。
                @elseif(! $site->contact_name)
                    当前缺少联系人姓名，建议先补联系人主体，再继续做日常跟进。
                @elseif(! $site->contact_phone && ! $site->contact_email)
                    当前缺少联系方式，建议至少补齐手机号或邮箱中的一项。
                @else
                    联系人信息已具备，可继续作为站点日常跟进入口。
                @endif
            </strong>
            <div class="muted mt-8">建议动作</div>
            <div class="inline-links mt-6">
                <a href="#site-profile">去看站点资料</a>
                @if(! $site->contact_name && ! $site->contact_phone && ! $site->contact_email)
                    <a href="#site-profile">优先补联系人与联系方式</a>
                @elseif(! $site->contact_name)
                    <a href="#site-profile">优先补联系人</a>
                @elseif(! $site->contact_phone && ! $site->contact_email)
                    <a href="#site-profile">优先补联系方式</a>
                @else
                    <a href="#site-records">去看承接记录</a>
                @endif
            </div>
        </div>
    </div>
</div>

<div class="card card-spaced">
    <h3 class="mt-0">工作台导航</h3>
    <div class="grid-3">
        <div class="nav-box">
            <div class="muted mb-8">商品工作台</div>
            <div class="inline-links">
                <a href="{{ $productWorkbenchUrl }}">高库存已上架</a>
                <a href="{{ $lowStockWorkbenchUrl }}">低库存补货</a>
                <a href="{{ $latestProductWorkbenchUrl }}">最近新增</a>
            </div>
        </div>
        <div class="nav-box">
            <div class="muted mb-8">订单工作台</div>
            <div class="inline-links">
                <a href="{{ $orderWorkbenchUrl }}">高金额已支付</a>
                <a href="{{ $pendingOrderWorkbenchUrl }}">待支付跟进</a>
                <a href="{{ $failedOrderWorkbenchUrl }}">支付失败排查</a>
            </div>
        </div>
        <div class="nav-box">
            <div class="muted mb-8">站点信息</div>
            <div class="inline-links">
                <a href="#site-profile">查看当前站点资料</a>
                <a href="#site-records">查看承接记录</a>
                <a href="{{ '/site-admin/merchants' . ($merchantExportQuery ? ('?' . $merchantExportQuery) : '') }}">返回当前筛选视图</a>
            </div>
        </div>
    </div>
</div>

<div class="card card-spaced" id="site-profile">
    <h3 class="mt-0">当前站点资料</h3>
    <div class="grid-3">
        <div class="profile-box">
            <div class="muted">站点名称</div>
            <strong class="num-sm">{{ $site->name }}</strong>
        </div>
        <div class="profile-box">
            <div class="muted">站点标识</div>
            <strong class="num-sm">{{ $site->slug }}</strong>
        </div>
        <div class="profile-box">
            <div class="muted">当前状态</div>
            <strong class="num-sm"><span class="badge badge-success">{{ $statusLabels[$site->status] ?? $site->status }}</span></strong>
        </div>
        <div class="profile-box">
            <div class="muted">当前套餐</div>
            <strong class="num-sm"><span class="badge badge-info">{{ $planLabels[$site->plan] ?? ($site->plan ?: '未设置') }}</span></strong>
        </div>
        <div class="profile-box">
            <div class="muted">联系人</div>
            <strong class="num-sm">{{ $site->contact_name ?: '未设置' }}</strong>
        </div>
        <div class="profile-box">
            <div class="muted">联系电话</div>
            <strong class="num-sm">{{ $site->contact_phone ?: '未设置' }}</strong>
        </div>
        <div class="profile-box">
            <div class="muted">联系邮箱</div>
            <strong class="num-sm">{{ $site->contact_email ?: '未设置' }}</strong>
        </div>
        <div class="profile-box">
            <div class="muted">激活时间</div>
            <strong class="num-sm">{{ $site->activated_at?->format('Y-m-d H:i') ?? '未激活' }}</strong>
        </div>
        <div class="profile-box span-3">
            <div class="muted">站点承接说明</div>
            <strong class="text-md">当前阶段由站点后台承接商品、订单与联系人信息查看；后续若拆出“站点 -> 商家”实体关系，这里将平滑升级为真正的站点运营台。</strong>
        </div>
    </div>
    <div class="actions mt-12">
        <a href="{{ $productWorkbenchUrl }}">查看站点商品</a>
        <a href="{{ $orderWorkbenchUrl }}">查看站点订单</a>
        <a href="{{ $lowStockWorkbenchUrl }}">查看低库存商品</a>
        <a href="{{ $pendingOrderWorkbenchUrl }}">查看待支付订单</a>
    </div>
    <div class="card card-dark card-dashed mt-12">
        <div class="muted mb-8">运营建议</div>
        <div class="inline-links">
            <a href="{{ $productWorkbenchUrl }}">高库存已上架商品</a>
            <a href="{{ $lowStockWorkbenchUrl }}">低库存补货视角</a>
            <a href="{{ $latestProductWorkbenchUrl }}">最近新增商品</a>
            <a href="{{ $orderWorkbenchUrl }}">高金额已支付订单</a>
            <a href="{{ $pendingOrderWorkbenchUrl }}">待支付订单跟进</a>
            <a href="{{ $failedOrderWorkbenchUrl }}">支付失败订单排查</a>
        </div>
    </div>
    <p class="muted helper-text">快捷入口默认带入更适合运营查看的排序与状态条件：商品优先看已上架高库存，订单优先看已支付高金额；同时也补了低库存、待支付、支付失败等常用运营视角。</p>
</div>

<div class="card" id="site-records">
    <h3 class="mt-0">当前站点承接记录</h3>
    <table>
        <thead><tr><th>ID</th><th>名称</th><th>Slug</th><th>状态</th><th>套餐</th><th>联系人</th><th>管理员数</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><span class="badge badge-success">{{ $statusLabels[$merchant->status] ?? $merchant->status }}</span></td>
                <td><span class="badge badge-info">{{ $planLabels[$merchant->plan] ?? ($merchant->plan ?: '未设置') }}</span></td>
                <td>{{ ($merchant->contact_name ?: '未设置') }} / {{ ($merchant->contact_phone ?: '未设置') }}</td>
                <td>{{ $merchant->admins_count ?? 0 }}</td>
                <td>{{ $merchant->users_count ?? 0 }}</td>
                <td>{{ $merchant->products_count ?? 0 }}</td>
                <td>{{ $merchant->orders_count ?? 0 }}</td>
                <td>
                    <div class="inline-links">
                        <a href="{{ $productWorkbenchUrl }}">查看商品</a>
                        <a href="{{ $orderWorkbenchUrl }}">查看订单</a>
                        <a href="{{ $lowStockWorkbenchUrl }}">低库存</a>
                        <a href="{{ $failedOrderWorkbenchUrl }}">支付失败</a>
                    </div>
                </td>
            </tr>
        @empty
            <tr>
                <td colspan="11" class="muted table-empty">暂无站点承接记录</td>
            </tr>
        @endforelse
        </tbody>
    </table>
</div>
@endsection
