/* eslint-disable no-param-reassign */
import { getLotteryInfos, getLotterySessionList, checkPremise, checkVote, checkQuestionnaire } from '@/api/modules/lottery'
import { Toast } from 'vant'
import Bus from '@/utils/Bus'
// import { cloneDeep } from 'lodash'

const state = {
  sessionList: [], // 抽奖场次列表
  lotteryInfo: {
    activeNum: 0, // 参与人数
    intro: '', // 活动介绍
    times: 0, // 抽奖次数
    startTime: null, // 开始时间
    endTime: null, // 结束时间
    status: null, // 抽奖状态 0未开始 1进行中 3已结束
    showResult: 0, // 是否显示中奖跑马灯 0否 1是
    type: null, // 抽奖类型 1 即时抽奖 2 定时抽奖
    record: [], // 抽奖记录列表
    prizeConfigs: [], // 奖品
    showType: null, // 定时抽奖条件 1 倒计时 2 参与人数
    newPremise: [], // 新的抽奖前置条件
    premise: [], // 旧的抽奖前置条件
    bindService: '', // 绑定服务
    bindServiceForceOpen: 0, // 绑定服务强制开启
    bindServiceId: 0 // 绑定服务id
  }, // 抽奖信息
  isShowBind: false, // 是否显示绑定手机
  premise: null, // 抽奖前置条件检测
  precondition: {
    isJoin: 0, // 是否参与
    url: '', // 回跳地址
    title: '', // 活动标题
  } // 抽奖前置活动检测
}

const mutations = {
  SET_SESSIONLIST: (state, list) => {
    state.sessionList = list
  },
  SET_LOTTERYINFO: (state, info) => {
    state.lotteryInfo = info
  },
  SET_ISSHOWBIND: (state, isShow) => {
    state.isShowBind = isShow
  },
  SET_PREMISE: (state, premise) => {
    state.premise = premise
  },
  SET_PRECONDITION: (state, precondition) => {
    state.precondition = precondition
  }
}

const actions = {
  // 获取抽奖场次信息
  async getSessionList({ commit, rootState }, params = {}) {
    const res = await getLotterySessionList(params)
    const { code, errorCode, data, errorMessage } = res
    if (code === 200 && errorCode === 0) {
      const { title, playConfig } = data
      // 设置场次数据
      commit('SET_SESSIONLIST', playConfig)
      // 设置网页标题
      document.title = title || '抽奖'
      // 停止加载效果
      commit('common/SET_ISPAGELOADING', false, { root: true })
      // 主要是为了设置premise信息
      commit('SET_LOTTERYINFO', {
        ...state.lotteryInfo,
        newPremise: data.premise,
        forceWrite: data.forceWrite,
        bindService: data.bindService,
        bindServiceId: data.bindServiceId,
        bindServiceForceOpen: data.bindServiceForceOpen
      })
    } else {
      Toast.fail(errorMessage)
      if (errorCode === 9) {
        this.$router.push('/error')
      }
    }
    return Promise.resolve(res)
  },
  // 获取抽奖基础数据
  getLotteryDetail({ commit, dispatch }, params = {}) {
    return new Promise((resolve, reject) => {
      getLotteryInfos(params).then(res => {
        const { code, errorCode, data, errorMessage } = res
        if (code === 200 && errorCode === 0) {
          commit('SET_LOTTERYINFO', data)
          document.title = data.title
          // 停止加载效果
          commit('common/SET_ISPAGELOADING', false, { root: true })
        } else {
          Toast.fail(errorMessage)
          if (errorCode === 9) {
            this.$router.push('/error')
          }
        }
        resolve(res.data)
      })
    })
  },
  updateInfo({ commit, state }, val) {
    // const lotteryInfo = cloneDeep(state.lotteryInfo)
    commit('SET_LOTTERYINFO', { ...state.lotteryInfo, ...val })
  },
  check_premise({ commit, state }, params = {}) {
    checkPremise(params).then(res => {
      const { code, errorCode, data, errorMessage } = res
      if (code === 200 && errorCode === 0) {
        commit('SET_PREMISE', data)
      } else {
        Toast.fail(errorMessage)
        if (errorMessage === '您未登录') {
          setTimeout(() => {
            Bus.$emit('showLogin')
          }, 1000)
        }
        if (errorCode === 9) {
          this.$router.push('/error')
        }
      }
    })
  },
  check_precondition({ commit, state }, params = {}) {
    return new Promise((resolve, reject) => {
      if (params.checkType === 'vote') {
        delete params.checkType
        checkVote(params).then(res => {
          const { code, errorCode, data, errorMessage } = res
          if (code === 200 && errorCode === 0) {
            commit('SET_PRECONDITION', data)
          } else {
            Toast.fail(errorMessage)
            if (errorCode === 9) {
              this.$router.push('/error')
            }
          }
          resolve(res.data)
        })
      } else if (params.checkType === 'questionnaire') {
        delete params.checkType
        checkQuestionnaire(params).then(res => {
          const { code, errorCode, data, errorMessage } = res
          if (code === 200 && errorCode === 0) {
            commit('SET_PRECONDITION', data)
          } else {
            Toast.fail(errorMessage)
            if (errorCode === 9) {
              this.$router.push('/error')
            }
          }
          resolve(res.data)
        })
      }
    })
  }
}
const getters = {
  sessionList: state => state.sessionList, // 场次列表
  lotteryInfo: state => state.lotteryInfo, // lotteryInfo
  isShowBind: state => state.isShowBind, // 是否显示绑定手机
  premise: state => state.premise, // 抽奖前置条件检测
  precondition: state => state.precondition // 抽奖前置活动检测
}
export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
