import {
  getData,
  listStore,
  putOnFile,
  getBoxFile,
  
} from "@/api/intelligent-warehouse/warehouse-management/file-onShelves"
import { getButtonList } from "@/api/user"
import {
  message
} from "@/utils/common"
const getDefaultState = () => {
  return {
    tableData: [],
    tableLoading: false,
    pagination: {
      pageSize: 10,
      pageNum: 1,
      total: 0,
    },
    storeHomes: [],
    flieList: [],
    showButtons: {
      put: false,
    },
  }
}
const state = getDefaultState()
const mutations = {
  GET_DATA(state, data) {
    state.tableData = data.list
    state.tableLoading = false
    state.pagination.total = data.total
  },
  LIST_STORE(state, data) {
    data.list.forEach(item => {
      item.activeImg = false
    })
    data.list[0].activeImg = true
    state.storeHomes = data.list
  },
  PUT_ON_FILE(state, data) {
    message('success', '上架成功', 3000)
  },
  BOX_FILE(state, data) {
    state.flieList = data
  },
  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 putOnFile({
    commit,
    dispatch
  }, data) {
    await putOnFile(data).then(res => {
      try {
        commit('PUT_ON_FILE', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async listStore({
    commit,
    dispatch
  }, data) {
    await listStore(data).then(res => {
      try {
        commit('LIST_STORE', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async getBoxFile({
    commit,
    dispatch
  }, data) {
    await getBoxFile(data).then(res => {
      try {
        commit('BOX_FILE', res)
      } 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
}
