<?php
/** PSI 销售订单详情 */
/** @var array $o @var array $items */
$soStatus = [
    'pending'  => ['label' => '待处理', 'cls' => 'tag-gray'],
    'partial'  => ['label' => '部分交付', 'cls' => 'tag-amber'],
    'delivered'=> ['label' => '已交付', 'cls' => 'tag-green'],
    'closed'   => ['label' => '已关闭', 'cls' => 'tag-gray'],
];
$s = $soStatus[$o['status']] ?? $soStatus['pending'];
$amt = array_sum(array_map(fn($i) => (float)($i['amount'] ?? 0), $items));
?>
<div class="page-head">
    <div><h1>销售订单详情</h1><div class="desc">订单号 <?php echo e($o['order_no']); ?></div></div>
    <div style="display:flex;gap:10px">
        <a class="btn-ghost" href="<?php echo site_url('PSI/sales_orders/print/' . $o['id']); ?>" target="_blank"><?php echo admin_icon('printer', 16); ?> 打印</a>
        <a class="btn-ghost" href="<?php echo site_url('PSI/sales_orders'); ?>">← 返回</a>
    </div>
</div>

<div class="panel two-col">
    <table class="tbl">
        <tr><th>客户</th><td><?php echo e($o['customer']); ?></td></tr>
        <tr><th>业务员</th><td><?php echo e($o['salesman']); ?></td></tr>
        <tr><th>渠道</th><td><?php echo $o['channel'] === 'export' ? '外贸' : '内贸'; ?><?php if ($o['region']): ?> · <?php echo e($o['region']); ?><?php endif; ?></td></tr>
        <tr><th>状态</th><td><span class="tag-mini <?php echo $s['cls']; ?>"><?php echo $s['label']; ?></span></td></tr>
        <tr><th>交货日期</th><td><?php echo e($o['delivery_date'] ?? '—'); ?></td></tr>
        <tr><th>备注</th><td class="muted"><?php echo e($o['remark'] ?: '—'); ?></td></tr>
    </table>
</div>

<div class="panel">
    <div class="panel-bar"><h3>商品明细</h3><span class="muted">合计 ¥<?php echo e(number_format($amt, 2)); ?></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></tr></thead>
            <tbody>
            <?php foreach ($items as $it): $remain = max(0, (int)($it['qty'] ?? 0) - (int)($it['delivered_qty'] ?? 0)); ?>
                <tr>
                    <td><?php echo e($it['name']); ?></td>
                    <td class="muted"><?php echo e($it['spec']); ?></td>
                    <td class="muted"><?php echo e($it['unit']); ?></td>
                    <td><?php echo e($it['qty']); ?></td>
                    <td>¥<?php echo e(number_format((float)($it['price'] ?? 0), 2)); ?></td>
                    <td>¥<?php echo e(number_format((float)($it['amount'] ?? 0), 2)); ?></td>
                    <td><?php echo e($it['delivered_qty'] ?? 0); ?></td>
                    <td><?php echo $remain; ?></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <div style="margin-top:16px;display:flex;gap:10px">
        <a class="btn-primary" href="<?php echo site_url('PSI/sales_orders/edit/' . $o['id']); ?>">编辑</a>
        <a class="btn-ghost" href="<?php echo site_url('PSI/outbounds/create?so=' . $o['order_no']); ?>"><?php echo admin_icon('package', 16); ?> 据此出库</a>
    </div>
</div>
