document.addEventListener('alpine:init', () => {
    Alpine.data('HF_MLOG_APP', () => ({
        nonceSaveSetting: hfngMLOGData.values.nonce, 
        noncePaging: hfngMLOGData.values.noncePaging,       
        burnNonce: hfngMLOGData.values.burnNonce,
        logType:hfngMLOGData.values.log_type, 
        enabledDb:hfngMLOGData.values.enabled_db,
        enabledSiem:hfngMLOGData.values.enabled_siem,
        siemEndpoint:hfngMLOGData.values.siem_endpoint,
        enabledSeverity:hfngMLOGData.values.enabled_severity,
        enabledFacility:hfngMLOGData.values.enabled_facility,
        allSeverities:hfngMLOGData.values.all_severities,
        allFacilities:hfngMLOGData.values.all_facilities,
        filterSeverity:hfngMLOGData.values.filter_severity,
        filterFacility:hfngMLOGData.values.filter_facility,
        filterDateFrom:hfngMLOGData.values.filter_date_from,
        filterDateTo:hfngMLOGData.values.filter_date_to,
        filterPaged:hfngMLOGData.values.filter_paged,
        filterPerPage:hfngMLOGData.values.filter_per_page,
        totalPages:hfngMLOGData.values.total_pages,
        pageLogs:[],
        myResult: '',
        myColor: 'hfng-success',       
        myShowResult: '',
        myShowColor: 'hfng-success',       
        saveStatus: '',
        featureEnabled: hfngMLOGData.values.feature_enabled,
        checkbox_on: hfngMLOGData.values.checkbox_on,
        checkbox_off: hfngMLOGData.values.checkbox_off,

        saveSettings() {
            fetch(hfngMLOGData.ajaxurl, {
                method: 'POST',
                headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                body: new URLSearchParams({
                    action: 'happyfenceng_MLOG_save_settings',                  
                    log_type:this.logType,
                    siem_endpoint:this.siemEndpoint,
                    all_severities: JSON.stringify(this.allSeverities), 
                    all_facilities: JSON.stringify(this.allFacilities),
                    security: this.nonceSaveSetting,
                    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);
            });
        },

        showLogs() {
            fetch(hfngMLOGData.ajaxurl, {
                method: 'POST',
                headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                body: new URLSearchParams({
                    action: 'happyfenceng_MLOG_paging',                  
                    filter_severity:this.filterSeverity,
                    filter_facility:this.filterFacility,
                    filter_date_from:this.filterDateFrom,
                    filter_date_to:this.filterDateTo,
                    filter_paged:this.filterPaged,
                    filter_per_page:this.filterPerPage,
                    security: this.noncePaging,
                    burn: this.burnNonce 
                })
            })
            .then(res => res.json())
            .then(res => {                
                    this.myShowResult = res.message;
                    this.myShowColor = (res.warning)? 'hfng-error':'hfng-success';
                    if (res.result === 'OK'){                                          
                        if(res.burn) {                        
                                this.burnNonce = res.burn; 
                                this.pageLogs=res.options['logs'];
                                this.totalPages=res.options['total_pages'];
                        }
                    }  else {
                    this.myColor = 'hfng-error';
                }
                setTimeout(() => this.myShowResult = '', 2000);
            });
        },
      
    }))
});

