document.addEventListener('alpine:init', () => {
    Alpine.data('HF_PRI_APP', () => ({
        nonce: hfngPRIData.values.nonce,
        burnNonce: hfngPRIData.values.burnNonce,
        enablePrivacyManagement: hfngPRIData.values.enable_privacy_management,
        topBannerText: hfngPRIData.values.top_banner_text,
        privacyUrl: hfngPRIData.values.privacy_url,
        colorModel: hfngPRIData.values.color,
        textColorModel: hfngPRIData.values.text_color,
        enablePolicyBanner: hfngPRIData.values.enable_policy_banner,
        cookieUrl: hfngPRIData.values.cookie_url,
        enableCookieBanner: hfngPRIData.values.enable_cookie_banner,
        termsUrl: hfngPRIData.values.terms_url,
        enableTermsBanner: hfngPRIData.values.enable_terms_banner,
        showManageLink: hfngPRIData.values.show_manage_link,
        companyName: hfngPRIData.values.company_name,
        ownerEmail: hfngPRIData.values.owner_email,
        treatmentLocation: hfngPRIData.values.treatment_location,
        privacyNoticeWarnings: hfngPRIData.values.privacy_notice_warnings,
        cookieNoticeWarnings: hfngPRIData.values.cookie_notice_warnings,
        termsNoticeWarnings: hfngPRIData.values.terms_notice_warnings,
        
        myResult: '',
        myColor: 'hfng-success',           
        featureEnabled: hfngPRIData.values.feature_enabled,
        checkbox_on: hfngPRIData.values.checkbox_on,
        checkbox_off: hfngPRIData.values.checkbox_off,

        saveSettings() {
            fetch(hfngPRIData.ajaxurl, {
                method: 'POST',
                headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                body: new URLSearchParams({
                    action: 'happyfenceng_PRI_save_settings',
                    enable_privacy_management:this.enablePrivacyManagement,
                    top_banner_text:this.topBannerText,
                    color:this.colorModel,
                    text_color:this.textColorModel,
                    
                    enable_policy_banner:this.enablePolicyBanner,
                    privacy_url:this.privacyUrl,

                    enable_cookie_banner:this.enableCookieBanner,
                    cookie_url:this.cookieUrl,

                    enable_terms_banner:this.enableTermsBanner,
                    terms_url:this.termsUrl,

                    show_manage_link:this.showManageLink,
                    company_name:this.companyName,
                    owner_email:this.ownerEmail,
                    treatment_location:this.treatmentLocation,

                    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; 
                        }
                       
                        this.privacyNoticeWarnings=res.options['privacy_notice_warnings'];
                        this.cookieNoticeWarnings=res.options['cookie_notice_warnings'];
                        this.termsNoticeWarnings=res.options['terms_notice_warnings'];
                    }  else {
                    this.myColor = 'hfng-error';
                }
                setTimeout(() => this.myResult = '', 3000);
            });
        },
    }))
});
