/* ==================================================
   Nuclear Fancy Pants Edition™ -- NUCLEAR+++
   File: /home/ploi/automarketer-ai.com/public/wp-content/plugins/automarketer-ai/assets/js/amai-comment-form.js
   Description: Frontend comment form submission handler
   Last Edited: 2026-05-15
   ================================================== */

(function () {
    'use strict';

    function init() {
        var wrap = document.getElementById('amai-comment-form-wrap');
        if (!wrap) return;

        var btn    = document.getElementById('amai-cf-submit');
        var notice = document.getElementById('amai-cf-notice');

        if (!btn || !notice) return;

        btn.addEventListener('click', function () {
            var name    = (document.getElementById('amai-cf-name') || {}).value || '';
            var email   = (document.getElementById('amai-cf-email') || {}).value || '';
            var content = (document.getElementById('amai-cf-content') || {}).value || '';
            var website = (document.getElementById('amai-cf-website') || {}).value || '';

            name    = name.trim();
            email   = email.trim();
            content = content.trim();

            if (name === '' || email === '' || content === '') {
                showNotice('error', 'Please fill in all required fields.');
                return;
            }

            btn.disabled = true;
            btn.textContent = 'Submitting...';
            clearNotice();

            var data = new FormData();
            data.append('action',           amaiCommentForm.action);
            data.append('nonce',            amaiCommentForm.nonce);
            data.append('post_id',          amaiCommentForm.postId);
            data.append('amai_cf_name',     name);
            data.append('amai_cf_email',    email);
            data.append('amai_cf_content',  content);
            data.append('amai_cf_website',  website);

            fetch(amaiCommentForm.ajaxUrl, {
                method: 'POST',
                credentials: 'same-origin',
                body: data
            })
            .then(function (res) { return res.json(); })
            .then(function (json) {
                if (json.success) {
                    showNotice('success', json.data.message || 'Thank you for your comment!');
                    // Clear the form fields.
                    var nameEl    = document.getElementById('amai-cf-name');
                    var emailEl   = document.getElementById('amai-cf-email');
                    var contentEl = document.getElementById('amai-cf-content');
                    if (nameEl)    nameEl.value    = '';
                    if (emailEl)   emailEl.value   = '';
                    if (contentEl) contentEl.value = '';
                    btn.disabled = false;
                    btn.textContent = wrap.querySelector('.amai-comment-form-title').textContent.indexOf('Review') !== -1
                        ? 'Submit Review'
                        : 'Post Comment';
                } else {
                    var msg = (json.data && json.data.message) ? json.data.message : 'Something went wrong. Please try again.';
                    showNotice('error', msg);
                    btn.disabled = false;
                    btn.textContent = wrap.querySelector('.amai-comment-form-title').textContent.indexOf('Review') !== -1
                        ? 'Submit Review'
                        : 'Post Comment';
                }
            })
            .catch(function () {
                showNotice('error', 'Something went wrong. Please try again.');
                btn.disabled = false;
                btn.textContent = 'Post Comment';
            });
        });

        function showNotice(type, message) {
            notice.className = 'amai-cf-notice amai-cf-notice--' + type;
            notice.textContent = message;
            notice.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
        }

        function clearNotice() {
            notice.className = 'amai-cf-notice';
            notice.textContent = '';
        }
    }
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', init);
    } else {
        init();
    }
}());