document.addEventListener('alpine:init', () => {
    Alpine.data('HF_UPG_APP', () => ({
        nonce: hfngUPGData.values.nonce,       
        burnNonce: hfngUPGData.values.burnNonce,
        enableGuard:hfngUPGData.values.enable_guard,
        allowedUploadExtensions:hfngUPGData.values.allowed_upload_extensions,
        
        myResult: '',
        myColor: 'hfng-success',           
        featureEnabled: hfngUPGData.values.feature_enabled,
        checkbox_on: hfngUPGData.values.checkbox_on,
        checkbox_off: hfngUPGData.values.checkbox_off,        

        saveSettings() {
            fetch(hfngUPGData.ajaxurl, {
                method: 'POST',
                headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                body: new URLSearchParams({
                    action: 'happyfenceng_UPG_save_settings',
                    enable_guard:this.enableGuard,
                    allowed_upload_extensions:this.allowedUploadExtensions,
                    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);
            });
        },



    }))
});

