<?php
/** PSI 交付明细报表：出库单 + 销售订单交付进度 */
/** @var array $list @var array $progress */
$soStatus = ['pending'=>'待处理','partial'=>'部分交付','delivered'=>'已交付','closed'=>'已关闭'];
?>
<div class="page-head">
    <div><h1>交付明细</h1><div class="desc">出库（交付）记录与销售订单交付进度、未交订单</div></div>
    <a class="btn-ghost" href="<?php echo site_url('PSI/reports'); ?>">← 报表中心</a>
</div>

<div class="panel">
    <div class="panel-bar"><h3>销售订单交付进度</h3><span class="muted">应发 / 已发 / 未发</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 ($progress as $p): ?>
                <tr>
                    <td><?php echo e($p['order_no']); ?></td>
                    <td><?php echo e($p['customer']); ?></td>
                    <td><?php echo e($p['salesman']); ?></td>
                    <td><span class="tag-mini <?php echo $p['status']==='delivered'?'tag-green':($p['status']==='partial'?'tag-amber':'tag-gray'); ?>"><?php echo $soStatus[$p['status']] ?? '待处理'; ?></span></td>
                    <td><?php echo $p['ordered']; ?></td>
                    <td><?php echo $p['delivered']; ?></td>
                    <td><?php echo $p['remain']; ?></td>
                    <td>¥<?php echo e(number_format($p['amount'], 2)); ?></td>
                </tr>
            <?php endforeach; ?>
            <?php if (empty($progress)): ?><tr><td colspan="8" class="muted" style="text-align:center;padding:16px">暂无销售订单交付数据</td></tr><?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<div class="panel">
    <div class="panel-bar"><h3>出库（交付）记录</h3><span class="muted">共 <?php echo count($list); ?> 单</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></tr></thead>
            <tbody>
            <?php foreach ($list as $o):
                $amt = array_sum(array_map(fn($i) => (float)($i['amount'] ?? 0), $o['_items'] ?? []));
            ?>
                <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>¥<?php echo e(number_format($amt, 2)); ?></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></td>
                </tr>
            <?php endforeach; ?>
            <?php if (empty($list)): ?><tr><td colspan="7" class="muted" style="text-align:center;padding:16px">暂无出库记录</td></tr><?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
