<?php
/** PSI 订单详情（合并自后台订单详情） */
/** @var array $o @var array $payments */
$canManage = \subsys_admin('psi');
$channelLabel = function ($c) {
    return $c === 'wechat' ? '微信' : ($c === 'alipay' ? '支付宝' : '—');
};
?>
<div class="page-head">
    <div><h1>订单详情</h1><div class="desc">订单号 <?php echo e($o['order_no']); ?></div></div>
    <a class="btn-ghost" href="<?php echo site_url('PSI/orders'); ?>">← 返回列表</a>
</div>

<?php echo flash_html(); ?>

<div class="panel">
    <table class="tbl">
        <tr><th>商品</th><td><?php echo e($o['product_name']); ?></td></tr>
        <tr><th>客户</th><td><?php echo e($o['customer_name']); ?>（<?php echo e($o['phone']); ?><?php if (!empty($o['email'])): ?>/ <?php echo e($o['email']); ?><?php endif; ?>）</td></tr>
        <tr><th>数量</th><td><?php echo e($o['qty']); ?> 套</td></tr>
        <tr><th>金额</th><td>¥<?php echo e(number_format((float)($o['amount'] ?? 0), 2)); ?></td></tr>
        <tr><th>支付通道</th><td><?php echo $channelLabel($o['channel']); ?></td></tr>
        <tr><th>状态</th><td>
            <?php echo $o['status'] === 'paid'
                ? '<span class="tag-mini" style="background:#dcfce7;color:#15803d">已支付</span>'
                : '<span class="tag-mini" style="background:#fef3c7;color:#b45309">待支付</span>'; ?>
            <?php if (!empty($o['paid_at'])): ?><span class="muted">（<?php echo e($o['paid_at']); ?>）</span><?php endif; ?>
        </td></tr>
        <tr><th>网关交易号</th><td><?php echo e($o['gateway_trade_no'] ?? '—'); ?></td></tr>
        <tr><th>下单时间</th><td><?php echo e($o['created_at']); ?></td></tr>
    </table>

    <?php if ($canManage && $o['status'] !== 'paid'): ?>
    <form method="post" action="<?php echo site_url('PSI/orders/markPaid/' . $o['id']); ?>" style="margin-top:16px">
        <?php echo csrf_field(); ?><button class="btn-primary" type="submit" data-confirm="标记为已支付？">标记为已支付</button>
    </form>
    <?php endif; ?>

    <?php if ($payments): ?>
    <h3 style="margin:24px 0 10px">付款记录</h3>
    <table class="tbl">
        <thead><tr><th>通道</th><th>交易号</th><th>金额</th><th>状态</th><th>付款时间</th></tr></thead>
        <tbody>
        <?php foreach ($payments as $p): ?>
            <tr>
                <td><?php echo $p['channel'] === 'wechat' ? '微信' : '支付宝'; ?></td>
                <td><?php echo e($p['trade_no']); ?></td>
                <td>¥<?php echo e(number_format((float)($p['amount'] ?? 0), 2)); ?></td>
                <td><?php echo e($p['status']); ?></td>
                <td><?php echo e($p['paid_at']); ?></td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
    <?php endif; ?>
</div>
