import Ajax from '../util/ajax'
import api from '../util/api'
const http = {}

// 请求数据
http.getLotteryList = (data) => {
  return new Promise((resolve, reject) => {
    Ajax(api.lottery.userRecordList.url, {
      method: api.lottery.userRecordList.method
    }).then(res => {
      console.log(res)
      resolve(res)
    }).catch((err) => {
      reject(err)
    })
  })
}

http.getLotteryDetail = (data) => {
  return new Promise((resolve, reject) => {
    Ajax(api.lottery.userRecordDetail.url, {
      method: api.lottery.userRecordDetail.method,
      urlData: {
        id: data.id
      }
    }).then(res => {
      resolve(res)
    }).catch((err) => {
      reject(err)
    })
  })
}

export default {
  namespaced: true,
  state: {
    list: {}, // 奖品列表
    detail: {} // 奖品详情
  },
  mutations: {
    setLotteryList (state, data) {
      state.list = data || []
    },
    setLotteryDetail (state, data) {
      state.detail = data
    }
  },
  actions: {
    getLotteryList (context, data) {
      return new Promise((resolve) => {
        http.getLotteryList(data).then((res) => {
          context.commit('setLotteryList', res)
          resolve(res)
        })
      })
    },

    getLotteryDetail (context, data) {
      return new Promise((resolve) => {
        http.getLotteryDetail(data).then((res) => {
          context.commit('setLotteryDetail', res)
          resolve(res)
        })
      })
    },
    getRushDetail (context, data) {
      return new Promise((resolve, reject) => {
        Ajax({
          ...api.lottery.rushDetail,
          urlData: { ...data }
        }).then(res => {
          context.commit('setLotteryDetail', res)
          resolve(res)
        }).catch((err) => {
          reject(err)
        })
      })
    },
    rushList (context, data) {
      return new Promise((resolve, reject) => {
        Ajax({
          ...api.lottery.rushList,
          queryData: {
            page: 1,
            pagesize: 10000
          }
        }).then(res => {
          context.commit('setLotteryList', res.list)
          resolve(res)
        }).catch((err) => {
          reject(err)
        })
      })
    }
  }
}
