import {
  getFondWithPage,
  saveFonds,
  updateFonds,
  deleteFonds
} from "@/api/system-management/allAdministration"
import { getButtonList } from "@/api/user"
import {
  message
} from '@/utils/common'
const getDefaultState = () => {
  return {
    tableData: [],
    loading: false,
    pagination: {
      pageSize: 10,
      pageNum: 1,
      total: 0,
    },
    showButtons: {
      add: false,
      edit: false,
      del: false,
    },
  }
}
const state = getDefaultState()
const mutations = {
  GET_DATA(state, data) {
    state.tableData = data.list
    state.pagination.total = data.total
    state.loading = false
  },
  SAVE_FONDS(state, data) {
    // state.tableData = data.list
    // state.pagination.total = data.total
    // state.loading = false
  },
  UPDATE_FONDS(state, data) {
    // state.tableData = data.list
    // state.pagination.total = data.total
    // state.loading = false
  },
  DELETE(state, data) {
    // state.tableData = data.list
    // state.pagination.total = data.total
    // state.loading = false
  },
  GET_BUTTON_LIST(state, data) {
    data.forEach(item => {
      state.showButtons[item.key] = true
    })
  }

}
const actions = {
  async getData({
    commit,
    dispatch
  }, data) {
    await getFondWithPage(data).then(res => {
      try {
        commit('GET_DATA', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async saveFonds({
    commit,
    dispatch
  }, data) {
    await saveFonds(data).then(res => {
      try {
        commit('SAVE_FONDS', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async updateFonds({
    commit,
    dispatch
  }, data) {
    await updateFonds(data).then(res => {
      try {
        commit('UPDATE_FONDS', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async deleteFonds({
    commit,
    dispatch
  }, data) {
    await deleteFonds(data).then(res => {
      try {
        commit('DELETE', 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
}
