<?php $this->start('body');
    $lead      = $this->data['lead'] ?? null;
    $customers = $this->data['customers'] ?? [];
    $stages    = $this->data['stages'] ?? [];
    $isEdit    = $lead !== null;
    $action    = $isEdit ? PROOT . 'BusinessLeads/edit/' . (int)$lead['c0d'] : PROOT . 'BusinessLeads/add';
?>

<div class="d-flex align-items-center justify-content-between mb-3">
    <h5 class="mb-0" style="font-weight:600;color:#334155;"><i class="fas fa-<?= $isEdit ? 'edit' : 'plus-circle'; ?> me-2" style="color:#7A8BA1;"></i><?= $isEdit ? 'Edit lead' : 'Add lead'; ?></h5>
    <a href="<?= PROOT; ?>BusinessLeads<?= $isEdit ? '/view/' . (int)$lead['c0d'] : ''; ?>" class="btn btn-sm" style="background:#F8FAFC;border:1px solid #E2E8F0;color:#334155;"><i class="fas fa-arrow-left me-1"></i>Back</a>
</div>

<form method="post" action="<?= $action; ?>" class="card border-0 shadow-sm" style="background:#F8FAFC;">
    <div class="card-body p-3">
        <div class="row g-2">
            <div class="col-md-6">
                <label class="form-label" style="color:#334155;">Lead name *</label>
                <input type="text" name="lead_name" required value="<?= htmlspecialchars((string)($lead['lead_name'] ?? '')); ?>" class="form-control form-control-sm">
            </div>
            <div class="col-md-3">
                <label class="form-label" style="color:#334155;">Project type *</label>
                <select name="project_type" class="form-select form-select-sm">
                    <?php foreach (['goods','services','works'] as $t): ?>
                        <option value="<?= $t; ?>" <?= (($lead['project_type'] ?? 'goods') === $t) ? 'selected' : ''; ?>><?= ucfirst($t); ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-3">
                <label class="form-label" style="color:#334155;">Source</label>
                <select name="lead_source" class="form-select form-select-sm">
                    <?php foreach (['DIRECT','REFERRAL','INBOUND','TENDER_BDM','NEST'] as $s): ?>
                        <option value="<?= $s; ?>" <?= (($lead['lead_source'] ?? 'DIRECT') === $s) ? 'selected' : ''; ?>><?= $s; ?></option>
                    <?php endforeach; ?>
                </select>
            </div>

            <div class="col-md-6">
                <label class="form-label" style="color:#334155;">Existing customer</label>
                <select name="customer_id" id="customer_id" class="form-select form-select-sm">
                    <option value="0">— none / new prospect —</option>
                    <?php foreach ($customers as $c): $n = trim(($c['fname'] ?? '') . ' ' . ($c['lname'] ?? '')) . (!empty($c['customer_no']) ? ' [' . $c['customer_no'] . ']' : ''); ?>
                        <option value="<?= (int)$c['c0d']; ?>" data-name="<?= htmlspecialchars($n); ?>" <?= (int)($lead['customer_id'] ?? 0) === (int)$c['c0d'] ? 'selected' : ''; ?>><?= htmlspecialchars($n); ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-6">
                <label class="form-label" style="color:#334155;">Prospect name (or company)</label>
                <input type="text" name="customer_name" id="customer_name" value="<?= htmlspecialchars((string)($lead['customer_name'] ?? '')); ?>" class="form-control form-control-sm">
            </div>

            <div class="col-md-3">
                <label class="form-label" style="color:#334155;">Opportunity value (TZS)</label>
                <input type="number" step="0.01" name="opportunity_value" value="<?= (float)($lead['opportunity_value'] ?? 0); ?>" class="form-control form-control-sm">
            </div>
            <div class="col-md-3">
                <label class="form-label" style="color:#334155;">Stage</label>
                <select name="status" class="form-select form-select-sm">
                    <?php foreach ($stages as $s): ?>
                        <option value="<?= $s; ?>" <?= (($lead['status'] ?? 'NEW') === $s) ? 'selected' : ''; ?>><?= $s; ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-3">
                <label class="form-label" style="color:#334155;">Next action date</label>
                <input type="date" name="next_action_date" value="<?= htmlspecialchars((string)($lead['next_action_date'] ?? '')); ?>" class="form-control form-control-sm">
            </div>
            <div class="col-md-3">
                <label class="form-label" style="color:#334155;">Next action note</label>
                <input type="text" name="next_action_note" value="<?= htmlspecialchars((string)($lead['next_action_note'] ?? '')); ?>" class="form-control form-control-sm">
            </div>
        </div>
    </div>
    <div class="card-footer bg-white d-flex justify-content-end gap-2" style="border-top:1px solid #E2E8F0;">
        <a href="<?= PROOT; ?>BusinessLeads" class="btn btn-sm" style="background:#F8FAFC;border:1px solid #E2E8F0;color:#334155;">Cancel</a>
        <button type="submit" class="btn btn-sm" style="background:#7A8BA1;border-color:#7A8BA1;color:#fff;font-weight:600;"><i class="fas fa-save me-1"></i><?= $isEdit ? 'Save changes' : 'Create lead'; ?></button>
    </div>
</form>

<?php $this->end(); ?>

<?php $this->start('section_js'); ?>
<script>
$('#customer_id').on('change', function () {
    var $o = $(this).find('option:selected');
    var name = $o.data('name');
    if (parseInt($o.val(), 10) > 0 && name) $('#customer_name').val(name);
});
</script>
<?php $this->end(); ?>
