<?php
/** CRM 商机表单 */
$l = $lead;
$isEdit = !empty($l);
$stageLabel = ['new' => '新线索', 'contact' => '已联系', 'quote' => '报价中', 'won' => '已成交', 'lost' => '已流失'];
$srcOpts = ['expo' => '展会', 'referral' => '转介绍', 'online' => '线上', 'web' => '官网', 'tel' => '电话', 'other' => '其他'];
$curSrc = $l['source'] ?? 'expo';
?>
<div class="page-head"><h1><?php echo $isEdit ? '编辑商机' : '新增商机'; ?></h1></div>
<div class="panel">
    <form method="post" action="<?php echo site_url($isEdit ? 'CRM/leads/update/' . $l['id'] : 'CRM/leads/store'); ?>">
        <?php echo csrf_field(); ?>
        <div class="form-grid">
            <div class="field"><label>关联客户 *</label>
                <select name="customer_id">
                    <option value="0">未关联</option>
                    <?php foreach ($customers as $c): ?>
                        <option value="<?php echo e($c['id']); ?>" <?php echo ($l['customer_id'] ?? 0) == $c['id'] ? 'selected' : ''; ?>><?php echo e($c['name']); ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="field"><label>商机标题 *</label><input name="title" value="<?php echo e($l['title'] ?? ''); ?>" required></div>
            <div class="field"><label>金额</label><input name="amount" type="number" step="0.01" value="<?php echo e($l['amount'] ?? '0'); ?>"></div>
            <div class="field"><label>阶段</label>
                <select name="stage">
                    <?php foreach ($stageLabel as $k => $v): ?>
                        <option value="<?php echo $k; ?>" <?php echo ($l['stage'] ?? 'new') === $k ? 'selected' : ''; ?>><?php echo $v; ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="field"><label>来源渠道</label>
                <select name="source">
                    <?php foreach ($srcOpts as $k => $v): ?>
                        <option value="<?php echo $k; ?>" <?php echo $curSrc === $k ? 'selected' : ''; ?>><?php echo $v; ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="field"><label>成交概率(%)</label><input name="probability" type="number" min="0" max="100" value="<?php echo e($l['probability'] ?? '0'); ?>"></div>
            <div class="field"><label>预计成交日期</label><input name="expected_close" type="date" value="<?php echo e($l['expected_close'] ?? ''); ?>"></div>
            <div class="field"><label>负责人</label><input name="owner" value="<?php echo e($l['owner'] ?? ''); ?>"></div>
        </div>
        <div class="field"><label>备注</label><textarea name="remark" rows="3"><?php echo e($l['remark'] ?? ''); ?></textarea></div>
        <button class="btn-primary" type="submit">保存</button>
        <a class="btn-soft" href="<?php echo site_url('CRM/leads'); ?>">取消</a>
    </form>
</div>
