document.addEventListener('alpine:init', () => {
    Alpine.data('HF_DSEC_APP', () => ({
        nonce: hfngDSECData.values.nonce,
        burnNonce: hfngDSECData.values.burnNonce,
        deleteTablesOnUninstall: hfngDSECData.values.delete_tables_on_uninstall,        
        saveStatus: '',
        myResult: '',
        myColor: 'hfng-success',   
        featureEnabled: hfngDSECData.values.feature_enabled,
        checkbox_on: hfngDSECData.values.checkbox_on,
        checkbox_off: hfngDSECData.values.checkbox_off,
        featureEnabled:hfngDSECData.values.feature_enabled,

        saveSettings() {
            fetch(hfngDSECData.ajaxurl, {
                method: 'POST',
                headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                body: new URLSearchParams({
                    action: 'happyfenceng_DSEC_save_settings',
                    delete_tables_on_uninstall:this.deleteTablesOnUninstall,                  
                    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);
            });
        },
    }))
});
