/**
 * 登录状态管理
 * 个人信息数据持久化
 */
import Vue from 'vue';
import Vuex from 'vuex';
// import $api from "@/api.js";
import Request from '@/js_sdk/luch-request/luch-request/index.js';
const http = new Request();
import md5 from '@/js_sdk/js-md5/md5.min.js';
import {config} from '@/config/config.js'
Vue.use(Vuex);
const userKey = md5("userInfo");
const hPageKey = md5('historyPage');
const sysTokenKey = md5('accessToken');
const loginUrl = '/pages/login/loginType';
const token = uni.getStorageSync(sysTokenKey);
let thetoken = 'Bearer ' + token.accessToken;
export default new Vuex.Store({
	state: {
		hasLogin: false,
		userInfo: {
			customerCompanyInfoForm:{}
		},
		token: '',
		hPage: ''
	},
	mutations: {
		login(state, token = '') {
			token = token instanceof Object ? token.accessToken : token;
			state.token = token || uni.getStorageSync(sysTokenKey).accessToken || '';
			state.hasLogin = Boolean(state.token);
			//保存token
			uni.setStorage({
				key: sysTokenKey,
				data: {
					accessToken: state.token
				}
			});
		},
		/**
		 * 退出登录，传入false只更新缓存不跳转
		 */
		logout(state, v = true) {
			state.hasLogin = false;
			state.userInfo = {};
			state.token = '';
			uni.removeStorage({
				key: userKey
			});
			uni.removeStorage({
				key: sysTokenKey
			});
			//跳到登录页
			// v && $api.goto(loginUrl);
		},
		// historyPage(state, pUrl = '') {
		// 	if($api.alterUrl('/',pUrl) != loginUrl){
		// 		state.hPage = pUrl || uni.getStorageSync(hPageKey).url;
		// 		//记录历史页
		// 		uni.setStorage({
		// 			key: hPageKey,
		// 			data: {
		// 				url: state.hPage
		// 			}
		// 		});
		// 	}
		// },
		async upInfo(state, provider = {}) {
			//用户信息结构
			const infoObj = {
				"guid": "",
				"account": "",
				"customerType": '',
				"customerName": "",
				"nickName": "",
				"phone": "",
				"sex": '',
				"headImage": "",
				"queryStr": '',
				"carbonAmount": '',
				"farmerNum": '',
				"farmerPersonNum": '',
				"loginType": '',
				"customerCompanyInfoForm": {
					"guid": "",
					"fkCustomerId": "",
					"companyName": "",
					"principals": "",
					"phone": "",
					"email": "",
					"companyAddress": "",
					"companyImages": "",
					"condition": "",
					"customerType": "",
					"imageList": '',
					"companyType": ''
				},
				"channelType": ''
			};
			const then = res => {
				console.log('222',res)
				// if (res.code == 0) {
				// 	let data = res.data instanceof Object ? res.data : infoObj;
				// 	//处理头像连接
				// 	// data.headImage = $api.alterUrl($api.href, data.headImage || "");
				// 	state.userInfo = data;
				// 	uni.setStorage({ //缓存用户登陆状态
				// 		key: userKey,
				// 		data
				// 	});
				// }
			}
			//读取本地缓存信息进行赋值
			then({
				code: 0,
				data: Object.assign(uni.getStorageSync(userKey),provider)
			});
			//在线更新取得用户信息
			// then(await $api.$http({
			// 	url: config.apiBaseurl + '/carbon-h5/wap/customer/getCustomerByToken'
			// }));
			then(await 
				http.get(config.apiBaseurl + '/carbon-h5/wap/customer/getCustomerByToken',{header: {Authorization:thetoken}}).then(res => {
					console.log('getCustomerByToken', res);
				}).catch(err => {
					// console.log('err', err)
				})
			);
			
			
		}
	}
});
