document.addEventListener('DOMContentLoaded', function () {
    var overlay = document.getElementById('imz-report-modal-overlay');
    var closeBtn = document.getElementById('imz-close-report-modal');
    var openBtn = document.getElementById('imz-open-report-modal');
    if (!overlay) return;
    var rcW = {}, rcQ = [];
    function renderRC(id) { var d = document.getElementById(id); var k = (typeof imz_report_ajax_data !== 'undefined' && imz_report_ajax_data.recaptcha_site_key) ? imz_report_ajax_data.recaptcha_site_key : null; if (!d || !k || rcW[id] !== undefined) return; if (typeof grecaptcha !== 'undefined' && grecaptcha && grecaptcha.render) { try { rcW[id] = grecaptcha.render(id, { sitekey: k }); } catch (e) {} } else { if (rcQ.indexOf(id) === -1) rcQ.push(id); } }
    function resetRC(id) { if (rcW[id] !== undefined && typeof grecaptcha !== 'undefined') try { grecaptcha.reset(rcW[id]); } catch (e) {} }
    function getRC(id) { if (rcW[id] !== undefined && typeof grecaptcha !== 'undefined') return grecaptcha.getResponse(rcW[id]); return null; }
    var _ci = setInterval(function () { if (typeof grecaptcha !== 'undefined' && grecaptcha && grecaptcha.render) { clearInterval(_ci); rcQ.forEach(renderRC); rcQ = []; } }, 500);
    var panels = overlay.querySelectorAll('.imz-report-panel');
    function showPanel(n) { panels.forEach(function (p) { p.classList.remove('active'); }); var t = overlay.querySelector('[data-panel="' + n + '"]'); if (t) { t.classList.add('active'); document.getElementById('imz-report-modal-content').scrollTop = 0; if (n === 'report') renderRC('imz-report-recaptcha'); if (n === 'copyright') { var a = overlay.querySelector('.imz-copyright-lvl.active'); if (a) renderRC('imz-copyright-recaptcha-l' + a.getAttribute('data-lvl-panel')); } } }
    overlay.addEventListener('click', function (e) { var b = e.target.closest('[data-goto]'); if (b) { e.preventDefault(); showPanel(b.getAttribute('data-goto')); } });
    var cT = overlay.querySelectorAll('.imz-copyright-tab'), cP = overlay.querySelectorAll('.imz-copyright-lvl');
    cT.forEach(function (t) { t.addEventListener('click', function () { cT.forEach(function (x) { x.classList.remove('active'); }); cP.forEach(function (x) { x.classList.remove('active'); }); t.classList.add('active'); var l = t.getAttribute('data-lvl'); var p = overlay.querySelector('[data-lvl-panel="' + l + '"]'); if (p) p.classList.add('active'); renderRC('imz-copyright-recaptcha-l' + l); }); });
    if (openBtn) openBtn.addEventListener('click', function (e) { e.preventDefault(); overlay.classList.remove('imz-modal-hidden'); showPanel('choice'); });
    function closeModal() { overlay.classList.add('imz-modal-hidden'); resetRC('imz-report-recaptcha'); resetRC('imz-copyright-recaptcha-l1'); resetRC('imz-copyright-recaptcha-l2'); }
    if (closeBtn) closeBtn.addEventListener('click', closeModal);
    overlay.addEventListener('click', function (e) { if (e.target === overlay) closeModal(); });
    var ncb = document.getElementById('report_notify_check'), ew = document.getElementById('imz-email-field-wrapper');
    if (ncb && ew) ncb.addEventListener('change', function () { ew.style.display = this.checked ? 'block' : 'none'; var ei = document.getElementById('reporter_email'); if (ei) ei.required = this.checked; });
    var rf = document.getElementById('imz-report-form'), rm = document.getElementById('imz-report-messages');
    if (rf) rf.addEventListener('submit', function (e) {
        e.preventDefault(); rm.innerHTML = '<p class="imz-message imz-message-warning">A enviar, por favor aguarde...</p>'; var btn = rf.querySelector('button[type="submit"]'); if (btn) btn.disabled = true;
        var fd = new FormData(rf); fd.append('action', 'imz_submit_report');
        if (document.getElementById('imz-report-recaptcha')) { if (typeof grecaptcha === 'undefined' || !grecaptcha) { rm.innerHTML = '<p class="imz-message imz-message-error">Erro ao carregar o reCAPTCHA. Recarregue a página.</p>'; if (btn) btn.disabled = false; return; } var resp = getRC('imz-report-recaptcha'); if (!resp) { rm.innerHTML = '<p class="imz-message imz-message-error">Por favor, complete a verificação "Não sou um robô".</p>'; if (btn) btn.disabled = false; return; } fd.append('g-recaptcha-response', resp); }
        fetch(imz_report_ajax_data.ajax_url, { method: 'POST', body: new URLSearchParams(fd) }).then(function (r) { return r.json(); }).then(function (res) { resetRC('imz-report-recaptcha'); if (res.success) { rf.style.display = 'none'; rm.innerHTML = '<p class="imz-message imz-message-success">' + res.data.message + '</p>'; } else throw new Error(res.data.message || 'Erro.'); }).catch(function (err) { resetRC('imz-report-recaptcha'); rm.innerHTML = '<p class="imz-message imz-message-error">' + err.message + '</p>'; if (btn) btn.disabled = false; });
    });
    function setupDrop(dI, iI, nI) { var d = document.getElementById(dI), i = document.getElementById(iI), n = document.getElementById(nI); if (!d || !i) return; d.addEventListener('click', function () { i.click(); }); d.addEventListener('dragover', function (e) { e.preventDefault(); d.style.borderColor = '#555'; }); d.addEventListener('dragleave', function () { d.style.borderColor = ''; }); d.addEventListener('drop', function (e) { e.preventDefault(); d.style.borderColor = ''; if (e.dataTransfer.files.length) { i.files = e.dataTransfer.files; chkFile(i, n, d); } }); i.addEventListener('change', function () { chkFile(i, n, d); }); }
    function chkFile(i, n, d) { if (i.files && i.files[0]) { var f = i.files[0]; if (f.size > 5242880) { n.textContent = 'Ficheiro demasiado grande (máximo 5MB)'; n.style.color = '#ef4444'; d.classList.remove('has-file'); i.value = ''; return; } n.textContent = '\u2713 ' + f.name; n.style.color = '#10b981'; d.classList.add('has-file'); } }
    setupDrop('imz-drop-id', 'imz-finput-id', 'imz-fname-id'); setupDrop('imz-drop-proof', 'imz-finput-proof', 'imz-fname-proof');
    function handleCS(fI, rI) { var f = document.getElementById(fI); if (!f) return; f.addEventListener('submit', function (e) { e.preventDefault(); var m = document.getElementById('imz-copyright-messages'), b = f.querySelector('button[type="submit"]'); m.innerHTML = '<p class="imz-message imz-message-warning">A enviar, por favor aguarde...</p>'; if (b) b.disabled = true; var fd = new FormData(f); fd.append('action', 'imz_submit_copyright_claim'); if (document.getElementById(rI)) { if (typeof grecaptcha === 'undefined' || !grecaptcha) { m.innerHTML = '<p class="imz-message imz-message-error">Erro ao carregar o reCAPTCHA. Recarregue a página.</p>'; if (b) b.disabled = false; return; } var resp = getRC(rI); if (!resp) { m.innerHTML = '<p class="imz-message imz-message-error">Por favor, complete a verificação "Não sou um robô".</p>'; if (b) b.disabled = false; return; } fd.append('g-recaptcha-response', resp); } fetch(imz_report_ajax_data.ajax_url, { method: 'POST', body: fd }).then(function (r) { return r.json(); }).then(function (res) { resetRC(rI); if (res.success) { f.style.display = 'none'; m.innerHTML = '<p class="imz-message imz-message-success">' + res.data.message + '</p>'; } else throw new Error(res.data.message || 'Erro.'); }).catch(function (err) { resetRC(rI); m.innerHTML = '<p class="imz-message imz-message-error">' + err.message + '</p>'; if (b) b.disabled = false; }); }); }
    handleCS('imz-copyright-form-l1', 'imz-copyright-recaptcha-l1'); handleCS('imz-copyright-form-l2', 'imz-copyright-recaptcha-l2');
});
