import {
  getPointTree,
  deletePoint,
  addPoint,
  updatePoint
} from "@/api/intelligent-warehouse/warehouse-setting/collection-point.js"
import {
  getButtonList
} from "@/api/user"
import {
  message
} from '@/utils/common'
const getDefaultState = () => {
  return {
    treeLoading: false,
    treeData: [],
    showButtons: {
      add: false,
      edit: false,
      del: false,
    },
  }
}
const state = getDefaultState()
const mutations = {
  GET_TREE(state, data) {
    state.treeData = data.tree
    state.treeLoading = false
  },
  DELECT_POINT(state, data) {
    message('success', '删除成功', 2000)
  },
  ADD_POINT(state, data) {
    message('success', '新增成功', 2000)
  },
  UPDATE_POINT(state, data) {
    message('success', '修改成功', 2000)
  },
  GET_BUTTON_LIST(state, data) {
    data.forEach(item => {
      state.showButtons[item.key] = true
    })
  }
}
const actions = {
  async getPointTree({
    commit,
    dispatch
  }, data) {
    await getPointTree(data).then(res => {
      try {
        commit('GET_TREE', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async deletePoint({
    commit,
    dispatch
  }, data) {
    await deletePoint(data).then(res => {
      try {
        commit('DELECT_POINT', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async addPoint({
    commit,
    dispatch
  }, data) {
    await addPoint(data).then(res => {
      try {
        commit('ADD_POINT', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async updatePoint({
    commit,
    dispatch
  }, data) {
    await updatePoint(data).then(res => {
      try {
        commit('UPDATE_POINT', 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
}
