<script>

    $(document).on('click' , '.mail' , function (e) {
        $('.notify_id').val($(this).data('id'))
        $('.usertype').val($(this).data('usertype'))
        $('.mailForm').attr('action', $(this).data('url'));
    })

    $(document).on('click' , '.sms' , function (e) {
        $('.notify_id').val($(this).data('id'))
        $('.usertype').val($(this).data('usertype'))
        $('.smsForm').attr('action', $(this).data('url'));
    })

    $(document).on('click' , '.notify' , function (e) {

        $('.notify_id').val($(this).data('id'));

        $('.usertype').val($(this).data('usertype'));

        $('.notifyForm').attr('action', $(this).data('url'));
    })

    $(document).on('click' , '.wallet' , function (e) {
        $('.userWallet').val($(this).data('id'))
        $('.usertype').val($(this).data('usertype'))
        $('.wallet-form').attr('action', $(this).data('url'));
    })

    $(document).ready(function(){

        let checkedIds = [];

        $('.checkSingle').on('click', function() {

            $('.checkSingle:checked').each(function() {
                console.log("dddd",2222);
                checkedIds.push($(this).attr('id'));
            });
            
        });
        
        $(document).on('submit','.notify-form',function(e){
            e.preventDefault();
            const url  = $(this).attr('action')

            const form = $(this); // Store the form reference
    
            let old_content = $(".send-notify-button", form).html();

            let data   = $(this).serializeArray();

            let objData={};

            $.each(data, function(i, v) {objData[v.name] = v.value;});

            objData.ids = [...new Set(checkedIds)];

            if(objData.message?.trim().length || objData.titleAr?.trim().length || objData.titleEn?.trim().length || objData.messageAr?.trim().length || objData.messageEn?.trim().length){

                $.ajax({
                url,

                method: 'post',

                data: objData,

                beforeSend: function(){
                    $(".send-notify-button", form).html('<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>').prop('disabled', true); // Access the button inside the form
                    console.log("test" ,$(this))
                    // $(".send-notify-button").html('<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>').attr('disable',true)
                },

                success: function(response){
                    $(".text-danger").remove()

                    $('.store input').removeClass('border-danger')

                    $(".send-notify-button", form).html(old_content).prop('disabled', false);

                    // $(".send-notify-button").html("<%= i18n.__('send') %>").attr('disable',false)

                    Swal.fire({
                        position: 'top-center',
                        icon : 'success',
                        type: 'success',
                        title: '<%= i18n.__("common.sendSuccessMessage") %>',
                        showConfirmButton: false,
                        timer: 1500,
                        confirmButtonClass: 'btn btn-primary',
                        buttonsStyling: false,
                    })
                    
                    setTimeout(function(){
                        window.location.reload()
                    }, 1000);
                },
                
                error: function (xhr) {
                    $(".send-notify-button", form).html(old_content).prop('disabled', false);

                    // $(".send-notify-button").html("<%= i18n.__('send') %>").attr('disable',false)
                    $(".text-danger").remove()
                    $('.store input').removeClass('border-danger')

                    Swal.fire({
                        position: 'top-center',
                        type: 'error',
                        title: xhr.responseJSON.message,
                        showConfirmButton: false,
                        timer: 1500,
                        confirmButtonClass: 'btn btn-danger',
                        buttonsStyling: false,
                    })
                },
            });
            }
           
        });
    });
    
    $(document).ready(function(){
        $(document).on('submit','.wallet-form',function(e){

            e.preventDefault();

            const url   = $(this).attr('action')

            let data    = $(this).serializeArray();

            let objData = {};

            $.each(data, function(i, v) {objData[v.name] = v.value;});

            if(!objData.balance){
                $('.wallet-form').addClass('needs-validation was-validated')
            }
            
            if(objData.balance){

                $.ajax({
                url,

                method: 'post',

                data: objData,
    
                beforeSend: function(){
                    $(".wallet-submit-button").html('<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>').attr('disable',true)
                },
                
                complete: function(){
                    $(".wallet-submit-button").html('<%= i18n.__("common.send") %>').attr('disable',false)
                },

                success: function(response){
                    $(".text-danger").remove()

                    $('.store input').removeClass('border-danger')

                    Swal.fire({
                        position: 'top-center',
                        icon : 'success',
                        type: 'success',
                        title: '<%= i18n.__("common.sendSuccessMessage") %>',
                        showConfirmButton: false,
                        timer: 1500,
                        confirmButtonClass: 'btn btn-primary',
                        buttonsStyling: false,
                    })

                    setTimeout(function(){
                        window.location.reload()
                    }, 1000);
                },

                error: function (xhr) {

                    $(".send-notify-button").html('<%= i18n.__("common.send") %>').attr('disable',false)
                    $(".text-danger").remove()
                    $('.store input').removeClass('border-danger')

                    Swal.fire({
                        position: 'top-center',
                        type: 'error',
                        title: xhr.responseJSON.message,
                        showConfirmButton: false,
                        timer: 1500,
                        confirmButtonClass: 'btn btn-danger',
                        buttonsStyling: false,
                    })
                },
            });
            }
           
        });
    });
    
</script>
