const { __ } = wp.i18n;

jQuery(".elex-multi-vendor-for-woocommerce-review-admin-reply-open-btn").click(function() {

    jQuery("#elex-multi-vendor-for-woocommerce-review-admin-reply").addClass("active");
    
     var comment_id=jQuery(this).data("comment-id");
     var comment_post_id=jQuery(this).data("comment-post-id");

    jQuery("#comment-id-for-reply").val(comment_id);
    jQuery("#comment-post-id-for-reply").val(comment_post_id);
});

jQuery(".elex-multi-vendor-for-woocommerce-review-admin-reply-close-btn").click(function() {
    jQuery("#elex-multi-vendor-for-woocommerce-review-admin-reply").removeClass("active");
});
jQuery(".elex-multi-vendor-for-woocommerce-review-reply-form-send-btn").click(function (e){
    e.preventDefault();
    var link= elex_mv_review_ajaxurl_object.ajax_url;
    var formData;
    link+="?action=elex_mv_review_reply";
    formData = new FormData(document.getElementById('elex-multi-vendor-for-woocommerce-review-reply-form'));

    jQuery.ajax({
        url:link,
        type:'POST',
        data: formData,
        enctype:'multipart/form-data',
        processData:false,
        contentType:false,
        success:function(result){
                jQuery('.innertext').html(__('Comment Saved Successfully !','elex-multi-vendor-for-woocommerce'));
            if(result.success){
                var saved=document.getElementById('comment_success_toast');
                var toast = new bootstrap.Toast(saved);
                toast.show();
                setTimeout(function(){
                    window.location.reload();
                        }, 1000);
                jQuery("#elex-multi-vendor-for-woocommerce-review-admin-reply").removeClass("active");
                jQuery("#floatingTextarea2").val(null);

            }
        },
        error: function(){
            jQuery('.innertext_incorrect').html(__('Failed to Save the Comment','elex-multi-vendor-for-woocommerce'));
            var error=document.getElementById('comment_error_toast');
            var toast = new bootstrap.Toast(error);
            toast.show();
            setTimeout(function(){
                window.location.reload();
                    }, 1000);
            jQuery("#elex-multi-vendor-for-woocommerce-review-admin-reply").removeClass("active");
            jQuery("#floatingTextarea2").val(null);
        }
        
    });
});

jQuery('.elex-multi-vendor-for-woocommerce-review-admin-approve-btn').on('click', function(e){
    e.preventDefault();
    var link= elex_mv_review_ajaxurl_object.ajax_url;
    var commentId = jQuery(this).data('comment-id');
    var commentPostId = jQuery(this).data('comment-post-id');
    link+="?action=elex_mv_approve_review";
    jQuery.ajax({
        type: 'POST',
        url: link,
        data: { 
            comment_id: commentId,
            comment_post_id: commentPostId,
            nonce: elex_mv_review_ajaxurl_object.nonce
        },
        success: function(result){
            // Handle success response
            jQuery('.innertext').html(__('Comment Approved Successfully !','elex-multi-vendor-for-woocommerce'));
            if(result.success){
                var saved=document.getElementById('comment_success_toast');
                var toast = new bootstrap.Toast(saved);
                toast.show();
                setTimeout(function(){
                    window.location.reload();
                        }, 1000);
               

            }
        },
        error: function(){
            // Handle error
            jQuery('.innertext_incorrect').html(__('Failed to Approve the review','elex-multi-vendor-for-woocommerce'));
            var error=document.getElementById('comment_error_toast');
            var toast = new bootstrap.Toast(error);
            toast.show();
            setTimeout(function(){
                window.location.reload();
                    }, 1000);
        }
    });
});

jQuery(".elex-multi-vendor-for-woocommerce-review-admin-edit-open-btn").click(function() {

    jQuery("#elex-multi-vendor-for-woocommerce-review-admin-edit").addClass("active");

    var reply_id=jQuery(this).data("reply-id");
    jQuery("#comment-id-for-edit").val(reply_id);
    var reply_content=jQuery(this).data("reply-content");
    
    jQuery("#floatingTextarea4").val(reply_content);
    
});

jQuery(".elex-multi-vendor-for-woocommerce-review-admin-edit-close-btn").click(function() {
    jQuery("#elex-multi-vendor-for-woocommerce-review-admin-edit").removeClass("active");
});

jQuery(".elex-multi-vendor-for-woocommerce-review-edit-form-send-btn").click(function (e){
    e.preventDefault();
    var link= elex_mv_review_ajaxurl_object.ajax_url;
    var formData;
    link+="?action=elex_mv_review_edit";
    formData = new FormData(document.getElementById('elex-multi-vendor-for-woocommerce-review-edit-form'));

    jQuery.ajax({
        url:link,
        type:'POST',
        data: formData,
        enctype:'multipart/form-data',
        processData:false,
        contentType:false,
        success:function(result){
            jQuery('.innertext').html(__('Comment Updated Successfully !','elex-multi-vendor-for-woocommerce'));
            if(result.success){
                var saved=document.getElementById('comment_success_toast');
                var toast = new bootstrap.Toast(saved); 
                toast.show();
                setTimeout(function(){
                    window.location.reload();
                        }, 1000);
            jQuery("#elex-multi-vendor-for-woocommerce-review-admin-edit").removeClass("active");
            jQuery("#floatingTextarea4").val(null);
            }
        },
        error: function(){
            
            jQuery('.innertext_incorrect').html(__('Failed to Update the Comment','elex-multi-vendor-for-woocommerce'));
            var error=document.getElementById('comment_error_toast');
            var toast = new bootstrap.Toast(error);
            toast.show();
            setTimeout(function(){
                window.location.reload();
                    }, 1000);
            jQuery("#elex-multi-vendor-for-woocommerce-review-admin-edit").removeClass("active");
            jQuery("#floatingTextarea4").val(null);
        }
        
    });
});

jQuery(".elex-multi-vendor-for-woocommerce-review-admin-delete-btn").click(function() {

    var link= elex_mv_review_ajaxurl_object.ajax_url;
    link+="?action=elex_mv_review_delete";
    var reply_id=jQuery(this).data("reply-id");
    var nonce= elex_mv_review_ajaxurl_object.nonce;
    var del=document.getElementById('delete_toast');        
    var toast = new bootstrap.Toast(del); 
    toast.show();
    jQuery("#confirm-delete").click(function(e){
        jQuery.ajax({
            url:link,
            type:'POST',   
            data:{reply_id, nonce },
            success:function(result){
 
                window.location.reload();
                
            },
            error: function(){
                
                window.location.reload();
                
            }
            
        });
    })
 
});
