import Vue from 'vue';
import user from '../../core/user.js';
import platDiff from '../../core/platDiff.js'

const state = {
	accessToken: null,
	customerAccessToken: null,
	info: null,
	showLoginModal: false,
	tempParentId: 0,
	cart_nums: 0,
	showAttention: false,
	showAttentionTwo: false,
	sign: {
		mobile: '',
		pic_captcha: '',
		sms_captcha: '',
		validate_code_id: -1
	},
	isSign: false,

	selectedProperties: uni.getStorageSync('selectedProperties')?uni.getStorageSync('selectedProperties'):{name: '绑定楼盘'},

	isEnterSales: false, //是否进入销售端
	isOrderBehalfBustomers: false, //是否代客下单
	
	readUserAgreement:uni.getStorageSync('_READUSERAGREEMENT')?uni.getStorageSync('_READUSERAGREEMENT'):false,
};

const getters = {
	accessToken(state) {
		return state.accessToken;
	},
	customerAccessToken(state) {
		return state.customerAccessToken;
	},
	info(state) {
		return state.info;
	},
	cart_nums(state) {
		return state.cart_nums;
	},
	showLoginModal(state) {
		return state.showLoginModal;
	},
	tempParentId(state) {
		return state.tempParentId;
	},
	is_vip(state) {
		return state.is_vip_card_user;
	},
	showAttention(state) {
		return state.showAttention;
	},
	showAttentionTwo(state) {
		return state.showAttentionTwo;
	},
	sign(state) {
		return state.sign;
	},
	isSign(state) {
		return state.isSign;
	},
	getIsEnterSales(state) {
		return state.isEnterSales;
	},
	getIsOrderBehalfBustomers(state) {
		return state.isOrderBehalfBustomers;
	}
};

const mutations = {
	accessToken(state, data) {
		state.accessToken = data;
	},
	customerAccessToken(state, data) {
		state.customerAccessToken = data;
	},
	info(state, data) {
		state.info = data;
		if(platDiff.route()==='/pages/sale/sale-user-center' && !state.info.salesperson_info){
			uni.redirectTo({
				url:'/pages/sale/sale_login/sale_login'
			})
		}
	},
	cart_nums(state, data) {
		state.cart_nums = data;
	},
	showLoginModal(state, data) {
		state.showLoginModal = data;
	},
	tempParentId(state, data) {
		state.tempParentId = data;
	},
	showAttention(state, data) {
		state.showAttention = data;
	},
	showAttentionTwo(state, data) {
		state.showAttentionTwo = data;
	},
	sign(state, data) {
		state.sign = data;
	},
	isSign(state, data) {
		state.isSign = data;
	},
	isEnterSales(state, data) {
		if (data) {
			console.log('进入销售端', data)
		} else {
			console.log('退出销售端', data)
		}
		state.isEnterSales = data;
	},
	isOrderBehalfBustomers(state,data){
		state.isOrderBehalfBustomers = data;
	},
	selectedProperties(state, data) {
		state.selectedProperties = data
	},
	readUserAgreement(state, data) {
		state.readUserAgreement = data
		uni.setStorageSync('_READUSERAGREEMENT',data)
	}
};

const actions = {
	isEnterSales(context, data) {
		context.commit('isEnterSales', data);
	},
	sign(context, data) {
		context.commit('sign', data);
	},
	isSign(context, data) {
		context.commit('isSign', data);
	},
	cart_nums(context, data) {
		context.commit('cart_nums', data);
	},
	accessToken(context) {
		if (!user.isLogin()) {
			context.commit('accessToken', null);
		}
		user.getAccessToken().then(accessToken => {
			context.commit('accessToken', accessToken);
		});
	},
	info(context, options) {
		if (!user.isLogin()) {
			context.commit('accessToken', null);
		}
		user.getAccessToken().then(accessToken => {
			context.commit('accessToken', accessToken);
			user.getInfo(options).then(data => {
				context.commit('info', data);
				if(context.state.selectedProperties.id==undefined){
					context.dispatch('selectedProperties', data.estate);
				}
			});
		}).catch((err) => {
			context.commit('showLoginModal', true);
		});
	},
	refreshInfo(context) {
		context.commit('accessToken', null);
	},
	refresh(context) {
		if (!user.isLogin()) {
			context.commit('accessToken', null);
		}
		user.getAccessToken().then(accessToken => {
			context.commit('accessToken', accessToken);
			user.getInfo({
				refresh: true,
			}).then(data => {
				context.commit('info', data);
			}).catch(e => {});
		}).catch(e => {});
	},
	setTempParentId(context, data) {
		context.commit('tempParentId', data);
	},
	loadAccessTokenFormCache(context) {
		if (context.accessToken) return;
		user.getAccessToken({
			cacheOnly: true,
		}).then(e => {
			if (!e) return;
			context.commit('accessToken', e);
		});
	},
	logout(context) {
		context.commit('accessToken', null);
		user.loginByToken(null);
	},
	showAttention(context, data) {
		context.commit('showAttention', data);
	},
	showAttentionTwo(context, data) {
		context.commit('showAttentionTwo', data);
	},
	selectedProperties(context, data){
		context.commit('selectedProperties', data);
		uni.setStorageSync('selectedProperties',data)
	}
};

export default {
	namespaced: true,
	state,
	getters,
	mutations,
	actions
}
