document.addEventListener('alpine:init', () => {
    Alpine.data('HF_CLISEC_APP', () => ({
        nonce: hfngCLISECData.values.nonce,
        burnNonce: hfngCLISECData.values.burnNonce,
        wpcliSecPass:hfngCLISECData.values.wpcli_secpass,       
        myResult: '',
        myColor: 'hfng-success',           
        featureEnabled: hfngCLISECData.values.feature_enabled,
        checkbox_on: hfngCLISECData.values.checkbox_on,
        checkbox_off: hfngCLISECData.values.checkbox_off,
        

        saveSettings() {
            fetch(hfngCLISECData.ajaxurl, {
                method: 'POST',
                headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                body: new URLSearchParams({
                    action: 'happyfenceng_CLISEC_save_settings',                    
                    security: this.nonce,
                    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'){
                        this.wpcliSecPass=res.options['newpass'];                                          
                        if(res.burn) {                        
                                this.burnNonce = res.burn; 
                        }
                    }  else {
                    this.myColor = 'hfng-error';
                }
                setTimeout(() => this.myResult = '', 2000);
            });
        },
    }))
});
