document.addEventListener('alpine:init', () => {
    Alpine.data('HF_MFA_APP', () => ({
        nonceSaveSetting: hfngMFAData.values.nonce,
        nonceChUser:hfngMFAData.values.nonceChUser,
        burnNonce: hfngMFAData.values.burnNonce,
        allPahes: hfngMFAData.values.allPages,
        enableMFA: hfngMFAData.values.mfa_enabled,
        savedRedirect: hfngMFAData.values.redirect_page,
        mfaForceAdmin:hfngMFAData.values.mfa_force_admin,
        mfaDisableAdmin:hfngMFAData.values.mfa_disable_admin,
        showTable:false,
        users:hfngMFAData.values.users_table,
        myResult: '',
        myColor: 'hfng-success',       
        saveStatus: '',
        featureEnabled: hfngMFAData.values.feature_enabled,
        checkbox_on: hfngMFAData.values.checkbox_on,
        checkbox_off: hfngMFAData.values.checkbox_off,

        saveSettings() {
            fetch(hfngMFAData.ajaxurl, {
                method: 'POST',
                headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                body: new URLSearchParams({
                    action: 'happyfenceng_MFA_save_settings',
                    mfa_enabled: this.enableMFA ? this.checkbox_on : this.checkbox_off,
                    redirect_page:this.savedRedirect,
                    mfa_force_admin:this.mfaForceAdmin,
                    mfa_disable_admin:this.mfaDisableAdmin,
                    security: this.nonceSaveSetting,
                    burn: this.burnNonce 
                })
            })
            .then(res => res.json())
            .then(res => {                
                    this.myResult = res.message;
                    this.myColor = (res.warning)? 'hfng-error':'hfng-success';
                    if (res.result === 'OK'){                                          
                        if(res.burn) {                        
                                this.burnNonce = res.burn; 
                        }
                    }  else {
                    this.myColor = 'hfng-error';
                }
                setTimeout(() => this.myResult = '', 2000);
            });
        },

        resetMfa(userId) {
            if (confirm( hfngMFAData.texts.confirm_change)) {
            
                this.myColor = 'hfng-success';
                fetch(hfngMFAData.ajaxurl, {
                    method: 'POST',
                    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                    body: new URLSearchParams({
                        action: 'happyfenceng_MFA_chuser',
                        user_id: userId,
                        security: this.nonceChUser,
                        burn: this.burnNonce 
                    })
                })
                .then(res => res.json())
                .then(res => {                
                    this.myIPResult = res.message;
                    this.myIPColor = (res.warning)? 'hfng-error':'hfng-success';
                    if (res.result === 'OK'){                    
                        
                        if(res.burn) {                        
                                this.burnNonce = res.burn; 
                        } 
                    } else {
                        this.myColor = 'hfng-error';
                    }       
                    setTimeout(() => this.myIPResult = '', 2000);                                         
                });
            }
        }
    }))
});

