import {
  queryTree,
  batchSave,
  batchDelete,
  batchAdd,
  batchUpdate
} from "@/api/intelligent-warehouse/warehouse-setting/room-setting.js"
import {
  getButtonList
} from "@/api/user"
import {
  message
} from '@/utils/common'
const getDefaultState = () => {
  return {
    treeLoading: false,
    treeData: [],
    showButtons: {
      addRoom: false,
      add: false,
      edit: false,
      del: false,
    },
  }
}
const state = getDefaultState()
const mutations = {
  GET_TREE(state, data) {
    state.treeData = data.tree
    state.treeLoading = false
  },
  SAVE_BATCH(state, data) {
    message('success', '新增库房成功', 3000)
  },
  DELECT_BATCH(state, data) {
    message('success', '删除成功', 3000)
  },
  ADD_BATCH(state, data) {
    message('success', '新增成功', 3000)
  },
  UPDATE_BATCH(state, data) {
    message('success', '修改成功', 3000)
  },
  GET_BUTTON_LIST(state, data) {
    data.forEach(item => {
      state.showButtons[item.key] = true
    })
  }
}
const actions = {
  async queryTree({
    commit,
    dispatch
  }, data) {
    await queryTree(data).then(res => {
      try {
        commit('GET_TREE', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async batchSave({
    commit,
    dispatch
  }, data) {
    await batchSave(data).then(res => {
      try {
        commit('SAVE_BATCH', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async batchDelete({
    commit,
    dispatch
  }, data) {
    await batchDelete(data).then(res => {
      try {
        commit('DELECT_BATCH', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async batchAdd({
    commit,
    dispatch
  }, data) {
    await batchAdd(data).then(res => {
      try {
        commit('ADD_BATCH', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async batchUpdate({
    commit,
    dispatch
  }, data) {
    await batchUpdate(data).then(res => {
      try {
        commit('UPDATE_BATCH', 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
}
