<div>
    <x-jetstream.button
        wire:click.prevent="openModal()"
        class="bg-theme-brand hover:bg-opacity-80">
        {{ __('New Call') }}
    </x-jetstream.button>

    {{-- No credits modal --}}
    <x-jetstream.dialog-modal wire:model.live="showNoCreditsModal" wire:key="showCallNoCreditsModal" closeButton="true">
        <x-slot name="title">
            {{ trans_with_platform('Post a @PLATFORM_NAME@ call') }}
        </x-slot>
        <x-slot name="content">
            @if ($spendableBalance !== null)
                <div class="mb-4 text-sm text-theme-secondary">
                    {{ __('Current balance total') }}: <span class="font-semibold text-theme-primary">{{ tbFormat($spendableBalance) }}</span>
                </div>
            @endif
            @livewire('static-post', ['type' => 'SiteContents\\Call\\NotAllowed', 'limit' => 1, 'hideAuthor' => true, 'fallbackText' => trans_with_platform('You need @PLATFORM_CURRENCY_NAME_PLURAL@ to post a call.')])
        </x-slot>
        <x-slot name="footer">
            <x-jetstream.button wire:click="$set('showNoCreditsModal', false)">
                {{ __('OK') }}
            </x-jetstream.button>
        </x-slot>
    </x-jetstream.dialog-modal>

    @if ($showModal)
        <x-jetstream.dialog-modal wire:model="showModal" wire:key="showCallModal" closeButton="true">
            <x-slot name="title">
                {{ trans_with_platform('Post a @PLATFORM_NAME@ call') }}
            </x-slot>

            <x-slot name="content">

                <div class="space-y-6">

                    {{-- Tag picker (dedicated component) --}}
                    <livewire:calls.call-skill-input wire:key="call-skill-input" />

                    @error('tagId')
                        <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                    @enderror

                    {{-- Content --}}
                    @php $contentMax = timebank_config('calls.content_max_input', 200); @endphp
                    <div wire:key="call-content"
                         x-data="{ remaining: {{ $contentMax - mb_strlen($content) }} }"
                         x-init="$nextTick(() => { const ta = $el.querySelector('textarea'); if (ta) { ta.addEventListener('input', () => { remaining = {{ $contentMax }} - ta.value.length; }); } })">
                        <label class="block text-sm text-theme-primary mb-2">
                            {{ __('Description') }}
                            <span class="text-theme-secondary text-xs ml-1" x-text="remaining + ' {{ __('characters left') }}'"></span>
                        </label>
                        <x-textarea
                            placeholder="{{ __('Describe your request in more detail...') }}"
                            rows="4"
                            maxlength="{{ $contentMax }}"
                            class="!text-base"
                            wire:model.blur="content" />
                        @error('content')
                            <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                        @enderror
                    </div>

                    {{-- Location --}}
                    <div wire:key="call-location">
                        <label class="block text-sm text-theme-primary mb-2">{{ __('Exchange location') }}</label>
                        <livewire:locations.locations-dropdown
                            :hide-label="true"
                            :country="$country"
                            :city="$city"
                            :division="$division"
                            :district="$district"
                            wire:key="call-location-dropdown-{{ $country }}-{{ $city }}" />
                        @error('country')
                            <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                        @enderror
                    </div>

                    {{-- Expiry date --}}
                    <div wire:key="call-till">
                        <label class="block text-sm text-theme-primary mb-2">{{ __('Expires on') }}</label>
                        @php
                            $tillMinDate = now()->addDay()->format('Y-m-d');
                            $activeProfileType = session('activeProfileType');
                            $tillMaxDays = ($activeProfileType && $activeProfileType !== \App\Models\User::class)
                                ? timebank_config('calls.till_max_days_non_user')
                                : timebank_config('calls.till_max_days');
                            $tillMaxDate = $tillMaxDays !== null ? now()->addDays($tillMaxDays)->format('Y-m-d') : null;
                        @endphp
                        <div wire:ignore>
                            <x-flatpickr
                                dateFormat="Y-m-d"
                                altFormat="d-m-Y"
                                :minDate="$tillMinDate"
                                :maxDate="$tillMaxDate"
                                placeholder="{{ __('Select a date') }}"
                                wire:model.defer="till"
                                class="mt-1 block border-theme-border focus:border-theme-accent focus:ring-1 focus:ring-theme-accent rounded-md shadow-sm" />
                        </div>
                        @error('till')
                            <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                        @enderror
                    </div>

                    {{-- Visibility --}}
                    <div wire:key="call-public">
                        <x-checkbox
                            id="call-is-public"
                            label="{{ __('Public, visible for search engines and sharable on social media') }}"
                            wire:model.live="isPublic" />
                        @if ($isPublic)
                            <p class="mt-2 text-sm text-red-600">
                                {{ str_replace(':username', $profileName, __('This exposes your username (:username), your profile photo and your profile and work locations!')) }}
                            </p>
                        @endif
                    </div>

                </div>

            </x-slot>

            <x-slot name="footer">
                <div wire:loading wire:target="save">
                    <x-mini-button flat primary rounded spinner />
                </div>
                <x-jetstream.secondary-button
                    class="ml-3 w-32 justify-center"
                    wire:click="$set('showModal', false)"
                    wire:loading.attr="disabled">
                    {{ __('Cancel') }}
                </x-jetstream.secondary-button>
                <x-jetstream.button
                    class="ml-3 w-32 justify-center"
                    wire:click.prevent="save()"
                    wire:loading.attr="disabled">
                    {{ __('Publish') }}
                </x-jetstream.button>
            </x-slot>
        </x-jetstream.dialog-modal>
    @endif
</div>

<script>
    function initCallFlatpickr() {
        const tillInput = document.querySelector('[wire\\:model\\.defer="till"]');
        if (tillInput && !tillInput._flatpickr) {
            if (window.LaravelFlatpickr) {
                window.LaravelFlatpickr.initializeFlatpickr(tillInput);
            }
        }
    }

    document.addEventListener('DOMContentLoaded', function() {
        setTimeout(initCallFlatpickr, 100);

        Livewire.hook('morph.updated', () => {
            setTimeout(initCallFlatpickr, 200);
            setTimeout(initCallFlatpickr, 500);
        });

        Livewire.on('showModal', () => {
            setTimeout(initCallFlatpickr, 300);
            setTimeout(initCallFlatpickr, 600);
        });
    });
</script>
