<form method="POST" action="<?php echo e($action); ?>" class="row g-2">
    <?php echo csrf_field(); ?>
    <?php if($method !== 'POST'): ?>
        <?php echo method_field($method); ?>
    <?php endif; ?>
    <div class="col-12">
        <label class="form-label">Trade fair <span class="text-danger">*</span></label>
        <select name="tradefair_id" id="tradefair_select" class="form-select <?php $__errorArgs = ['tradefair_id'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>" required>
            <option value="" disabled <?php echo e(old('tradefair_id', optional($lead)->tradefair_id) ? '' : 'selected'); ?>>Select trade fair…</option>
            <?php $__currentLoopData = $tradefairs; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $tf): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                <option value="<?php echo e($tf->id); ?>"
                    data-country="<?php echo e($tf->country); ?>"
                    data-venue="<?php echo e($tf->eventvenue); ?>"
                    <?php echo e((string) old('tradefair_id', optional($lead)->tradefair_id) === (string) $tf->id ? 'selected' : ''); ?>>
                    <?php echo e($tf->fair_name); ?>

                </option>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
            <option value="__create__">+ Create new trade fair…</option>
        </select>
        <?php $__errorArgs = ['tradefair_id'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
            <div class="invalid-feedback d-block"><?php echo e($message); ?></div>
        <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
        <div class="form-text">Venue and country below are taken from the trade fair record when you save.</div>
    </div>
    <div class="col-md-6">
        <label class="form-label">Venue (from trade fair)</label>
        <?php
            $lv = old('tradefair_venue_display');
            if ($lv === null && $lead) {
                $lv = optional($lead->tradefair)->eventvenue ?? $lead->city;
            }
        ?>
        <input type="text" class="form-control bg-light" id="tradefair_venue_display" readonly tabindex="-1" value="<?php echo e($lv ?? ''); ?>">
    </div>
    <div class="col-md-6">
        <label class="form-label">Country (from trade fair)</label>
        <?php
            $lc = old('tradefair_country_display');
            if ($lc === null && $lead) {
                $lc = optional(optional($lead->tradefair)->fairCountry)->name ?? $lead->country;
            }
        ?>
        <input type="text" class="form-control bg-light" id="tradefair_country_display" readonly tabindex="-1" value="<?php echo e($lc ?? ''); ?>">
    </div>
    <div class="col-md-6">
        <label class="form-label">Contact person name</label>
        <input name="contact_person_name" class="form-control" maxlength="255" value="<?php echo e(old('contact_person_name', optional($lead)->contact_person_name)); ?>" placeholder="Primary contact at client">
    </div>
    <div class="col-md-6">
        <label class="form-label">Source location</label>
        <input name="source_location" class="form-control" maxlength="255" placeholder="e.g. Website, Referral"
               value="<?php echo e(old('source_location', optional($lead)->source_location)); ?>">
    </div>
    <div class="col-md-6">
        <label class="form-label">Booth Area (sqm)</label>
        <input type="number" step="0.01" name="booth_area_sqm" class="form-control" value="<?php echo e(old('booth_area_sqm', optional($lead)->booth_area_sqm)); ?>">
    </div>
    <div class="col-md-6">
        <label class="form-label">Booth Area (sqft)</label>
        <input type="number" step="0.01" name="booth_area_sqft" class="form-control" value="<?php echo e(old('booth_area_sqft', optional($lead)->booth_area_sqft)); ?>">
    </div>
    <div class="col-md-6">
        <label class="form-label">Lead Budget</label>
        <input type="number" step="0.01" name="lead_budget" class="form-control" value="<?php echo e(old('lead_budget', optional($lead)->lead_budget)); ?>">
    </div>
    <div class="col-12">
        <label class="form-label">Company Name</label>
        <input name="company_name" class="form-control" value="<?php echo e(old('company_name', optional($lead)->company_name)); ?>">
    </div>
    <div class="col-12">
        <label class="form-label">Email ID</label>
        <input type="email" name="email_id" class="form-control" value="<?php echo e(old('email_id', optional($lead)->email_id)); ?>" required>
    </div>
    <div class="col-12">
        <label class="form-label">Phone Number</label>
        <input name="phone_number" class="form-control" value="<?php echo e(old('phone_number', optional($lead)->phone_number)); ?>" required>
    </div>
    <div class="col-12">
        <label class="form-label">Description</label>
        <textarea name="description" class="form-control" rows="3"><?php echo e(old('description', optional($lead)->description)); ?></textarea>
    </div>
    <div class="col-12">
        <label class="form-label">Lead Status</label>
        <select name="lead_status" class="form-select">
            <?php $__currentLoopData = $statuses; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $status): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                <option value="<?php echo e($status); ?>" <?php echo e(old('lead_status', optional($lead)->clean_lead_status ?? 'New Lead') === $status ? 'selected' : ''); ?>><?php echo e($status); ?></option>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
        </select>
    </div>
    <div class="col-12 d-grid mt-2">
        <button class="btn btn-primary"><?php echo e($method === 'POST' ? 'Save Lead' : 'Update Lead'); ?></button>
    </div>
