<?php
/** PSI 进销存仪表盘 */
?>
<div class="page-head">
    <h1>进销存仪表盘</h1>
    <p class="sub">服装生产 · 进出口贸易 · 终端销售 一体化库存</p>
</div>

<div class="stat-grid">
    <div class="stat-card"><div class="stat-num"><?php echo e($materialTotal); ?></div><div class="stat-label">物料种类</div></div>
    <div class="stat-card"><div class="stat-num"><?php echo e($productTotal); ?></div><div class="stat-label">成品种类</div></div>
    <div class="stat-card"><div class="stat-num"><?php echo e(number_format($matStock, 0)); ?></div><div class="stat-label">物料库存量</div></div>
    <div class="stat-card"><div class="stat-num"><?php echo e(number_format($prodStock, 0)); ?></div><div class="stat-label">成品库存量</div></div>
    <div class="stat-card"><div class="stat-num">¥<?php echo e(number_format($purchaseAmt, 0)); ?></div><div class="stat-label">采购总额</div></div>
    <div class="stat-card"><div class="stat-num">¥<?php echo e(number_format($salesAmt, 0)); ?></div><div class="stat-label">销售总额</div></div>
</div>

<!-- 订单业务看板 -->
<div class="stat-grid">
    <a class="stat-card" href="<?php echo site_url('PSI/reports/soPo?type=sales'); ?>" style="text-decoration:none">
        <div class="stat-ic"><?php echo admin_icon('trending-up', 20); ?></div>
        <div class="stat-num">¥<?php echo e(number_format($salesAmount, 0)); ?></div><div class="stat-label">订单销售额（累计）</div>
    </a>
    <a class="stat-card" href="<?php echo site_url('PSI/reports/soPo?type=sales'); ?>" style="text-decoration:none">
        <div class="stat-ic"><?php echo admin_icon('trending-up', 20); ?></div>
        <div class="stat-num">¥<?php echo e(number_format($salesAmountMonth, 0)); ?></div><div class="stat-label">当月订单销售额</div>
    </a>
    <a class="stat-card" href="<?php echo site_url('PSI/reports/delivery'); ?>" style="text-decoration:none">
        <div class="stat-ic" style="color:#f59e0b"><?php echo admin_icon('alert', 20); ?></div>
        <div class="stat-num" style="color:#f59e0b"><?php echo e($undelivered['count']); ?> <small>单 / ¥<?php echo e(number_format($undelivered['amount'], 0)); ?></small></div>
        <div class="stat-label">未交订单</div>
    </a>
    <a class="stat-card" href="<?php echo site_url('PSI/reports/delivery'); ?>" style="text-decoration:none">
        <div class="stat-ic" style="color:#10b981"><?php echo admin_icon('check', 20); ?></div>
        <div class="stat-num" style="color:#10b981"><?php echo e($monthDeliveries['count']); ?> <small>单 / ¥<?php echo e(number_format($monthDeliveries['amount'], 0)); ?></small></div>
        <div class="stat-label">当月交付订单</div>
    </a>
</div>

<div class="panel">
    <div class="panel-bar"><h3>按业务员（订单销售额）</h3>
        <a class="btn-sm btn-ghost" href="<?php echo site_url('PSI/reports/soPo?type=sales'); ?>">查看明细</a></div>
    <?php if (empty($bySalesman)): ?><p class="muted">暂无销售订单数据</p><?php else: ?>
    <div class="table-wrap">
        <table class="tbl">
            <thead><tr><th>业务员</th><th>订单数</th><th>销售额</th></tr></thead>
            <tbody>
            <?php foreach ($bySalesman as $r): ?>
                <tr><td><?php echo e($r['salesman']); ?></td><td><?php echo e($r['orders']); ?></td>
                    <td>¥<?php echo e(number_format((float)($r['amount'] ?? 0), 2)); ?></td></tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php endif; ?>
</div>

