import Ajax from '../../util/ajax'
import api from '../../util/api'
import wxsdk from '@/util/share'

export default {
  namespaced: true,
  state: {
    userInfo: null,
    appid: null,
    shareNum: 0
  },
  mutations: {
    updateAppId (state, data) {
      state.appid = data.message.appid
    },
    updateUserInfo (state, data) {
      state.userInfo = data || {}
    },
    updateShareNum (state, data) {
      state.shareNum = data || 0
    }
  },
  actions: {
    getAppId (context) {
      return new Promise((resolve, reject) => {
        Ajax({
          ...api.getAppId
        }).then(res => {
          context.commit('updateAppId', res)
          resolve(res)
        }).catch((res) => {
          reject(res)
        })
      })
    },
    getUser (context, payload) {
      return new Promise((resolve, reject) => {
        wxsdk({ link: encodeURIComponent(window.location.href.split('#')[0]), code: payload.code }, {
          title: '破晓·谋远·共行',
          desc: '2019银城地产营销新春年会邀请函',
          link: `${window.location.origin}${window.location.pathname}#/`,
          thu_image: `https://jingcheng-h5.oss-cn-beijing.aliyuncs.com/nh/%E9%93%B6%E5%9F%8Elogo.png`
        }).then((res) => {
          context.commit('updateUserInfo', res)
          resolve(res)
        }).catch((err) => {
          reject(err)
        })
      })
    },
    getShare (context) {
      return new Promise((resolve, reject) => {
        Ajax({
          ...api.getShare
        }).then(res => {
          context.commit('updateShareNum', res)
          resolve(res)
        }).catch((res) => {
          reject(res)
        })
      })
    },
    putShare (context) {
      console.log(context)
      return new Promise((resolve, reject) => {
        Ajax({
          ...api.putShare
        }).then(res => {
          resolve(res)
        }).catch((res) => {
          reject(res)
        })
      })
    }
  }
}
