@props([
    'title' => 'Apakah anda yakin ingin membatalkan?',
    'description' => 'Tindakan ini memerlukan persetujuan supervisor.',
])

<div x-data="{
    open: false,
    action: '',
    title: '{{ $title }}',
    description: '{{ $description }}',
    reason: '',
    init() {
        this.$watch('open', value => {
            if (value) {
                document.body.style.overflow = 'hidden';
            } else {
                document.body.style.overflow = 'unset';
            }
        });
        window.addEventListener('open-cancel-modal', event => {
            this.open = true;
            this.action = event.detail.action;
            this.reason = '';
            if (event.detail.title) this.title = event.detail.title;
            if (event.detail.description) this.description = event.detail.description;
        });
    }
}" x-show="open" x-cloak @keydown.escape.window="open = false"
    class="fixed inset-0 z-99999 flex items-center justify-center overflow-y-auto p-4"
    {{ $attributes }}>

    <!-- Backdrop -->
    <div @click="open = false" class="fixed inset-0 h-full w-full bg-gray-900/50 backdrop-blur-sm"
        x-transition:enter="transition ease-out duration-200" x-transition:enter-start="opacity-0"
        x-transition:enter-end="opacity-100" x-transition:leave="transition ease-in duration-150"
        x-transition:leave-start="opacity-100" x-transition:leave-end="opacity-0">
    </div>

    <!-- Modal Content -->
    <div @click.stop class="relative w-full max-w-md rounded-2xl bg-white p-6 shadow-xl dark:bg-gray-900"
        x-transition:enter="transition ease-out duration-200" x-transition:enter-start="opacity-0 scale-95"
        x-transition:enter-end="opacity-100 scale-100" x-transition:leave="transition ease-in duration-150"
        x-transition:leave-start="opacity-100 scale-100"
        x-transition:leave-end="opacity-0 scale-95">

        <div class="text-center">
            <!-- Warning Icon -->
            <div class="mx-auto mb-4 flex h-14 w-14 items-center justify-center rounded-full bg-warning-50 dark:bg-warning-500/15">
                <svg class="h-7 w-7 text-warning-500" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                    <path stroke-linecap="round" stroke-linejoin="round" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z" />
                </svg>
            </div>

            <!-- Title -->
            <h3 class="mb-2 text-lg font-semibold text-gray-900 dark:text-white" x-text="title"></h3>

            <!-- Description -->
            <p class="mb-4 text-sm text-gray-500 dark:text-gray-400" x-text="description" x-show="description"></p>

            <!-- Reason Input -->
            <form :action="action" method="POST" class="text-left">
                @csrf
                <div class="mb-4">
                    <label for="cancel-reason" class="mb-1.5 block text-sm font-medium text-gray-700 dark:text-gray-300">
                        Alasan Pembatalan <span class="text-error-500">*</span>
                    </label>
                    <textarea
                        x-model="reason"
                        name="reason"
                        id="cancel-reason"
                        rows="3"
                        required
                        placeholder="Masukkan alasan pembatalan..."
                        class="w-full rounded-lg border border-gray-300 bg-transparent px-4 py-2.5 text-sm text-gray-800 placeholder-gray-400 focus:border-brand-300 focus:outline-none focus:ring-2 focus:ring-brand-500/20 dark:border-gray-700 dark:text-white/90 dark:placeholder-gray-500"
                    ></textarea>
                </div>

                <!-- Action Buttons -->
                <div class="flex flex-col-reverse gap-2 sm:flex-row sm:justify-center">
                    <button @click="open = false" type="button"
                        class="w-full rounded-lg border border-gray-200 bg-white px-4 py-2.5 text-sm font-medium text-gray-700 transition-colors hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-gray-200 dark:border-gray-700 dark:bg-gray-800 dark:text-gray-300 dark:hover:bg-gray-700 dark:focus:ring-gray-700 sm:w-auto">
                        Batal
                    </button>
                    <button type="submit" :disabled="!reason.trim()"
                        class="w-full rounded-lg bg-warning-500 px-4 py-2.5 text-sm font-medium text-white transition-colors hover:bg-warning-600 focus:outline-none focus:ring-2 focus:ring-warning-300 disabled:opacity-50 disabled:cursor-not-allowed dark:focus:ring-warning-800 sm:w-auto">
                        Ya, Batalkan
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

<style>
    [x-cloak] {
        display: none;
    }
</style>
