/* CIX Orbit Academy - Installer JavaScript */
document.addEventListener('DOMContentLoaded', function() {
    document.querySelectorAll('.toggle-password').forEach(function(btn) {
        btn.addEventListener('click', function() {
            var input = this.parentElement.querySelector('input[type="password"], input[type="text"]');
            if (!input) return;
            if (input.type === 'password') {
                input.type = 'text';
                this.innerHTML = '<i class="fas fa-eye-slash"></i>';
            } else {
                input.type = 'password';
                this.innerHTML = '<i class="fas fa-eye"></i>';
            }
        });
    });

    var testBtn = document.getElementById('testDbBtn');
    if (testBtn) {
        testBtn.addEventListener('click', function(e) {
            e.preventDefault();
            var btn = this;
            var originalText = btn.innerHTML;
            btn.innerHTML = '<span class="spinner"></span> Testing...';
            btn.disabled = true;

            var formData = new FormData(document.getElementById('dbForm'));
            formData.append('test_connection', '1');

            fetch('step4.php?ajax=1', { method: 'POST', body: formData })
                .then(function(r) { return r.json(); })
                .then(function(data) {
                    var msg = document.getElementById('dbTestResult');
                    if (data.success) {
                        msg.innerHTML = '<div class="alert alert-success"><i class="fas fa-check-circle"></i> ' + data.message + '</div>';
                    } else {
                        msg.innerHTML = '<div class="alert alert-error"><i class="fas fa-exclamation-circle"></i> ' + data.error + '</div>';
                    }
                    btn.innerHTML = originalText;
                    btn.disabled = false;
                })
                .catch(function() {
                    document.getElementById('dbTestResult').innerHTML = '<div class="alert alert-error"><i class="fas fa-exclamation-circle"></i> Connection test failed. Please check your details.</div>';
                    btn.innerHTML = originalText;
                    btn.disabled = false;
                });
        });
    }

    var confirmPw = document.getElementById('confirm_password');
    if (confirmPw) {
        confirmPw.addEventListener('input', function() {
            var pw = document.getElementById('admin_password');
            this.style.borderColor = pw && this.value !== pw.value ? '#ff4444' : '#00ff41';
        });
    }

    document.querySelectorAll('form[data-validate]').forEach(function(form) {
        form.addEventListener('submit', function(e) {
            var valid = true;
            this.querySelectorAll('[required]').forEach(function(field) {
                if (!field.value.trim()) {
                    field.style.borderColor = '#ff4444';
                    valid = false;
                } else {
                    field.style.borderColor = '';
                }
            });
            if (!valid) {
                e.preventDefault();
                alert('Please fill in all required fields.');
            }
        });
    });

    document.querySelectorAll('.check-item').forEach(function(item, i) {
        item.style.opacity = '0';
        item.style.transform = 'translateX(20px)';
        setTimeout(function() {
            item.style.transition = 'all 0.3s ease';
            item.style.opacity = '1';
            item.style.transform = 'translateX(0)';
        }, 100 + (i * 50));
    });
});
