<template>
	<view class="index">
		<TabHome v-if="actived === 'TabHome'" ref="TabHome"></TabHome>
		<!-- <TabTrade v-if="actived === 'TabTrade'" ref="TabTrade"></TabTrade> -->
		<!-- <TabInteraction v-if="actived === 'TabInteraction'" ref="TabInteraction"></TabInteraction> -->
		<Msg v-if="actived === 'Msg'" ref="Msg"></Msg>

		<TabUser v-if="actived === 'TabUser'" ref="TabUser"></TabUser>
		<!-- <view class="tabbar-placeholder">

		</view> -->
		<tabbar :list="tabPages" :actived="actived" @change="onChanged"></tabbar>
	</view>
</template>

<script>
	import {
		getUUID,
		versionCompare,
		getMacAddress,
	} from "@/common/utils/index.js";
	import tabbar from "./components/tabbar.vue";
	import TabHome from "../home/index.vue";
	// import TabTrade from "../trade/index.vue";
	import TabInteraction from "../interaction/index.vue";
	import TabUser from "../user/index.vue";
	import url from "@/common/http/url.js";
	import Msg from "../chat/index.vue";
	import {
		mapMutations
	} from "vuex";

	let userInfo = uni.getStorageSync("userInfo");
	const upDater = uni.requireNativePlugin("CL-UpDater"); // 版本升级
	export default {
		components: {
			tabbar,
			TabHome,
			// TabTrade,
			TabInteraction,
			TabUser,
			Msg
		},
		data() {
			return {
				actived: "TabHome",
				tabPages: [{
						name: "首页",
						url: "/pages/home/index",
						iconPath: "/static/home-page/home.png",
						selectedIconPath: "/static/home-page/home-use.png",
						component: "TabHome",
						activeColor: '#22bd5e',
						inactiveColor: '#333333'
					},
					{
						name: "我的",
						url: "/pages/user/index",
						iconPath: "/static/home-page/my.png",
						selectedIconPath: "/static/home-page/my-use.png",
						component: "TabUser",
						activeColor: '#22bd5e',
						inactiveColor: '#333333'
					},
				],
			};
		},
		onShow() {
			if (this.$refs["TabUser"]) {
				this.$refs["TabUser"].reset();
			}
		},
		onLoad(e) {
			if (e.tab) this.actived = e.tab;
		},
		methods: {
			...mapMutations(["logout"]),
			getArrayValueByCode(array, code) {
				let value = "";
				array.forEach((v) => {
					if (v.keyCode === code) {
						value = v.keyValue;
						return false;
					}
				});
				return value;
			},
			async versionApp(showLoad) {
				let version = null; // 当前版本

				await plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
					console.log("widgetInfo", widgetInfo);
					version = widgetInfo.version;
				});

				let res = await this.$http("GET", url.common.getAppVersion);
				console.log("res**********", res);

				// let version = plus.runtime.version; // 当前版本
				let iosVersion = res.data["IOS"].keyValue; // ios版本
				let androidVersion = res.data["Android"].keyValue; // android版本
				console.log(
					uni.getSystemInfoSync().platform,
					"当前版本=====version",
					version,
					iosVersion,
					"安卓版本=====androidVersion",
					androidVersion
				);
				if (uni.getSystemInfoSync().platform === "ios") {
					if (!versionCompare(version, iosVersion)) {
						let list = res.data["IOS"].children;
						// 1-选择升级 2-强制升级 3-个人中心手动升级 4-忽略升级
						let type = this.getArrayValueByCode(list, "app_version_ios_upgrade");
						let con = this.getArrayValueByCode(list, "app_version_ios_detail");
						let downUrl = this.getArrayValueByCode(list, "app_version_ios_url");
						let options = {
							title: "发现新版本",
							con: con,
							downUrl: downUrl,
							hidCancelbtn: false, //是否隐藏取消按钮;
							btnBgColor: "#F8D247", //设置按钮背景色颜色
							updateBtnText: "立即更新", //升级按钮文字,默认为立即升级
							hidBackBtn: true, //可选 默认为false（即会显示后台更新按钮）， 设置android 整包更新弹框是否显示后台更新按钮，这个只有android整包更新才有这个参数，热更新是是无法设置的。
							// verCode:"1.0.1",//最新apk版本号 可选  用于Android整包升级,避免用户取消安装apk时,重复下载问题,如果不传的话,apk将会重新下载
						};
						if (type === "1" || type === "2" || (showLoad && type === "3")) {
							upDater.startUpdate(options, () => {
								if (type === "2") {
									plus.runtime.restart();
								}
							});
						}
					}
					if (showLoad) {
						this.$msg("当前已是最新版～");
					}
				} else if (uni.getSystemInfoSync().platform === "android") {
					console.log(
						"versionCompare(version, androidVersion*********",
						versionCompare(version, androidVersion)
					);
					if (!versionCompare(version, androidVersion)) {
						let list = res.data["Android"].children;
						// 1-选择升级 2-强制升级 3-浏览器升级(暂不用) 4-个人中心手动升级 5-忽略升级
						let type = this.getArrayValueByCode(
							list,
							"app_version_android_upgrade"
						);
						let con = this.getArrayValueByCode(
							list,
							"app_version_android_detail"
						);
						let isMust = this.getArrayValueByCode(list, "qzgx");
						console.log("type***********", type);
						console.log("con**************", con);
						if (type === "1" || type === "2" || (showLoad && type === "4")) {
							let downUrl = this.getArrayValueByCode(
								list,
								"app_version_android_url"
							);
							let options = {
								title: "发现新版本",
								con: con,
								downUrl: downUrl,
								hidCancelbtn: isMust == 0 ? false : true, //是否隐藏取消按钮;
								btnBgColor: "#F8D247", //设置按钮背景色颜色
								updateBtnText: "立即更新", //升级按钮文字,默认为立即升级
								hidBackBtn: false, //可选 默认为false（即会显示后台更新按钮）， 设置android 整包更新弹框是否显示后台更新按钮，这个只有android整包更新才有这个参数，热更新是是无法设置的。
								// verCode:"1.0.1",//最新apk版本号 可选  用于Android整包升级,避免用户取消安装apk时,重复下载问题,如果不传的话,apk将会重新下载
							};
							upDater.startUpdate(options, () => {
								if (type === "2") {
									plus.runtime.quit();
								}
							});
						}
					}
					if (showLoad) {
						this.$msg("当前已是最新版～");
					}
				}
			},
			async onChanged(tab) {
				// 中间短视频
				if (tab.midButton) {
					uni.navigateTo({
						url: tab.url,
					});
					return;
				}
				// #ifdef APP-PLUS
				this.versionApp();
				// #endif
				// #ifndef APP-PLUS
				// this.$msg("当前版本不支持检测更新～");
				// #endif
				this.actived = tab.component;
				this.checkToken()
			},
			//就是点击下边四个tab的时候调用一下这个接口，返回false的话就调用一下退出登录，给他退掉
			checkToken() {
				this.$http("GET", "/user/userCache").then((res) => {
					if (!res.data) {
						this.logout();
					}
				});
			},
		},
		onLoad() {
			this.checkToken();
		},
		onPullDownRefresh() {
			const component = this.$refs[this.actived] || {};
			component.pullDownRefresh ?
				component.pullDownRefresh() :
				uni.stopPullDownRefresh();
		},
		onReachBottom() {
			const component = this.$refs[this.actived] || {};
			component.reachBottom && component.reachBottom();
		},
		onBackPress() {
			const component = this.$refs[this.actived] || {};
			component.backPress && component.backPress();
		},
		onPageScroll(e) {
			const component = this.$refs[this.actived] || {};
			component.pageScroll && component.pageScroll(e);
		},
	};
</script>

<style lang="scss">
	.index {
		.tabbar-placeholder {
			display: block;
			width: 100%;
			height: 98upx;
			background: transparent;
		}
	}
</style>