import {
  getTree,
} from "@/api/archives-management/fileManagement/notStoredArchives.js";
import {
  compareByField,
  compareByFieldChn,
  getRecordList,
  selectListByMetadataId,
  selectMetadataEntityByClassId
} from "@/api/system-management/categorySetting"
import {
  message
} from '@/utils/common'
const getDefaultState = () => {
  return {
    treeData: [],
    tableData: [],
    treeLoading: false,
    RecordList: [],
    diaTableData: [],
    pattern: ""
  }
}
const state = getDefaultState()
const mutations = {
  GET_TREE(state, data) {
    state.treeData = data.list
    state.treeLoading = false
  },
  GET_RECORD(state, data) {
    state.RecordList = data.nameList
  },
  GET_DIATABLE(state, data) {
    state.diaTableData = data.list
  },
  CN_TABLE(state, data) {
    state.diaTableData = data.ChineseMetadataStandardMatchSetting[0]
  },
  EN_TABLE(state, data) {
    state.diaTableData = data.EnglishMetadataStandardMatchSetting[0]
  },
  GET_TABLEDATA(state, data) {
    state.tableData = data.resultList
    if (data.resultList.length) {
      state.pattern = data.resultList[0].pattern
    } else {
      state.pattern = ""
    }
  },
}
const actions = {
  async getTree({
    commit,
    dispatch
  }, data) {
    await getTree(data).then(res => {
      try {
        commit('GET_TREE', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async getRecordList({
    commit,
    dispatch
  }, data) {
    await getRecordList(data).then(res => {
      try {
        commit('GET_RECORD', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async compareByFieldChn({
    commit,
    dispatch
  }, data) {
    await compareByFieldChn(data).then(res => {
      try {
        commit('CN_TABLE', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async compareByField({
    commit,
    dispatch
  }, data) {
    await compareByField(data).then(res => {
      try {
        commit('EN_TABLE', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async selectListByMetadataId({
    commit,
    dispatch
  }, data) {
    await selectListByMetadataId(data).then(res => {
      try {
        commit('GET_DIATABLE', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async selectMetadataEntityByClassId({
    commit,
    dispatch
  }, data) {
    await selectMetadataEntityByClassId(data).then(res => {
      try {
        commit('GET_TABLEDATA', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
