
<script>    
    $('.delete_all_button').hide();
    $(document).on('change','#checkedAll',function(){
        if(this.checked){
            // setTimeout(function (){
            $(".checkSingle").each(function(index, element){
                this.checked = true;
                $('.delete_all_button').show()
            })
            // },500);
        }else{
            $(".checkSingle").each(function(){
                this.checked=false;
                $('.delete_all_button').hide()
            })
        }
    });
    $(document).on('click','.checkSingle',function () {
        if ($(this).is(":checked")){
            var isAllChecked = 0;
            $(".checkSingle").each(function(){
                if(!this.checked)
                    isAllChecked = 1;
            })
            if(isAllChecked == 0){ $("#checkedAll").prop("checked", true); }
            $('.delete_all_button').show()
        }else {
            var count = 0;
            $(".checkSingle").each(function(){
                if(this.checked)
                    count ++;
            })
            if (count > 0 ) {
                $('.delete_all_button').show()
            }else{
                $('.delete_all_button').hide()
            }
            $("#checkedAll").prop("checked", false);
        }
    });
    $('.delete_all_button').on('click', function (e) {
        e.preventDefault()
        Swal.fire({
            title: '<%= i18n.__("common.continueMessage") %>',
            text: "<%= i18n.__('common.deleteConfirmationMessage') %>",
            type: '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 usersIds = [];
                $('.checkSingle:checked').each(function () {
                    var id = $(this).attr('id');
                    usersIds.push(id);
                });
     
                if (usersIds.length > 0) {
                    e.preventDefault();

                        $.ajax({
                        type: "DELETE",
                        url: $(this).data('route'),
                        data: {ids:usersIds},
                        success: function( res ) {
                            console.log("res", res.key);
                            if (res === 'success' || res.key == 'success') {
                                $('.delete_all_button').hide()
                                Swal.fire(
                                    {
                                        position: 'top-start',
                                        type: 'success',
                                        title: '<%= i18n.__("common.deletionSuccessMessage") %>',
                                        showConfirmButton: false,
                                        timer: 1500,
                                        confirmButtonClass: 'btn btn-primary',
                                        buttonsStyling: false,
                                    })
                                $('.checkSingle:checked').each(function () {
                                    $(this).closest('td').parent('tr').animate({
                                        "opacity" : 0.2, //property
                                        height: "toggle",
                                      
                                    });
                                });
                            }
                        },
                        error:function (error){
                            console.log('error', error);
                            Swal.fire(
                                {
                                    position: 'top-start',
                                    type: 'error',
                                    title: error ? `${error?.responseJSON?.message || error.responseJSON ||error.message}` :  '<%= i18n.__("deletionErrorMessage") %>',
                                    showConfirmButton: false,
                                    timer: 1500,
                                    confirmButtonClass: 'btn btn-primary',
                                    buttonsStyling: false,
                                })
                        }
                    });
                   
                }
            }
        })
    });

</script>