import {
  getData,
  addData,
  updateData,
  deleteData
} from "@/api/intelligent-warehouse/warehouse-setting/denseFrame-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 addData({
    commit,
    dispatch
  }, data) {
    await addData(data).then(res => {
      try {
        commit('ADD_DATA', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async updateData({
    commit,
    dispatch
  }, data) {
    await updateData(data).then(res => {
      try {
        commit('UPDATE_DATA', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async deleteData({
    commit,
    dispatch
  }, data) {
    await deleteData(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
}
