import {
  getEntity,
  getTree,
  getClass,
  insertYearList,
  getNoList,
  getTableFieldList,
  saveArchiveNoFormat,
  deleteYearScope,
  deleteArchiveNoFormat,
  updateArchiveNoFormat,
  updateYearScope
} from "@/api/system-management/categoryManagement"
import {
  getClassTreeByClassName
} from "@/api/system-management/archiveSettings"
import {
  getFondWithPage
} from "@/api/system-management/allAdministration"
import {
  getFindList,
} from "@/api/system-management/templateSetting"
import {
  message
} from '@/utils/common'
const getDefaultState = () => {
  return {
    EntityList: [], //实体下拉值
    treeData: [], //门类树
    treeLoading: false,
    downTypeList: [],
    ClassData: {},

    //===   five   ===//
    fiveLoading: false,
    fiveTree: [],
    qzList: [],
    fondsname: null,
    fiveTableData: [],
    fiveSelectList: []
  }
}
const state = getDefaultState()
const mutations = {
  GET_TREE(state, data) {
    state.treeData = data.list
  },
  GET_FINDLIST(state, data) {
    state.downTypeList = data.list

  },
  GET_ENTITY(state, data) {
    state.EntityList = data.list[0].tentityType
  },
  GET_CLASS(state, data) {
    state.ClassData = data.classTree
  },
  GET_FIVE_TREE(state, data) {
    state.fiveLoading = false
    state.fiveTree = data.list
  },
  GET_QZ(state, data) {
    state.qzList = data.list
    state.fondsname = data.list[0].fondscode
  },
  ADD_YEAR(state, data) {
    message('success', '新增成功', 3000)
  },
  GET_TABLELIST(state, data) {
    state.fiveTableData = data.list
  },
  GET_SELECT_LIST(state, data) {
    state.fiveSelectList = data.list
  },
  SAVE_DH(state, data) {
    message('success', '保存成功', 3000)
  },
  DEL_YEAR(state, data) {
    message('success', '删除成功', 3000)
  },
  DEL_FORMAT(state, data) {
    message('success', '删除成功', 3000)
  },
  UPDATA_FORMAT(state, data) {
    message('success', '修改成功', 3000)
  },
  UPDATA_YEAR(state, data) {
    message('success', '修改成功', 3000)
  },
}
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 getFindList({
    commit,
    dispatch
  }, data) {
    await getFindList(data).then(res => {
      try {
        commit('GET_FINDLIST', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async getEntity({
    commit,
    dispatch
  }, data) {
    await getEntity(data).then(res => {
      try {
        commit('GET_ENTITY', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async getClass({
    commit,
    dispatch
  }, data) {
    await getClass(data).then(res => {
      try {
        commit('GET_CLASS', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async getClassTreeByClassName({
    commit,
    dispatch
  }, data) {
    await getClassTreeByClassName(data).then(res => {
      try {
        commit('GET_FIVE_TREE', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async getFondWithPage({
    commit,
    dispatch
  }, data) {
    await getFondWithPage(data).then(res => {
      try {
        commit('GET_QZ', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async insertYearList({
    commit,
    dispatch
  }, data) {
    await insertYearList(data).then(res => {
      try {
        commit('ADD_YEAR', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async getNoList({
    commit,
    dispatch
  }, data) {
    await getNoList(data).then(res => {
      try {
        commit('GET_TABLELIST', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async getTableFieldList({
    commit,
    dispatch
  }, data) {
    await getTableFieldList(data).then(res => {
      try {
        commit('GET_SELECT_LIST', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async saveArchiveNoFormat({
    commit,
    dispatch
  }, data) {
    await saveArchiveNoFormat(data).then(res => {
      try {
        commit('SAVE_DH', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async deleteYearScope({
    commit,
    dispatch
  }, data) {
    await deleteYearScope(data).then(res => {
      try {
        commit('DEL_YEAR', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async deleteArchiveNoFormat({
    commit,
    dispatch
  }, data) {
    await deleteArchiveNoFormat(data).then(res => {
      try {
        commit('DEL_FORMAT', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async updateArchiveNoFormat({
    commit,
    dispatch
  }, data) {
    await updateArchiveNoFormat(data).then(res => {
      try {
        commit('UPDATA_FORMAT', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async updateYearScope({
    commit,
    dispatch
  }, data) {
    await updateYearScope(data).then(res => {
      try {
        commit('UPDATA_YEAR', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },


}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
