document.addEventListener('alpine:init', () => {
    Alpine.data('HF_RESH_APP', () => ({
        nonce: hfngRESHData.values.nonce,
        nonceTrust: hfngRESHData.values.nonce_trust,
        nonceDelete: hfngRESHData.values.nonce_delete,
        nonceQuarantine: hfngRESHData.values.nonce_quarantine,
        burnNonce: hfngRESHData.values.burnNonce,
        enableShellScanner: hfngRESHData.values.enable_shell_scanner,
        enableScanWpContent: hfngRESHData.values.enable_scan_wpcontent,
        enableScanPlugin: hfngRESHData.values.enable_scan_plugins,
        enableScanMuPlugin: hfngRESHData.values.enable_scan_mu_plugins,
        enableScanUploads: hfngRESHData.values.enable_scan_uploads,
        enableScanLogs: hfngRESHData.values.enable_scan_logs,
        customScanSubdirs: hfngRESHData.values.custom_scan_subdirs,
        quarantineFiles: hfngRESHData.values.quarantine_files,

        
       
        
        myResult: '',
        myColor: 'hfng-success',           
        featureEnabled: hfngRESHData.values.feature_enabled,
        checkbox_on: hfngRESHData.values.checkbox_on,
        checkbox_off: hfngRESHData.values.checkbox_off,

        saveSettings() {
            fetch(hfngRESHData.ajaxurl, {
                method: 'POST',
                headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                body: new URLSearchParams({
                    action: 'happyfenceng_RESH_save_settings',
                    enable_shell_scanner:this.enableShellScanner,
                    enable_scan_wpcontent:this.enableScanWpContent,
                    enable_scan_plugins:this.enableScanPlugin,
                    enable_scan_mu_plugins:this.enableScanMuPlugin,
                    enable_scan_uploads: this.enableScanUploads,
                    enable_scan_logs:this.enableScanLogs,
                    custom_scan_subdirs:this.customScanSubdirs,
                    
                    
                    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);
            });
        },

         doTrust(current_id) {
            fetch(hfngRESHData.ajaxurl, {
                method: 'POST',
                headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                body: new URLSearchParams({
                    action: 'happyfenceng_RESH_trust',
                    id: current_id,
                    cookie_url:this.cookieUrl,
                    security: this.nonceTrust,
                    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);
            });
        },

         doDelete(current_id) {
            fetch(hfngRESHData.ajaxurl, {
                method: 'POST',
                headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                body: new URLSearchParams({
                    action: 'happyfenceng_RESH_delete',
                    id:current_id,
                    cookie_url:this.cookieUrl,
                    security: this.nonceDelete,
                    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);
            });
        },

         doQuarantine(current_id) {
            fetch(hfngRESHData.ajaxurl, {
                method: 'POST',
                headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                body: new URLSearchParams({
                    action: 'happyfenceng_RESH_quarantine',
                    id:current_id,
                    cookie_url:this.cookieUrl,
                    security: this.nonceQuarantine,
                    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);
            });
        },
    }))
});
