<script>
	export default {
		data() {
			return {
				progress: 0
			}
		},
		onLaunch: function() {
			console.log('App Launch')
			// #ifdef APP-PLUS
			this.checkUpdate();
			// #endif

			const value = uni.getStorageSync('launchFlag');
			if (!value) {
				// launchFlag=true直接跳转到首页
				uni.reLaunch({
					url: '/pages/index/index'
				});
			}
			uni.$on(["login_succeeded", "logout"], () => {
				if (this.$store.state.user.group_id === 2) {
					let iconIndex = 2
					// #ifdef H5
					iconIndex = 3
					// #endif
					uni.setTabBarItem({
						index: iconIndex,
						text: '商品库',
						pagePath: '/pages/product/my-product-tabbar',
						iconPath: "static/images/kan.png",
						selectedIconPath: "static/images/kan_selected.png",
					})
					uni.setTabBarItem({
						index: 1,
						text: '待发货',
						pagePath: "pages/order/back-order",
						iconPath: "static/images/qiang.png",
						selectedIconPath: "static/images/qiang_selected.png",
					})
				} else {
					let iconIndex = 2
					// #ifdef H5
					iconIndex = 3
					// #endif
					uni.setTabBarItem({
						index: iconIndex,
						text: '购物车',
						pagePath: '/pages/shopping-cart/shopping-cart',
						iconPath: "static/images/kan.png",
						selectedIconPath: "static/images/kan_selected.png",
					})
					uni.setTabBarItem({
						index: 1,
						text: '待收货',
						pagePath: "pages/order/back-order",
						iconPath: "static/images/qiang.png",
						selectedIconPath: "static/images/qiang_selected.png",
					})
				}
			});
			uni.onTabBarMidButtonTap(() => {
				if (this.$store.state.user.token) {
					uni.chooseImage({
						count: 1,
						sourceType: ['camera']
					})
				} else {
					uni.navigateTo({
						url: "pages/user/login"
					})
				}
			})
			uni.$on("refresh_userinfo", () => {
				this.$http.get({
					url: "/user/current",
					success: (res) => {
						this.$store.dispatch("user/save", res.data.data)
					}
				})

			})
		},
		onShow: function() {
			console.log('App Show')
			this.$store.commit("user/load")
			this.$store.dispatch("cart/load");

			uni.$emit("login_succeeded");
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {
			checkUpdate() {
				this.$http.get({
					url: '/update',
					success: (res) => {

						let temp = [];
						let last_version = res.data.data.versionName.substring(1);
						temp = last_version.split(".");
						let last_version_code = parseInt(temp[0]) * 10000 + parseInt(temp[1]) * 100 + parseInt(
							temp[2]);

						let cur_version = plus.runtime.version;
						temp = cur_version.split(".");
						let cur_version_code = parseInt(temp[0]) * 10000 + parseInt(temp[1]) * 100 + parseInt(
							temp[2]);

						console.log(`最新版本：${last_version} 当前版本${cur_version}`)
						// uni.showModal({
						// 	content:`${最新版本：${last_version} ${last_version_code} 当前版本${cur_version} ${cur_version_code} 当前环境 ${process.env.NODE_ENV}`
						// })
						if (last_version_code > cur_version_code || process.env.NODE_ENV === 'development') {
							uni.showModal({
								title: "版本更新",
								content: `检测到新版本，是否更新？`,
								confirmText: "立即更新",
								success: (e) => {
									if (e.confirm) {
										console.log('用户点击确定');
										if (plus.os.name === 'iOS') {
											plus.runtime.openURL(
												"https://apps.apple.com/cn/app/id387682726")
										}
										if (plus.os.name === 'Android') {
											let downloadTask = uni.downloadFile({
												url: this.$http.baseUrl + res.data.data
													.apk_file,
												success: (downloadResult) => {
													if (downloadResult.statusCode ===
														200) {
														plus.runtime.install(
															downloadResult
															.tempFilePath, {
																force: false
															},
															function() {
																console.log(
																	'install success...'
																);
																plus.runtime
																	.restart();
															},
															function(e) {
																console.error(
																	'install fail...'
																);
															});
													}
												}
											});
											let loading = plus.nativeUI.showWaiting("已下载0%", {
												width: "30%",
												height: "15%"
											})
											downloadTask.onProgressUpdate((e) => {
												// console.log('下载进度' + e.progress);
												// console.log('已经下载的数据长度' + e.totalBytesWritten);
												// console.log('预期需要下载的数据总长度' + e.totalBytesExpectedToWrite);
												if (e.progress % 5 === 0) {
													loading.setTitle("已下载" + e.progress +
														"%")
												}
												if (e.progress === 100) {
													loading.close();
												}
											})
										}

									} else if (e.cancel) {
										console.log('用户点击取消');
									}
								}
							})

						}
					}
				});
			}
		}
	}
</script>

<style>
	page {
		background: #F8F8F8;
	}

	uni-button:after {
		border: none;
	}

	@font-face {
		font-family: "Roboto";
		src: url(static/Roboto-Regular.ttf);
	}

	@font-face {
		font-family: "Roboto-Bold";
		src: url(static/Roboto-Bold.ttf);
	}

	/*每个页面公共css */
	::-webkit-scrollbar {
		display: none;
		width: 0 !important;
		height: 0 !important;
		-webkit-appearance: none;
		background: transparent;
	}
</style>
