<?php
/** PSI 采购订单列表 */
/** @var array $orders */
$poStatus = [
    'pending'  => ['label' => '待处理', 'cls' => 'tag-gray'],
    'partial'  => ['label' => '部分收货', 'cls' => 'tag-amber'],
    'received' => ['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/purchase_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>
            </tr></thead>
            <tbody>
            <?php foreach ($orders as $o):
                $amt = array_sum(array_map(fn($i) => (float)($i['amount'] ?? 0), $o['_items'] ?? []));
                $s = $poStatus[$o['status']] ?? $poStatus['pending'];
            ?>
                <tr>
                    <td><?php echo e($o['order_no']); ?></td>
                    <td><?php echo e($o['supplier_name']); ?></td>
                    <td><?php echo e($o['salesman']); ?></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['expected_at'] ?? '—'); ?></td>
                    <td class="row-actions">
                        <a href="<?php echo site_url('PSI/purchase_orders/show/' . $o['id']); ?>">详情</a>
                        <a href="<?php echo site_url('PSI/purchase_orders/print/' . $o['id']); ?>" target="_blank">打印</a>
                        <?php if ($o['status'] !== 'received'): ?>
                        <a href="<?php echo site_url('PSI/purchase_orders/edit/' . $o['id']); ?>">编辑</a>
                        <form method="post" class="inline-form" action="<?php echo site_url('PSI/purchase_orders/receive/' . $o['id']); ?>" data-confirm="确认收货入库？将增加对应成品库存。">
                            <?php echo csrf_field(); ?><button class="link-ok" type="submit">收货</button>
                        </form>
                        <?php endif; ?>
                        <form method="post" class="inline-form" action="<?php echo site_url('PSI/purchase_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="8" class="muted" style="text-align:center;padding:24px">暂无采购订单</td></tr><?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
