<?php
/** PSI 采购订单详情 */
/** @var array $o @var array $items */
$poStatus = [
    'pending'  => ['label' => '待处理', 'cls' => 'tag-gray'],
    'partial'  => ['label' => '部分收货', 'cls' => 'tag-amber'],
    'received' => ['label' => '已收货', 'cls' => 'tag-green'],
    'closed'   => ['label' => '已关闭', 'cls' => 'tag-gray'],
];
$s = $poStatus[$o['status']] ?? $poStatus['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/purchase_orders/print/' . $o['id']); ?>" target="_blank"><?php echo admin_icon('printer', 16); ?> 打印</a>
        <a class="btn-ghost" href="<?php echo site_url('PSI/purchase_orders'); ?>">← 返回</a>
    </div>
</div>

<div class="panel two-col">
    <table class="tbl">
        <tr><th>供应商</th><td><?php echo e($o['supplier_name']); ?></td></tr>
        <tr><th>采购员</th><td><?php echo e($o['salesman']); ?></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['expected_at'] ?? '—'); ?></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): ?>
                <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; ?>
            </tbody>
        </table>
    </div>
    <div style="margin-top:16px;display:flex;gap:10px">
        <?php if ($o['status'] !== 'received'): ?>
            <a class="btn-primary" href="<?php echo site_url('PSI/purchase_orders/edit/' . $o['id']); ?>">编辑</a>
            <form method="post" action="<?php echo site_url('PSI/purchase_orders/receive/' . $o['id']); ?>" data-confirm="确认收货入库？将增加对应成品库存。">
                <?php echo csrf_field(); ?><button class="btn-ok" type="submit">收货入库</button>
            </form>
        <?php endif; ?>
    </div>
</div>
