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

const http = {}

// 请求数据
http.getUserInfo = (data) => {
  let queryData = {}
  if (data.code) {
    queryData = {
      code: data.code
    }
  } else {
    queryData = {}
  }
  return new Promise((resolve, reject) => {
    Ajax(api.lottery.userInfo.url, {
      method: api.lottery.userInfo.method,
      queryData: queryData,
      urlData: {
        id: data.luckdrawid
      }
    }).then(res => {
      resolve(res)
    }).catch((err) => {
      reject(err)
    })
  })
}

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

http.setPrize = (data) => {
  return new Promise((resolve, reject) => {
    Ajax(api.lottery.setPrize.url, {
      method: api.lottery.setPrize.method,
      urlData: {
        id: data.id
      }
      // data: {
      //   openid: data.openId
      //   // openid: 'oMOpz0hNdM4vvxI6CghCSUWiRsJA'
      // }
    }).then(res => {
      resolve(res)
    }).catch((err) => {
      reject(err)
    })
  })
}

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

http.setUser = (data) => {
  return new Promise((resolve, reject) => {
    Ajax(api.lottery.setUser.url, {
      method: api.lottery.setUser.method,
      urlData: {
        id: data.toId
      },
      data: {
        from: data.fromId || '',
        isnew: data.isnew ? data.isnew : 0
      }
    }).then(res => {
      resolve(res)
    }).catch((err) => {
      reject(err)
    })
  })
}

export default {
  namespaced: true,
  state: {
    lotteryId: '1',
    userInfo: {}, // 用户信息
    prize: {}, // 抽奖信息
    record: [], // 中奖列表
    runState: 0 // 0为未启动，1为进行中，2为已结束
  },
  mutations: {
    setLotteryId (state, data) {
      state.lotteryId = data
    },
    setUserInfo (state, data) {
      state.userInfo = data
    },
    setPrize (state, data) {
      state.prize = data
    },
    setRecord (state, data) {
      state.record = data
    },
    runState (state, data) {
      state.runState = data
    }
  },
  actions: {
    LotteryId (context, data) {
      context.commit('setLotteryId', data)
    },
    getUserInfo (context, data) {
      return new Promise((resolve) => {
        http.getUserInfo(data).then((res) => {
          context.commit('setUserInfo', res)
          resolve(res)
        })
      })
    },

    getPrize (context, data) {
      return new Promise((resolve) => {
        http.getPrize(data).then((res) => {
          context.commit('setPrize', res)
          resolve(res)
        })
      })
    },

    setPrize (context, data) {
      return new Promise((resolve, reject) => {
        http.setPrize(data).then((res) => {
          resolve(res)
        }).catch((err) => {
          reject(err)
        })
      })
    },

    getRecord (context, data) {
      return new Promise((resolve) => {
        http.getRecord(data).then((res) => {
          if (!res) {
            res = []
          }
          res = res.filter(item => item.IsReality === 1)
          if (res.length >= 100) {
            res = res.slice(0, 100)
          }
          context.commit('setRecord', res)
          resolve(res)
        })
      })
    },

    setUser (context, data) {
      console.log(data)
      return new Promise((resolve) => {
        http.setUser(data).then((res) => {
          resolve(res)
        })
      })
    },

    setRunState (context, data) {
      context.commit('runState', data)
    }
  }
}
