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

export 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 参与人数
  }, // 抽奖信息
  isShowBind: false, // 是否显示绑定手机
  premise: 0
})

export 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
  }
}

export 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 })
    } else {
      Toast.fail(errorMessage)
      if (errorCode === 9) {
        this.app.context.error({ statusCode: 404, message: 'This page could not be found' })
      }
    }
    return Promise.resolve(res)
  },
  // 获取抽奖基础数据
  getLotteryDetail({ commit, dispatch }, params = {}) {
    return getLotteryInfos(params).then(res => {
      const { code, errorCode, data, errorMessage } = res
      if (code === 200 && errorCode === 0) {
        commit('SET_LOTTERYINFO', data)
        // 停止加载效果
        commit('common/SET_ISPAGELOADING', false, { root: true })
      } else {
        Toast.fail(errorMessage)
        if (errorCode === 9) {
          this.app.context.error({ statusCode: 404, message: 'This page could not be found' })
        }
      }
    })
  },
  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 (errorCode === 9) {
          this.app.context.error({ statusCode: 404, message: 'This page could not be found' })
        }
      }
    })
  },
}
export const getters = {
  sessionList: state => state.sessionList, // 场次列表
  lotteryInfo: state => state.lotteryInfo, // lotteryInfo
  isShowBind: state => state.isShowBind, // 是否显示绑定手机
  premise: state => state.premise // 抽奖前置条件检测
}
