

<script>

(function () {

    function isDeleteForm(form) {

        if (!form || form.tagName !== 'FORM') return null;

        if (form.classList.contains('swal-delete-form') || form.classList.contains('swal-confirm-delete')) {

            return form;

        }

        const methodInput = form.querySelector('input[name="_method"]');

        if (methodInput && String(methodInput.value).toUpperCase() === 'DELETE') {

            return form;

        }

        return null;

    }



    function isConfirmForm(form) {

        return form && form.tagName === 'FORM' && form.classList.contains('swal-confirm-form') ? form : null;

    }



    function bindSwalForms(root) {

        root.querySelectorAll('select[data-swal-status]:not([data-swal-bound])').forEach(function (select) {

            select.dataset.swalBound = '1';

            const original = select.value;

            select.addEventListener('change', function () {

                const form = select.closest('form');

                const newVal = select.value;

                Swal.fire({

                    title: select.dataset.swalTitle || 'Update status?',

                    text: 'Change status to "' + newVal.charAt(0).toUpperCase() + newVal.slice(1) + '"?',

                    icon: 'question',

                    showCancelButton: true,

                    confirmButtonColor: '#f97316',

                    cancelButtonColor: '#6b7280',

                    confirmButtonText: 'Yes, update',

                    cancelButtonText: 'Cancel',

                }).then(function (result) {

                    if (result.isConfirmed) {

                        form.submit();

                    } else {

                        select.value = original;

                    }

                });

            });

        });

    }



    function bindSwalFetchDeletes(root) {

        root.querySelectorAll('[data-swal-fetch-delete]:not([data-swal-fetch-bound])').forEach(function (btn) {

            btn.dataset.swalFetchBound = '1';

            btn.addEventListener('click', function (e) {

                e.preventDefault();

                Swal.fire({

                    title: btn.dataset.swalTitle || 'Are you sure?',

                    text: btn.dataset.swalText || 'This action cannot be undone.',

                    icon: 'warning',

                    showCancelButton: true,

                    confirmButtonColor: '#dc2626',

                    cancelButtonColor: '#6b7280',

                    confirmButtonText: btn.dataset.swalConfirmText || 'Yes, delete it',

                    cancelButtonText: 'Cancel',

                }).then(function (result) {

                    if (!result.isConfirmed) return;



                    fetch(btn.dataset.swalUrl, {

                        method: btn.dataset.swalMethod || 'DELETE',

                        headers: {

                            'Content-Type': 'application/json',

                            'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content'),

                            'Accept': 'application/json',

                        },

                        body: btn.dataset.swalBody || null,

                    })

                    .then(function (res) { return res.json(); })

                    .then(function (data) {

                        if (data.success) {

                            const removeSel = btn.dataset.swalRemove;

                            const el = removeSel ? btn.closest(removeSel) : null;

                            if (el) el.remove();

                            if (btn.dataset.swalReload === '1') {

                                location.reload();

                                return;

                            }

                            if (data.message) swalToast('success', data.message);

                        } else {

                            Swal.fire({

                                icon: 'error',

                                title: 'Error',

                                text: data.message || 'Could not complete delete.',

                                confirmButtonColor: '#f97316',

                            });

                        }

                    })

                    .catch(function () {

                        Swal.fire({

                            icon: 'error',

                            title: 'Error',

                            text: 'Could not complete delete.',

                            confirmButtonColor: '#f97316',

                        });

                    });

                });

            });

        });



        root.querySelectorAll('.remove-from-cart:not([data-swal-fetch-bound])').forEach(function (btn) {

            btn.dataset.swalFetchBound = '1';

            btn.addEventListener('click', function (e) {

                e.preventDefault();

                const row = btn.closest('[data-id]');

                if (!row) return;

                const id = row.getAttribute('data-id');

                const url = btn.dataset.swalUrl;

                if (!url) return;



                Swal.fire({

                    title: btn.dataset.swalTitle || 'Remove item?',

                    text: btn.dataset.swalText || 'Remove this item from your cart?',

                    icon: 'warning',

                    showCancelButton: true,

                    confirmButtonColor: '#dc2626',

                    cancelButtonColor: '#6b7280',

                    confirmButtonText: 'Yes, remove it',

                    cancelButtonText: 'Cancel',

                }).then(function (result) {

                    if (!result.isConfirmed) return;



                    fetch(url, {

                        method: 'DELETE',

                        headers: {

                            'Content-Type': 'application/json',

                            'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content'),

                            'Accept': 'application/json',

                        },

                        body: JSON.stringify({ id: id }),

                    })

                    .then(function (res) { return res.json(); })

                    .then(function (data) {

                        if (data.success) {

                            location.reload();

                        }

                    });

                });

            });

        });

    }



    function tagDeleteForms(root) {

        root.querySelectorAll('form:not(.swal-delete-form)').forEach(function (form) {

            const methodInput = form.querySelector('input[name="_method"]');

            if (methodInput && String(methodInput.value).toUpperCase() === 'DELETE') {

                form.classList.add('swal-delete-form');

                if (!form.dataset.title) {

                    form.dataset.title = 'Are you sure?';

                }

                if (!form.dataset.text) {

                    form.dataset.text = 'This action cannot be undone.';

                }

            }

        });

    }



    function initSwalHandlers(root) {

        if (!root || !window.Swal) return;

        tagDeleteForms(root);

        bindSwalForms(root);

        bindSwalFetchDeletes(root);

    }



    document.addEventListener('submit', function (e) {

        const form = e.target;

        if (!form || form.tagName !== 'FORM') return;



        if (form.dataset.swalConfirmed === '1') {

            delete form.dataset.swalConfirmed;

            return;

        }



        const deleteForm = isDeleteForm(form);

        if (deleteForm) {

            e.preventDefault();

            e.stopPropagation();

            e.stopImmediatePropagation();



            Swal.fire({

                title: deleteForm.dataset.title || deleteForm.dataset.swalTitle || 'Are you sure?',

                text: deleteForm.dataset.text || deleteForm.dataset.swalText || 'This action cannot be undone.',

                icon: 'warning',

                showCancelButton: true,

                confirmButtonColor: '#dc2626',

                cancelButtonColor: '#6b7280',

                confirmButtonText: deleteForm.dataset.confirmText || 'Yes, delete it',

                cancelButtonText: 'Cancel',

            }).then(function (result) {

                if (result.isConfirmed) {

                    deleteForm.dataset.swalConfirmed = '1';

                    if (typeof deleteForm.requestSubmit === 'function') {

                        deleteForm.requestSubmit();

                    } else {

                        deleteForm.submit();

                    }

                }

            });

            return;

        }



        const confirmForm = isConfirmForm(form);

        if (confirmForm) {

            e.preventDefault();

            e.stopPropagation();

            e.stopImmediatePropagation();



            Swal.fire({

                title: confirmForm.dataset.title || 'Confirm action?',

                text: confirmForm.dataset.text || '',

                icon: 'question',

                showCancelButton: true,

                confirmButtonColor: '#f97316',

                cancelButtonColor: '#6b7280',

                confirmButtonText: confirmForm.dataset.confirmText || 'Yes, proceed',

                cancelButtonText: 'Cancel',

            }).then(function (result) {

                if (result.isConfirmed) {

                    confirmForm.dataset.swalConfirmed = '1';

                    if (typeof confirmForm.requestSubmit === 'function') {

                        confirmForm.requestSubmit();

                    } else {

                        confirmForm.submit();

                    }

                }

            });

        }

    }, true);



    document.addEventListener('turbo:load', function () { initSwalHandlers(document); });

    document.addEventListener('DOMContentLoaded', function () { initSwalHandlers(document); });

    document.addEventListener('turbo:page-ready', function (e) {

        initSwalHandlers(e.detail.frame || document);

    });



    window.swalConfirm = function (opts) {

        return Swal.fire(Object.assign({

            showCancelButton: true,

            confirmButtonColor: '#f97316',

            cancelButtonColor: '#6b7280',

            confirmButtonText: 'Yes',

            cancelButtonText: 'Cancel',

        }, opts));

    };



    window.swalToast = function (icon, message) {

        Swal.fire({

            icon: icon,

            title: message,

            timer: 3000,

            timerProgressBar: true,

            showConfirmButton: false,

            toast: true,

            position: 'top-end',

        });

    };



    window.removeSpecRow = function (buttonElement) {

        swalConfirm({

            title: 'Remove specification?',

            text: 'Remove this specification row?',

            icon: 'warning',

            confirmButtonText: 'Yes, remove it',

        }).then(function (result) {

            if (result.isConfirmed) {

                const row = buttonElement.closest('.spec-row');

                if (row) row.remove();

            }

        });

    };

})();

</script>

<?php /**PATH C:\xampp\htdocs\mjs-bikes-marketplace\resources\views/partials/sweetalert.blade.php ENDPATH**/ ?>