<?php
/** CRM 仪表盘 */
$stageLabel = ['new' => '新线索', 'contact' => '已联系', 'quote' => '报价中', 'won' => '已成交', 'lost' => '已流失'];
$typeLabel  = ['brand' => '品牌方', 'dealer1' => '一级经销商', 'dealer2' => '二级经销商', 'retail' => '直营门店', 'ecom' => '电商', 'export' => '外贸出口', 'oem' => '生产代工'];
$statusLabel = [
    'lead' => '潜在客户', 'intent' => '意向客户', 'quote' => '报价中',
    'cooperating' => '合作中', 'won' => '已成交', 'lost' => '已流失', 'dormant' => '休眠',
];
$statusClass = [
    'lead' => 'st-grey', 'intent' => 'st-blue', 'quote' => 'st-purple',
    'cooperating' => 'st-green', 'won' => 'st-gold', 'lost' => 'st-red', 'dormant' => 'st-grey',
];
$recentCustomers = $recentCustomers ?? [];
?>
<div class="page-head">
    <h1>CRM 仪表盘</h1>
    <p class="sub">客户需求与客户关系总览</p>
</div>

<div class="stat-grid">
    <div class="stat-card"><div class="stat-num"><?php echo e($customerTotal); ?></div><div class="stat-label">客户总数</div></div>
    <div class="stat-card"><div class="stat-num"><?php echo e($leadTotal); ?></div><div class="stat-label">商机线索</div></div>
    <div class="stat-card"><div class="stat-num"><?php echo e($followTotal); ?></div><div class="stat-label">跟进记录</div></div>
    <div class="stat-card"><div class="stat-num">¥<?php echo e(number_format($amountTotal, 2)); ?></div><div class="stat-label">商机金额合计</div></div>
</div>

<div class="quick-actions">
    <a class="qa-card" href="<?php echo site_url('CRM/customers/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('CRM/contacts'); ?>">
        <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('CRM/customers'); ?>">
        <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('CRM/leads/create'); ?>">
        <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($stageCount)): ?><p class="muted">暂无商机数据</p><?php else: ?>
        <table class="tbl">
            <thead><tr><th>阶段</th><th>数量</th></tr></thead>
            <tbody>
            <?php foreach ($stageCount as $k => $v): ?>
                <tr><td><?php echo e($stageLabel[$k] ?? $k); ?></td><td><?php echo e($v); ?></td></tr>
            <?php endforeach; ?>
            </tbody>
        </table>
        <?php endif; ?>
    </div>
    <div class="panel">
        <h3>客户类型分布</h3>
        <?php if (empty($typeCount)): ?><p class="muted">暂无客户数据</p><?php else: ?>
        <table class="tbl">
            <thead><tr><th>类型</th><th>数量</th></tr></thead>
            <tbody>
            <?php foreach ($typeCount as $k => $v): ?>
                <tr><td><?php echo e($typeLabel[$k] ?? $k); ?></td><td><?php echo e($v); ?></td></tr>
            <?php endforeach; ?>
            </tbody>
        </table>
        <?php endif; ?>
    </div>
</div>

<div class="panel">
    <h3>最近客户</h3>
    <?php if (empty($recentCustomers)): ?><p class="muted">暂无客户，点击上方「录入新客户」开始</p><?php else: ?>
    <table class="tbl">
        <thead><tr><th>ID</th><th>客户名称</th><th>类型</th><th>状态</th><th>负责人</th><th>操作</th></tr></thead>
        <tbody>
        <?php foreach ($recentCustomers as $c): ?>
            <tr>
                <td><?php echo e($c['id']); ?></td>
                <td><?php echo e($c['name']); ?></td>
                <td><?php echo e($typeLabel[$c['type'] ?? ''] ?? $c['type'] ?? ''); ?></td>
                <td><span class="badge <?php echo e($statusClass[$c['status'] ?? 'lead'] ?? 'st-grey'); ?>"><?php echo e($statusLabel[$c['status'] ?? 'lead'] ?? '潜在客户'); ?></span></td>
                <td><?php echo e($c['owner'] ?? ''); ?></td>
                <td class="row-actions">
                    <a href="<?php echo site_url('CRM/customers/edit/' . $c['id']); ?>">编辑</a>
                    <a href="<?php echo site_url('CRM/contacts?customer_id=' . $c['id']); ?>">联系人</a>
                </td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
    <?php endif; ?>
</div>

<div class="panel">
    <h3>最近跟进</h3>
    <?php if (empty($recent)): ?><p class="muted">暂无跟进记录</p><?php else: ?>
    <table class="tbl">
        <thead><tr><th>时间</th><th>客户ID</th><th>内容</th><th>下次跟进</th></tr></thead>
        <tbody>
        <?php foreach ($recent as $r): ?>
            <tr>
                <td><?php echo e($r['created_at'] ?? ''); ?></td>
                <td>#<?php echo e($r['customer_id'] ?? ''); ?></td>
                <td><?php echo e(mb_substr($r['content'] ?? '', 0, 40)); ?></td>
                <td><?php echo e($r['next_at'] ?? ''); ?></td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
    <?php endif; ?>
</div>
