import ajax from '../../util/ajax'
import api from '../../util/api'

export default {
  namespaced: true,
  state: {
    gymCardList: [],
  },
  mutations: {
    updateList (state, payload) {
      state.gymCardList = payload || []
    },
  },
  actions: {
    getGymCardList ({ commit }, payload) { // 获取卡列表
      return new Promise((resolve, reject) => {
        ajax(api.gymCardManager.gymCardList.url, {
          method: api.gymCardManager.gymCardList.method,
          queryData: {
            ...payload,
          }
        }).then(res => {
          commit('updateList', res)
          resolve(res)
        }).catch(reject)
      })
    },
    addGymCard ({ commit }, payload) { // 新增卡
      return new Promise((resolve, reject) => {
        ajax(api.gymCardManager.addGymCard.url, {
          method: api.gymCardManager.addGymCard.method,
          data: {
            info: JSON.stringify(payload)
          }
        }).then(res => {
          resolve(res)
        }).catch(reject)
      })
    },
    getGymCardById ({ commit }, { id }) { // 获取卡详情
      return new Promise((resolve, reject) => {
        ajax(api.gymCardManager.getGymCardById.url, {
          method: api.gymCardManager.getGymCardById.method,
          urlData: {
            id,
          }
        }).then(res => {
          resolve(res)
        }).catch(reject)
      })
    },
    editGymCard ({ commit }, payload) { // 编辑更新卡
      return new Promise((resolve, reject) => {
        ajax(api.gymCardManager.editGymCard.url, {
          method: api.gymCardManager.editGymCard.method,
          data: {
            info: JSON.stringify(payload)
          },
        }).then(res => {
          resolve(res)
        }).catch(reject)
      })
    },
    giveCard ({ commit }, { id, users }) {
      return new Promise((resolve, reject) => {
        ajax(api.gymCardManager.giveCard.url, {
          method: api.gymCardManager.giveCard.method,
          urlData: {
            id,
            users
          },
        }).then(res => {
          resolve(res)
        }).catch(reject)
      })
    }
  }
}
