import {
  getFondWithPage,
} from "@/api/system-management/allAdministration"
import {
  getFindList,
} from "@/api/system-management/templateSetting"
import {
  getYearList,
  getArchiveCtgTree,
  addYearScope,
  updateYearScope,
  saveArchiveCtg,
  deleteYearScope,
  updateArchiveCtg,
  delArchiveCtg,
  saveArchiveScope,
  getTableList,
  updateArchiveScope,
  deleteArchiveScope
} from "@/api/system-management/fileSettings"
import {
  message
} from "@/utils/common"
const getDefaultState = () => {
  return {
    fondsname: '',
    qzList: [],
    yearList: [],
    filingYear: '',
    treeList: [],
    treeLoading: false,
    findList: [],
    pagination: {
      pageSize: 10,
      pageNum: 1,
      total: 0,
    },
    tableData: []
  }
}
const state = getDefaultState()
const mutations = {
  GET_QZ(state, data) {
    state.qzList = data.list
    state.fondsname = data.list[0].fondscode
  },
  GET_YEARLIST(state, data) {
    state.yearList = data.list
    state.filingYear = data.list[0].yearScope
  },
  GET_TREELIST(state, data) {
    state.treeList = data.list
    state.treeLoading = false
  },
  ADD_YEAR(state, data) {
    message('success', data.msg, 3000)
  },
  UPDATA_YEAR(state, data) {
    message('success', data.msg, 3000)
  },
  DEL_YEAR(state, data) {
    message('success', '删除成功', 3000)
  },
  SAVE_ARCHIVE(state, data) {
    message('success', '新增成功', 3000)
  },
  UPDATA_ARCHIVE(state, data) {
    message('success', '修改成功', 3000)
  },
  DEL_ARCHIVE(state, data) {
    message('success', '删除成功', 3000)
  },
  FINDLIST(state, data) {
    state.findList = data.list
  },
  SAVE_ARCH(state, data) {
    message('success', '新增成功', 3000)
  },
  UPDATA_ARCH(state, data) {
    message('success', '修改成功', 3000)
  },
  DEL_ARCH(state, data) {
    message('success', '删除成功', 3000)
  },
  GET_TABLE(state, data) {
    state.tableData = data.list
    state.pagination.total = data.total
  },
}
const actions = {
  async getFondWithPage({
    commit,
    dispatch
  }, data) {
    await getFondWithPage(data).then(res => {
      try {
        commit('GET_QZ', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async getYearList({
    commit,
    dispatch
  }, data) {
    await getYearList(data).then(res => {
      try {
        commit('GET_YEARLIST', 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 addYearScope({
    commit,
    dispatch
  }, data) {
    await addYearScope(data).then(res => {
      try {
        commit('ADD_YEAR', res)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async updateYearScope({
    commit,
    dispatch
  }, data) {
    await updateYearScope(data).then(res => {
      try {
        commit('UPDATA_YEAR', res)
      } 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 saveArchiveCtg({
    commit,
    dispatch
  }, data) {
    await saveArchiveCtg(data).then(res => {
      try {
        commit('SAVE_ARCHIVE', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async updateArchiveCtg({
    commit,
    dispatch
  }, data) {
    await updateArchiveCtg(data).then(res => {
      try {
        commit('UPDATA_ARCHIVE', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async delArchiveCtg({
    commit,
    dispatch
  }, data) {
    await delArchiveCtg(data).then(res => {
      try {
        commit('DEL_ARCHIVE', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async getFindList({
    commit,
    dispatch
  }, data) {
    await getFindList(data).then(res => {
      try {
        commit('FINDLIST', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async saveArchiveScope({
    commit,
    dispatch
  }, data) {
    await saveArchiveScope(data).then(res => {
      try {
        commit('SAVE_ARCH', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async getTableList({
    commit,
    dispatch
  }, data) {
    await getTableList(data).then(res => {
      try {
        commit('GET_TABLE', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async updateArchiveScope({
    commit,
    dispatch
  }, data) {
    await updateArchiveScope(data).then(res => {
      try {
        commit('UPDATA_ARCH', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async deleteArchiveScope({
    commit,
    dispatch
  }, data) {
    await deleteArchiveScope(data).then(res => {
      try {
        commit('DEL_ARCH', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
