import {
  getData,
  returnData
} from "@/api/archiveUtilize/returnPhysicalArchives"
import {
  getButtonList
} from "@/api/user"
import {
  message
} from '@/utils/common'
const getDefaultState = () => {
  return {
    tableData: [],
    loading: false,
    pagination: {
      pageSize: 10,
      pageNum: 1,
      total: 0,
    },
    showButtons: {
      look: false,
      return: false,
    },
  }
}
const state = getDefaultState()
const mutations = {
  GET_DATA(state, data) {
    state.tableData = data.list
    state.pagination.total = data.total
    state.loading = false
  },
  RETURN_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 returnData({
    commit,
    dispatch
  }, data) {
    await returnData(data).then(res => {
      try {
        commit('RETURN_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
}
