import {
  getData,
  parameterAdd,
  parameterUpdate,
  parameterDelect
} from "@/api/intelligent-warehouse/warehouse-setting/file-parameters.js"
import {
  getButtonList
} from "@/api/user"
import {
  message
} from '@/utils/common'
const getDefaultState = () => {
  return {
    tableLoading: false,
    tableData: [],
    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.tableLoading = false
    state.pagination.total = data.total
  },
  ADD_DATA(state, data) {
    message('success', '新增成功', 3000)
  },
  UPDATE_DATA(state, data) {
    message('success', '修改成功', 3000)
  },
  DELECT_DATA(state, data) {
    message('success', '删除成功', 3000)
  },
  GET_BUTTON_LIST(state, data) {
    data.forEach(item => {
      state.showButtons[item.key] = true
    })
  }
}
const actions = {
  async getData({
    commit,
    dispatch
  }, data) {
    await getData(data).then(res => {
      try {
        commit('GET_DATA', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async parameterAdd({
    commit,
    dispatch
  }, data) {
    await parameterAdd(data).then(res => {
      try {
        commit('ADD_DATA', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async parameterUpdate({
    commit,
    dispatch
  }, data) {
    await parameterUpdate(data).then(res => {
      try {
        commit('UPDATE_DATA', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async parameterDelect({
    commit,
    dispatch
  }, data) {
    await parameterDelect(data).then(res => {
      try {
        commit('DELECT_DATA', 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
}
