import {
  getClassTreeByClassName
} from "@/api/system-management/archiveSettings"
import {
  getFondWithPageUser
} from "@/api/system-management/allAdministration";
import {
  getArchiveCtgTree,
} from "@/api/system-management/fileSettings"
import {
  getOneTable,
  deleteObjectAnjuan,
  delectTable,
  saveObject,
  updateObject,
} from "@/api/archives-management/fileManagement/notStoredArchives.js";
import {
  getButtonList
} from "@/api/user"
import {
  message
} from '@/utils/common'
const getDefaultState = () => {
  return {
    pagination: {
      pageSize: 10,
      pageNum: 1,
      total: 0,
    },
    treeData: [],
    tableLoading: false,
    loading: false,
    tableData: [],
    fondsList: [],
    treeList: [],
    treeLoading: false,
    showButtons: {
      add: false,
      del: false,
      edit: false
    },
  }
}
const state = getDefaultState()
const mutations = {
  GET_TREE(state, data) {
    state.treeData = data.list
    state.loading = false
  },
  GET_TABLEDATA(state, data) {
    state.tableData = data.list
    state.pagination.total = data.total
    state.tableLoading = false
  },
  DELECT_TABLE(state, data) {
    message('success', '删除成功', 3000)
  },
  FONDLIST(state, data) {
    state.fondsList = data.list
  },
  GET_TREELIST(state, data) {
    state.treeList = data.list
    state.treeLoading = false
  },
  SAVE_OBJ(state, data) {
    message('success', '新增成功', 3000)
  },
  UPDATA_OBJ(state, data) {
    message('success', '修改成功', 3000)
  },
  GET_BUTTON_LIST(state, data) {
    data.forEach(item => {
      state.showButtons[item.key] = true
    })
  }
}
const actions = {
  async getClassTreeByClassName({
    commit,
    dispatch
  }, data) {
    await getClassTreeByClassName(data).then(res => {
      try {
        commit('GET_TREE', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async getOneTable({
    commit,
    dispatch
  }, data) {
    await getOneTable(data).then(res => {
      try {
        commit('GET_TABLEDATA', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async deleteObjectAnjuan({
    commit,
    dispatch
  }, data) {
    await deleteObjectAnjuan(data).then(res => {
      try {
        commit('DELECT_TABLE', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async getFondWithPageUser({
    commit,
    dispatch
  }, data) {
    await getFondWithPageUser(data).then(res => {
      try {
        commit('FONDLIST', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async getArchiveCtgTree({
    commit,
    dispatch
  }, data) {
    await getArchiveCtgTree(data).then(res => {
      try {
        commit('GET_TREELIST', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async saveObject({
    commit,
    dispatch
  }, data) {
    await saveObject(data).then(res => {
      try {
        commit('SAVE_OBJ', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async updateObject({
    commit,
    dispatch
  }, data) {
    await updateObject(data).then(res => {
      try {
        commit('UPDATA_OBJ', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async getButtonList({
    commit
  }, params) {
    await getButtonList(params).then(res => {
      commit('GET_BUTTON_LIST', res.body.list)
    })
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
