document.addEventListener('alpine:init', () => {
    Alpine.data('HF_GEO_APP', () => ({
        nonce: hfngGEOData.values.nonce,
        burnNonce: hfngGEOData.values.burnNonce,
        enableGeoIP:hfngGEOData.values.geoip_block_enable,
        geoIpProvider:hfngGEOData.values.geoip_provider,
        geoIPApiKey:hfngGEOData.values.geoip_api_key,
        geoIPBlockedCountries:hfngGEOData.values.geoip_blocked_countries,
        allCountries:hfngGEOData.values.all_countries,
        myResult: '',
        myColor: 'hfng-success',           
        featureEnabled: hfngGEOData.values.feature_enabled,
        checkbox_on: hfngGEOData.values.checkbox_on,
        checkbox_off: hfngGEOData.values.checkbox_off,
        

        saveSettings() {
            fetch(hfngGEOData.ajaxurl, {
                method: 'POST',
                headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                body: new URLSearchParams({
                    action: 'happyfenceng_GEO_save_settings',
                    geoip_block_enable:this.enableGeoIP,
                    geoip_provider:this.geoIpProvider,
                    geoip_api_key:this.geoIPApiKey,
                    geoip_blocked_countries:this.geoIPBlockedCountries,
                    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'){                                          
                        if(res.burn) {                        
                                this.burnNonce = res.burn; 
                        }
                    }  else {
                    this.myColor = 'hfng-error';
                }
                setTimeout(() => this.myResult = '', 2000);
            });
        },
    }))
});
