/**
 * Pay4All for TWINT - wizard admin page runtime.
 *
 * Consolidates every small piece of DOM wiring that used to live as inline
 * `<script>` blocks inside src/Backend/ConfigPage.php:
 *
 *   - certificate upload label swap + password reveal toggle (step 2)
 *   - gateway on/off checkbox that submits the toggle form and shows the
 *     "please wait" overlay while the request is in flight (step 5)
 *
 * All handlers guard on element presence so this file is safe to enqueue on
 * every wizard step even when the target nodes are not rendered.
 */
(function () {
    'use strict';

    document.addEventListener('DOMContentLoaded', function () {
        wireCertificateUpload();
        wireGatewayToggle();
    });

    function wireCertificateUpload() {
        var input = document.getElementById('t4a_twint_cert_file');
        if (input) {
            var label = document.querySelector('.t4a-upload-filename');
            if (label) {
                input.addEventListener('change', function () {
                    var hasFile = !!(this.files && this.files[0]);
                    label.textContent = hasFile ? this.files[0].name : label.getAttribute('data-empty');
                    label.classList.toggle('has-file', hasFile);
                });
            }
        }

        var pwInput = document.getElementById('t4a_twint_settings_certpw');
        var toggle = document.getElementById('t4a_certpw_toggle');
        var eyeShow = document.getElementById('t4a_certpw_eye_show');
        var eyeHide = document.getElementById('t4a_certpw_eye_hide');
        if (pwInput && toggle) {
            toggle.addEventListener('click', function () {
                var isHidden = pwInput.type === 'password';
                pwInput.type = isHidden ? 'text' : 'password';
                if (eyeShow) { eyeShow.style.display = isHidden ? 'none' : ''; }
                if (eyeHide) { eyeHide.style.display = isHidden ? '' : 'none'; }
            });
        }
    }

    function wireGatewayToggle() {
        var cb = document.getElementById('t4a-gateway-checkbox');
        var form = document.getElementById('t4a-toggle-gateway-form');
        var overlay = document.getElementById('t4a-twint-overlay');
        if (!cb || !form || !overlay) { return; }
        cb.addEventListener('change', function () {
            overlay.style.display = 'flex';
            form.submit();
        });
    }
})();
