const util = require('@/utils/util.js').default;
// import requestConfig from '@/static/lib/requestConfig';
export default {
	data() {
		return {
            locusValue:"",
		}
	},
	mounted() {
        this.locusValue = this.$route.query.locusValue || "";
        console.warn("---mounted---",this.pvId,this.$route.meta.keepAlive,this.locusValue)
		if(this.pvId && !this.$route.meta.keepAlive){
			window.removeEventListener('beforeunload', this.pvCloseHandle);
			window.removeEventListener('visibilitychange', this.visibilitychange);
			window.currentPage = this;
			if(this.starRender && typeof(this.starRender)==='function'){
				this.starRender()
			}
            if(this.pvId != "p_2cmina_23080401" && !(this.pvId=="p_2cmina_23080404" && !this.curHouseObj)){
                let param = {
                	type: 'PV',
                	pvId: this.pvId,
                	locusBehaviorName: this.locusBehaviorName,
                	pvCurPageName: this.pvCurPageName,
                	locusValue: this.locusValue || this.$route.query.locusValue || "",
                };
                util.trackRequest(param);//发送统一PV埋点
            }
			
			window.addEventListener('beforeunload', this.pvCloseHandle);
			window.addEventListener('visibilitychange', this.visibilitychange);
		}
	},
	//页面销毁时，注销监听程序|返回上一页，去往下一页等
	destroyed(){
		window.removeEventListener('beforeunload', this.pvCloseHandle);
		window.removeEventListener('visibilitychange', this.visibilitychange);
	},
	// beforeDestroy() {
	// 	console.warn("---beforeDestroy---")
	// 	this.pvCloseHandle()
	// },
	// /**
	//  * 生命周期函数--监听页面隐藏
	//  */
	// onHide: function() {
	// 	console.warn("---onhide---")
	// 	this.pvCloseHandle()
	// },
	// beforeUpdate() {
	// 	console.warn("beforeUpdate");
	// 	// 移除visibilitychange事件监听器
	// 	window.removeEventListener('visibilitychange', this.visibilitychange);
	// },
	beforeRouteLeave(to, from, next) {
		console.warn("router:beforeRouteLeave:",from.name);
		// 在离开当前路由前执行的方法
		// 例如保存表单数据、清理定时器等操作
		this.pvCloseHandle();
		if(this.stopRender && typeof(this.stopRender)==='function'){
			this.stopRender();//停止webgl的渲染
		}
		window.removeEventListener('beforeunload', this.pvCloseHandle);//注销监听刷新事件
		window.removeEventListener('visibilitychange', this.visibilitychange);
		next();
	},
	beforeRouteEnter(to, from, next) {
		console.warn("router:beforeRouteEnter:",to.name,to);
		next();
	},
	activated(){
		console.warn("activated:",this.pvId); 
		if(this.pvId && this.$route.meta.keepAlive){
			window.removeEventListener('beforeunload', this.pvCloseHandle);
			window.removeEventListener('visibilitychange', this.visibilitychange);
			window.currentPage = this;
            window.addEventListener('beforeunload', this.pvCloseHandle);
            window.addEventListener('visibilitychange', this.visibilitychange);
			if(this.starRender && typeof(this.starRender)==='function'){
				this.starRender()
			}
            //条件不具备，不发送埋点
            if((this.pvId=="p_2cmina_23080401" || this.pvId=="p_2cmina_23080404") && !this.curHouseObj){
                return false
            }
            if(this.backAysncData && typeof(this.backAysncData)==='function'){
            	this.backAysncData()
            }
			let param = {
				type: 'PV',
				pvId: this.pvId,
				locusBehaviorName: this.locusBehaviorName,
				pvCurPageName: this.pvCurPageName,
                locusValue: this.locusValue,
			};
			util.trackRequest(param);//发送统一PV埋点
		}
	},
	methods: {
		visibilitychange(){
			console.warn("visibilitychange");
			if (document.visibilityState !== 'visible') { // 离开页面
				if(this.stopRender && typeof(this.stopRender)==='function'){
					this.stopRender();//停止webgl的渲染
				}
				this.pvCloseHandle()
			} else { // 进入页面
				if(this.starRender && typeof(this.starRender)==='function'){
					this.starRender();//开始webgl的渲染
				}
				this.pvShowHandle()
			}
		},
		pvShowHandle(){
			if(this.pvId){
				let param = {
					type: 'PV',
					pvId: this.pvId,
					locusBehaviorName: this.locusBehaviorName,
					pvCurPageName: this.pvCurPageName,
                    locusValue: this.locusValue,
				};
				util.trackRequest(param);//发送统一PV埋点
			}
            if(this.onShow && typeof(this.onShow) ==='function'){
                this.onShow()
            }
		},
		pvCloseHandle(){
			if(this.pvId){
				let param = {
					type: 'PV',
					pvId: this.pvId + '_close',
					pvCurPageName: this.pvCurPageName,
					locusBehaviorName: this.locusBehaviorName,
				};
				util.trackRequest(param);
			}
		},
	}
}
