import Vue from 'vue';
import Vuex from 'vuex';
import { http } from '@/utils/request';
import { userInfo, userInfoOnline } from '@/api/userInfo';
import { getCompetitionId } from '@/api/apply';

Vue.use(Vuex);

const ACCESSTOKEN = uni.getStorageSync('accessToken') || '';
const SIGNUP = uni.getStorageSync('signUp') || '';
const BABY = uni.getStorageSync('baby') || '';
const USER = uni.getStorageSync('user') || '';
const WXUSER = uni.getStorageSync('wxUser') || '';
const STAGELIST = uni.getStorageSync('stageList') || '';
const ONLINEID = uni.getStorageSync('onlineId') || '';
const OFFLINEID = uni.getStorageSync('offlineId') || '';
const APPLYTYPE = uni.getStorageSync('applyType') || '';
const PARENT = uni.getStorageSync('parent') || '';
const ONLINECOMPETITION = uni.getStorageSync('onlineCompetition') || '';
const OFFLINECOMPETITION = uni.getStorageSync('offlineCompetition') || '';
const RESULT = uni.getStorageSync('result') || '';
const REFRESHTOKEN = uni.getStorageSync('refreshToken') || '';

const store = new Vuex.Store({
  state: {
		accessToken: ACCESSTOKEN,
		refreshToken: REFRESHTOKEN,
		signUp: SIGNUP,
		baby: BABY,
		parent: PARENT,
		user: USER,
		wxUser: WXUSER,
		stageList: STAGELIST,
		onlineId: ONLINEID,
		offlineId: OFFLINEID,
		applyType: APPLYTYPE,
		onlineCompetition: ONLINECOMPETITION,
		offlineCompetition: OFFLINECOMPETITION,
		result: RESULT
  },
  getters: {
    // 判断用户是否登录
		hasLogin: state => {
			return !!state.accessToken;
		}
  },
  mutations: {
    login(state, provider) {
			state.accessToken = provider.user.id || '';
			state.refreshToken = provider.user.refreshToken || '';
			state.user = provider.user || '';
			state.baby = provider.baby || '';
			// state.signUp = provider.signUp || '';
			// state.stageList = provider.stageList || '';

			uni.setStorageSync('user', provider.user);
			uni.setStorageSync('baby', provider.baby);
			// uni.setStorageSync('signUp', provider.signUp);
			// uni.setStorageSync('stageList', provider.stageList);
			uni.setStorageSync('accessToken', provider.user.id);
			uni.setStorageSync('refreshToken', provider.user.refreshToken);
		},
		logout(state) {
			state.accessToken = '';
			state.userInfo = {};
			uni.removeStorageSync('accessToken');
			uni.removeStorageSync('userInfo');
		},
		signUp(state, provider) {
			state.signUp = provider;
			uni.setStorageSync('signUp', provider);
		},
		baby(state, provider) {
			state.baby = provider;
			uni.setStorageSync('baby', provider);
		},
		parent(state, provider) {
			state.parent = provider;
			uni.setStorageSync('parent', provider);
		},
		wxUser(state, provider) {
			state.wxUser = provider;
			uni.setStorageSync('wxUser', provider);
		},
		stageList(state, provider) {
			state.stageList = provider;
			uni.setStorageSync('stageList', provider);
		},
		onlineId(state, provider) {
			state.onlineId = provider;
			uni.setStorageSync('onlineId', provider);
		},
		offlineId(state, provider) {
			state.offlineId = provider;
			uni.setStorageSync('offlineId', provider);
		},
		onlineCompetition(state, provider) {
			state.onlineCompetition = provider;
			uni.setStorageSync('onlineCompetition', provider);
		},
		offlineCompetition(state, provider) {
			state.offlineCompetition = provider;
			uni.setStorageSync('offlineCompetition', provider);
		},
		applyType(state, provider) {
			state.applyType = provider;
			uni.setStorageSync('applyType', provider);
		},
		result(state, provider) {
			state.result = provider;
			uni.setStorageSync('result', provider);
		}
  },
  actions: {
    logout({ commit }) {
			commit('logout');
		},
		getUserInfo({ commit }, competitionChildId) {
			return new Promise((resolev, reject) => {
				http
				.get(userInfo, {
					...competitionChildId
				})
				.then(res => {
					const { baby, signUp, stageList } = res.data;
					commit('baby', baby || null);
					commit('signUp', signUp || null);
					commit('stageList', stageList || null);
					resolev(res);
				})
				.catch(() => {
					reject();
				})
			}) 
		},
		getUserInfoOnline({ commit }, competitionChildId) {
			return new Promise((resolev, reject) => {
				http
				.get(userInfoOnline, {
					...competitionChildId
				})
				.then(res => {
					const { baby, signUp, stageList } = res.data;
					commit('baby', baby || null);
					commit('signUp', signUp || null);
					commit('stageList', stageList || null);
					resolev(res);
				})
				.catch(() => {
					reject();
				})
			}) 
		},
		getCompetitionId({ commit }) {
			return new Promise((resolev, reject) => {
				http
					.get(getCompetitionId)
					.then(res => {
						commit('onlineId', res.data.onlineId || null);
						commit('offlineId', res.data.offlineId || null);
						commit('onlineCompetition', res.data.onlineCompetition || null);
						commit('offlineCompetition', res.data.offlineCompetition || null);
						resolev(res);
					})
					.catch(() => {
						reject()
					})
			})
		}
  }
});

export default store;
