@extends('daylight::layouts.app')

@section('title', __('daylight::store.create_product'))

@section('content')
    <x-daylight::page-header :title="__('daylight::store.create_product')"/>

    <x-daylight::session-messages/>

    <form method="post" action="{{ route('daylight.store.products.store') }}" x-data="{ name: '{{ old('name') }}'}">
        @csrf

        <input type="hidden" name="locale" value="{{ request()->get('locale', app()->getLocale()) }}">

        <div x-data="{ activeTab: 'general', type: '{{ old('type', 'variable') }}' }" class="w-full">
            <div class="flex border-b">
                <button @click.prevent="activeTab = 'general'"
                        :class="{ 'bg-gray-100 text-sky-600': activeTab === 'general', 'text-gray-600': activeTab !== 'tab1' }"
                        class="px-4 py-2 focus:outline-none font-semibold text-sm cursor-pointer rounded border-1 mb-4 border-blue-500">{{ __('daylight::settings.general') }}</button>
                <button @click.prevent="activeTab = 'variants'" x-show="type !== 'simple'"
                        :class="{ 'bg-gray-100 text-sky-600': activeTab === 'variants', 'text-gray-600': activeTab !== 'tab2' }"
                        class="px-4 py-2 focus:outline-none font-semibold text-sm cursor-pointer rounded border-1 mb-4 border-blue-500">{{ __('daylight::store.variants') }}</button>
                <button @click.prevent="activeTab = 'content'"
                        :class="{ 'bg-gray-100 text-sky-600': activeTab === 'content', 'text-gray-600': activeTab !== 'tab2' }"
                        class="px-4 py-2 focus:outline-none font-semibold text-sm cursor-pointer rounded border-1 mb-4 border-blue-500">{{ __('daylight::general.content') }}</button>
            </div>

            <div x-show="activeTab === 'general'">
                <h2 class="mt-6">{{ __('daylight::settings.general') }}</h2>

                <x-daylight::input.group
                    :label="__('daylight::store.product_type')"
                    :error="$errors->first('type')"
                    for="type"
                >
                    <x-daylight::input.select name="type" id="type" x-model="type"
                                              @change="document.querySelector('#sku').value = null;">
                        <option value="simple">{{ __('daylight::store.simple') }}</option>
                        <option value="variable">{{ __('daylight::store.variable') }}</option>
                    </x-daylight::input.select>
                </x-daylight::input.group>

                <x-daylight::input.group
                        :label="__('daylight::general.order')"
                        :error="$errors->first('order')"
                        for="order"
                >
                    <x-daylight::input.text
                            name="order"
                            id="order"
                            value="{{ old('order', 100) }}"
                    />
                </x-daylight::input.group>

                <div x-cloak x-show="type === 'simple'">
                    <x-daylight::input.group
                        :label="__('daylight::store.sku')"
                        :error="$errors->first('sku')"
                        for="type"
                    >
                        <livewire:daylight::sku-input
                            :sku="old('sku')"
                        />
                    </x-daylight::input.group>
                </div>

                <x-daylight::input.group
                    :label="__('daylight::store.brand_required')"
                    :error="$errors->first('brand')"
                    for="brand_id"
                >
                    <x-daylight::input.select name="brand_id" id="brand_id" searchable :value="old('brand_id')">
                        @foreach($brands as $brand)
                            <option value="{{ $brand->id }}">{{ $brand->name }}</option>
                        @endforeach
                    </x-daylight::input.select>
                </x-daylight::input.group>

                <x-daylight::input.group :label="__('daylight::store.labels')" :error="$errors->first('labels')" for="labels">
                    @foreach($labels as $label)
                        <div class="flex gap-2">
                            <div class="flex h-6 shrink-0 items-center">
                                <div class="group grid size-4 grid-cols-1">
                                    <input
                                        {{ in_array($label->id, old('labels', [])) ? 'checked' : '' }}
                                        id="labels-{{ $label->id }}" type="checkbox" value="{{ $label->id }}"
                                        name="labels[]"
                                        class="col-start-1 row-start-1 appearance-none rounded border border-gray-300 bg-white checked:border-sky-600 checked:bg-sky-600 indeterminate:border-sky-600 indeterminate:bg-sky-600 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600 disabled:border-gray-300 disabled:bg-gray-100 disabled:checked:bg-gray-100 forced-colors:appearance-auto"/>
                                    <svg viewBox="0 0 14 14" fill="none"
                                         class="pointer-events-none col-start-1 row-start-1 size-3.5 self-center justify-self-center stroke-white group-has-[:disabled]:stroke-gray-950/25 dark:group-has-[:disabled]:stroke-white/25">
                                        <path d="M3 8L6 11L11 3.5" stroke-width="2" stroke-linecap="round"
                                              stroke-linejoin="round"
                                              class="opacity-0 group-has-[:checked]:opacity-100"/>
                                        <path d="M3 7H11" stroke-width="2" stroke-linecap="round"
                                              stroke-linejoin="round"
                                              class="opacity-0 group-has-[:indeterminate]:opacity-100"/>
                                    </svg>
                                </div>
                            </div>
                            <div class="text-sm/6">
                                <label for="labels-{{ $label->id }}" class="font-medium text-gray-900 dark:text-white">
                                    {{ $label->translations->first()->name ?? __('daylight::general.not_translated') }}
                                </label>
                            </div>
                        </div>
                    @endforeach
                </x-daylight::input.group>

                <h2 class="mt-12 mb-4">{{ __('daylight::store.category_attributes') }}</h2>

                <livewire:daylight::attribute-selector
                    :categories="$categories"
                    :activeCategories="old('categories')"
                    :availableAttributes="$attributes"
                    :activeAttributes="old('attributes')"
                />

                <h2 class="mt-12 mb-4">{{ __('daylight::store.downloads') }}</h2>

                <x-daylight::input.group
                    :label="__('daylight::store.downloads')"
                    :error="$errors->first('downloads')"
                    for="downloads"
                >
                    <livewire:daylight::media-selector
                        name="downloads"
                        :initial-media="old('downloads') ? json_decode(old('downloads')) : []"
                        :single-file="false"
                        display-type="list"
                    />
                </x-daylight::input.group>

                <h2 class="mt-12 mb-4">{{ __('daylight::general.status') }}</h2>

                <x-daylight::input.group
                    :label="__('daylight::store.status_required')"
                    :error="$errors->first('status')"
                    for="status"
                >
                    <x-daylight::input.select name="status" id="status">
                        @foreach($statuses as $key => $value)
                            <option value="{{ $key }}">{{ $value }}</option>
                        @endforeach
                    </x-daylight::input.select>
                </x-daylight::input.group>
            </div>

            <div x-show="activeTab === 'variants'" x-cloak>
                @error('options')
                <div class="mt-2 text-xs text-red-500">{{ $errors->first('options') }}</div>
                @enderror
                @error('variants')
                <div class="mt-2 text-xs text-red-500">{{ $errors->first('variants') }}</div>
                @enderror

                <x-daylight::variation-builder
                    :options="$options"
                    :variants="$variants"
                    :selectedVariants="[]"
                />
            </div>

            <div x-show="activeTab === 'content'" x-cloak>
                <h2 class="mt-6 mb-8">{{ __('daylight::general.content') }}</h2>

                <div x-data="{ currentLocale: '{{ request()->get('locale', app()->getLocale()) }}' }">
                    <div class="sm:hidden mb-6 bg-gray-100 -mx-4 p-4">
                        <select x-model="currentLocale" id="tabs" name="tabs" class="block w-full rounded-md border-gray-300 focus:border-indigo-500 focus:ring-indigo-500">
                            @foreach(\Daylight\Core\Modules\Multilingualism\Multilingualism::getAvailableLocales() as $locale)
                                <option
                                    {{ request()->get('locale', app()->getLocale()) === $locale ? 'selected' : '' }} value="{{ $locale }}">
                                    {{ strtoupper($locale) }}
                                </option>
                            @endforeach
                        </select>
                    </div>
                    <div class="hidden sm:flex items-center">
                        <p class="text-sm text-gray-600">{{ __('daylight::store.locale_required') }}:</p>
                        <nav class="ml-4 flex space-x-4" aria-label="Tabs">
                            @foreach(\Daylight\Core\Modules\Multilingualism\Multilingualism::getAvailableLocales() as $locale)
                                <button
                                    @click.prevent="currentLocale = '{{ $locale }}'"
                                    :class="{
                                        'text-sky-700 bg-sky-100': currentLocale === '{{ $locale }}',
                                        'text-gray-500 hover:text-gray-700': currentLocale !== '{{ $locale }}'
                                    }"
                                    class="rounded-md  px-3 py-2 text-sm font-medium hover:text-gray-700 flex items-center"
                                >
                                    <img
                                        src="{{ daylightAsset('resources/assets/images/flags/'.$locale.'.svg') }}"
                                        alt="{{ $locale }}" class="h-5 w-5 rounded-full ring-2 ring-white mr-2"
                                    >
                                    {{ strtoupper($locale) }}
                                </button>
                            @endforeach
                        </nav>
                    </div>

                    @foreach(\Daylight\Core\Modules\Multilingualism\Multilingualism::getAvailableLocales() as $locale)
                        <div
                            x-show="currentLocale === '{{ $locale }}'" {{ $loop->first ? 'x-cloak' : '' }}
                        x-data="{
                                name: '{{ old('translations.'.$locale.'.name') }}',
                                slug: '{{ old('translations.'.$locale.'.slug') }}'
                            }">
                            <x-daylight::input.group
                                :label="__('daylight::store.name_required')"
                                :error="$errors->first('translations.'.$locale.'.name')"
                                for="{{ $locale }}_name"
                            >
                                <x-daylight::input.text
                                    :name="'translations[' . $locale . '][name]'"
                                    id="{{ $locale }}_name"
                                    value="{{ old('translation.'.$locale.'.name') }}"
                                    x-model="name"
                                />
                            </x-daylight::input.group>

                            <x-daylight::input.group
                                :label="__('daylight::store.slug_required')"
                                :error="$errors->first('translations.'.$locale.'.slug')"
                                for="{{ $locale }}_slug"
                            >
                                <x-daylight::input.text
                                    :name="'translations[' . $locale . '][slug]'"
                                    id="{{ $locale }}_slug"
                                    value="{{ old('translations.'.$locale.'.slug') }}"
                                    readonly
                                    x-slug="name"
                                />
                            </x-daylight::input.group>

                            <x-daylight::input.group
                                :label="__('daylight::store.description')"
                                :error="$errors->first('translations.'.$locale.'.description')"
                                for="{{ $locale }}_description"
                            >
                                <div id="{{ $locale }}_description" x-data='{ description: @json(old('translations.'.$locale . '.description')) }'>
                                    <x-daylight::input.wysiwyg
                                        x-model="description"
                                        name="translations[{{ $locale }}][description]"
                                        placeholder="{{ __('daylight::store.write_something') }}"
                                    />
                                </div>
                            </x-daylight::input.group>

                            <x-daylight::input.group
                                :label="__('daylight::store.pros')"
                                for="{{ $locale }}_pros"
                            >
                                <div x-data='{ pros: @json(array_filter(old('translations.'.$locale.'.pros', []))) }'>
                                    <template x-for="(pro, index) in pros" :key="index">
                                        <div class="flex items-center mb-2">
                                            <input name="translations[{{ $locale }}][pros][]" x-model="pros[index]"
                                                   type="text"
                                                   class="block w-full rounded-md border border-gray-300 py-1.5 text-gray-900 shadow-sm placeholder:text-gray-400 sm:max-w-lg sm:text-sm sm:leading-6">

                                            <button @click.prevent="pros.splice(index, 1)"
                                                    class="ml-2 p-2 bg-red-500 hover:bg-red-700 transition rounded-full">
                                                <svg class="text-white w-4 h-4" data-slot="icon" fill="none"
                                                     stroke-width="1.5" stroke="currentColor" viewBox="0 0 24 24"
                                                     xmlns="http://www.w3.org/2000/svg" aria-hidden="true">
                                                    <path stroke-linecap="round" stroke-linejoin="round"
                                                          d="m14.74 9-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 0 1-2.244 2.077H8.084a2.25 2.25 0 0 1-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 0 0-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 0 1 3.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 0 0-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 0 0-7.5 0"></path>
                                                </svg>
                                            </button>
                                        </div>
                                    </template>

                                    <x-daylight::buttons.primary @click.prevent="pros.push('')">
                                        {{ __('daylight::store.add_new') }}
                                    </x-daylight::buttons.primary>
                                </div>

                            </x-daylight::input.group>

                            <x-daylight::input.group
                                :label="__('daylight::store.cons')"
                                :error="$errors->first('translations.'.$locale.'.cons')"
                                for="{{ $locale }}_cons"
                            >
                                <div x-data='{ cons: @json(array_filter(old('translations.'.$locale.'.cons', []))) }'>
                                    <template x-for="(con, index) in cons" :key="index">
                                        <div class="flex items-center mb-2">
                                            <input name="translations[{{ $locale }}][cons][]" x-model="cons[index]"
                                                   type="text"
                                                   class="block w-full rounded-md border border-gray-300 py-1.5 text-gray-900 shadow-sm placeholder:text-gray-400 sm:max-w-lg sm:text-sm sm:leading-6">

                                            <button @click.prevent="cons.splice(index, 1)"
                                                    class="ml-2 p-2 bg-red-500 hover:bg-red-700 transition rounded-full">
                                                <svg class="text-white w-4 h-4" data-slot="icon" fill="none"
                                                     stroke-width="1.5" stroke="currentColor" viewBox="0 0 24 24"
                                                     xmlns="http://www.w3.org/2000/svg" aria-hidden="true">
                                                    <path stroke-linecap="round" stroke-linejoin="round"
                                                          d="m14.74 9-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 0 1-2.244 2.077H8.084a2.25 2.25 0 0 1-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 0 0-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 0 1 3.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 0 0-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 0 0-7.5 0"></path>
                                                </svg>
                                            </button>
                                        </div>
                                    </template>

                                    <x-daylight::buttons.primary @click.prevent="cons.push('')">
                                        {{ __('daylight::store.add_new') }}
                                    </x-daylight::buttons.primary>
                                </div>

                            </x-daylight::input.group>

                            <div>
                                <h2 class="mt-12">SEO</h2>

                                <x-daylight::input.group
                                    :label="__('daylight::store.robots_required')"
                                    :error="$errors->first('translations.'.$locale.'.robots')"
                                    for="{{ $locale }}_robots"
                                >
                                    <x-daylight::input.select name="translations[{{ $locale }}][robots]"
                                                              id="{{ $locale }}_robots">
                                        <option
                                            value="index, follow" {{ old('translations.'.$locale.'.robots') === 'index, follow' ? 'selected' : '' }}>
                                            Default (index, follow)
                                        </option>
                                        <option
                                            value="nofollow" {{ old('translations.'.$locale.'.robots') === 'nofollow' ? 'selected' : '' }}>
                                            nofollow
                                        </option>
                                        <option
                                            value="noindex" {{ old('translations.'.$locale.'.robots') === 'noindex' ? 'selected' : '' }}>
                                            noindex
                                        </option>
                                        <option
                                            value="noindex, nofollow" {{ old('translations.'.$locale.'.robots') === 'noindex, nofollow' ? 'selected' : '' }}>
                                            noindex, nofollow
                                        </option>
                                    </x-daylight::input.select>
                                </x-daylight::input.group>

                                <x-daylight::input.group
                                    :label="__('daylight::general.title')"
                                    :error="$errors->first('translations.'.$locale.'.meta_title')"
                                    for="{{ $locale }}_meta_title"
                                >
                                    <x-daylight::input.text name="translations[{{ $locale }}][meta_title]"
                                                            id="{{ $locale }}_meta_title"
                                                            value="{{ old('translations.'.$locale.'.meta_title') }}"/>
                                </x-daylight::input.group>

                                <x-daylight::input.group
                                    :label="__('daylight::general.description')"
                                    :error="$errors->first('translations.'.$locale.'.meta_description')"
                                    for="{{ $locale }}_meta_description"
                                >
                                    <x-daylight::input.textarea name="translations[{{ $locale }}][meta_description]"
                                                                id="{{ $locale }}_meta_description">
                                        {{ old('translations.'.$locale.'.meta_description') }}
                                    </x-daylight::input.textarea>
                                </x-daylight::input.group>
                            </div>
                        </div>
                    @endforeach
                </div>
            </div>
        </div>

        <div class="mt-6 flex items-center justify-end gap-x-3">
            <x-daylight::buttons.secondary href="{{ route('daylight.store.products.index') }}" element="a">
                {{ __('daylight::general.cancel') }}
            </x-daylight::buttons.secondary>

            <x-daylight::buttons.primary type="submit">
                {{ __('daylight::general.save') }}
            </x-daylight::buttons.primary>
        </div>
    </form>

    <script>
        document.addEventListener('variant-selected', function (event) {
            let nameInput = document.querySelector('#en_name')
            let originalDescription = document.querySelector('#original-description')

            if (nameInput && nameInput.value === '') {
                nameInput.value = event.detail[0].variant.title;

                nameInput.dispatchEvent(new Event('input'));
            }

            if (event.detail[0].variant.description) {
                originalDescription.querySelector('p').innerHTML = event.detail[0].variant.description;
                originalDescription.classList.remove('hidden');
            } else {
                originalDescription.classList.add('hidden');
            }
        });
    </script>
@endsection
