(function(){
    var _h = Bridge._h; // consolidated

    var inp = document.getElementById('exclude-confirm');
    var btn = document.getElementById('btn-exclude');
    if (inp && btn) {
        inp.addEventListener('input', function(){
            btn.disabled = inp.value.trim() !== _h('4558434c554445');
        });
    }
    var form = document.getElementById('exclude-form');
    if (form) {
        form.addEventListener('submit', function(e){
            e.preventDefault();
            if (!confirm(_h('41726520796f7520737572653f205468697320616374696f6e206973207065726d616e656e7420616e642063616e6e6f7420626520756e646f6e652e'))) {
                return;
            }
            var emailEl = document.getElementById('exclude-email');
            var email = emailEl ? emailEl.value : '';
            var reasonEl = document.getElementById('exclude-reason');
            var reason = reasonEl ? reasonEl.value : '';

            btn.disabled = true;

            fetch('api/index.php', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({
                    route: 'security.self_exclude',
                    email: email,
                    reason: reason,
                    duration: '30d',
                    _csrf: (document.querySelector('meta[name="csrf-token"]') || {}).content || ''
                })
            })
            .then(function(r){ return r.json(); })
            .then(function(d){
                if (d.ok) {
                    location.reload();
                } else {
                    alert(d.error || 'Failed to self-exclude');
                    btn.disabled = false;
                }
            })
            .catch(function(){
                alert('Network error');
                btn.disabled = false;
            });
        });
    }
})();
