jQuery(document).ready(function($) {
    $('#clear-post-reports').on('click', function() {
        var $btn   = $(this);
        var postId = $btn.data('post-id');
        var $msg   = $('#clear-reports-msg');

        $btn.prop('disabled', true);
        $msg.text('Clearing…');

        $.post(reportPostAdmin.ajax_url, {
            action: 'clear_post_reports',
            post_id: postId,
            nonce:   reportPostAdmin.nonce
        }, function(res) {
            if (res.success) {
                $msg.html('<span style="color:green;">' + res.data + '</span>');
                $('#report-meta-box-content table, #report-meta-box-content p:not(:has(#clear-post-reports))').remove();
                $('#report-meta-box-content').append('<p>No reports yet.</p>');
                $btn.prop('disabled', true);
                // Update the column count without page reload
                $('td.reports.column-reports a').text('0');
				//location.reload();
            } else {
                $msg.html('<span style="color:red;">Error: ' + res.data + '</span>');
                $btn.prop('disabled', false);
            }
        }).fail(function() {
            $msg.html('<span style="color:red;">Request failed.</span>');
            $btn.prop('disabled', false);
        });
    });
});