<?php
/** CRM 客户联系人表单（新增/编辑共用） */
$ct = $contact;
$isEdit = !empty($ct);
$customers = $customers ?? [];
$preId = $preId ?? 0;
$curCustomer = $ct['customer_id'] ?? $preId;
?>
<div class="page-head">
    <h1><?php echo $isEdit ? '编辑联系人' : '新增联系人'; ?></h1>
</div>
<div class="panel">
    <form method="post" action="<?php echo site_url($isEdit ? 'CRM/contacts/update/' . $ct['id'] : 'CRM/contacts/store'); ?>">
        <?php echo csrf_field(); ?>
        <div class="form-grid">
            <div class="field"><label>所属客户 *</label>
                <select name="customer_id" required>
                    <option value="">— 请选择客户 —</option>
                    <?php foreach ($customers as $cu): ?>
                        <option value="<?php echo $cu['id']; ?>" <?php echo (int)($cu['id']) === (int)$curCustomer ? 'selected' : ''; ?>><?php echo e($cu['name']); ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="field"><label>姓名 *</label><input name="name" value="<?php echo e($ct['name'] ?? ''); ?>" required></div>
            <div class="field"><label>职位</label><input name="title" value="<?php echo e($ct['title'] ?? ''); ?>" placeholder="如：采购总监"></div>
            <div class="field"><label>电话</label><input name="phone" value="<?php echo e($ct['phone'] ?? ''); ?>"></div>
            <div class="field"><label>邮箱</label><input name="email" value="<?php echo e($ct['email'] ?? ''); ?>"></div>
            <div class="field"><label>微信</label><input name="wechat" value="<?php echo e($ct['wechat'] ?? ''); ?>"></div>
            <div class="field field-check">
                <label><input type="checkbox" name="is_primary" value="1" <?php echo !empty($ct['is_primary']) ? 'checked' : ''; ?>> 设为主联系人</label>
            </div>
        </div>
        <div class="field"><label>备注</label><textarea name="remark" rows="3"><?php echo e($ct['remark'] ?? ''); ?></textarea></div>
        <button class="btn-primary" type="submit">保存</button>
        <a class="btn-soft" href="<?php echo site_url($curCustomer ? 'CRM/contacts?customer_id=' . $curCustomer : 'CRM/contacts'); ?>">取消</a>
    </form>
</div>
