<?php
/** PSI 销售订单列表 */
/** @var array $orders */
$soStatus = [
    'pending'  => ['label' => '待处理', 'cls' => 'tag-gray'],
    'partial'  => ['label' => '部分交付', 'cls' => 'tag-amber'],
    'delivered'=> ['label' => '已交付', 'cls' => 'tag-green'],
    'closed'   => ['label' => '已关闭', 'cls' => 'tag-gray'],
];
?>
<div class="page-head">
    <div><h1>销售订单</h1><div class="desc">录入客户销售订单，可关联出库单进行交付（直接打印）</div></div>
    <a class="btn-primary" href="<?php echo site_url('PSI/sales_orders/create'); ?>"><?php echo admin_icon('plus', 16); ?> 新建销售订单</a>
</div>

<div class="panel">
    <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):
                $amt = array_sum(array_map(fn($i) => (float)($i['amount'] ?? 0), $o['_items'] ?? []));
                $s = $soStatus[$o['status']] ?? $soStatus['pending'];
            ?>
                <tr>
                    <td><?php echo e($o['order_no']); ?></td>
                    <td><?php echo e($o['customer']); ?></td>
                    <td><?php echo e($o['salesman']); ?></td>
                    <td><?php echo $o['channel'] === 'export' ? '外贸' : '内贸'; ?></td>
                    <td class="muted"><?php echo count($o['_items'] ?? []); ?> 项</td>
                    <td>¥<?php echo e(number_format($amt, 2)); ?></td>
                    <td><span class="tag-mini <?php echo $s['cls']; ?>"><?php echo $s['label']; ?></span></td>
                    <td class="muted"><?php echo e($o['delivery_date'] ?? '—'); ?></td>
                    <td class="row-actions">
                        <a href="<?php echo site_url('PSI/sales_orders/show/' . $o['id']); ?>">详情</a>
                        <a href="<?php echo site_url('PSI/sales_orders/print/' . $o['id']); ?>" target="_blank">打印</a>
                        <a href="<?php echo site_url('PSI/sales_orders/edit/' . $o['id']); ?>">编辑</a>
                        <a href="<?php echo site_url('PSI/outbounds/create?so=' . $o['order_no']); ?>" class="link-ok">出库</a>
                        <form method="post" class="inline-form" action="<?php echo site_url('PSI/sales_orders/destroy/' . $o['id']); ?>" data-confirm="确认删除该销售订单？">
                            <?php echo csrf_field(); ?><button class="link-danger" type="submit">删除</button>
                        </form>
                    </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>
