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

export default {
  namespaced: true,
  state: {
    gameid: '1',
    game: {},
    userInfo: {},
    recordInfo: {},
    supports: [],
    recordUser: {}
  },
  mutations: {
    updateGame (state, data) {
      state.game = data || {}
    },
    updateUserInfo (state, data) {
      state.userInfo = data || {}
    },
    updateRecordInfo (state, data) {
      state.recordInfo = data.record || {}
      state.supports = data.supports || []
      state.recordUser = data.customer || {}
    }
  },
  actions: {
    getGame (context) {
      return new Promise((resolve, reject) => {
        ajax({
          ...api.game,
          urlData: {
            gameid: context.state.gameid,
            customerid: context.state.userInfo.CustomerId
          }
        }).then((res) => {
          context.commit('updateGame', res)
          resolve(res)
        }).catch(reject)
      })
    },
    customer (context, data) {
      return new Promise((resolve, reject) => {
        ajax({
          ...api.customer,
          urlData: {
            code: data
          }
        }).then((res) => {
          context.commit('updateUserInfo', res)
          resolve(res)
        }).catch(reject)
      })
    },
    signUp (context, data) {
      return new Promise((resolve, reject) => {
        ajax({
          ...api.signup,
          urlData: {
            customerid: context.state.userInfo.CustomerId
          },
          data: data
        }).then((res) => {
          resolve(res)
        }).catch(reject)
      })
    },
    setRecord (context) {
      return new Promise((resolve, reject) => {
        ajax({
          ...api.setRecord,
          urlData: {
            customerid: context.state.userInfo.CustomerId,
            gameid: context.state.gameid
          }
        }).then((res) => {
          resolve(res)
        }).catch(reject)
      })
    },
    getRecord (context, data) {
      return new Promise((resolve, reject) => {
        ajax({
          ...api.getRecord,
          urlData: {
            recordid: data
          }
        }).then((res) => {
          context.commit('updateRecordInfo', res)
          resolve(res)
        }).catch(reject)
      })
    },
    setSupport (context, data) {
      return new Promise((resolve, reject) => {
        ajax({
          ...api.setSupport,
          urlData: {
            ...data
          }
        }).then((res) => {
          resolve(res)
        }).catch(reject)
      })
    },
  }
}
