document.addEventListener('alpine:init', () => {
    Alpine.data('HF_PF_APP', () => ({
        nonce: hfngPFData.values.nonce,
        burnNonce: hfngPFData.values.burnNonce,
        enablePostFilter:hfngPFData.values.enable_post_filter,
        enableUpdateOptionFilter:hfngPFData.values.enable_update_option_filter,
        enableRestPreDispatchFilter:hfngPFData.values.enable_rest_pre_dispatch_filter,
        myResult: '',
        myColor: 'hfng-success',           
        featureEnabled: hfngPFData.values.feature_enabled,
        checkbox_on: hfngPFData.values.checkbox_on,
        checkbox_off: hfngPFData.values.checkbox_off,

        saveSettings() {
            fetch(hfngPFData.ajaxurl, {
                method: 'POST',
                headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                body: new URLSearchParams({
                    action: 'happyfenceng_PF_save_settings',
                    enable_post_filter:this.enablePostFilter,
                    enable_update_option_filter:this.enableUpdateOptionFilter,
                    enable_rest_pre_dispatch_filter:this.enableRestPreDispatchFilter,
                    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);
            });
        },
    }))
});
