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

export default {
  namespaced: true,
  state: {
    vipList: [],
    vipinfo: {},
  },
  mutations: {
    updateList (state, payload) {
      state.vipList = payload || []
    },
    updateInfo (state, payload) {
      state.vipinfo = payload || {}
    },
  },
  actions: {
    GetVipList ({ commit }, payload) {
      return new Promise((resolve, reject) => {
        ajax(api.cardManager.viplist.url, {
          method: api.cardManager.viplist.method,
          queryData: {
            ...payload,
          }
        }).then(res => {
          commit('updateList', res)
          resolve(res)
        }).catch(reject)
      })
    },
    AddVip ({ commit }, payload) {
      return new Promise((resolve, reject) => {
        ajax(api.cardManager.vipadd.url, {
          method: api.cardManager.vipadd.method,
          data: {
            ...payload,
          }
        }).then(res => {
          resolve(res)
        }).catch(reject)
      })
    },
    GetVipByCode ({ commit }, { code }) {
      return new Promise((resolve, reject) => {
        ajax(api.cardManager.vipbycode.url, {
          method: api.cardManager.vipbycode.method,
          urlData: {
            code,
          }
        }).then(res => {
          commit('updateInfo', res)
          resolve(res)
        }).catch(reject)
      })
    },
    VipCharge ({ commit }, payload) {
      return new Promise((resolve, reject) => {
        ajax(api.cardManager.vipcharge.url, {
          method: api.cardManager.vipcharge.method,
          urlData: {
            code: payload.code
          },
          data: {
            ...payload,
          }
        }).then(res => {
          resolve(res)
        }).catch(reject)
      })
    },
    SetVipInfoNull ({ commit }) {
      commit('updateInfo', {})
    }
  }
}
