import { cacheDictionary } from "@/api/scan/request"

const project_mess = {
  namespaced: true,
  state: {
    datasourceId: "", //数据源id
    protocolType: "", //数据源类型
    dataName: "",
    dictionary: [], // 数据字典
    cacheTime: ''
  },
  mutations: {
    set_datasource(state, val) {
      state.datasourceId = val
    },
    set_protocol_type(state, val) {
      state.protocolType = val
    },
    set_datasource_name(state, val) {
      state.dataName = val
    }
  },
  actions: {
    getDictionary({ state }) {
      // let pa = ["project", "building", "floor", "equipment", "generalSystem", "tenant", "component", "space"];
      let pa = ["project", "building", "floor", "equipment", "system", "component", "space"];
      let firstLevelMap = {
        Project: 'Pj',
        Building: 'Bd',
        Floor: 'Fl',
        Tenant: 'VOTn',
        Equipment: 'Eq',
        GeneralSystem: 'Sy',
        Space: 'Sp',
        Component: 'Ec'
      }
      return new Promise((resolve, reject) => {
        cacheDictionary(pa, res => {
          state.dictionary = res.content.map(t => {
            t.infoPointCode = firstLevelMap[t.infoPointCode];
            return t;
          });
          let date = new Date();
          state.cacheTime = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`;
          resolve(res);
        })
      })
    }
  },
  getters: {
    datasourceId: state => state.datasourceId,
    protocolType: state => state.protocolType,
    dataName: state => state.dataName,
    dictionary: state => state.dictionary,
    cacheTime: state => state.cacheTime
  }
}

export default project_mess;
