document.addEventListener('alpine:init', () => {
    Alpine.data('HF_SUSQ_APP', () => ({
        nonce: hfngSUSQData.values.nonce,       
        burnNonce: hfngSUSQData.values.burnNonce,
        patternsModel: hfngSUSQData.values.patterns,
        
        myResult: '',
        myColor: 'hfng-success',           
        featureEnabled: hfngSUSQData.values.feature_enabled,
        checkbox_on: hfngSUSQData.values.checkbox_on,
        checkbox_off: hfngSUSQData.values.checkbox_off,

        selectAll() {            
            Object.keys(this.patternsModel).forEach(key => {
                this.patternsModel[key] = true;
            });
        },

        unselectAll() {            
            Object.keys(this.patternsModel).forEach(key => {
                this.patternsModel[key] = false;
            });
        },

        saveSettings() {
            fetch(hfngSUSQData.ajaxurl, {
                method: 'POST',
                headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                body: new URLSearchParams({
                    action: 'happyfenceng_SUSQ_save_settings',
                    susq:JSON.stringify(this.patternsModel),
                    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);
            });
        },



    }))
});

