document.addEventListener('alpine:init', () => {
    Alpine.data('HF_CFC_APP', () => ({
        nonce: hfngCFCData.values.nonce,
        burnNonce: hfngCFCData.values.burnNonce,
        enableGuard: hfngCFCData.values.enable_cf_guard,
        allowed_ips: hfngCFCData.values.allowed_ips,
        checkAdminOnly: hfngCFCData.values.check_admin_only,
        myResult: '',
        myColor: 'hfng-success',   
        saveStatus: '',
        featureEnabled: hfngCFCData.values.feature_enabled,
        checkbox_on: hfngCFCData.values.checkbox_on,
        checkbox_off: hfngCFCData.values.checkbox_off,

        saveSettings() {
            fetch(hfngCFCData.ajaxurl, {
                method: 'POST',
                headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                body: new URLSearchParams({
                    action: 'happyfenceng_CFC_save_settings',
                    enable_cf_guard: this.enableGuard ? this.checkbox_on : this.checkbox_off,
                    allowed_ips: this.allowed_ips,
                    enable_cf_guard_admin_only: this.checkAdminOnly ? this.checkbox_on : this.checkbox_off,
                    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);
            });
        },
    }))
});
