import {
	ACCESS_TOKEN,
	USER_ID,
	USER_INFO
} from '@/store/mutation-types'
import storage from '@/utils/storage'
import * as LoginApi from '@/api/login'
import * as UserProfileApi from '@/api/love-user-profile.js'
import {
	alipayLogin
} from '@/api/login.js'
import {
 		getUser,
 		updateUser
 	} from '@/api/user.js'

// 登陆成功后执行
const loginSuccess = ({commit,dispatch}, data) => {
	const {
		access_token,
		user
	} = data
	// 过期时间30天
	const expiryTime = 30 * 86400
	// 保存tokne和userId到缓存
	storage.set(USER_ID, user.userId, expiryTime)
	storage.set(USER_INFO, user, expiryTime)
	storage.set('Phone',user.phone)
	storage.set(ACCESS_TOKEN, access_token, expiryTime)
	// 记录到store全局变量
	commit('SET_TOKEN', access_token)
	commit('SET_USER_ID', user.userId)
	commit('SET_USER', user)
	commit('SET_PHONE',user.phone)
	uni.setStorageSync('Phone',user.phone)
	
	
	dispatch('ConnectSocket')
	console.log("user: ",user);
}

const user = {
	state: {
		// 用户认证token
		token: '',
		// 用户ID
		userId: null,
		// 手机号码
		phone: null,
		// 用户
		userInfo: null,
		
		userProfile: null
	},

	mutations: {
		SET_TOKEN: (state, value) => {
			state.token = value
		},
		SET_USER_ID: (state, value) => {
			state.userId = value
		},
		SET_USER: (state, value) => {
			state.userInfo = value
			storage.set(USER_INFO, value)
		},
		SET_PHONE: (state,value) => {
			state.phone = value
			storage.set('Phone', value)
		}
	},

	actions: {
		GetUserInfo({commit}, data){
			getUser().then(res => {
				if (res.code == 0 && res.data.username != 'www') {
					commit('SET_USER', res.data)
				} 
			})
			
		},
		GetUserProfile({state, commit}, data) {
			
		},
		// 用户登录 (普通登录: 输入手机号和验证码)
		Login({
			commit,dispatch
		}, data) {
			return new Promise((resolve, reject) => {
				LoginApi.login(data)
					.then(response => {
						const result = response.data
						loginSuccess({commit,dispatch}, result)
						resolve(response)
					})
					.catch(reject)
			})
		},

		// 微信小程序一键授权登录 (获取用户基本信息)
		LoginMpWx({
			commit, dispatch
		}, data) {
			return new Promise((resolve, reject) => {
				LoginApi.loginMpWx({
						form: data
					}, {
						isPrompt: false
					})
					.then(response => {
						const result = response.data
						loginSuccess({commit, dispatch}, result)
						resolve(response)
					})
					.catch(reject)
			})
		},

		// 支付宝小程序一键授权登录 (获取用户基本信息)
		LoginMpAlipay({
			commit
		}, data) {
			return new Promise((resolve, reject) => {
				alipayLogin({
						data
					}, {
						isPrompt: false
					})
					.then(response => {
						const result = response.data
						loginSuccess(commit, result)
						resolve(response)
					})
					.catch(reject)
			})
		},

		// 微信公众号一键授权登录 (获取用户基本信息)
		LoginWxOfficial({
			commit
		}, data) {
			return new Promise((resolve, reject) => {
				LoginApi.loginWxOfficial({
						form: data
					}, {
						isPrompt: false
					})
					.then(response => {
						const result = response.data
						loginSuccess(commit, result)
						resolve(response)
					})
					.catch(reject)
			})
		},

		// 微信小程序一键授权登录 (授权手机号)
		LoginMpWxMobile({
			commit, dispatch
		}, data) {
			console.log("data: ",data);
			return new Promise((resolve, reject) => {
				LoginApi.loginMpWxMobile(data)
					.then(response => {
						const result = response.data
						console.log("result999999: ",result);
						console.log("commit: ",commit);
						loginSuccess({commit, dispatch}, result)
						resolve(response)
					})
					.catch(reject)
			})
		},

		// 退出登录
		Logout({
			commit,dispatch
		}, data) {
			const store = this
			return new Promise((resolve, reject) => {
				if (store.getters.userId > 0) {
					// 删除缓存中的tokne和userId
					storage.remove(USER_ID)
					storage.remove(ACCESS_TOKEN)
					storage.remove(USER_INFO)
					// 记录到store全局变量
					commit('SET_TOKEN', '')
					commit('SET_USER_ID', null)
					commit('SET_USER', null)
					resolve()
				}
				dispatch('CloseSocket')
			})
		},

		setUserInfo({
			commit
		}, data) {
			commit('SET_USER', data)
		},
		setToken({
			commit
		}, data) {
			commit('SET_TOKEN', data)
		},
		setUserId({
			commit
		}, data) {
			commit('SET_USER_ID', data)
		}

	}
}

export default user