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

// 请求数据
export default {
  namespaced: true,
  state: {
    cardShare: {},
    couponShare: {},
    msg: ''
  },
  mutations: {
    setCardShare (state, data) {
      state.cardShare = data || {}
    },
    setCouponShare (state, data) {
      state.couponShare = data || {}
    },
    setMsg (state, msg) {
      state.msg = msg || ''
    }
  },
  actions: {
    getCardShare (context, data) {
      return new Promise((resolve, reject) => {
        Ajax({
          ...api.share.card,
          urlData: {
            id: data.id
          }
        }).then(res => {
          context.commit('setCardShare', res)
          resolve(res)
        }).catch((err) => {
          reject(err)
        })
      })
    },
    getCouponShare (context, data) {
      return new Promise((resolve, reject) => {
        Ajax({
          ...api.share.coupon,
          urlData: {
            id: data.id
          }
        }).then(res => {
          context.commit('setCouponShare', res)
          resolve(res)
        }).catch((err) => {
          reject(err)
        })
      })
    },
    setCardShare (context, data) {
      return new Promise((resolve, reject) => {
        Ajax({
          ...api.share.setCard,
          urlData: {
            id: data.id
          },
          data: {
            saleid: data.salesid,
            serialcode: data.serialcode
          }
        }).then(res => {
          resolve(res)
        }).catch((err) => {
          context.commit('setMsg', err)
          reject(err)
        })
      })
    },
    setCouponShare (context, data) {
      return new Promise((resolve, reject) => {
        Ajax({
          ...api.share.setCoupon,
          urlData: {
            id: data.id
          },
          data: {
            saleid: data.salesid,
            serialcode: data.serialcode
          }
        }).then(res => {
          resolve(res)
        }).catch((err) => {
          context.commit('setMsg', err)
          reject(err)
        })
      })
    },
    setChannelCardShare (context, data) {
      return new Promise((resolve, reject) => {
        Ajax({
          ...api.share.setChannelCard,
          urlData: {
            id: data.id
          }
        }).then(res => {
          resolve(res)
        }).catch((err) => {
          context.commit('setMsg', err)
          reject(err)
        })
      })
    },
    setChannelCouponShare (context, data) {
      return new Promise((resolve, reject) => {
        Ajax({
          ...api.share.setChannelCoupon,
          urlData: {
            id: data.id
          }
        }).then(res => {
          resolve(res)
        }).catch((err) => {
          context.commit('setMsg', err)
          reject(err)
        })
      })
    }
  }
}
