// ==================================================
// Nuclear Fancy Pants Edition™ — NUCLEAR+++
// File: /assets/js/amai-overview.js
// Description: Overview page JS — license modal, validation, auto-fade notices
// Last Edited: 2026-05-20
// ==================================================
(function ($) {
    'use strict';
    function openModal() {
        $('#am-lic-error').hide().text('');
        $('#am-first-name, #am-email').removeClass('am-lic-input--error');
        $('#amai-license-modal').addClass('show');
        setTimeout(function () { $('#am-first-name').focus(); }, 120);
    }
    function closeModal() {
        $('#amai-license-modal').removeClass('show');
    }
    $(document).ready(function () {
        $('#am-open-license-modal, #am-open-license-modal-2').on('click', openModal);
        $('#am-lic-close, #am-lic-cancel').on('click', closeModal);
        $('#amai-license-modal').on('click', function (e) {
            if (e.target === this) { closeModal(); }
        });
        $(document).on('keydown', function (e) {
            if (e.key === 'Escape') { closeModal(); }
        });
        $('#am-first-name, #am-email, #am-consent').on('input change', function () {
            $(this).removeClass('am-lic-input--error');
            $('#am-lic-error').hide().text('');
        });
        $('#am-lic-submit').on('click', function () {
            var name    = $.trim($('#am-first-name').val());
            var email   = $.trim($('#am-email').val());
            var consent = $('#am-consent').is(':checked');
            var msgs    = [];
            $('#am-first-name, #am-email').removeClass('am-lic-input--error');
            $('#am-lic-error').hide().text('');
            if (!name) {
                $('#am-first-name').addClass('am-lic-input--error');
                msgs.push('first name');
            }
            if (!email || !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
                $('#am-email').addClass('am-lic-input--error');
                msgs.push('a valid email address');
            }
            if (!consent) {
                msgs.push('consent to data collection');
            }
            if (msgs.length > 0) {
                $('#am-lic-error').text('Please provide: ' + msgs.join(', ') + '.').show();
                return;
            }
            var $btn = $('#am-lic-submit');
            $btn.prop('disabled', true)
                .addClass('am-lic-btn-submit--loading')
                .html('<svg class="am-lic-spinner" viewBox="0 0 13 13" fill="none" width="14" height="14"><path d="M11 2.5A5.5 5.5 0 1 0 12 6.5" stroke="currentColor" stroke-width="1.4" stroke-linecap="round"/></svg> Verifying\u2026');
            setTimeout(function () {
                $('#am-lic-form').trigger('submit');
            }, 400);
        });
    });
}(jQuery));