<?php
/** PSI 采购订单明细报表 */
/** @var array $orders */
$poStatus = ['pending'=>'待处理','partial'=>'部分收货','received'=>'已收货','closed'=>'已关闭'];
?>
<div class="page-head">
    <div><h1>采购订单明细</h1><div class="desc">每张采购订单的物料/成品明细（共 <?php echo count($orders); ?> 张）</div></div>
    <a class="btn-ghost" href="<?php echo site_url('PSI/reports'); ?>">← 报表中心</a>
</div>

<?php foreach ($orders as $o):
    $amt = array_sum(array_map(fn($i) => (float)($i['amount'] ?? 0), $o['_items'] ?? []));
?>
<div class="panel">
    <div class="panel-bar">
        <h3><?php echo e($o['order_no']); ?> · <?php echo e($o['supplier_name']); ?>
            <span class="tag-mini tag-gray" style="margin-left:6px"><?php echo $poStatus[$o['status']] ?? '待处理'; ?></span></h3>
        <span class="muted">采购员 <?php echo e($o['salesman']); ?> · 合计 ¥<?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 (($o['_items'] ?? []) as $it): ?>
                <tr>
                    <td><?php echo $it['item_type'] === 'product' ? '成品' : '物料'; ?></td>
                    <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['received_qty'] ?? 0); ?></td>
                </tr>
            <?php endforeach; ?>
            <?php if (empty($o['_items'])): ?><tr><td colspan="8" class="muted" style="text-align:center;padding:16px">无明细</td></tr><?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endforeach; ?>
<?php if (empty($orders)): ?><div class="panel"><div class="muted" style="text-align:center;padding:24px">暂无采购订单</div></div><?php endif; ?>