</form>

<div class="modal fade" id="createTradefairModal" tabindex="-1" aria-labelledby="createTradefairModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg modal-dialog-scrollable">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="createTradefairModalLabel">New trade fair</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form id="quickTradefairForm" action="<?php echo e(route('tradefairs.store')); ?>" method="POST">
                <?php echo csrf_field(); ?>
                <input type="hidden" name="status" value="1">
                <div class="modal-body row g-2">
                    <div id="quickTradefairErrors" class="col-12"></div>
                    <div class="col-12">
                        <label class="form-label">Fair Name <span class="text-danger">*</span></label>
                        <input name="fair_name" class="form-control" required maxlength="120" id="quick_fair_name">
                    </div>
                    <div class="col-12">
                        <label class="form-label">Continent <span class="text-danger">*</span></label>
                        <select name="continent_id" id="quick_continent_id" class="form-select" required>
                            <option value="">— Select —</option>
                            <?php $__currentLoopData = $continentsQuick; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $c): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($c->id); ?>"><?php echo e($c->name); ?></option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                    </div>
                    <div class="col-12">
                        <label class="form-label">Country <span class="text-danger">*</span></label>
                        <select name="country_id" id="quick_country_id" class="form-select" required disabled>
                            <option value="">— Select continent —</option>
                        </select>
                    </div>
                    <div class="col-12">
                        <label class="form-label">City (venue) <span class="text-danger">*</span></label>
                        <select name="city_id" id="quick_city_id" class="form-select" required disabled>
                            <option value="">— Select country —</option>
                        </select>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Start Date</label>
                        <input type="date" name="event_start_date" class="form-control">
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">End Date</label>
                        <input type="date" name="event_end_date" class="form-control">
                    </div>
                    <div class="col-12">
                        <label class="form-label">Fair Logo (URL or notes)</label>
                        <textarea name="fair_logo" class="form-control" rows="2"></textarea>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-primary" id="quickTradefairSubmit">Create &amp; use</button>
                </div>
            </form>
        </div>
    </div>
</div>

