<x-filament::page>
    <form wire:submit.prevent="create" class="space-y-6">
        {{-- Form Section --}}
        <div class="filament-form space-y-6">
            {{ $this->form }}
        </div>

        {{-- <livewire:notification-sound /> --}}

        {{-- Livewire Component (Invoice Table) --}}
        {{-- <div class="bg-white shadow rounded-xl p-4">
            <livewire:sticker-validation :ref-data="$ref_number" />
        </div> --}}

        {{-- Actions --}}
        <div class="filament-actions mt-6">
            <x-filament::actions>
                @foreach ($this->getFormActions() as $action)
                    {{ $action }}
                @endforeach
            </x-filament::actions>
        </div>
    </form>
</x-filament::page>

<script>
window.addEventListener('open-stickers-sequence', async (event) => {
    const urls = event.detail.urls;

    for (const url of urls) {
        const win = window.open(url, '_blank');

        win.onload = () => {
            win.focus();
            win.print();
        };

        // wait before next
        await new Promise(r => setTimeout(r, 1500));
    }
});
</script>

<script>
window.addEventListener('open-sticker-pdf', event => {

    console.log('PDF event:', event.detail);

    let url = null;

    // Livewire v3 wraps payload in array
    if (Array.isArray(event.detail)) {
        url = event.detail[0]?.url;
    }

    // fallback (if direct object comes)
    else if (event.detail?.url) {
        url = event.detail.url;
    }

    if (url) {
        window.open(url, '_blank');
    } else {
        console.error('PDF URL missing', event.detail);
    }

});
</script>

