import {
  getData,
  getDataAll,
  getViewData,
  getNodeList,
  queryApproveDetails
} from "@/api/approvalManagement/approval";
import {
  getButtonList
} from "@/api/user"
const getDefaultState = () => {
  return {
    tableAllData:[], // 查询所有
    tableData: [],
    pagination: {
      pageSize: 10,
      pageNum: 1,
      total: 0,
    },
    viewData: [],
    historyData: [],
    loading: false,
    viewLoading: false,
    historyLoading: false,
    showButtons: {
      sp: false,
      lssp: false,
    },

  }
}
const state = getDefaultState()
const mutations = {
  GET_DATA(state, data) {
    state.tableData = data.list
    state.pagination.total = data.total
    state.loading = false
  },
  GET_All_DATA(state, data) {
    state.tableAllData = data.list
    state.pagination.total = data.total
    state.loading = false
  },
  GET_VIEW_DATA(state, data) {
    state.viewData = data.list
    state.viewLoading = false
  },
  GET_HISTORY_DATA(state, data) {
    state.historyData = data.list
    state.historyLoading = false
  },
  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 getAllData({
    commit,
    dispatch
  }, data) {
    await getDataAll(data).then(res => {
      try {
        commit('GET_All_DATA', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async getViewData({
    commit,
    dispatch
  }, data) {
    await getViewData(data).then(res => {
      try {
        commit('GET_VIEW_DATA', res.body)
      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async queryApproveDetails({
    commit,dispatch
  },data){
    await queryApproveDetails(data).then(res => {
      try {
        commit('GET_VIEW_DATA', res.body)

      } catch (err) {
        throw new Error(err)
      }
    })
  },
  async getHistoryData({
    commit,
    dispatch
  }, data) {
    await getNodeList(data).then(res => {
      try {
        commit('GET_HISTORY_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
}
