// ===============================
// FAQ ACCORDION
// ===============================
document.addEventListener('DOMContentLoaded', function() {
    document.querySelectorAll('.reeid-faq-q').forEach(function(btn){
        btn.addEventListener('click', function() {
            var item = btn.closest('.reeid-faq-item');
            if (!item) return;

            var panel = item.querySelector('.reeid-faq-a');
            if (!panel) return;

            var expanded = btn.getAttribute('aria-expanded') === 'true';

            // Only close others in same column
            var col = item.parentNode;
            if (col) {
                col.querySelectorAll('.reeid-faq-item.open').forEach(function(openItem){
                    if (openItem !== item) {
                        openItem.classList.remove('open');
                        var q = openItem.querySelector('.reeid-faq-q');
                        var a = openItem.querySelector('.reeid-faq-a');
                        if (q) q.setAttribute('aria-expanded', 'false');
                        if (a) a.setAttribute('hidden', 'hidden');
                    }
                });
            }

            if (expanded) {
                btn.setAttribute('aria-expanded', 'false');
                panel.setAttribute('hidden', 'hidden');
                item.classList.remove('open');
            } else {
                btn.setAttribute('aria-expanded', 'true');
                panel.removeAttribute('hidden');
                item.classList.add('open');
            }
        });
    });
});

document.addEventListener('DOMContentLoaded', function () {
    var questions = document.querySelectorAll('.reeid-faq-question');
    var answers = document.querySelectorAll('.reeid-faq-answer');
    var search = document.getElementById('reeid-faq-search');
    var openAll = document.getElementById('reeid-open-all');
    var closeAll = document.getElementById('reeid-close-all');

    questions.forEach(function (question) {
        question.addEventListener('click', function () {
            var id = this.getAttribute('data-target');
            var answer = document.getElementById(id);
            var isOpen = answer && answer.style.display === 'block';

            answers.forEach(function (item) {
                item.style.display = 'none';
            });

            questions.forEach(function (item) {
                item.classList.remove('reeid-open');
            });

            if (!isOpen && answer) {
                answer.style.display = 'block';
                this.classList.add('reeid-open');
            }
        });
    });

    if (openAll) {
        openAll.addEventListener('click', function () {
            answers.forEach(function (item) {
                item.style.display = 'block';
            });
            questions.forEach(function (item) {
                item.classList.add('reeid-open');
            });
        });
    }

    if (closeAll) {
        closeAll.addEventListener('click', function () {
            answers.forEach(function (item) {
                item.style.display = 'none';
            });
            questions.forEach(function (item) {
                item.classList.remove('reeid-open');
            });
        });
    }

    if (search) {
        search.addEventListener('input', function () {
            var term = this.value.toLowerCase();

            document.querySelectorAll('.reeid-faq-item').forEach(function (item) {
                item.style.display = item.innerText.toLowerCase().indexOf(term) !== -1 ? '' : 'none';
            });
        });
    }
});

document.addEventListener('DOMContentLoaded', function () {
    function reeidPollProgress() {
        var bar = document.getElementById('reeid-ui-progress-bar');
        var label = document.getElementById('reeid-ui-progress-label');

        if (!bar && !label) {
            return;
        }

        fetch(ajaxurl + '?action=reeid_ui_scan_progress')
            .then(function (r) {
                return r.json();
            })
            .then(function (resp) {
                if (!resp.success) {
                    return;
                }

                var p = resp.data;

                if (!p.running) {
                    return;
                }

                var percent = Math.floor((p.done / p.total) * 100);

                if (bar) {
                    bar.style.width = percent + '%';
                }

                if (label) {
                    label.innerText = p.done + ' / ' + p.total + ' strings';
                }

                setTimeout(reeidPollProgress, 2000);
            });
    }

    reeidPollProgress();
});

jQuery(function ($) {
    $(document).on('change', '#reeid-tr-langselect', function () {
        var code = $(this).val();

        $('.reeid-tr-pane').hide();
        $('.reeid-tr-pane[data-code="' + code + '"]').show();
    });

    $(document).on('click', '#reeid-wc-del-all', function (event) {
        event.preventDefault();

        var $button = $(this);
        var productId = parseInt($button.data('product-id'), 10);
        var actionUrl = String($button.data('action-url') || '');
        var nonce = String($button.data('nonce') || '');
        var message = String($button.data('msg') || 'Delete ALL translations? This cannot be undone.');

        if (!productId || !actionUrl || !nonce) {
            alert('Missing configuration (pid/url/nonce).');
            return;
        }

        if (!window.confirm(message)) {
            return;
        }

        var form = document.createElement('form');
        form.method = 'POST';
        form.action = actionUrl;

        var actionInput = document.createElement('input');
        actionInput.type = 'hidden';
        actionInput.name = 'action';
        actionInput.value = 'reeid_wc_delete_all_translations';
        form.appendChild(actionInput);

        var productInput = document.createElement('input');
        productInput.type = 'hidden';
        productInput.name = 'product_id';
        productInput.value = String(productId);
        form.appendChild(productInput);

        var nonceInput = document.createElement('input');
        nonceInput.type = 'hidden';
        nonceInput.name = '_wpnonce';
        nonceInput.value = nonce;
        form.appendChild(nonceInput);

        document.body.appendChild(form);
        form.submit();
    });
});



// ===============================
// MENU LABEL OVERRIDES — UNSAVED CHANGES WARNING
// ===============================
document.addEventListener('DOMContentLoaded', function () {
    var isDirty = false;
    var selector = 'input[name^="reeid_menu_overrides"]';

    document.querySelectorAll(selector).forEach(function (field) {
        field.addEventListener('input', function () {
            isDirty = true;
        });

        field.addEventListener('change', function () {
            isDirty = true;
        });
    });

    document.addEventListener('submit', function (event) {
        var form = event.target;

        if (!form || !form.querySelector) {
            return;
        }

        if (
            form.querySelector('input[name="reeid_menu_labels_nonce"]') &&
            form.querySelector(selector)
        ) {
            isDirty = false;
        }
    });

    window.addEventListener('beforeunload', function (event) {
        if (!isDirty) {
            return;
        }

        event.preventDefault();
        event.returnValue = '';
    });
});