<template>
	<view class="app">
		<view class="content">
			<view class="" v-if="Number(region)==2&&Number(userinfo.cityCode)==2">
				<image v-if="countyName=='东营港经济开发区'" :src="baseurl+ '/profile/weChatPicture/dygmainPage.png'" />
				<image v-else-if="countyName=='东营经济开发区'" :src="baseurl+ '/profile/weChatPicture/dyjjmainPage.png'" />
				<image v-else-if="countyName=='东营区'" :src="baseurl+ '/profile/weChatPicture/dyqmainPage.png'" mode="" />
				<image v-else-if="countyName=='广饶县'" :src="baseurl+ '/profile/weChatPicture/grgmainPage.png'" mode="" />
				<image v-else-if="countyName=='河口区'" :src="baseurl+ '/profile/weChatPicture/hkmainPage.png'" mode="" />
				<image v-else-if="countyName=='垦利区'" :src="baseurl+ '/profile/weChatPicture/klgmainPage.png'" mode="" />
				<image v-else-if="countyName=='利津县'" :src="baseurl+ '/profile/weChatPicture/ljgmainPage.png'" mode="" />
				<image v-else :src="baseurl+ '/profile/weChatPicture/dysmainPage.png'" mode="" />
			</view>
			<image v-else-if="Number(region)==2&&Number(userinfo.cityCode)!=2"
				:src="baseurl+ '/profile/weChatPicture/dysmainPage.png'" mode="" />
			<image v-else :src="baseurl+ '/profile/weChatPicture/mainPage.png'" mode="" />
			<view class="loginmount" @tap="city">
				<image src="../../static/pages/address.png" mode=""></image>
				<text>
					{{address?address:'请选择城市'}}
				</text>
			</view>
		</view>
		<view class="contentcenter" :style="Number(region)==2?'height:450rpx':'height:500rpx'">
			<view class="home" @tap="goonline">
				<image v-if="Number(region)==2" src="@/static/hkqianyue.png" mode=""></image>
				<image v-else src="@/static/qianyue.png" mode=""></image>
				<view class="name">
					家医签约
				</view>
			</view>
			<view class="home" @tap="goHealthrecords">
				<image v-if="Number(region)==2" src="@/static/hkhealth.png" mode=""></image>
				<image v-else src="@/static/health.png" mode=""></image>
				<view class="name">
					健康档案
				</view>
			</view>
			<view class="home" @tap="goappoint" v-if="Number(region)!=2">
				<image src="@/static/serveappoint.png" mode=""></image>
				<view class="name">
					服务预约
				</view>
			</view>
			<view class="home" @tap="count" v-if='region==2'>
				<image src="@/static/dyexchange.png" mode=""></image>
				<view class="name">
					东营健康银行
				</view>
			</view>
			<view class="home" @tap="count" v-if='region!=2'>
				<image src="@/static/exchange.png" mode=""></image>
				<view class="name">
					积分兑换
				</view>
			</view>
			<view class="home" @tap="myappointment" v-if="Number(region)!=2">
				<image src="@/static/appointmen.png" mode=""></image>
				<view class="name">
					我的预约
				</view>
			</view>
			<view class="home" @tap="servicerecord" v-if="Number(region)!=2">
				<image src="@/static/SERVERECOD.png" mode=""></image>
				<view class="name">
					服务记录
				</view>
			</view>
			<view class="home" @tap='gothreeHighsAndSixDiseases' v-if="Number(region)==2">
				<image src="@/static/threesix.png" mode=""></image>
				<view class="name">
					三高六病
				</view>
			</view>
			<view class="home" v-if="Number(region)==2" @tap='sign'>
				<image src="@/static/zizhujiance.png" mode=""></image>
				<view class="name">
					自主检测
				</view>
			</view>
			<view class="home" v-if="Number(region)==2" @tap="gothreeHundredAndSixty">
				<image src="@/static/360.png" mode=""></image>
				<view class="name">
					360健康画像
				</view>
			</view>
		</view>
		<view class="contentbottom" :style="Number(region)==2?'height:350rpx':''">
			<view class="health">
				健康管理
			</view>
			<view class="item">
				<view class="signmyself" @tap="sign" v-if="Number(region)!=2">
					<image src="@/static/tizheng.png" mode=""></image>
				</view>
				<view class="sign" :class="Number(region)==2?'notwo':''">
					<image @tap="healthtest" src="@/static/healthmyself.png" mode=""></image>
					<image @tap="gorecords" src="@/static/records.png" mode=""></image>
				</view>
			</view>
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {
		getOpenId,
		detail,
		checkSignApply,
		wxSportDecrypt,
		wxsportadd
	} from '@/api/pages/homepage/homepage.js'
	import {
		mapActions
	} from "vuex";
	import baseurl from '@/api/baseurl.js'
	export default {
		data() {
			return {
				userinfo: {
					cityCode: ''
				},
				infotitle: null,
				region: '',
				checkSign: null,
				address: '',
				openid: '',
				baseurl: '',
				sportlist: [],
				countyName: '',
			}
		},
		onLoad() {},
		onShow() {
			this.baseurl = baseurl
			this.userinfo = uni.getStorageSync('userinfo')
			this.address = uni.getStorageSync('location')
			this.openid = uni.getStorageSync('openid');
			this.region = Number(uni.getStorageSync('region'))
			this.detailinfo();
			if (this.userinfo && this.region != 3) {
				this.checkSignApplyinfo();
			}
			if (uni.getStorageSync('userinfo')) {
				this.wxSportDecryptinfo();
			}
		},
		methods: {
			//查询签约机构
			detailinfo() {
				detail(this.userinfo.cardNo).then(res => {
					if (res.code == 200) {
						this.countyName = res.data.countyName
					}
				})
			},
			wxSportDecryptinfo() {
				let that = this
				let currentDate = new Date();
				let year = currentDate.getFullYear();
				let month = currentDate.getMonth() + 1;
				let day = currentDate.getDate();
				month >= 1 && month <= 9 ? (month = "0" + month) : "";
				day >= 0 && day <= 9 ? (day = "0" + day) : "";
				let time = `${year}-${month}-${day}`
				let identtime = `${this.userinfo.cardNo}${year}-${month}-${day}`
				if (uni.getStorageSync('sportlist')) {
					if (uni.getStorageSync('sportlist')[0].date != time) {
						uni.removeStorageSync('sportlist');
					} else {
						this.sportlist = uni.getStorageSync('sportlist');
					}
				}
				if (!this.sportlist.some(e => e.text == identtime)) {
					wx.getSetting({
						success: function(res) {
							// 开启微信运动授权
							if (res.authSetting['scope.werun']) {
								// 在小程序中获取微信步数
								wx.login({
									success: function(res) {
										if (res.code) {
											wx.getWeRunData({
												success: function(resp) {
													that.wxSport(res, resp)
												}
											});
										}
									}
								});
							}
						}
					})
				}
			},
			wxSport(res, resp) {
				let obj = {
					code: res.code,
					encryptedData: resp.encryptedData,
					iv: resp.iv
				}
				wxSportDecrypt(obj).then(Sportres => {
					if (Sportres.data && Sportres.data[0].step >= 5000) {
						wxsportadd(this.userinfo.cardNo, Sportres.data[0].step).then(sportaddres => {
							if (sportaddres.code == 200) {
								let sportitem = {
									text: `${this.userinfo.cardNo}${Sportres.data[0].date}`,
									date: Sportres.data[0].date
								}
								this.sportlist.unshift(sportitem)
								uni.setStorageSync('sportlist', this.sportlist);
							}
						})
					}
				})
			},
			checkSignApplyinfo() {
				checkSignApply(this.userinfo.cardNo).then(res => {
					if (res.data) {
						this.checkSign = res.data.code
						this.infotitle = res.data.info
					}
				})
			},
			...mapActions(["openPopup"]),
			//跳转选择地址
			city() {
				uni.navigateTo({
					url: '/pagesB/location/location'
				})
			},
			// 登录
			login() {
				uni.navigateTo({
					url: "/pagesB/login/login"
				})
			},
			gologin() {
				this.$refs.uToast.show({
					title: '您未登录,请先登录',
					type: 'error',
					duration: '1000',
					url: '/pagesB/login/login'
				})
			},
			// 家医功能提示
			family() {
				this.$refs.uToast.show({
					title: '当前绑定地市不支持,请点击左上角选择城市',
					type: 'error',
					duration: '1500',
				})
			},
			// 家医签约
			goonline() {
				this.openPopup();
				setTimeout(e => {
					if (!this.userinfo) {
						this.gologin();
					} else if (this.userinfo) {
						if (this.region == 3) {
							this.family()
						} else {
							if (this.region == 1 || this.region == 2) {
								if (this.checkSign == "0") {
									uni.navigateTo({
										url: `/pagesC/Onlinesigning/Onlinesigning?checkSign=${this.checkSign}`,
									})
								} else if (this.checkSign == "01") {
									this.$refs.uToast.show({
										title: this.infotitle,
										duration: '1500',
										type: 'error',
									})
								} else {
									uni.navigateTo({
										url: "/pagesB/mysigning/mysigning"
									})
								}
							} else {
								this.family()
							}
						}
					}
				}, 50)
			},
			// 积分兑换
			count() {
				if (!this.userinfo) {
					this.gologin();
				} else {
					if (this.region == 3) {
						this.family()
					} else {
						if (this.region == 1 || this.region == 2) {
							if (this.checkSign == "0") {
								uni.navigateTo({
									url: `/pagesC/Onlinesigning/Onlinesigning?checkSign=${this.checkSign}`,
								})
							} else if (this.checkSign == "01") {
								this.$refs.uToast.show({
									title: this.infotitle,
									type: 'error',
									duration: '1500',
								})
							} else {
								uni.navigateTo({
									url: '/pagesB/Behaviorpoints/Behaviorpoints'
								})
							}
						} else {
							this.family()
						}
					}
				}
			},
			// 健康档案
			goHealthrecords() {
				if (!this.userinfo) {
					this.gologin();
				} else {
					if (this.region == 3) {
						this.family()
					} else {
						uni.navigateTo({
							url: "/pagesC/Healthrecords/Healthrecords"
						})
					}
				}
			},
			// 筛查记录
			gorecords() {
				if (!this.userinfo) {
					this.gologin();
				} else {
					uni.navigateTo({
						url: "/pagesC/Screeningrecords/Screeningrecords"
					})
				}
			},
			// 健康自评
			healthtest() {
				if (!this.userinfo) {
					this.gologin();
				} else {
					uni.navigateTo({
						url: "/pagesC/healthtest/healthtest"
					})
				}
			},
			// 体征检测
			sign() {
				if (!this.userinfo) {
					this.gologin();
				} else {
					uni.navigateTo({
						url: "/pagesC/Physicalexamination/Physicalexamination"
					})
				}
			},
			// 服务预约
			goappoint() {
				this.openPopup();
				setTimeout(e => {
					if (!this.userinfo) {
						this.gologin();
					} else {
						if (this.region == 3) {
							this.family()
						} else {
							if (this.region == 1 || this.region == 2) {
								if (this.checkSign == "0") {
									uni.navigateTo({
										url: `/pagesC/Onlinesigning/Onlinesigning?checkSign=${this.checkSign}`,
									})
								} else if (this.checkSign == "01") {
									this.$refs.uToast.show({
										title: this.infotitle,
										duration: '1500',
										type: 'error',
									})
								} else {
									uni.navigateTo({
										url: "/pagesC/ServiceAppointment/ServiceAppointment"
									})
								}
							} else {
								this.family()
							}
						}
					}
				}, 50)
			},
			// 服务记录
			servicerecord() {
				if (!this.userinfo) {
					this.gologin();
				} else {
					if (this.region == 3) {
						this.family()
					} else {
						if (this.region == 1 || this.region == 2) {
							if (this.checkSign == "0") {
								uni.navigateTo({
									url: `/pagesC/Onlinesigning/Onlinesigning?checkSign=${this.checkSign}`,
								})
							} else if (this.checkSign == "01") {
								this.$refs.uToast.show({
									title: this.infotitle,
									type: 'error',
									duration: '1500',
								})
							} else {
								uni.navigateTo({
									url: "/pagesC/servicerecord/servicerecord"
								})
							}
						} else {
							this.family()
						}
					}
				}
			},
			// 我的预约
			myappointment() {
				if (!this.userinfo) {
					this.gologin();
				} else {
					if (this.region == 3) {
						this.family()
					} else {
						if (this.region == 1 || this.region == 2) {
							if (this.checkSign == "0") {
								uni.navigateTo({
									url: `/pagesC/Onlinesigning/Onlinesigning?checkSign=${this.checkSign}`,
								})
							} else if (this.checkSign == "01") {
								this.$refs.uToast.show({
									title: this.infotitle,
									type: 'error',
									duration: '1500',
								})
							} else {
								uni.navigateTo({
									url: "/pagesC/Myappointment/Myappointment"
								})
							}
						} else {
							this.family()
						}
					}
				}
			},
			gothreeHighsAndSixDiseases() {
				if (!this.userinfo) {
					this.gologin();
				} else {
					uni.navigateTo({
						url: "/pagesB/threeHighsAndSixDiseases/threeHighsAndSixDiseases"
					})
				}
			},
			gothreeHundredAndSixty() {
				if (!this.userinfo) {
					this.gologin();
				} else {
					uni.navigateTo({
						url: "/pagesB/threeHundredAndSixty/threeHundredAndSixty"
					})
				}
			},
		}
	}
</script>

<style lang="scss">
	@import "./homepage.scss";
</style>