import { computed, ref } from "vue";
import { ElMessage } from "element-plus";
import { getjumpwindow, getfaultread, postjumpwindow, postfaultread, getAlarmConfig } from "/@/api/api.js";
import { async } from "q";

const system = ref({
    enableWarn: false,
    enableSpeak: false
})

export const common = () => {
    const identity = sessionStorage.getItem('identity')
    const username = sessionStorage.getItem('ms_username')
    const notEdit = computed(() => identity == 'user');
    const setenableWarn = async(warn, speak) => {
        system.value = {
            enableWarn: warn,
            enableSpeak: speak
        }
        const res1 = await postjumpwindow(warn)
        const res2 = await postfaultread(speak)
        ElMessage.success("保存成功!");
    };
    const getenableWarn = async() => {
        const res = await getAlarmConfig();
        const enableWarn = res.data.sd.find(ele => {
            return ele.nemCode === "jumpwindow";
        });
        const enableSpeak = res.data.sd.find(ele => {
            return ele.nemCode === "faultread";
        });
        system.value.enableWarn = (!!enableWarn.enable) || true;
        system.value.enableSpeak = (!!enableSpeak.enable) || true;

        // const res1 = await getjumpwindow()
        // system.value.enableWarn = res1.data
        // const res2 = await getfaultread()
        // system.value.enableSpeak = res2.data
    };
    return {
        notEdit,
        username,
        system,
        setenableWarn,
        getenableWarn,
    };
};