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

export default {
  namespaced: true,
  state: {
    luckDrawList: [],
  },
  mutations: {
    updateLuckDrawList (state, payload) {
      state.luckDrawList = payload || []
    },
  },
  actions: {
    getLuckdrawList ({ commit }, payload) { // 获取抽奖活动列表
      return new Promise((resolve, reject) => {
        ajax(api.activityManager.luckdrawList.url, {
          method: api.activityManager.luckdrawList.method,
          queryData: {
            ...payload,
          }
        }).then(res => {
          commit('updateLuckDrawList', res)
          resolve(res)
        }).catch(reject)
      })
    },
    getLuckdrawById ({ commit }, id) { // 获取抽奖活动详情
      return new Promise((resolve, reject) => {
        ajax(api.activityManager.luckdrawInfoById.url, {
          method: api.activityManager.luckdrawInfoById.method,
          urlData: {
            id,
          }
        }).then(res => {
          resolve(res)
        }).catch(reject)
      })
    },
    addLuckdraw ({ commit }, payload) { // 新增抽奖活动
      return new Promise((resolve, reject) => {
        ajax(api.activityManager.addLuckdraw.url, {
          method: api.activityManager.addLuckdraw.method,
          data: { info: JSON.stringify(payload) },
        }).then(res => {
          resolve(res)
        }).catch(reject)
      })
    },
    editLuckdraw ({ commit }, payload) { // 编辑抽奖活动
      return new Promise((resolve, reject) => {
        ajax(api.activityManager.editLuckdraw.url, {
          method: api.activityManager.editLuckdraw.method,
          urlData: {
            id: payload.Id,
          },
          data: { info: JSON.stringify(payload) },
        }).then(res => {
          resolve(res)
        }).catch(reject)
      })
    },
    deleteLuckdraw ({ commit }, id) { // 删除抽奖活动
      return new Promise((resolve, reject) => {
        ajax(api.activityManager.deleteLuckdraw.url, {
          method: api.activityManager.deleteLuckdraw.method,
          urlData: {
            id,
          },
        }).then(res => {
          resolve(res)
        }).catch(reject)
      })
    },
    getDrawTemplateList () { // 获取抽奖活动模板
      return new Promise((resolve, reject) => {
        ajax(api.activityManager.getDrawTemplateList.url, {
          method: api.activityManager.getDrawTemplateList.method,
        }).then(res => {
          resolve(res)
        }).catch(reject)
      })
    },
    getRecordsList ({ commit }, { id, payload }) { // 获取抽奖记录列表
      return new Promise((resolve, reject) => {
        ajax(api.activityManager.getRecordsList.url, {
          method: api.activityManager.getRecordsList.method,
          urlData: {
            id,
          },
          queryData: { ...payload },
        }).then(res => {
          resolve(res)
        }).catch(reject)
      })
    },
    getShareList ({ commit }, { id, payload }) { // 获取分享记录列表
      return new Promise((resolve, reject) => {
        ajax(api.activityManager.getShareList.url, {
          method: api.activityManager.getShareList.method,
          urlData: {
            id,
          },
          queryData: { ...payload },
        }).then(res => {
          resolve(res)
        }).catch(reject)
      })
    },
    openActivity ({ commit }, id) { // 开启抽奖活动
      return new Promise((resolve, reject) => {
        ajax(api.activityManager.openActivity.url, {
          method: api.activityManager.openActivity.method,
          urlData: {
            id,
          },
        }).then(res => {
          resolve(res)
        }).catch(reject)
      })
    },
    stopActivity ({ commit }, id) { // 停用抽奖活动
      return new Promise((resolve, reject) => {
        ajax(api.activityManager.stopActivity.url, {
          method: api.activityManager.stopActivity.method,
          urlData: {
            id,
          },
        }).then(res => {
          resolve(res)
        }).catch(reject)
      })
    },
  }
}
