<?php
$opportunity = $opportunity ?? [];
$companies   = $companies ?? [];
$stages      = $stages ?? [];
$competitors = $competitors ?? [];
?>
<div class="enterprise-settings-fields">
    <div class="enterprise-field">
        <label class="enterprise-field-label">Opportunity Name <span class="text-danger">*</span></label>
        <input type="text" name="name" class="form-control" value="<?= esc(old('name', $opportunity['name'] ?? '')) ?>" required>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Company</label>
        <select name="company_id" class="form-select">
            <option value="">— None —</option>
            <?php foreach ($companies as $c): ?>
                <option value="<?= $c['id'] ?>" <?= (string) old('company_id', $opportunity['company_id'] ?? '') === (string) $c['id'] ? 'selected' : '' ?>>
                    <?= esc($c['name']) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Sales Stage</label>
        <select name="sales_stage_id" class="form-select">
            <option value="">— None —</option>
            <?php foreach ($stages as $st): ?>
                <option value="<?= $st['id'] ?>"
                        data-probability="<?= (int) $st['default_probability'] ?>"
                        <?= (string) old('sales_stage_id', $opportunity['sales_stage_id'] ?? '') === (string) $st['id'] ? 'selected' : '' ?>>
                    <?= esc($st['name']) ?> (<?= (int) $st['default_probability'] ?>%)
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Competitor</label>
        <select name="competitor_id" class="form-select">
            <option value="">— None —</option>
            <?php foreach ($competitors as $comp): ?>
                <option value="<?= $comp['id'] ?>" <?= (string) old('competitor_id', $opportunity['competitor_id'] ?? '') === (string) $comp['id'] ? 'selected' : '' ?>>
                    <?= esc($comp['name']) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Expected Revenue</label>
        <input type="number" name="expected_revenue" class="form-control" step="0.01" min="0"
               value="<?= esc(old('expected_revenue', $opportunity['expected_revenue'] ?? '0')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Probability (%)</label>
        <input type="number" name="probability" class="form-control" min="0" max="100"
               value="<?= esc(old('probability', $opportunity['probability'] ?? '')) ?>"
               placeholder="Defaults from stage if empty">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Expected Close Date</label>
        <input type="date" name="expected_close_date" class="form-control"
               value="<?= esc(old('expected_close_date', $opportunity['expected_close_date'] ?? '')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Status</label>
        <select name="status" class="form-select">
            <?php foreach (['open', 'won', 'lost', 'abandoned'] as $s): ?>
                <option value="<?= $s ?>" <?= old('status', $opportunity['status'] ?? 'open') === $s ? 'selected' : '' ?>><?= ucfirst($s) ?></option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field" style="grid-column: 1 / -1;">
        <label class="enterprise-field-label">Description</label>
        <textarea name="description" class="form-control" rows="3"><?= esc(old('description', $opportunity['description'] ?? '')) ?></textarea>
    </div>
</div>
