import {
  getClassTreeByClassName
} from "@/api/system-management/archiveSettings"
import {
  getFondWithPageUser,
} from "@/api/system-management/allAdministration"
import {
  getArchiveCtgTree,
} from "@/api/system-management/fileSettings"
import {
  getOneTable,
} from "@/api/archives-management/fileManagement/notStoredArchives.js";
import {
  getButtonList
} from "@/api/user"
import {
  selecttargetClassList,
  showFileArchive,
  createArchives,
} from "@/api/preFiling/file-management/filing.js";
import {
  message
} from '@/utils/common'
const getDefaultState = () => {
  return {
    pagination: {
      pageSize: 10,
      pageNum: 1,
      total: 0,
    },
    treeData: [],
    tableLoading: false,
    loading: false,
    tableData: [],
    fondsList: [],
    classList: [],
    treeList: [],
    treeLoading: false,
    dhForm: [],
    showButtons: {
      gd: false,
    },
  }
}
const state = getDefaultState()
const mutations = {
  GET_BUTTON_LIST(state, data) {
    data.forEach(item => {
      state.showButtons[item.key] = true
    })
  },
  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
  },
  GET_FORM(state, data) {
    data.list.forEach(item => {
      item.value = ""
    })
    state.dhForm = data.list
  },
  GET_CLASS(state, data) {
    state.classList = data.list.filter(item => item)
  },
  FONDLIST(state, data) {
    state.fondsList = data.list
  },
  CREATER(state, data) {
    message('success', '归档成功', 2000)
  },
}
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 showFileArchive({
    commit,
    dispatch
  }, data) {
    await showFileArchive(data).then(res => {
      try {
        commit('GET_FORM', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async selecttargetClassList({
    commit,
    dispatch
  }, data) {
    await selecttargetClassList(data).then(res => {
      try {
        commit('GET_CLASS', 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 createArchives({
    commit,
    dispatch
  }, data) {
    await createArchives(data).then(res => {
      try {
        commit('CREATER', 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
}