<div class="quick-actions">
    <a class="qa-card" href="<?php echo site_url('PSI/materials/create'); ?>">
        <span class="qa-ico">🧵</span><span class="qa-t">录入物料</span><span class="qa-d">登记面料/辅料/纱线</span>
    </a>
    <a class="qa-card" href="<?php echo site_url('PSI/products/create'); ?>">
        <span class="qa-ico">👕</span><span class="qa-t">录入成品</span><span class="qa-d">款号/颜色/尺码</span>
    </a>
    <a class="qa-card" href="<?php echo site_url('PSI/purchases/create'); ?>">
        <span class="qa-ico">📥</span><span class="qa-t">采购入库</span><span class="qa-d">生成入库单与流水</span>
    </a>
    <a class="qa-card" href="<?php echo site_url('PSI/sales/create'); ?>">
        <span class="qa-ico">📤</span><span class="qa-t">销售出库</span><span class="qa-d">生成出库单与流水</span>
    </a>
    <a class="qa-card" href="<?php echo site_url('PSI/sales_orders/create'); ?>">
        <span class="qa-ico">📝</span><span class="qa-t">销售订单</span><span class="qa-d">录入并可打印</span>
    </a>
    <a class="qa-card" href="<?php echo site_url('PSI/purchase_orders/create'); ?>">
        <span class="qa-ico">📋</span><span class="qa-t">采购订单</span><span class="qa-d">录入并收货入库</span>
    </a>
    <a class="qa-card" href="<?php echo site_url('PSI/outbounds/create'); ?>">
        <span class="qa-ico">🚚</span><span class="qa-t">出库单</span><span class="qa-d">交付并扣库存</span>
    </a>
    <a class="qa-card" href="<?php echo site_url('PSI/reports'); ?>">
        <span class="qa-ico">📊</span><span class="qa-t">报表中心</span><span class="qa-d">订单/交付明细</span>
    </a>
</div>

<div class="two-col">
    <div class="panel">
        <h3>最近采购入库</h3>
        <?php if (empty($recentPurchases)): ?><p class="muted">暂无采购单</p><?php else: ?>
        <table class="tbl">
            <thead><tr><th>单号</th><th>物料/成品</th><th>数量</th><th>金额</th><th>日期</th></tr></thead>
            <tbody>
            <?php foreach ($recentPurchases as $p): ?>
                <tr>
                    <td><?php echo e($p['no'] ?? $p['id'] ?? ''); ?></td>
                    <td><?php echo e($p['name'] ?? ''); ?></td>
                    <td><?php echo e((float)($p['qty'] ?? 0)); ?></td>
                    <td>¥<?php echo e(number_format((float)($p['amount'] ?? 0), 2)); ?></td>
                    <td><?php echo e($p['created_at'] ?? ''); ?></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
        <?php endif; ?>
    </div>
    <div class="panel">
        <h3>最近销售出库</h3>
        <?php if (empty($recentSales)): ?><p class="muted">暂无销售单</p><?php else: ?>
        <table class="tbl">
            <thead><tr><th>单号</th><th>物料/成品</th><th>数量</th><th>金额</th><th>日期</th></tr></thead>
            <tbody>
            <?php foreach ($recentSales as $s): ?>
                <tr>
                    <td><?php echo e($s['no'] ?? $s['id'] ?? ''); ?></td>
                    <td><?php echo e($s['name'] ?? ''); ?></td>
                    <td><?php echo e((float)($s['qty'] ?? 0)); ?></td>
                    <td>¥<?php echo e(number_format((float)($s['amount'] ?? 0), 2)); ?></td>
                    <td><?php echo e($s['created_at'] ?? ''); ?></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
        <?php endif; ?>
    </div>
</div>

<div class="panel">
    <h3>库存预警（低于 20）</h3>
    <?php if (empty($lowStock)): ?>
        <p class="muted">当前库存充足 ✅</p>
    <?php else: ?>
        <table class="tbl">
            <thead><tr><th>名称</th><th>类型</th><th>当前库存</th></tr></thead>
            <tbody>
            <?php foreach ($lowStock as $x): ?>
                <tr>
                    <td><?php echo e($x['item']['name']); ?></td>
                    <td><?php echo e($x['type']); ?></td>
                    <td style="color:#dc2626;font-weight:700"><?php echo e((float)($x['item']['stock'] ?? 0)); ?></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    <?php endif; ?>
</div>
