<script>

    $(document).on("click", ".delete-row", function (e) {
        e.preventDefault();
        let deleteCount = $(this).data('delete');

        Swal.fire({
            title: '<%= i18n.__("common.continueMessage") %>',
            text: '<%= i18n.__("common.deleteConfirmationMessage") %>',
            icon: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: '<%= i18n.__("common.confirm") %>',
            confirmButtonClass: 'btn btn-primary',
            cancelButtonText: '<%= i18n.__("common.cancel") %>',
            cancelButtonClass: 'btn btn-danger ml-1',
            buttonsStyling: false,
        }).then((result) => {
            if (result.value) {
                var id = $(this)[0].attributes['id'].nodeValue;

                const isLastRow = $(".delete_row").length === 1 || $(".notification-row").length === 1 // Check if it's the last row

                $.ajax({
                    type: "delete",
                    url: $(this).data("url"),
                    data: { ids: [id] },
                    success: function (msg) {

                        Swal.fire({
                            position: "top-center",
                            icon: 'success',
                            title: '<%= i18n.__("common.deletionSuccessMessage") %>',
                            showConfirmButton: false,
                            timer: 1500,
                            confirmButtonClass: "btn btn-primary",
                            buttonsStyling: false,
                        }).then(() => {
                            if (isLastRow) window.location.reload();
                        });
                        $("#tr" + id).animate(
                            {
                                opacity: 0.25,
                                height: "toggle",
                            },
                            function () {
                                $("#tr" + id).remove();
                            }
                        );

                        if ($("#tr" + id).closest('tr').css('display') !== 'none') {
                            $("#tr" + id).closest('tr').fadeTo(600, 0.25, function () {
                                $("#tr" + id).css('display', 'none');
                            });
                        }

                        $(`.${deleteCount}`).each(function () {
                            const currentCount = parseInt($(this).text());
                            $(this).text(currentCount - 1);
                        });
                        window.location.reload();
                    },
                    error: function (err) {
                        Swal.fire({
                            icon: 'error',
                            title: `${err?.responseJSON?.message || err.responseJSON || err.message}`,
                            showConfirmButton: false,
                            timer: 1500
                        })
                    },
                });
            }
        });
    });



    $(document).on('click', '.delete-one', function (e) {


        e.preventDefault();
        Swal.fire({
            title: '<%= i18n.__("common.continueMessage") %>',
            text: '<%= i18n.__("common.deleteConfirmationMessage") %>',
            icon: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: '<%= i18n.__("common.confirm") %>',
            confirmButtonClass: 'btn btn-primary',
            cancelButtonText: '<%= i18n.__("common.cancel") %>',
            cancelButtonClass: 'btn btn-danger ml-1',
            buttonsStyling: false,
        }).then((result) => {
            if (result.value) {

                var id = $(this)[0].attributes['id'].nodeValue;

                $.ajax({
                    type: "delete",
                    url: $(this).data("url"),
                    data: { ids: [id] },
                    success: function (res) {
                        Swal.fire({
                            position: 'top-center',
                            icon: 'success',
                            title: '<%= i18n.__("common.deletionSuccessMessage") %>',
                            showConfirmButton: false,
                            timer: 1500,
                            confirmButtonClass: 'btn btn-primary',
                            buttonsStyling: false,
                        });

                        const currentUrl = window.location.pathname;
                        const urlSegments = currentUrl.split('/');

                        const newUrl = `/${urlSegments[1]}/${urlSegments[2]}/all`;
                        window.location.href = newUrl;

                    },
                    error: function (err) {
                        Swal.fire({
                            icon: 'error',
                            title: `${err?.responseJSON?.message || err.responseJSON || err.message}`,
                            showConfirmButton: false,
                            timer: 1500
                        })
                    },
                });
            }
        });
    });


</script>
