import { apiConfig } from '@/api/app'
// import { apiUserCentre, apiDistributionCode } from '@/api/user'
import { USER_INFO, TOKEN, INVITE_CODE } from '@/config/cachekey'
import wechath5 from '@/utils/wechath5'
import Cache from '@/utils/cache'
import { getClient } from '@/utils/tools'
import { apiserviceConfig, apishareConfig } from '@/api/app'

const state = {
    userInfo: {},
    token: Cache.get(TOKEN) || null,
    client: null,
    serviceConfig: {},
    shareConfig: [],
	globalData: {
	    miniVersion: '', // 小程序版本
	    share: false, // 分享默认为false
	    height: 0,
	    statusBarData: {},
	    statusBarHeight: uni.getSystemInfoSync()['statusBarHeight'],
	    tim: null,
	    context: null,
	    isLogOut: false,
	    initTim: false,
	    userData: {},
	    docObj: {},
	    locObj: {},
	    patObj: {},
	    schDate: "",
	    backPage: 2,
	    writeObj: {
	        schDateKey: "",
	        schTimeKey: "",
	        fileList: [],
	        disesTime: "",
	        disesNote: ""
	    },
	    isUpdatePatList: false, // 是否需要更新就诊人列表数据
	    SDKAPPID: 0,
	    SECRETKEY: '',
	    reservationEntrance: "", // 预约入口标识
	    platform: '', // 当前平台
	    provider: '',
	    appId: '2021004100602607', // 应用APPID
	    alipayQuery: '' // 支付宝小程序关联普通二维码参数值
	}
}

const mutations = {
	setGlobalData(state, data) {
		const keys = Object.keys(data)
		keys.map(key => {
			state.globalData[key] = data[key]
		})
	},
    setshareConfig(state, data) {
        state.shareConfig = data
    },
    setConfig(state, data) {
        state.config = data
        Cache.set(CONFIG, data)
    },
    login(state, data) {
        state.token = data.token
        Cache.set(TOKEN, data.token)
        // 登录完成绑定邀请码
        const code = Cache.get(INVITE_CODE)
        if (code) {
            // apiDistributionCode({
            //     code,
            //     hide: 1
            // }).finally(() => {
            //     Cache.remove(INVITE_CODE)
            // })
        }
    },
    logout(state) {
        state.token = ''
        state.userInfo = {}
        Cache.remove(TOKEN)
    },
    setUserInfo(state, data) {
        state.userInfo = data
    }
}

const actions = {
	setGlobalData({ state, commit }, data) {
		console.log('setGlobalData == ', data)
		commit('setGlobalData', data)
	},
    getsetshareConfig({ state, commit }) {
        return apishareConfig()
            .then((res) => {
                commit('setshareConfig', res)
                return Promise.resolve(res)
            })
            .catch(() => {
                return Promise.reject()
            })
    },
    getsetserviceConfig({ state, commit }) {
        return apiserviceConfig()
            .then((res) => {
                commit('setserviceConfig', res)
                return Promise.resolve(res)
            })
            .catch(() => {
                return Promise.reject()
            })
    },
    getConfig({ state, commit }) {
        return apiConfig()
            .then((res) => {
                commit('setConfig', res)
                return Promise.resolve(res)
            })
            .catch(() => {
                return Promise.reject()
            })
    },
    getUser({ state, commit }) {
        return new Promise((resolve, reject) => {
			resolve()
        })
    },
    setWxShare({ state }, opt) {
        // #ifdef H5
        const { share_image, share_intro, share_title } = state.config
        const inviteCode = state.userInfo.code
        const href = window.location.href
        const sym = href.includes('?') ? '&' : '?'
        const option = {
            shareTitle: share_title,
            shareLink: inviteCode ? `${href}${sym}invite_code=${inviteCode}` : href,
            shareImage: share_image,
            shareDesc: share_intro
        }
        wechath5.share(Object.assign(option, opt))
        // #endif
    }
}

export default {
    state,
    mutations,
    actions
}
