<?php
/** PSI 订单管理（合并自后台订单） */
/** @var array $orders */
$canManage = \subsys_admin('psi');
$statusTag = function ($s) {
    return $s === 'paid'
        ? '<span class="tag-mini" style="background:#dcfce7;color:#15803d">已支付</span>'
        : '<span class="tag-mini" style="background:#fef3c7;color:#b45309">待支付</span>';
};
$channelLabel = function ($c) {
    return $c === 'wechat' ? '微信' : ($c === 'alipay' ? '支付宝' : '—');
};
?>
<div class="page-head">
    <div><h1>订单管理</h1><div class="desc">客户下单与付款处理（合并自后台订单，便于在 PSI 内快速处理）</div></div>
</div>

<?php echo flash_html(); ?>

<div class="panel">
    <div class="panel-bar">
        <h3>订单列表</h3>
        <span class="muted">共 <?php echo count($orders); ?> 笔</span>
    </div>
    <div class="table-wrap">
        <table class="tbl">
            <thead><tr>
                <th>订单号</th><th>商品</th><th>客户</th><th>数量</th><th>金额</th>
                <th>通道</th><th>状态</th><th>下单时间</th><th>操作</th>
            </tr></thead>
            <tbody>
            <?php foreach ($orders as $o): ?>
                <tr>
                    <td><?php echo e($o['order_no']); ?></td>
                    <td><?php echo e($o['product_name']); ?></td>
                    <td><?php echo e($o['customer_name']); ?><br><span class="muted"><?php echo e($o['phone']); ?></span></td>
                    <td><?php echo e($o['qty']); ?></td>
                    <td>¥<?php echo e(number_format((float)($o['amount'] ?? 0), 2)); ?></td>
                    <td><?php echo $channelLabel($o['channel']); ?></td>
                    <td><?php echo $statusTag($o['status']); ?></td>
                    <td class="muted"><?php echo e($o['created_at']); ?></td>
                    <td class="row-actions">
                        <a href="<?php echo site_url('PSI/orders/show/' . $o['id']); ?>">详情</a>
                        <?php if ($canManage && $o['status'] !== 'paid'): ?>
                        <form method="post" class="inline-form" action="<?php echo site_url('PSI/orders/markPaid/' . $o['id']); ?>" data-confirm="标记为已支付？">
                            <?php echo csrf_field(); ?><button type="submit" class="link-ok">标记支付</button>
                        </form>
                        <?php endif; ?>
                        <?php if ($canManage): ?>
                        <form method="post" class="inline-form" action="<?php echo site_url('PSI/orders/destroy/' . $o['id']); ?>" data-confirm="确认删除该订单？">
                            <?php echo csrf_field(); ?><button type="submit" class="link-danger">删除</button>
                        </form>
                        <?php endif; ?>
                    </td>
                </tr>
            <?php endforeach; ?>
            <?php if (empty($orders)): ?>
                <tr><td colspan="9" class="muted" style="text-align:center;padding:24px">暂无订单</td></tr>
            <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
