@extends('site_admin.layouts.app')

@section('title', '站点订单')
@section('page_title', '站点订单')

@section('content')
<div class="card card-spaced">
    <p class="muted mt-0">当前页面展示站点范围内的订单列表，已补最小筛选与导出入口，便于站点侧先做基础运营查看。</p>
    @if(!empty($filters['validation_errors'] ?? []))
        <div class="error-box mt-12">
            <strong>筛选条件有误：</strong>
            <ul class="ml-18">
                @foreach(($filters['validation_errors'] ?? []) as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif
    @php
        $orderExportQuery = http_build_query(array_filter([
            'keyword' => $filters['keyword'] ?? '',
            'status' => $filters['status'] ?? '',
            'payment_status' => $filters['payment_status'] ?? '',
            'platform' => $filters['platform'] ?? '',
            'device_type' => $filters['device_type'] ?? '',
            'payment_channel' => $filters['payment_channel'] ?? '',
            'min_pay_amount' => $filters['min_pay_amount'] ?? '',
            'max_pay_amount' => $filters['max_pay_amount'] ?? '',
            'sort' => $filters['sort'] ?? 'latest',
        ], fn ($value) => $value !== null && $value !== ''));
    @endphp
    <h3>筛选条件</h3>
    <form method="get" action="/site-admin/orders">
        <div class="grid-5 form-grid">
            <input type="text" name="keyword" placeholder="关键词：订单号 / 买家信息" 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="payment_status">
                <option value="">全部支付状态</option>
                @foreach(($filterOptions['paymentStatuses'] ?? []) as $status)
                    <option value="{{ $status }}" @selected(($filters['payment_status'] ?? '') === $status)>{{ ($paymentStatusLabels[$status] ?? $status) }}</option>
                @endforeach
            </select>
            <select name="platform">
                <option value="">全部平台</option>
                @foreach(($filterOptions['platforms'] ?? []) as $platform)
                    <option value="{{ $platform }}" @selected(($filters['platform'] ?? '') === $platform)>{{ ($platformLabels[$platform] ?? $platform) }}</option>
                @endforeach
            </select>
            <select name="device_type">
                <option value="">全部设备类型</option>
                @foreach(['desktop','mobile','mini-program','mobile-webview','app-api'] as $deviceType)
                    <option value="{{ $deviceType }}" @selected(($filters['device_type'] ?? '') === $deviceType)>{{ $deviceTypeLabels[$deviceType] ?? $deviceType }}</option>
                @endforeach
            </select>
            <select name="payment_channel">
                <option value="">全部支付渠道</option>
                @foreach(($filterOptions['paymentChannels'] ?? []) as $paymentChannel)
                    <option value="{{ $paymentChannel }}" @selected(($filters['payment_channel'] ?? '') === $paymentChannel)>{{ ($paymentChannelLabels[$paymentChannel] ?? $paymentChannel) }}</option>
                @endforeach
            </select>
            <select name="sort">
                @foreach(($filterOptions['sortOptions'] ?? []) as $value => $label)
                    <option value="{{ $value }}" @selected(($filters['sort'] ?? 'latest') === $value)>{{ $label }}</option>
                @endforeach
            </select>
            <input type="number" step="0.01" min="0" name="min_pay_amount" placeholder="最低实付金额" value="{{ $filters['min_pay_amount'] ?? '' }}">
            <input type="number" step="0.01" min="0" name="max_pay_amount" placeholder="最高实付金额" value="{{ $filters['max_pay_amount'] ?? '' }}">
        </div>
        <div class="actions mt-12">
            <button type="submit">应用筛选</button>
            <a href="/site-admin/orders">重置</a>
            <a href="{{ '/site-admin/orders/export' . ($orderExportQuery ? ('?' . $orderExportQuery) : '') }}">导出当前筛选结果 CSV</a>
        </div>
    </form>
</div>

<div class="card card-spaced">
    <h3 class="mt-0">当前筛选摘要</h3>
    <div class="grid-3">
        @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-5">
        <div class="stat-box">
            <div class="muted">订单总数</div>
            <strong class="num-md">{{ $summaryStats['total_orders'] ?? 0 }}</strong>
        </div>
        <div class="stat-box">
            <div class="muted">实付总额</div>
            <strong class="num-md">¥{{ number_format($summaryStats['total_pay_amount'] ?? 0, 2) }}</strong>
        </div>
        <div class="stat-box">
            <div class="muted">平均客单价</div>
            <strong class="num-md">¥{{ number_format($summaryStats['average_order_amount'] ?? 0, 2) }}</strong>
        </div>
        <div class="stat-box">
            <div class="muted">已支付订单数</div>
            <strong class="num-md">{{ $summaryStats['paid_orders'] ?? 0 }}</strong>
        </div>
        <div class="stat-box">
            <div class="muted">支付失败订单</div>
            <strong class="num-md">{{ $summaryStats['failed_payment_orders'] ?? 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">{{ $operationsFocus['headline'] ?? '当前站点订单运营信息已就绪。' }}</strong>
            <div class="muted mt-8">建议动作</div>
            <div class="inline-links mt-6">
                @foreach(($operationsFocus['actions'] ?? []) as $action)
                    <a href="{{ $action['url'] }}">{{ $action['label'] }}</a>
                @endforeach
            </div>
        </div>
        <div class="focus-box">
            <div class="muted">当前信号</div>
            <div class="grid-3 mt-8">
                @foreach(($operationsFocus['signals'] ?? []) as $label => $value)
                    <div class="summary-box">
                        <div class="muted">{{ $label }}</div>
                        <strong class="num-sm">{{ $value }}</strong>
                    </div>
                @endforeach
            </div>
        </div>
        <div class="focus-box">
            <div class="muted">工作台导航</div>
            <div class="inline-links mt-8">
                @foreach(($operationsFocus['workbench'] ?? []) as $label => $url)
                    <a href="{{ $url }}">{{ $label }}</a>
                @endforeach
            </div>
        </div>
    </div>
</div>

<div class="card card-dark card-spaced">
    <h3 class="mt-0">订单状态统计</h3>
    @php
        $orderBaseQuery = [
            'payment_status' => ($filters['payment_status'] ?? '') ?: null,
            'platform' => ($filters['platform'] ?? '') ?: null,
            'device_type' => ($filters['device_type'] ?? '') ?: null,
            'payment_channel' => ($filters['payment_channel'] ?? '') ?: null,
            'keyword' => ($filters['keyword'] ?? '') ?: null,
            'min_pay_amount' => ($filters['min_pay_amount'] ?? '') ?: null,
            'max_pay_amount' => ($filters['max_pay_amount'] ?? '') ?: null,
            'sort' => ($filters['sort'] ?? 'latest') !== 'latest' ? ($filters['sort'] ?? 'latest') : null,
        ];
    @endphp
    <div class="grid-6">
        <a class="status-card {{ ($filters['status'] ?? '') === '' ? 'status-card-active' : '' }}" href="{{ '/site-admin/orders?' . http_build_query(array_filter($orderBaseQuery, fn ($value) => $value !== null && $value !== '')) }}">
            <div class="muted">全部</div>
            <strong class="num-md">{{ $statusStats['all'] ?? 0 }}</strong>
        </a>
        @foreach(($filterOptions['statuses'] ?? []) as $status)
            <a class="status-card {{ ($filters['status'] ?? '') === $status ? 'status-card-active' : '' }}" href="{{ '/site-admin/orders?' . http_build_query(array_filter(array_merge($orderBaseQuery, ['status' => $status]), fn ($value) => $value !== null && $value !== '')) }}">
                <div class="muted">{{ ($statusLabels[$status] ?? $status) }}</div>
                <strong class="num-md">{{ $statusStats[$status] ?? 0 }}</strong>
            </a>
        @endforeach
    </div>
</div>

<div class="card">
    <h3>订单列表</h3>
    <table>
        <thead><tr><th>ID</th><th>订单号</th><th>状态</th><th>支付状态</th><th>平台</th><th>设备</th><th>支付渠道</th><th>实付金额</th></tr></thead>
        <tbody>
        @forelse($orders as $order)
            <tr>
                <td>{{ $order->id }}</td>
                <td>{{ $order->order_no }}</td>
                <td>{{ $statusLabels[$order->status] ?? $order->status }}</td>
                <td>{{ $paymentStatusLabels[$order->payment_status] ?? $order->payment_status }}</td>
                <td>{{ $platformLabels[$order->platform] ?? $order->platform }}</td>
                <td>{{ $deviceTypeLabels[$order->device_type] ?? $order->device_type }}</td>
                <td>{{ $paymentChannelLabels[$order->payment_channel] ?? $order->payment_channel }}</td>
                <td>{{ number_format((float) $order->pay_amount, 2) }}</td>
            </tr>
        @empty
            <tr><td colspan="8" class="muted">暂无订单</td></tr>
        @endforelse
        </tbody>
    </table>
</div>
<div class="mt-16">{{ $orders->links() }}</div>
@endsection
