import {
  getData,
  selectDoc,
  boxToStorage
} from "@/api/intelligent-warehouse/warehouse-management/file-storage"
import {
  getClassTreeByClassName
} from "@/api/system-management/archiveSettings"
import { getButtonList } from "@/api/user"
import {
  message
} from '@/utils/common'
const getDefaultState = () => {
  return {
    tableLoading: false,
    tableData: [],
    pagination: {
      pageSize: 10,
      pageNum: 1,
      total: 0,
    },
    treeData: [],
    treeLoading: false,
    labelList: [],
    showButtons: {
      ware: false,
    },
  }
}
const state = getDefaultState()
const mutations = {
  GET_DATA(state, data) {
    state.tableData = data.list
    state.pagination.total = data.total
    state.tableLoading = false
  },
  GET_TREE(state, data) {
    state.treeData = data.list
    state.treeLoading = false
  },
  SELECT_DOC(state, data) {
    state.labelList = data.list
  },
  BOX_TO_STORAGE(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 getClassTreeByClassName({
    commit,
    dispatch
  }, data) {
    await getClassTreeByClassName(data).then(res => {
      try {
        commit('GET_TREE', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async selectDoc({
    commit,
    dispatch
  }, data) {
    await selectDoc(data).then(res => {
      try {
        commit('SELECT_DOC', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async boxToStorage({
    commit,
    dispatch
  }, data) {
    await boxToStorage(data).then(res => {
      try {
        commit('BOX_TO_STORAGE', 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
}
