@extends('merchant_admin.layouts.app')

@section('title', '商家订单管理')
@section('page_title', '商家订单管理')

@section('content')
<div class="card mb-20">
    <p class="muted muted-tight">当前订单列表已按登录商家过滤，商家管理员只能处理自己店铺的数据。</p>
    <p class="muted">当前订单列表已接入缓存：{{ $cacheMeta['store'] }} / TTL {{ $cacheMeta['ttl'] }}。</p>
    @php
        $exportQuery = http_build_query(array_filter($filters, fn ($value) => $value !== null && $value !== ''));
    @endphp
    <div class="mb-12"><a href="{{ '/merchant-admin/orders/export' . ($exportQuery ? ('?' . $exportQuery) : '') }}">导出当前筛选结果 CSV</a></div>
    <h3>筛选条件</h3>
    @if(!empty($filters['validation_errors']))
        <div class="filter-error"><strong>筛选条件有误：</strong><ul class="list-indent">@foreach($filters['validation_errors'] as $validationError)<li>{{ $validationError }}</li>@endforeach</ul></div>
    @endif
    <form method="get" action="/merchant-admin/orders">
        <div class="grid-5">
            <select name="status"><option value="">全部状态</option>@foreach($filterOptions['statuses'] as $status)<option value="{{ $statusLabels[$status] ?? $status }}" @selected($filters['status'] === $status)>{{ $statusLabels[$status] ?? $status }}</option>@endforeach</select>
            <select name="payment_status"><option value="">全部支付状态</option>@foreach($filterOptions['paymentStatuses'] as $paymentStatus)<option value="{{ $paymentStatusLabels[$paymentStatus] ?? $paymentStatus }}" @selected($filters['payment_status'] === $paymentStatus)>{{ $paymentStatusLabels[$paymentStatus] ?? $paymentStatus }}</option>@endforeach</select>
            <select name="platform"><option value="">全部平台</option>@foreach($filterOptions['platforms'] as $platform)<option value="{{ $platformLabels[$platform] ?? $platform }}" @selected($filters['platform'] === $platform)>{{ $platformLabels[$platform] ?? $platform }}</option>@endforeach</select>
            <select name="device_type"><option value="">全部设备类型</option>@foreach($filterOptions['deviceTypes'] 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>
            <input name="keyword" placeholder="订单号 / 买家 / 手机 / 邮箱" value="{{ $filters['keyword'] }}">
            <select name="time_range">@foreach($filterOptions['timeRanges'] as $timeRangeValue => $timeRangeLabel)<option value="{{ $timeRangeValue }}" @selected(($filters['time_range'] ?? 'all') === $timeRangeValue)>{{ $timeRangeLabel }}</option>@endforeach</select>
            <input type="date" name="start_date" value="{{ $filters['start_date'] }}">
            <input type="date" name="end_date" value="{{ $filters['end_date'] }}">
            <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'] }}">
            <select name="sort">@foreach($filterOptions['sortOptions'] as $sortValue => $sortLabel)<option value="{{ $sortValue }}" @selected(($filters['sort'] ?? 'latest') === $sortValue)>{{ $sortLabel }}</option>@endforeach</select>
            <div class="actions"><button type="submit">筛选</button><a href="/merchant-admin/orders">重置</a></div>
        </div>
    </form>
</div>

<div class="card section-dark mb-20"><h3 class="mt-0">当前筛选摘要</h3><div class="grid-3">@foreach($activeFilterSummary as $summaryLabel => $summaryValue)<div class="summary-box"><div class="muted">{{ $summaryLabel }}</div><strong class="num-md">{{ $summaryValue }}</strong></div>@endforeach</div></div>

<div class="card mb-20"><h3 class="mt-0">订单汇总</h3><div class="grid-5"><div class="stat-box-light"><div class="muted">订单总数</div><strong class="num-md">{{ $summaryStats['total_orders'] ?? 0 }}</strong></div><div class="stat-box-light"><div class="muted">实付总额</div><strong class="num-md">¥{{ number_format($summaryStats['total_pay_amount'] ?? 0, 2) }}</strong></div><div class="stat-box-light"><div class="muted">平均客单价</div><strong class="num-md">¥{{ number_format($summaryStats['average_order_amount'] ?? 0, 2) }}</strong></div><div class="stat-box-light"><div class="muted">待支付金额</div><strong class="num-md">¥{{ number_format($summaryStats['unpaid_pay_amount'] ?? 0, 2) }}</strong></div><div class="stat-box-light"><div class="muted">已支付金额</div><strong class="num-md">¥{{ number_format($summaryStats['paid_pay_amount'] ?? 0, 2) }}</strong></div><div class="stat-box-light"><div class="muted">已支付订单数</div><strong class="num-md">{{ $summaryStats['paid_orders'] ?? 0 }}</strong></div><div class="stat-box-light"><div class="muted">支付率</div><strong class="num-md">{{ number_format($summaryStats['payment_rate'] ?? 0, 2) }}%</strong></div><div class="stat-box-light"><div class="muted">退款订单数</div><strong class="num-md">{{ $summaryStats['refunded_orders'] ?? 0 }}</strong></div><div class="stat-box-light"><div class="muted">退款率</div><strong class="num-md">{{ number_format($summaryStats['refund_rate'] ?? 0, 2) }}%</strong></div><div class="stat-box-light"><div class="muted">待发货订单数</div><strong class="num-md">{{ $summaryStats['pending_shipment_orders'] ?? 0 }}</strong></div><div class="stat-box-light"><div class="muted">完成率</div><strong class="num-md">{{ number_format($summaryStats['completion_rate'] ?? 0, 2) }}%</strong></div><div class="stat-box-light"><div class="muted">支付失败订单</div><strong class="num-md">{{ $summaryStats['failed_payment_orders'] ?? 0 }}</strong></div><div class="stat-box-light"><div class="muted">已取消订单</div><strong class="num-md">{{ $summaryStats['cancelled_orders'] ?? 0 }}</strong></div><div class="stat-box-light"><div class="muted">取消率</div><strong class="num-md">{{ number_format($summaryStats['cancellation_rate'] ?? 0, 2) }}%</strong></div></div></div>

<div class="card mb-20"><h3 class="mt-0">时间趋势指标</h3><div class="grid-5"><div class="stat-box-light"><div class="muted">今日订单数</div><strong class="num-md">{{ $trendStats['today_orders'] ?? 0 }}</strong></div><div class="stat-box-light"><div class="muted">今日实付金额</div><strong class="num-md">¥{{ number_format($trendStats['today_pay_amount'] ?? 0, 2) }}</strong></div><div class="stat-box-light"><div class="muted">近7天订单数</div><strong class="num-md">{{ $trendStats['last_7_days_orders'] ?? 0 }}</strong></div><div class="stat-box-light"><div class="muted">近7天实付金额</div><strong class="num-md">¥{{ number_format($trendStats['last_7_days_pay_amount'] ?? 0, 2) }}</strong></div></div></div>

<div class="card section-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 mb-20">
    <h3 class="mt-0">状态统计</h3>
    @php
        $baseQuery = [
            '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,
            'start_date' => $filters['start_date'] ?: null,
            'end_date' => $filters['end_date'] ?: null,
            'min_pay_amount' => $filters['min_pay_amount'] ?: null,
            'max_pay_amount' => $filters['max_pay_amount'] ?: null,
            'time_range' => ($filters['time_range'] ?? 'all') !== 'all' ? $filters['time_range'] : null,
            'sort' => ($filters['sort'] ?? 'latest') !== 'latest' ? $filters['sort'] : null,
        ];
    @endphp
    <div class="grid-6">
        <a href="{{ '/merchant-admin/orders?' . http_build_query(array_filter($baseQuery, fn ($value) => $value !== null && $value !== '')) }}" class="status-link-light {{ $filters['status'] === '' ? 'is-active-light' : '' }}"><div class="muted">全部</div><strong class="num-md">{{ $statusStats['all'] ?? 0 }}</strong></a>
        @foreach($filterOptions['statuses'] as $status)
            <a href="{{ '/merchant-admin/orders?' . http_build_query(array_filter(array_merge($baseQuery, ['status' => $status]), fn ($value) => $value !== null && $value !== '')) }}" class="status-link-light {{ $filters['status'] === $status ? 'is-active-light' : '' }}"><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><th>发货时间</th><th>完成时间</th><th>状态</th><th>操作</th></tr></thead>
        <tbody>
        @forelse($orders as $order)
            <tr>
                <td>{{ $order->id }}</td><td><a href="/merchant-admin/orders/{{ $order->id }}">{{ $order->order_no }}</a></td><td>{{ $platformLabels[$order->platform] ?? $order->platform }}</td><td>{{ $order->buyer_name }}</td><td><div>{{ $paymentChannelLabels[$order->payment_channel] ?? $order->payment_channel }}</div><div class="muted">{{ $paymentStatusLabels[$order->payment_status] ?? $order->payment_status }}</div></td><td>¥{{ number_format($order->pay_amount, 2) }}</td><td>{{ $order->created_at?->format('Y-m-d H:i') }}</td><td>{{ $order->paid_at?->format('Y-m-d H:i') ?? '-' }}</td><td>{{ $order->shipped_at?->format('Y-m-d H:i') ?? '-' }}</td><td>{{ $order->completed_at?->format('Y-m-d H:i') ?? '-' }}</td><td>{{ $statusLabels[$order->status] ?? $order->status }}</td><td><form class="inline" method="post" action="/merchant-admin/orders/{{ $order->id }}/status">@csrf <select name="status"><option value="pending" @selected($order->status==='pending')>待处理</option><option value="paid" @selected($order->status==='paid')>已支付</option><option value="shipped" @selected($order->status==='shipped')>已发货</option><option value="completed" @selected($order->status==='completed')>已完成</option><option value="cancelled" @selected($order->status==='cancelled')>已取消</option></select><button type="submit">更新</button></form></td>
            </tr>
        @empty
            <tr><td colspan="12" class="muted text-center">暂无订单</td></tr>
        @endforelse
        </tbody>
    </table>
</div>

<div class="pagination-wrap">{{ $orders->links() }}</div>
@endsection