<?php $__env->startPush('scripts'); ?>
<script>
(function () {
    var countriesUrl = <?php echo json_encode(route('locations.countries-by-continent'), 15, 512) ?>;
    var citiesUrl = <?php echo json_encode(route('locations.cities-by-country'), 15, 512) ?>;

    function fillSelect(select, items, placeholder) {
        select.innerHTML = '';
        var p = document.createElement('option');
        p.value = '';
        p.textContent = placeholder;
        select.appendChild(p);
        (items || []).forEach(function (row) {
            var o = document.createElement('option');
            o.value = row.id;
            o.textContent = row.name;
            select.appendChild(o);
        });
    }

    function loadCountriesModal(continentId, countrySel, citySel) {
        if (!continentId) {
            countrySel.disabled = true;
            fillSelect(countrySel, [], '— Select continent —');
            citySel.disabled = true;
            fillSelect(citySel, [], '— Select country —');
            return;
        }
        countrySel.disabled = true;
        fetch(countriesUrl + '?continent_id=' + encodeURIComponent(continentId), {
            headers: { 'Accept': 'application/json', 'X-Requested-With': 'XMLHttpRequest' }
        }).then(function (r) { return r.json(); }).then(function (rows) {
            countrySel.disabled = false;
            fillSelect(countrySel, rows, '— Select country —');
            citySel.disabled = true;
            fillSelect(citySel, [], '— Select country —');
        });
    }

    function loadCitiesModal(countryId, citySel) {
        if (!countryId) {
            citySel.disabled = true;
            fillSelect(citySel, [], '— Select country —');
            return;
        }
        citySel.disabled = true;
        fetch(citiesUrl + '?country_id=' + encodeURIComponent(countryId), {
            headers: { 'Accept': 'application/json', 'X-Requested-With': 'XMLHttpRequest' }
        }).then(function (r) { return r.json(); }).then(function (rows) {
            citySel.disabled = false;
            fillSelect(citySel, rows, '— Select city —');
        });
    }

    var qCont = document.getElementById('quick_continent_id');
    var qCoun = document.getElementById('quick_country_id');
    var qCity = document.getElementById('quick_city_id');
    if (qCont && qCoun && qCity) {
        qCont.addEventListener('change', function () {
            loadCountriesModal(this.value, qCoun, qCity);
        });
        qCoun.addEventListener('change', function () {
            loadCitiesModal(this.value, qCity);
        });
    }

    var select = document.getElementById('tradefair_select');
    var modalEl = document.getElementById('createTradefairModal');
    var form = document.getElementById('quickTradefairForm');
    var errBox = document.getElementById('quickTradefairErrors');
    var venueEl = document.getElementById('tradefair_venue_display');
    var countryEl = document.getElementById('tradefair_country_display');
    if (!select || !venueEl || !countryEl) return;

    function syncLocationFromSelect() {
        var opt = select.options[select.selectedIndex];
        if (!opt || opt.value === '' || opt.value === '__create__') {
            venueEl.value = '';
            countryEl.value = '';
            return;
        }
        venueEl.value = opt.getAttribute('data-venue') || '';
        countryEl.value = opt.getAttribute('data-country') || '';
    }

    select.addEventListener('change', function () {
        if (this.value === '__create__') {
            this.value = '';
            errBox.innerHTML = '';
            var m = bootstrap.Modal.getOrCreateInstance(modalEl);
            m.show();
            var nameInput = document.getElementById('quick_fair_name');
            if (nameInput) nameInput.focus();
            return;
        }
        syncLocationFromSelect();
    });

    syncLocationFromSelect();

    if (!modalEl || !form) return;

    modalEl.addEventListener('hidden.bs.modal', function () {
        errBox.innerHTML = '';
        form.reset();
        var st = form.querySelector('input[name="status"]');
        if (st) st.value = '1';
        if (qCont && qCoun && qCity) {
            qCoun.disabled = true;
            fillSelect(qCoun, [], '— Select continent —');
            qCity.disabled = true;
            fillSelect(qCity, [], '— Select country —');
        }
    });

    form.addEventListener('submit', function (e) {
        e.preventDefault();
        errBox.innerHTML = '';
        var token = document.querySelector('meta[name="csrf-token"]');
        var fd = new FormData(form);
        fetch(form.action, {
            method: 'POST',
            headers: {
                'X-Requested-With': 'XMLHttpRequest',
                'Accept': 'application/json',
                'X-CSRF-TOKEN': token ? token.getAttribute('content') : ''
            },
            body: fd,
            credentials: 'same-origin'
        }).then(function (res) {
            if (res.status === 422) {
                return res.json().then(function (data) {
                    var msgs = (data.errors && typeof data.errors === 'object') ? data.errors : {};
                    var parts = [];
                    Object.keys(msgs).forEach(function (k) {
                        (msgs[k] || []).forEach(function (line) {
                            parts.push(line);
                        });
                    });
                    errBox.innerHTML = '<div class="alert alert-danger mb-0"><ul class="mb-0">' +
                        parts.map(function (p) { return '<li>' + p.replace(/</g, '&lt;') + '</li>'; }).join('') +
                        '</ul></div>';
                });
            }
            if (!res.ok) {
                errBox.innerHTML = '<div class="alert alert-danger mb-0">Could not save trade fair. Try again.</div>';
                return null;
            }
            return res.json();
        }).then(function (data) {
            if (!data || !data.id) return;
            var opt = document.createElement('option');
            opt.value = data.id;
            opt.textContent = data.fair_name;
            opt.setAttribute('data-country', data.country || '');
            opt.setAttribute('data-venue', data.eventvenue || '');
            var createOpt = select.querySelector('option[value="__create__"]');
            if (createOpt) {
                select.insertBefore(opt, createOpt);
            } else {
                select.appendChild(opt);
            }
            select.value = String(data.id);
            syncLocationFromSelect();
            bootstrap.Modal.getInstance(modalEl).hide();
        }).catch(function () {
            errBox.innerHTML = '<div class="alert alert-danger mb-0">Could not save trade fair. Try again.</div>';
        });
    });
})();
</script>
<?php $__env->stopPush(); ?>
<?php /**PATH D:\laragon\www\lt\resources\views/leads/partials/form.blade.php ENDPATH**/ ?>