<?php
$rule = $rule ?? [];
$criteria = [
    'has_email'       => 'Has email address',
    'has_phone'       => 'Has phone number',
    'has_company'     => 'Has company name',
    'source'          => 'Lead source matches',
    'estimated_value' => 'Estimated value',
    'priority'        => 'Priority matches',
];
$operators = [
    'is_set'       => 'Is set / populated',
    'equals'       => 'Equals',
    'not_equals'   => 'Does not equal',
    'greater_than' => 'Greater than',
    'less_than'    => 'Less than',
];
?>
<div class="enterprise-settings-fields enterprise-settings-fields--single">
    <div class="enterprise-field">
        <label class="enterprise-field-label">Rule Name <span class="text-danger">*</span></label>
        <input type="text" name="name" class="form-control" value="<?= esc(old('name', $rule['name'] ?? '')) ?>" required>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Criterion <span class="text-danger">*</span></label>
        <select name="criterion" class="form-select" required>
            <?php foreach ($criteria as $key => $label): ?>
                <option value="<?= $key ?>" <?= old('criterion', $rule['criterion'] ?? '') === $key ? 'selected' : '' ?>><?= esc($label) ?></option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Operator <span class="text-danger">*</span></label>
        <select name="operator" class="form-select" required>
            <?php foreach ($operators as $key => $label): ?>
                <option value="<?= $key ?>" <?= old('operator', $rule['operator'] ?? 'is_set') === $key ? 'selected' : '' ?>><?= esc($label) ?></option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Value</label>
        <input type="text" name="value" class="form-control" value="<?= esc(old('value', $rule['value'] ?? '')) ?>"
               placeholder="Source ID, priority (low/medium/high), or numeric threshold">
        <div class="form-text">Required for equals, not equals, greater than, and less than operators.</div>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Points <span class="text-danger">*</span></label>
        <input type="number" name="points" class="form-control" value="<?= esc(old('points', $rule['points'] ?? 0)) ?>" required>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Status</label>
        <select name="status" class="form-select">
            <?php foreach (['active', 'inactive'] as $s): ?>
                <option value="<?= $s ?>" <?= old('status', $rule['status'] ?? 'active') === $s ? 'selected' : '' ?>><?= ucfirst($s) ?></option>
            <?php endforeach; ?>
        </select>
    </div>
</div>
