@props([
    'action',
    'method' => 'POST',
    'city' => null,
    'countryOptions' => [],
    'submitLabel',
])

@php
    $names = is_array($city?->name) ? $city->name : [];
    $countryId = old('country_id', $city?->region?->country_id);
@endphp

<form method="POST" action="{{ $action }}" novalidate @if ($city === null) data-geo-cascade @endif>
    @csrf
    @if (strtoupper($method) !== 'POST')
        @method($method)
    @endif

    <div class="card">
        <div class="card-body">
            <div class="row g-3">
                <div class="col-md-6">
                    <x-ui.form.search-select
                        name="country_id"
                        lookup="countries"
                        :label="__('geography.fields.country')"
                        :options="$countryOptions"
                        :selected="$countryId"
                        :placeholder="__('geography.placeholders.country')"
                        optional
                    />
                </div>
                <div class="col-md-6">
                    <x-ui.form.search-select
                        name="region_id"
                        lookup="regions"
                        lookup-parent="country_id"
                        :label="__('geography.fields.region')"
                        :options="[]"
                        :selected="old('region_id', $city?->region_id)"
                        :placeholder="__('geography.placeholders.region')"
                        required
                    />
                </div>
                <div class="col-md-3">
                    <x-ui.form.input
                        name="sort_order"
                        type="number"
                        min="0"
                        :label="__('geography.fields.sort_order')"
                        :value="old('sort_order', $city?->sort_order ?? 0)"
                    />
                </div>
                @if ($city !== null)
                    <div class="col-md-3">
                        <x-ui.form.select
                            name="is_active"
                            :label="__('geography.fields.status')"
                            :options="[
                                1 => __('geography.status.active'),
                                0 => __('geography.status.inactive'),
                            ]"
                            :selected="old('is_active', $city->is_active ? 1 : 0)"
                            required
                        />
                    </div>
                @endif
                <div class="col-md-6">
                    <x-ui.form.input
                        name="name_en"
                        :label="__('geography.fields.name_en')"
                        :value="old('name_en', $names['en'] ?? '')"
                        required
                    />
                </div>
                <div class="col-md-6">
                    <x-ui.form.input
                        name="name_ar"
                        :label="__('geography.fields.name_ar')"
                        :value="old('name_ar', $names['ar'] ?? '')"
                    />
                </div>
            </div>
        </div>
        <div class="card-footer d-flex justify-content-end gap-2 bg-white">
            <x-ui.buttons.secondary :href="route('settings.geography.cities.index')">
                {{ __('common.cancel') }}
            </x-ui.buttons.secondary>
            <x-ui.buttons.primary type="submit">{{ $submitLabel }}</x-ui.buttons.primary>
        </div>
    </div>
</form>

@if ($city === null)
    @push('scripts')
        <script src="{{ asset('assets/admin/js/erp-geo-cascade.js') }}"></script>
    @endpush
@endif
