<flux:main>
    <div class="flex justify-between items-center">
        <div>
            <flux:heading size="xl">{{ $pageTitle }}</flux:heading>
        </div>
        <div class="flex gap-2">
            @can('create reward')
                <flux:modal.trigger name="form-modal">
                    <flux:button variant="primary" class="text-sm"
                        wire:click="$dispatch('modal:open', {method: 'create', 'modalTitle': 'Tambah Hadiah'})">Tambah
                    </flux:button>
                </flux:modal.trigger>
            @endcan
        </div>
    </div>

    <div class="mt-6">
        <livewire:datatable.loyalty.rewards-table />
    </div>

    @include('components.modals.confirmation', [
        'modalName' => 'delete-confirmation',
        'modalTitle' => 'Apakah Anda yakin?',
        'modalMessage' => 'Data yang berelasi dengan data ini juga akan ikut terhapus.',
        'buttonVariant' => 'primary',
        'buttonColor' => 'danger',
        'buttonText' => 'Ya, Hapus',
    ])
    <flux:modal name="form-modal" class="w-[95%] max-w-sm md:max-w-xl mx-auto" @close="closeModal('form-modal')">
        <div class="p-4 space-y-6">
            <flux:heading size="lg">{{ $modalTitle }}</flux:heading>

            <flux:field>
                <flux:label>Nama <span class="text-red-500 ms-1">*</span></flux:label>
                <flux:input placeholder="Blue Emotion 10ml" wire:model="form.name" autofocus autocomplete="off" />
                <flux:error name="form.name" />
            </flux:field>

            <div class="grid grid-cols-1 md:grid-cols-2 gap-4">

                <flux:field>
                    <flux:label>Poin <span class="text-red-500 ms-1">*</span></flux:label>
                    <flux:input placeholder="300" x-mask:dynamic="$money($input, ',')" wire:model="form.points"
                        autocomplete="off" />
                    <flux:error name="form.points" />
                </flux:field>
                <flux:field>
                    <flux:label>Kategori <span class="text-red-500 ms-1">*</span></flux:label>
                    <flux:select variant="listbox" placeholder="Pilih kategori" wire:model="form.category">
                        @foreach (\App\Enums\RewardCategory::cases() as $category)
                            <flux:select.option value="{{ $category->value }}">{{ $category->label() }}
                            </flux:select.option>
                        @endforeach
                    </flux:select>
                    <flux:error name="form.category" />
                </flux:field>
            </div>

            <flux:field>
                <flux:label>Stok </flux:label>
                <flux:description>Kosongkan jika tidak ada batas maksimum.</flux:description>
                <flux:input placeholder="300" x-mask:dynamic="$money($input, ',')" wire:model="form.stock"
                    autocomplete="off" />
                <flux:error name="form.stock" />
            </flux:field>

            <flux:field>
                <flux:label>Visibilitas <span class="text-red-500 ms-1">*</span></flux:label>
                <flux:radio.group wire:model="form.is_show" variant="buttons" class="w-full *:flex-1">
                    @foreach (\App\Enums\IsShow::cases() as $item)
                        <flux:radio value="{{ $item->value }}">
                            {{ $item->label() }}
                        </flux:radio>
                    @endforeach
                </flux:radio.group>
                <flux:error name="form.is_show" />
            </flux:field>

            <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                <flux:field>
                    <flux:label>Tanggal Mulai <span class="text-red-500 ms-1">*</span></flux:label>
                    <flux:date-picker with-today wire:model="form.start_date" placeholder="Pilih Tanggal"
                        autocomplete="off" locale="id-ID" />
                    <flux:error name="form.start_date" />
                </flux:field>

                <flux:date-picker with-today label="Tanggal Selesai" wire:model="form.end_date"
                    placeholder="Pilih Tanggal" autocomplete="off" locale="id-ID" />
            </div>

            <flux:field>
                <flux:label>Tier <span class="text-red-500 ms-1">*</span></flux:label>
                <flux:select placeholder="Pilih Tier" wire:model="form.tier_ids" multiple searchable variant="listbox">
                    @foreach ($tiers as $key => $value)
                        <flux:select.option value="{{ $key }}">{{ $value }}</flux:select.option>
                    @endforeach
                </flux:select>
                <flux:error name="form.tier_ids" />
            </flux:field>

            <div class="space-y-3">
                <h3 class="text-sm font-medium">Thumbnail</h3>
                <div class="dropzone-wrapper">
                    <livewire:dropzone wire:model="form.thumbnail" :rules="['image', 'mimes:png,jpeg', 'max:10420']" :max-files="1" :key="'thumbnail'"
                        :files="$form->thumbnail ?? []" />
                    @error('form.thumbnail')
                        <div role="alert" aria-live="polite" aria-atomic="true"
                            class="mt-3 text-sm font-medium text-red-500 dark:text-red-400">
                            <svg class="shrink-0 [:where(&amp;)]:size-5 inline" data-flux-icon=""
                                xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor"
                                aria-hidden="true" data-slot="icon">
                                <path fill-rule="evenodd"
                                    d="M8.485 2.495c.673-1.167 2.357-1.167 3.03 0l6.28 10.875c.673 1.167-.17 2.625-1.516 2.625H3.72c-1.347 0-2.189-1.458-1.515-2.625L8.485 2.495ZM10 5a.75.75 0 0 1 .75.75v3.5a.75.75 0 0 1-1.5 0v-3.5A.75.75 0 0 1 10 5Zm0 9a1 1 0 1 0 0-2 1 1 0 0 0 0 2Z"
                                    clip-rule="evenodd"></path>
                            </svg>
                            {{ $message }}
                        </div>
                    @enderror
                </div>
            </div>

            <div class="flex">
                <flux:spacer />
                <flux:button variant="primary" class="sm:w-auto cursor-pointer" wire:click="{{ $method }}">
                    Simpan
                </flux:button>
            </div>
        </div>
    </flux:modal>
</flux:main>
