<template>
	<view class="app">
		<view class="topbanner">
			<!-- 	<view class="title">
				我的
			</view> -->
			<image :src="baseurl + '/profile/weChatPicture/background.png'" mode="" class="userbanner" />
			<image :src="baseurl+ '/profile/weChatPicture/headsculpture.png'" class="headsculpture" @tap='updatainfo'
				v-if="!appPersonallist.headPictureUrl" />
			<image :src="baseurl+appPersonallist.headPictureUrl" mode="" class="headsculpture" @tap='updatainfo'
				v-else />
			<view class="namesigning" @tap='updatainfo' v-if="appPersonallist.patientName">
				<span class="name">
					{{appPersonallist.patientName}}
				</span>
				<span class="signing" v-if="appPersonallist.signNo">
					已签约
				</span>
				<span class="signing" v-else>
					未签约
				</span>
			</view>
			<view class="nologin namesigning" @tap='updatainfo' v-else>
				点击登录
			</view>
			<view class="phone" v-if="appPersonallist.phone">
				{{appPersonallist.phone}}
			</view>
			<view class="switch btn" @tap='getpersnoal'>
				切换家庭成员
			</view>
			<view class="manage btn" @click="gomanagefamily">
				管理家庭成员
			</view>
		</view>
		<view class="Threecategories">
			<view class="item" @tap="goBehaviorpoints">
				<image src="@/static/jifen.png" mode=""></image>
				<view class="text">
					健康积分
					<span style="padding: 0 10rpx;"> {{appPersonallist.score>0?appPersonallist.score:0}}</span>
				</view>
			</view>
			<!-- <view class="item" @tap='gointegral'>
				<view class="number">
					{{appPersonallist.integral>0?appPersonallist.integral:0}}
				</view>
				<view class="text">
					健康豆
				</view>
			</view> -->
			<view class="item" @tap='gocoupon'>
				<image src="@/static/yhj.png" mode=""></image>
				<view class="text">
					优惠券
					<span
						style="padding: 0 10rpx;">{{appPersonallist.patientCouponCount>0?appPersonallist.patientCouponCount:0}}</span>
					张
				</view>
			</view>
		</view>
		<view class="myorder" style="margin-top: 120rpx;">
			<view class="title">
				我的订单
			</view>
			<view class="righttitle" @tap="goorder(0,'')">
				查看全部
				<image src="@/static/huijiantou.png" mode=""></image>
			</view>
			<view class="orderStatus">
				<view class="item" @tap="goorder(1,'WAIT_PAY')">
					<image src="@/static/daifukuan.png" mode=""></image>
					<view class="text">
						待付款
					</view>
					<view class="orderCount" v-if="appPersonallist.waitPayCount>0&&appPersonallist.waitPayCount<100">
						{{appPersonallist.waitPayCount}}
					</view>
					<view class="orderCount" style="width: 57rpx;border-radius: 17rpx;right:25rpx"
						v-if="appPersonallist.waitPayCount>=100">
						99+
					</view>
				</view>
				<view class="item" @tap="goorder(2,'WAIT_RECEIVED_GOODS')">
					<image src="@/static/daishouhuo.png" mode=""></image>
					<view class="text">
						待收货
					</view>
					<view class="orderCount"
						v-if="appPersonallist.waitReceivedGoodsCount>0&&appPersonallist.waitReceivedGoodsCount<100">
						{{appPersonallist.waitReceivedGoodsCount}}
					</view>
					<view class="orderCount" style="width: 57rpx;border-radius: 17rpx;right:25rpx"
						v-if="appPersonallist.waitReceivedGoodsCount>=100">
						99+
					</view>
				</view>
				<view class="item" @tap="goorder(3,'RECEIVED_GOODS')">
					<image src="@/static/daipingjia.png" mode=""></image>
					<view class="text">
						待评价
					</view>
					<view class="orderCount"
						v-if="appPersonallist.receivedGoodsCount>0&&appPersonallist.receivedGoodsCount<100">
						{{appPersonallist.receivedGoodsCount}}
					</view>
					<view class="orderCount" style="width: 57rpx;border-radius: 17rpx;right:25rpx"
						v-if="list.receivedGoodsCount>=100">
						99+
					</view>
				</view>
				<view class="item" @tap="goorder(4,'EVALUATED')">
					<image src="@/static/yiwancheng.png" mode=""></image>
					<view class="text">
						已完成
					</view>
					<!-- <view class="orderCount" v-if="appPersonallist.evaluatedCount>0&&appPersonallist.evaluatedCount<100">
						{{appPersonallist.evaluatedCount}}
					</view>
					<view class="orderCount" v-if="appPersonallist.evaluatedCount>=100">
						99+
					</view> -->
				</view>
			</view>
		</view>
		<view class="myorder">
			<view class="title">
				常用功能
			</view>
			<view class="orderStatus">
				<!-- <view class="item" @tap='goHealthrecords'>
					<image  :src="require('@/pagesC/images/jiankangdangan.png')"mode=""></image>
					<view class="text">
						健康档案
					</view>
				</view> -->
				<view class="item" @tap="gomyappiont">
					<image src="@/static/wodeyuyue.png" mode=""></image>
					<view class="text">
						我的预约
					</view>
				</view>
				<view class="item" @tap="evaluation">
					<image src="@/static/fuwupingjia.png" mode=""></image>
					<view class="text">
						服务评价
					</view>
				</view>
				<view class="item"></view>
			</view>
		</view>
		<view class="myorder titles" @tap="gonursestation">
			<view class="title">
				护理服务订单
			</view>
			<view class="righttitle">
				<image src="@/static/huijiantou.png" mode=""></image>
			</view>
		</view>
		<view class="myorder titles" @tap="expertOrder">
			<view class="title">
				专家咨询订单
			</view>
			<view class="righttitle">
				<image src="@/static/huijiantou.png" mode=""></image>
			</view>
		</view>
		<view class="myorder titles" @tap="Signing">
			<view class="title">
				签约信息
			</view>
			<view class="righttitle">
				<image src="@/static/huijiantou.png" mode=""></image>
			</view>
		</view>
		<view class="myorder titles" @tap="remove()">
			<view class="title">
				退出登录
			</view>
			<view class="righttitle">
				<image src="@/static/huijiantou.png" mode=""></image>
			</view>
		</view>
		<view class="mask" @click.stop='showhome = false' v-if="showhome">
			<view class="masktext" @click.stop=''>
				<view class="maskcontent" v-if="familyList">
					<template v-for="item in familyList">
						<view :class="item.cardNo == appPersonallist.cardNo ? 'p1' : 'p2'"
							@click.stop="changeFamilyInfo(item)">
							<text>{{item.patientName}}</text>
							<text
								v-if="item.cardNo">{{item.cardNo.substring(0,4)}}************{{item.cardNo.substring(16,18)}}</text>
						</view>
					</template>
				</view>
				<view class="add" @click.stop="addFamilyItem">
					添加家庭成员
				</view>
			</view>
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {
		appPersonal,
		getFamilyList,
		registerdata,
		getCurrentUser,
		switchResident
	} from '@/api/pages/myinformation/myinformation.js';
	import {
		logingetCurrentUser,
	} from '@/api/pages/homepage/homepage.js'
	import {
		login
	} from '@/api/pages/login/index.js'
	import {
		existPatientInfo
	} from '@/api/pages/startup/index.js'
	import {
		checkSignApply
	} from '@/api/pages/homepage/homepage.js'
	import {
		mapMutations
	} from "vuex";
	import baseurl from '@/api/baseurl.js'
	export default {
		data() {
			return {
				timer: undefined,
				baseurl: '',
				appPersonallist: null, //获取个人信息
				list: {},
				showhome: false, //切换
				familyList: null, //切换列表
				region: '', //城市
				infotitle: null,
				checkSign: null,
			};
		},
		onShow() {
			this.appPersonallist = uni.getStorageSync('userinfo')
			this.region = Number(uni.getStorageSync('region'))
			this.baseurl = baseurl
			this.myInfo()
			if (this.appPersonallist && this.region != 3) {
				checkSignApply(this.appPersonallist.cardNo).then(res => {
					if (res.data) {
						this.checkSign = res.data.code
						this.infotitle = res.data.info
					}
				})
			}
		},
		methods: {
			gomyappiont() {
				uni.navigateTo({
					url: '/pagesC/Myappointment/Myappointment'
				})
			},
			// 退出登录
			remove() {
				let that = this
				const value = uni.getStorageSync('patientId');
				if (value) {
					uni.showModal({
						title: '提示',
						content: '确认要退出此账号吗',
						success: function(res) {
							if (res.confirm) {
								that.$refs.uToast.show({
									title: '退出账号成功',
									type: 'success',
									duration: '2000'
								})
								// uni.clearStorageSync();、
								that.removes()
								uni.closeSocket();
								clearInterval(that.$store.state.timeoutObj);
							}
						}
					});
				} else {
					that.$refs.uToast.show({
						title: '您未登录,请先登录',
						type: 'error',
						duration: '1000'
					})
				}
			},
			// 切换家庭成员
			getpersnoal() {
				const value = uni.getStorageSync('userinfo');
				const value2 = uni.getStorageSync('patientId');
				if (value && value2) {
					const openid = uni.getStorageSync('openid');
					if (openid) {
						getCurrentUser(openid).then(res => {
							this.familyList = res.data
							this.showhome = true
						})
					}
					// this.myInfo()
				} else {
					this.gologin();
				}
			},
			// 切换成员
			changeFamilyInfo(e) {
				const value = uni.getStorageSync('openid');
				const value2 = uni.getStorageSync('patientId');
				if (value && value2) {
					switchResident(value, e.patientCode).then(res => {
						if (res.code == 200) {
							this.appPersonallist = res.data
							uni.setStorageSync('userinfo', res.data);
							uni.setStorageSync('patientId', res.data.id);
							uni.setStorageSync('phone', res.data.phone);
							uni.setStorageSync('patientName', res.data.patientName);
							this.$refs.uToast.show({
								title: '切换成功',
								type: 'success',
								duration: '1500',
							})
							clearInterval(this.$store.state.timeoutObj);
							uni.closeSocket();
						} else {
							this.$refs.uToast.show({
								title: res.msg,
								type: 'error',
								duration: '1000',
							})
						}
					})
					this.showhome = false
				}
			},
			removes() {
				this.appPersonallist = null
				uni.removeStorageSync('patientId');
				// uni.removeStorageSync('openid');
				uni.removeStorageSync('phone');
				uni.removeStorageSync('userinfo');
			},
			//获取个人信息
			myInfo() {
				var that = this
				const value = uni.getStorageSync('openid');
				const value2 = uni.getStorageSync('patientId');
				if (value && value2) {
					// existPatientInfo(value).then(res => {
					// 	if (res.code == 200 && res.msg == 'LOGIN') {
					appPersonal(value2).then(Response => {
						if (Response.code == 200) {
							uni.setStorageSync('userinfo', Response.data);
							that.appPersonallist = Response.data
							that.appPersonallist.homeLatitude = Number(that.appPersonallist
								.homeLatitude)
							that.appPersonallist.homeLongitude = Number(that.appPersonallist
								.homeLongitude)
						} else {
							that.removes();
						}
					})
				}
				// })
				// } else {
				// that.removes();
				// }
			},
			//获取当前位置
			getAddress() {
				let that = this;
				uni.getLocation({
					type: 'wgs84',
					success: (res) => {
						let location = res.latitude + ',' + res.longitude
						//调用逆解析接口
						uni.request({
							url: 'https://apis.map.qq.com/ws/geocoder/v1/?location=location', //腾讯官方逆解析接口，可直接复制
							data: {
								location: location,
								'key': "7Y7BZ-PVXRA-IPYKT-C6YKF-YS3VF-LMFWJ", //自己申请的Key
							},
							method: "GET",
							success(res) {
								if (!res.data.result) {
									uni.setStorageSync("region", 2)
									uni.setStorageSync("location", '东营市')
								} else {
									var address = res.data.result.address_component.city
									if (address == '德州市') {
										uni.setStorageSync("region", 1)
									} else if (address == '东营市') {
										uni.setStorageSync("region", 2)
									} else if (address == '济南市') {
										uni.setStorageSync("region", 3)
									}
									uni.setStorageSync('location', res.data.result.address_component
										.city)
								}
							},
							fail: res => {
								console.log(res.errMsg, '解析失败返回的错误信息');
							}
						})
					},
					//失败
					fail: (res) => {
						uni.showToast({
							icon: 'none',
							title: '获取地址失败,请打开定位',
						})
					}
				})
			},
			sportinfo() {
				wx.getWeRunData({
					success: function(res) {}
				});
			},
			...mapMutations(['scoket']),
			updatainfo() {
				let that = this;
				const value = uni.getStorageSync('userinfo');
				const value2 = uni.getStorageSync('patientId');
				if (value && value2) {
					uni.navigateTo({
						url: `/pagesB/modify/modify`
					})
				} else {
					if (!uni.getStorageSync('openid') && !value2) {
						if (!uni.getStorageSync('location') || !uni.getStorageSync('region')) {
							that.getAddress()
						}
						that.sportinfo();
						wx.login({
							provider: 'weixin',
							success: function(loginRes) {
								login(loginRes.code).then(resp => {
									if (!resp.data) {
										that.$refs.uToast.show({
											title: '登录失败',
											type: 'error',
										})
										return
									}
									if (resp.data.code == '0') {
										that.$refs.uToast.show({
											title: '未注册,请先注册',
											type: 'error',
											duration: '1500',
										})
										if (that.timer) {
											clearTimeout(that.timer)
										}
										that.timer = setTimeout(() => {
											uni.navigateTo({
												url: `/pagesB/register/register?openid=${resp.data.openid}`,
											})
										}, 1500)
									} else if (resp.data.code == '2') {
										that.$refs.uToast.show({
											title: '已被其他人注册',
											type: 'error',
											duration: '1500',
										})
									} else if (resp.data.code == '1') {
										if (resp.data.openid) {
											uni.setStorageSync("openid", resp.data.openid)
										}
										that.outlogin(that, resp.data.openid)
									} else {
										that.$refs.uToast.show({
											title: '登录失败',
											type: 'error',
										})
									}
								})
							}
						});
					} else {
						that.outlogin(that, uni.getStorageSync('openid'))
					}
				}
			},
			outlogin(that, openid) {
				logingetCurrentUser(openid).then(res => {
					if (res.code == 200) {
						if (res.data) {
							that.appPersonallist = res.data
							uni.setStorageSync('patientId', res.data.id);
							uni.setStorageSync('userinfo', res.data);
							uni.setStorageSync('phone', res.data
								.phone);
							that.scoket();
							that.$refs.uToast.show({
								title: '登录成功',
								type: 'success',
								back: 1
							})
						} else {
							that.$refs.uToast.show({
								title: '未查询到信息，请先去注册',
								type: 'error',
								duration: '1500',
							})
							if (that.timer) {
								clearTimeout(that.timer)
							}
							that.timer = setTimeout(() => {
								uni.navigateTo({
									url: `/pagesB/register/register?openid=${uni.getStorageSync('openid')}`,
								})
							}, 1500)
						}
					} else {
						that.$refs.uToast.show({
							title: '登录失败',
							type: 'error',
						})
					}
				})
			},
			//护理站服务订单
			gonursestation() {
				const value = uni.getStorageSync('userinfo');
				const value2 = uni.getStorageSync('patientId');
				if (value && value2) {
					uni.navigateTo({
						url: '/pagesB/Nursingstationserviceorder/Nursingstationserviceorder'
					})
				} else {
					this.gologin();
				}
			},
			// 服务评价
			evaluation() {
				uni.navigateTo({
					url: '/pagesB/Serviceevaluation/Serviceevaluation'
				})
			},
			// 家医功能提示
			family() {
				this.$refs.uToast.show({
					title: '当前绑定地市不支持',
					type: 'error',
					duration: '3000',
				})
			},
			// 签约信息
			Signing() {
				if (!this.appPersonallist) {
					this.gologin();
				} else if (this.appPersonallist) {
					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}`,
								})
								// this.$refs.uToast.show({
								// 	title: '未签约，请先签约',
								// 	type: 'error',
								// 	url: "/pagesC/Onlinesigning/Onlinesigning"
								// })
							} else if (this.checkSign == "01") {
								this.$refs.uToast.show({
									title: this.infotitle,
									type: 'error',
								})
							} else {
								uni.navigateTo({
									url: "/pagesB/mysigning/mysigning"
								})
							}
						} else {
							this.family()
						}
					}
				}
			},
			//优惠券
			gocoupon() {
				const value = uni.getStorageSync('userinfo');
				const value2 = uni.getStorageSync('patientId');
				if (value && value2) {
					uni.navigateTo({
						url: '/pagesB/coupon/coupon'
					})
				} else {
					this.gologin();
				}
			},
			//健康档案
			goHealthrecords() {
				uni.navigateTo({
					url: '/pagesC/Healthrecords/Healthrecords'
				})
			},
			//全部订单
			goorder(index, item) {
				const value = uni.getStorageSync('userinfo');
				const value2 = uni.getStorageSync('patientId');
				if (value && value2) {
					uni.navigateTo({
						url: `/pagesB/CommodityOrder/CommodityOrder?toindex=${index}&&orderStatus=${item}`
					})
				} else {
					this.gologin();
				}
			},
			//去登陆
			gologin() {
				this.$refs.uToast.show({
					title: '您未登录,请点击上方登录',
					type: 'error',
				})
			},
			//护理站服务订单
			gonursestation() {
				const value = uni.getStorageSync('userinfo');
				const value2 = uni.getStorageSync('patientId');
				if (value && value2) {
					uni.navigateTo({
						url: '/pagesB/Nursingstationserviceorder/Nursingstationserviceorder'
					})
				} else {
					this.gologin();
				}
			},
			//健康积分
			goBehaviorpoints() {
				const value = uni.getStorageSync('userinfo');
				const value2 = uni.getStorageSync('patientId');
				if (value && value2) {
					if (this.region == 3) {
						this.family()
					} else {
						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',
							})
						} else {
							uni.navigateTo({
								url: '/pagesB/Behaviorpoints/Behaviorpoints'
							})
						}
					}
				} else {
					this.gologin();
				}
			},
			//管理家庭成员
			gomanagefamily() {
				const value = uni.getStorageSync('userinfo');
				const value2 = uni.getStorageSync('patientId');
				const value3 = uni.getStorageSync('openid');
				if (value && value2 && value3) {
					uni.navigateTo({
						url: '/pagesB/managefamily/managefamily'
					})
				} else {
					this.gologin();
				}
			},
			// 添加家庭成员
			addFamilyItem() {
				if (uni.getStorageSync('openid')) {
					uni.navigateTo({
						url: '/pagesB/AddMembers/AddMembers'
					})
					this.showhome = false
				}
				// if (!this.userInfo) {
				//   uni.showModal({
				//     title: "提示",
				//     content: "您还未注册，是否去注册？",
				//     confirmText: '是',
				//     cancelText: '否',
				//     success(res) {
				//       if (res.confirm) {
				//       } else {
				//         wx.exitMiniProgram()
				//       }
				//     },
				//   })
				// } else {
				//   this.getSetting()
				// }
			},
			//积分页面
			gointegral() {
				const value = uni.getStorageSync('userinfo');
				const value2 = uni.getStorageSync('patientId');
				if (value && value2) {
					uni.navigateTo({
						url: `/pagesB/healthybeans/healthybeans?integral=${this.appPersonallist.integral}`
					})
				} else {
					this.gologin();
				}
			},
			// 专家咨询订单
			expertOrder() {
				uni.navigateTo({
					url: "/pagesB/ExpertlookOrder/ExpertlookOrder"
				})
			},
		}
	}
</script>

<style lang="scss">
	@import './myinformation.scss'
</style>