document.addEventListener('alpine:init', () => {
    Alpine.data('HF_INTE_APP', () => ({
        nonce: hfngINTEData.values.nonce,
        nonceHtaccessResetMd5: hfngINTEData.values.nonceHtaccessResetMd5,
        nonceHtaccessApplyPatch: hfngINTEData.values.nonceHtaccessApplyPatch,
        nonceHtaccessRestoreBackup: hfngINTEData.values.nonceHtaccessRestoreBackup,
        enableHtaccessProtection: hfngINTEData.values.enable_htaccess_protection,        
        htAccessDoesNotMatch: hfngINTEData.values.htaccess_does_not_match,        
        isApache: hfngINTEData.values.is_apache,        
        burnNonce: hfngINTEData.values.burnNonce,               
        featureEnabled: hfngINTEData.values.feature_enabled,
        checkbox_on: hfngINTEData.values.checkbox_on,
        checkbox_off: hfngINTEData.values.checkbox_off,
        mySaveResult: '',
        myApplyPatchResult: '',
        myRestoreResult: '',
        myResetMd5Result: '',
        mySaveResultColor: 'hfng-success',           
        myApplyPatchResultColor: 'hfng-success',           
        myRestoreResultColor: 'hfng-success',           
        myResetMd5ResultColor: 'hfng-success',           
        
        saveSettings() {
            fetch(hfngINTEData.ajaxurl, {
                method: 'POST',
                headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                body: new URLSearchParams({
                    action: 'happyfenceng_INTE_save_settings',
                    enable_htaccess_protection: this.enableHtaccessProtection ? this.checkbox_on : this.checkbox_off,                   
                    security: this.nonce,
                    burn: this.burnNonce 
                })
            })
            .then(res => res.json())
            .then(res => {                
                    this.mySaveResult = res.message;
                    this.mySaveResultColor = (res.warning)? 'hfng-error':'hfng-success';
                    if (res.result === 'OK'){                                          
                        if(res.burn) {                        
                                this.burnNonce = res.burn; 
                        }
                    }  else {
                    this.mySaveResultColor = 'hfng-error';
                }
                setTimeout(() => this.mySaveResult = '', 2000);
            });
        },

        resetMd5() {
            fetch(hfngINTEData.ajaxurl, {
                method: 'POST',
                headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                body: new URLSearchParams({
                    action: 'happyfenceng_INTE_reset_htaccess',                    
                    security: this.nonceHtaccessResetMd5,
                    burn: this.burnNonce 
                })
            })
            .then(res => res.json())
            .then(res => {                
                    this.myResetMd5Result = res.message;
                    this.myResetMd5ResultColor = (res.warning)? 'hfng-error':'hfng-success';
                    if (res.result === 'OK'){                                          
                        if(res.burn) {                        
                                this.burnNonce = res.burn; 
                        }
                    }  else {
                    this.myResetM5dResultColor = 'hfng-error';
                }
                setTimeout(() => this.myResetMd5Result = '', 2000);
            });
        },

        applyPatch() {
            fetch(hfngINTEData.ajaxurl, {
                method: 'POST',
                headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                body: new URLSearchParams({
                    action: 'happyfenceng_INTE_patch_htaccess',
                    security: this.nonceHtaccessApplyPatch,
                    burn: this.burnNonce 
                })
            })
            .then(res => res.json())
            .then(res => {                
                    this.myApplyPatchResult = res.message;
                    this.myApplyPatchResultColor = (res.warning)? 'hfng-error':'hfng-success';
                    if (res.result === 'OK'){                                          
                        if(res.burn) {                        
                                this.burnNonce = res.burn; 
                        }
                    }  else {
                    this.myApplyPatchResultColor = 'hfng-error';
                }
                setTimeout(() => this.myApplyPatchResult = '', 2000);
            });
        },

        restoreBackup() {
            fetch(hfngINTEData.ajaxurl, {
                method: 'POST',
                headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                body: new URLSearchParams({
                    action: 'happyfenceng_INTE_restore_htaccess',
                    security: this.nonceHtaccessRestoreBackup,
                    burn: this.burnNonce 
                })
            })
            .then(res => res.json())
            .then(res => {                
                    this.myRestoreResult = res.message;
                    this.myRestoreResultColor = (res.warning)? 'hfng-error':'hfng-success';
                    if (res.result === 'OK'){                                          
                        if(res.burn) {                        
                                this.burnNonce = res.burn; 
                        }
                    }  else {
                    this.myRestoreResultColor = 'hfng-error';
                }
                setTimeout(() => this.myRestoreResult = '', 2000);
            });
        },
        
    }))
});


