@extends('admin.layouts.app')

@section('title', '订单详情')
@section('page_title', '订单详情')

@section('content')
@php
    $incomingBack = (string) request()->query('back', '');
    $safeBackForLinks = \App\Support\BackUrl::sanitizeForLinks($incomingBack);

    // back 参数用于“返回上一页（保留上下文）”，但 back 本身不应再包含 back（避免无限嵌套导致 URL 膨胀）
    $selfWithoutBack = \App\Support\BackUrl::selfWithoutBack();

    // 去重降噪：仅在没有 incoming back 时，提供“返回订单列表（保留上下文）”入口
    $ordersIndexUrl = \App\Support\BackUrl::withBack('/admin/orders', $selfWithoutBack);
@endphp

<div class="page-header mb-20" data-page="admin.orders.show">
    <div class="page-header-main">
        <div>
            <div class="page-header-title">订单详情</div>
            <div class="page-header-subtitle">订单号 {{ $order->order_no }}（商家：{{ $order->merchant?->name ?? ('商家#'.$order->merchant_id) }}）</div>
        </div>
        <div class="page-header-actions">
            @if($safeBackForLinks)
                <a class="btn btn-secondary btn-sm" href="{!! $safeBackForLinks !!}">返回上一页（保留上下文）</a>
            @else
                <a class="btn btn-secondary btn-sm" href="{!! $ordersIndexUrl !!}">返回订单列表（保留上下文）</a>
            @endif
        </div>
    </div>

    <div class="page-header-meta">
        <div>订单状态：{{ $order->status }}</div>
        <div>支付状态：{{ $order->payment_status }}</div>
        <div>实付：¥{{ number_format((float) $order->pay_amount, 2) }}</div>
    </div>
</div>

<div class="card list-card mb-20">
    <div class="list-card-header">
        <div>
            <h3 class="list-card-title">订单信息</h3>
        </div>
    </div>

    <div class="list-card-body">
        <table class="list-card-table">
            <tbody>
            <tr><th style="width:160px;">ID</th><td>{{ $order->id }}</td></tr>
            <tr><th>商家</th><td>{{ $order->merchant?->name ?? ('商家#'.$order->merchant_id) }}</td></tr>
            <tr><th>平台</th><td>{{ $order->platform }}</td></tr>
            <tr><th>订单状态</th><td>{{ $order->status }}</td></tr>
            <tr><th>支付渠道</th><td>{{ $order->payment_channel }}</td></tr>
            <tr><th>支付状态</th><td>{{ $order->payment_status }}</td></tr>
            <tr><th>买家</th><td>{{ $order->buyer_name }}</td></tr>
            <tr><th>手机</th><td>{{ $order->buyer_phone }}</td></tr>
            <tr><th>邮箱</th><td>{{ $order->buyer_email }}</td></tr>
            <tr><th>商品金额</th><td>¥{{ number_format((float) $order->product_amount, 2) }}</td></tr>
            <tr><th>优惠金额</th><td>¥{{ number_format((float) $order->discount_amount, 2) }}</td></tr>
            <tr><th>运费</th><td>¥{{ number_format((float) $order->shipping_amount, 2) }}</td></tr>
            <tr><th>应付金额</th><td>¥{{ number_format((float) $order->pay_amount, 2) }}</td></tr>
            <tr><th>备注</th><td>{{ $order->remark }}</td></tr>
            <tr><th>创建时间</th><td>{{ $order->created_at }}</td></tr>
            </tbody>
        </table>
    </div>
</div>

<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>SKU</th>
                <th>单价</th>
                <th>数量</th>
                <th>小计</th>
                <th>快照</th>
            </tr>
            </thead>
            <tbody>
            @forelse($order->items as $item)
                <tr>
                    <td>{{ $item->id }}</td>
                    <td>{{ $item->product_title }}</td>
                    <td>{{ $item->product_sku }}</td>
                    <td>¥{{ number_format((float) $item->product_price, 2) }}</td>
                    <td>{{ $item->quantity }}</td>
                    <td>¥{{ number_format((float) $item->line_total_amount, 2) }}</td>
                    <td>{{ $item->snapshot['category'] ?? '-' }}</td>
                </tr>
            @empty
                <tr><td colspan="7" class="muted table-empty">暂无订单明细</td></tr>
            @endforelse
            </tbody>
        </table>
    </div>
</div>
@endsection
