<script>
	let platform = ""
	let SystemVersion = ""
	import {
		mapState
	} from "vuex";
	export default {
		data() {
			return {}
		},
		computed: {
			...mapState(["user", "bleValue"]),
		},
		onLaunch: function() {
			let that = this
			// 返回的是apk包信息
			// #ifdef APP-PLUS||APP
			uni.getSystemInfo({
				success(e) {
					platform = e.platform
					that.$store.commit('changePhoneInfo', {
						platform: e.platform
					})
				}
			})
			if (platform === 'ios') { // ios首次安装没有网络
				uni.onNetworkStatusChange(function(res) {
					if (res.isConnected == true) {
						that.handleoginversion()
					}
				});
			}
			plus.runtime.getProperty(plus.runtime.appid, function(info) {
				uni.setStorageSync('VERSION', info.version)
				SystemVersion = info.version
				that.$store.commit('changePhoneInfo', {
					info: info
				})
			})
			// #endif
			that.handleoginversion()
			console.log('App Launch')
		},
		onShow: function() {
			let that = this
			that.handleTabBarItem()
			uni.switchTab({
				url: "/pages/index/index"
			})
		},
		onHide: function() {
			console.log('App Hide')
			// #ifdef APP-PLUS||APP
			uni.offNetworkStatusChange(function(res) {
				console.log("取消网络监听")
			})
			// #endif
		},
		methods: {
			// 公共信息
			handleoginversion() {
				let that = this
				that.$model.getLoginVersion({}).then(res => {
					console.log("版本", res.data)
					let language = res.data.language == "zh" ? 'zh-Hans' : res.data.language
					uni.setLocale(language)
					that.$i18n.locale = language
					that.$store.commit('changeLocale', res.data.language)
					uni.setStorageSync('language', res.data.language)
					that.$store.dispatch("getCardAllList") // 全部卡片
					that.$store.dispatch("getPublicRecord") // 卡片手动记录配置
					that.$store.dispatch("getHomeConfig") //配置接口信息
					if (res.code == 0) {
						that.handleUserList()
					} else {
						uni.setStorageSync('token', "")
						uni.setStorageSync('aan_id', null)
						setTimeout(() => {
							uni.reLaunch({
								url: '/body/login/login',
							})
						}, 500);
					}
					// #ifdef APP-PLUS||APP
					that.$store.commit('changePhoneInfo', {
						versionUrl: res.data
					})

					let versionPK = that.$tools.compareVersions(SystemVersion, res.data.version)
					if (versionPK == -1) {
						// 版本1小于版本2
						return uni.showModal({
							title: that.$t('newVersion'),
							content: that.$t('newVersion') + res.data.version + that.$t('IsUpdate'),
							cancelText: that.$t("btnSancellation"), //取消按钮文字
							confirmText: that.$t("btnConfirm"), //确认按钮文字
							success: (modalRes) => {
								if (modalRes.confirm) { //确定更新
									if (platform === 'android') { //安卓更新
										uni.navigateTo({
											url: "/body/my/about"
										})
									} else { //ios跳转
										plus.runtime.launchApplication({
											action: res.data.ios_address
										})
									}
								} else {
									that.$tools.msg(that.$t('UpdateProgram'))
								}
							}
						});
					}
					// #endif
				}).catch(error => {
					// console.error('Error fetching data:', error);
				});
			},
			handleUserList() {
				let that = this
				that.$model.getUserList({
					type: 2
				}).then(res => {
					if (res.code != 0) {
						that.$tools.msg(res.msg)
						return
					}
					that.$store.commit('changeFamilay', res.data.user_list)
					if (res.data.user_list.length) {
						let userid = ""
						if (uni.getStorageSync('userid')) {
							let found = res.data.user_list.find(e => e.id == uni.getStorageSync('userid'));
							if (found !== undefined) {
								userid = found.id
							} else {
								userid = res.data.user_list[0].id
								uni.setStorageSync('userid', res.data.user_list[0].id)
							}
						} else {
							userid = res.data.user_list[0].id
							uni.setStorageSync('userid', res.data.user_list[0].id)
						}
						// 用户信息接口
						that.$store.dispatch('getUserInfo', {
							aud_id: userid
						})
						// 厨房秤接口
						that.$store.dispatch("getCountFoodInfo", {
							aud_id: userid,
							time: that.$tools.getDate("start")
						})
						// 
						that.$store.dispatch("getResult", {
							aud_id: userid
						})

						setTimeout(() => {
							that.$isResolve()
						}, 100)
					}

				}).catch(err => {})
			},
			handleTabBarItem() {
				let that = this
				uni.setTabBarItem({
					index: 0,
					text: that.$t('titleHealth')
				})
				uni.setTabBarItem({
					index: 1,
					text: that.$t('titleDiet')
				})
				uni.setTabBarItem({
					index: 2,
					text: that.$t('titleExercise')
				})
				uni.setTabBarItem({
					index: 3,
					text: that.$t('titleMe')
				})
			},
		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	/* #ifndef APP-NVUE */
	@import "/uni.scss";
	@import "/scss/food.scss";
	@import "/scss/common.scss";
	@import "/scss/iconfont.css";
	@import "/scss/iconfont-weapp-icon.css";

	/* #endif*/

	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		background-color: #f7f7f7;
		min-height: 100vh;
	}
</style>