<?php
/** CRM 客户联系人列表 */
$customer = $customer ?? null;
$title = $customer ? ('『' . e($customer['name']) . '』的联系人') : '全部客户联系人';
$addUrl = $customer ? site_url('CRM/contacts/create?customer_id=' . $customer['id']) : site_url('CRM/contacts/create');
?>
<div class="page-head">
    <h1><?php echo e($title); ?></h1>
    <a class="btn-primary" href="<?php echo $addUrl; ?>">+ 新增联系人</a>
</div>

<?php if ($customer): ?>
    <p class="sub"><a href="<?php echo site_url('CRM/customers'); ?>">← 返回客户列表</a></p>
<?php endif; ?>

<div class="panel">
    <table class="tbl">
        <thead><tr>
            <th>ID</th><?php if (!$customer): ?><th>客户</th><?php endif; ?>
            <th>姓名</th><th>职位</th><th>电话</th><th>邮箱</th><th>微信</th><th>主联系人</th><th>操作</th>
        </tr></thead>
        <tbody>
        <?php foreach ($contacts as $ct): ?>
            <tr>
                <td><?php echo e($ct['id']); ?></td>
                <?php if (!$customer): ?><td><?php echo e($ct['customer_name'] ?? '—'); ?></td><?php endif; ?>
                <td><?php echo e($ct['name']); ?></td>
                <td><?php echo e($ct['title'] ?? ''); ?></td>
                <td><?php echo e($ct['phone'] ?? ''); ?></td>
                <td><?php echo e($ct['email'] ?? ''); ?></td>
                <td><?php echo e($ct['wechat'] ?? ''); ?></td>
                <td><?php echo !empty($ct['is_primary']) ? '★ 是' : ''; ?></td>
                <td class="row-actions">
                    <a href="<?php echo site_url('CRM/contacts/edit/' . $ct['id']); ?>">编辑</a>
                    <a href="<?php echo site_url('CRM/contacts/destroy/' . $ct['id']); ?>" data-confirm="确认删除该联系人？">删除</a>
                </td>
            </tr>
        <?php endforeach; ?>
        <?php if (empty($contacts)): ?><tr><td colspan="<?php echo $customer ? 8 : 9; ?>" class="muted">暂无联系人，点击右上角新增</td></tr><?php endif; ?>
        </tbody>
    </table>
</div>
