<x-admin-app-layout>
    @section('title', __('Edit Advertisement' . ' - ' . config('application_info.company_info.name')))

    <div class="white-box">
        <x-admin::page-header title="{{ __('Edit Advertisement') }}" :buttons="$buttons" />

        <form action="{{ route('admin.settings.ads.update', $ad->id) }}" method="POST" class="space-y-6 form-submit-edit">
            @csrf
            @method('PUT')

            <div class="w-full grid grid-cols-1 md:grid-cols-4 gap-4 xxl:gap-6">
                <div
                    class="col-span-3 border border-neutral-30 dark:border-neutral-500 rounded p-4 xl:p-6 flex flex-col gap-4">
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                        <div class="input-group">
                            <x-admin::label for="position" required>
                                {{ __('Select Position') }}
                            </x-admin::label>
                            <x-admin::select-option class="select-2" name="position" label="Position"
                                value="{{ old('position', $ad->position->value) }}">
                                @foreach (\App\Enums\AdsPositionEnum::values() as $position)
                                    <option value="{{ $position }}" @selected($ad->position->value == $position)>
                                        {{ snackCaseToNormalWord($position) }}
                                    </option>
                                @endforeach
                            </x-admin::select-option>
                            <x-admin::input-error name="position" />
                        </div>
                        <div class="input-group">
                            <x-admin::label for="type" required>
                                {{ __('Select Ads Type') }}
                            </x-admin::label>
                            <x-admin::select-option class="select-2" id="type" name="type" label="Type">
                                @foreach (\App\Enums\AdsTypeEnum::values() as $type)
                                    <option value="{{ $type }}" @selected($ad->type->value == $type)>
                                        {{ snackCaseToNormalWord($type) }}
                                    </option>
                                @endforeach
                            </x-admin::select-option>
                            <x-admin::input-error name="type" />
                        </div>
                    </div>
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-2">
                        <x-admin::text-input-group id="title" name="title" placeholder="Title" :value="old('title', $ad->title)"
                            type="Example Ads" required label="Title" />
                        <div id="link">
                            <x-admin::text-input-group id="link" name="link" placeholder="Link"
                                :value="old('link', $ad->link)" type="https://example.com" label="Link" />
                        </div>
                    </div>

                    <div class="grid grid-cols-1" id="html-code">
                        <div class="form-group">
                            <x-admin::label for="code">
                                {{ __('Enter Ads Code (From google or Custom code)') }}
                            </x-admin::label>
                            <div class="editorContainer" data-input="code"></div>
                            <input type="hidden" name="code" value="{{ old('code', $ad->code) }}" id="code">
                        </div>
                    </div>
                    <div class="grid grid-cols-1" id="adsterra">
                        <x-admin::text-input-group id="code" :value="$ad->code" name="adsterra_key"
                            placeholder="7116783195337" type="Example Code" required label="Key" />
                    </div>
                    @php
                        $code = $ad->code;
                        if ($ad->type === \App\Enums\AdsTypeEnum::ADSENSE) {
                            $code = json_decode($ad->code, true);
                        }
                    @endphp
                    <div id="adsense-fields" class="grid grid-cols-1 md:grid-cols-2 gap-2">
                        <x-admin::text-input-group id="client" :value="$code['client'] ?? ''" name="client"
                            placeholder="ca-PUB-7116783195337" type="Example Ads" required label="Client" />
                        <x-admin::text-input-group id="slot" :value="$code['slot'] ?? ''" name="slot"
                            placeholder="7116783195337" type="Example Slot" required label="Slot" />
                    </div>
                </div>
                <div
                    class="col-span-1 border border-neutral-30 dark:border-neutral-500 rounded p-4 xl:p-6 flex flex-col gap-4">
                    <div class="input-group">
                        <x-admin::label for="status" required>
                            {{ __('Select Status') }}
                        </x-admin::label>
                        <x-admin::switch name="status" value="{{ old('status', $ad->status) }}" :types="array_reverse(
                            array_map(
                                fn($status) => ['value' => $status, 'label' => snackCaseToNormalWord($status)],
                                \App\Enums\AdsStatusEnum::values(),
                            ),
                        )" />
                        <x-admin::input-error name="status" />
                    </div>

                    <div class="input-group" id="image">
                        <x-admin::label for="image">{{ __('Image') }}</x-admin::label>
                        <x-admin::file-uploader type="text" name="image" label="Image" required
                            :value="$ad->image" />
                        <x-admin::input-error name="image" />
                    </div>
                    <x-admin::text-input-group id="width" name="width" type="number" :value="old('width', $ad->width)"
                        step="1" label="Width" placeholder="1" />
                    <x-admin::text-input-group id="height" name="height" :value="old('height', $ad->height)" type="number"
                        step="1" label="Height" placeholder="1" />

                    <div class="flex justify-end gap-4">
                        <x-admin::primary-button type="submit">
                            {{ __('Save') }}
                        </x-admin::primary-button>
                    </div>
                </div>
            </div>
        </form>
    </div>

    @push('scripts')
        @vite('resources/admin/js/ads/create.js')
    @endpush
</x-admin-app-layout>
