<?php
/** PSI 出库单列表 */
/** @var array $list */
$obStatus = ['delivered' => ['label' => '已交付', 'cls' => 'tag-green'], 'partial' => ['label' => '部分交付', 'cls' => 'tag-amber']];
?>
<div class="page-head">
    <div><h1>出库单</h1><div class="desc">按销售订单发货出库（自动扣减库存并回写已交付量，可直接打印）</div></div>
    <a class="btn-primary" href="<?php echo site_url('PSI/outbounds/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 ($list as $o):
                $amt = array_sum(array_map(fn($i) => (float)($i['amount'] ?? 0), $o['_items'] ?? []));
                $s = $obStatus[$o['status']] ?? $obStatus['delivered'];
            ?>
                <tr>
                    <td><?php echo e($o['order_no']); ?></td>
                    <td><?php echo $o['so_no'] ? e($o['so_no']) : '<span class="muted">—</span>'; ?></td>
                    <td><?php echo e($o['customer']); ?></td>
                    <td><?php echo e($o['salesman']); ?></td>
                    <td class="muted"><?php echo e($o['warehouse']); ?></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/outbounds/show/' . $o['id']); ?>">详情</a>
                        <a href="<?php echo site_url('PSI/outbounds/print/' . $o['id']); ?>" target="_blank">打印</a>
                        <a href="<?php echo site_url('PSI/outbounds/edit/' . $o['id']); ?>">编辑</a>
                        <form method="post" class="inline-form" action="<?php echo site_url('PSI/outbounds/destroy/' . $o['id']); ?>" data-confirm="确认删除该出库单？将回冲库存与已交付量。">
                            <?php echo csrf_field(); ?><button class="link-danger" type="submit">删除</button>
                        </form>
                    </td>
                </tr>
            <?php endforeach; ?>
            <?php if (empty($list)): ?><tr><td colspan="9" class="muted" style="text-align:center;padding:24px">暂无出库单</td></tr><?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
