jQuery(document).ready(function($) {
    $('.report-post-link').on('click', function(e) {
        e.preventDefault();
        var postId = $(this).data('post-id');
        $('#report-post-id').val(postId);
        $('#report-modal').show();
    });

    $('.report-close, .report-modal').on('click', function(e) {
        if (e.target === this) {
            $('#report-modal').hide();
            $('#report-form')[0].reset();
            $('#report-message').empty();
        }
    });

    $('#report-form').on('submit', function(e) {
        e.preventDefault();
        var formData = $(this).serialize() + '&action=report_post&nonce=' + reportPost.nonce;

        $.post(reportPost.ajax_url, formData, function(res) {
            if (res.success) {
                $('#report-message').html('<span style="color:green;">' + res.data + '</span>');
                setTimeout(function(){ $('#report-modal').hide(); }, 2000);
            } else {
                $('#report-message').html('<span style="color:red;">Error: ' + res.data + '</span>');
            }
        });
    });
});