<?php
$contact = $contact ?? [];
$prefillCompanyId = $prefillCompanyId ?? null;
?>
<div class="enterprise-settings-fields">
    <div class="enterprise-field">
        <label class="enterprise-field-label">Company <span class="text-danger">*</span></label>
        <select name="company_id" class="form-select" required>
            <option value="">— Select —</option>
            <?php foreach ($companies as $c): ?>
                <option value="<?= $c['id'] ?>" <?= (string) old('company_id', $contact['company_id'] ?? $prefillCompanyId) === (string) $c['id'] ? 'selected' : '' ?>>
                    <?= esc($c['name']) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">First Name <span class="text-danger">*</span></label>
        <input type="text" name="first_name" class="form-control" value="<?= esc(old('first_name', $contact['first_name'] ?? '')) ?>" required>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Last Name <span class="text-danger">*</span></label>
        <input type="text" name="last_name" class="form-control" value="<?= esc(old('last_name', $contact['last_name'] ?? '')) ?>" required>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Job Title</label>
        <input type="text" name="job_title" class="form-control" value="<?= esc(old('job_title', $contact['job_title'] ?? '')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Email</label>
        <input type="email" name="email" class="form-control" value="<?= esc(old('email', $contact['email'] ?? '')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Phone</label>
        <input type="text" name="phone" class="form-control" value="<?= esc(old('phone', $contact['phone'] ?? '')) ?>">
    </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', $contact['status'] ?? 'active') === $s ? 'selected' : '' ?>><?= ucfirst($s) ?></option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label d-block">Primary Contact</label>
        <div class="form-check mt-2">
            <input type="checkbox" name="is_primary" value="1" class="form-check-input" id="is_primary"
                   <?= old('is_primary', $contact['is_primary'] ?? '') ? 'checked' : '' ?>>
            <label class="form-check-label" for="is_primary">Mark as primary for this company</label>
        </div>
    </div>
    <div class="enterprise-field" style="grid-column: 1 / -1;">
        <label class="enterprise-field-label">Notes</label>
        <textarea name="notes" class="form-control" rows="3"><?= esc(old('notes', $contact['notes'] ?? '')) ?></textarea>
    </div>
</div>
