// store/index.js
import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);
import {
	getConfigByKey
} from '@/api/index.js'
import {
	getAppInfo
} from '@/api/common.js'
export default new Vuex.Store({
  state: {
	 coureLogin: uni.getStorageSync('coureLogin') || 0,
	 webviewUrl: '',
	 logoimg:'',
	 wsDanmuUrl: 'wss://userapp.cqsft.vip', // 弹幕评论接口地址
	 //红包领取规则:
	 //获取惠选的图片,logo加s
	 answerType: 1, //红包领取规则 0：完课且最后一分钟（第二次无需最后一分钟）， 1:按完课百分比答题领红包
	 isSpare:0, // 0，主要小程序，1：备选
	 uploadFile: 'https://userapp.cqsft.vip',
	imgpath: 'https://kntobs.jnmyunl.com',//惠选商城图片请求地址
	// https://kntobs.jnmyunl.com/shop  惠选商城图片链接
	logoname:'德信严选',
	appid:'wx1de020b57c05a990',//臻选
	// appid:'wxd2edd379beb6581b',//惠选
	// appid:'wx06c66d8f7b8b01d0',//其他
  },
  mutations: {
    setCount(state, value) {
      state.count = value;
    },
    setUserInfo(state, info) {
      state.userInfo = info;
    },
	setWebviewUrl(state, value) {
		state.webviewUrl = value;
	},
	setlogoimg(state, value) {
		state.logoimg = value;
	},
	setCoureLogin(state, payload) {
		uni.setStorageSync('coureLogin', payload);
		state.coureLogin = payload;
	}
  },
  getters: {
    coureLogin: (state) => state.coureLogin,
	logoimg:(state) => state.logoimg,
  },
  actions: {
    fetchUser({ commit }) {
      // const res = await uni.request({ url: '/api/user' });
      commit('setUserInfo', 'noe');
    },
	getAppInfos({commit}){
		const data={
			appId:wx.getAccountInfoSync().miniProgram.appId
		}
		return new Promise((resolve, reject) => {
			getAppInfo(data).then(res => {
				if (res.code == 200) {
					console.log('=====',res)
					commit('setlogoimg', res.data.img);
					resolve()
				}
			}).catch(error => {
				reject(error)
			});
		})
		// getAppInfo(data).then(res=>{
		// 	if(res.code==200){
				
		// 		commit('logoimg', res.data.img);
		// 		resolve()
		// 	}else{
		// 		uni.showToast({
		// 			title: res.msg,
		// 			icon: 'none'
		// 		});
		// 	}
		// })
	},
	getWebviewUrl({commit}) {
		var that = this;
		var data = {
			key: 'course.config'
		}
		return new Promise((resolve, reject) => {
			getConfigByKey(data).then(res => {
				if (res.code == 200) {
					console.log("getConfigByKey====", JSON.parse(res.data))
					let data = JSON.parse(res.data)
					commit('setWebviewUrl', data.userCourseAuthDomain);
					resolve()
				}
			}).catch(error => {
				reject(error)
			});
		})
	}
  }
});