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

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