<template>
	<view class="content">
		<view class="cont">
			<view class="bg">
				<image src="https://fs-1319721001.cos.ap-chongqing.myqcloud.com/fs/20240229/bbe743373f4d4b78852ea5fd9824ade4.png"></image>
			</view>
			<view class="cont-box">
				<view class="status_bar" :style="{height: statusBarHeight}"></view>
				<view class="top-title">
				</view>
				<view class="user"  @click.stop="loginNavTo('/pages_user/personInfo')">
					<view class="left"  >
						<image   :src="user.avatar==null?'https://fs-1319721001.cos.ap-chongqing.myqcloud.com/fs/20240229/90d9eb0f8f87482b977611eb36b66d82.jpg':user.avatar"></image>
					</view>
					<view class="right" >
						<view class="name-box" >
							<view class="name">{{user.nickName}}</view>
							<view class="btns" >
								<image src="../../static/images/icon_set.png"></image>
							</view>
						</view>
						<view class="phone" v-if="user.phone!=''">{{$parsePhone(user.phone)}}</view>
						<view class="counts">
							<view class="count" @click="loginNavTo('/pages_user/integral')">积分 {{user.integral}}</view>
							<!-- <view class="count">我的健康金 {{user.balance}}</view> -->
						</view>
					</view>
				</view>
				<view class="menus">
					<view class="title-box"  >
						<view class="line"></view>
						<view class="title">我的订单</view>
					</view>
					<view class="menu-box">
						<view class="menu" @click="loginNavTo('/pages_order/inquiryOrderList')">
							<image src="../../static/images/icon_inquiry_order.png"></image>
							<view class="title">咨询订单</view>
						</view>
						<view class="menu" @click="loginNavTo('/pages_order/storeOrderList')">
							<image src="../../static/images/icon_store_order.png"></image>
							<view class="title">处方订单</view>
						</view>
						<view class="menu" @click="loginNavTo('/pages_order/packageOrderList')">
							<image src="../../static/images/icon_qianyue.png"></image>
							<view class="title">服务包</view>
						</view>
						<view class="menu" @click="loginNavTo('/pages_order/storeOrderRefundList')">
							<image src="../../static/images/sales_orders_icon.png"></image>
							<view class="title">售后服务</view>
						</view>
						 
					</view>
					
				</view>
				<view class="menus">
					<view class="title-box">
						<view class="line"></view>
						<view class="title">医疗服务</view>
					</view>
					<view class="menu-box">
						<view class="menu" @click="loginNavTo('/pages_user/myDoctorList')">
							<image src="../../static/images/icon_my_doctor.png"></image>
							<view class="title">我的医生</view>
						</view>
						<view class="menu" @click="loginNavTo('/pages_order/prescribeList')">
							<image src="../../static/images/icon_my_prescription.png"></image>
							<view class="title">我的处方</view>
						</view>
						<view class="menu" @click="loginNavTo('/pages_user/followList')">
							<image src="../../static/images/icon_my_follow.png"></image>
							<view class="title">我的随访</view>
						</view>
						<view class="menu"  @click="toIM()">
							<image src="https://cos.his.cdwjyyh.com/fs/20240429/6e68b42d9d824532945a1dda73350576.png"></image>
							<view class="title">用药咨询</view>
						</view>
						<view class="menu"  @click="navTo('/pages_user/drugReportList')" >
							<image src="https://cos.his.cdwjyyh.com/fs/20240429/6b9f340b16e74fa2bc095265d525769e.png"></image>
							<view class="title">用药报告</view>
						</view>
					</view>
					
				</view>
				<view class="menus">
					<view class="title-box">
						<view class="line"></view>
						<view class="title">常用应用</view>
					</view>
					<view class="menu-box">
						<view class="menu" @click="loginNavTo('/pages_user/patient')">
							<image class="min-image" src="../../static/images/icon_visitor_management.png"></image>
							<view class="title">就诊管理</view>
						</view>
						
						<view class="menu" @click="loginNavTo('/pages_user/address')">
							<image class="min-image" src="../../static/images/icon_address_management.png"></image>
							<view class="title">地址管理</view>
						</view>
						<view class="menu"  @click="loginNavTo('/pages_user/healthRecords/index')">
							<image class="min-image" src="../../static/images/icon_visitor_management.png"></image>
							<view class="title">健康档案</view>
						</view>
						<view class="menu"  @click="loginNavTo('/pages_user/tongue/tongueList')">
							<image class="min-image" src="../../static/images/icon_visitor_management.png"></image>
							<view class="title">舌苔报告</view>
						</view>
						 
						<view class="menu"  @click="loginNavTo('/pages_user/addHealthButler')">
							<image class="min-image" src="../../static/images/icon_my_doctor.png"></image>
							<view class="title">健康管家</view>
						</view>
						
						
						
					</view>
					<view class="menu-box">
						<view class="menu" @click="loginNavTo('/pages_user/myCouponList')">
							<image class="min-image" src="../../static/images/icon_my_coupon.png"></image>
							<view class="title">我的优惠卷</view>
						</view>
						<view class="menu"  @click="loginNavTo('/pages_company/couponList?couponType=6')">
							<image class="min-image" src="../../static/images/icon_coupon.png"></image>
							<view class="title">领券中心</view>
						</view>
						<view class="menu" @click="loginNavTo('/pages_user/cert')">
							<image class="min-image" src="../../static/images/cert.png"></image>
							<view class="title">资质证书</view>
						</view>
						<view class="menu"  @click="loginNavTo('/pages_user/price')">
							<image class="min-image" src="https://cos.his.cdwjyyh.com/fs/20240808/415e1a6a58ec49ffaa1bb62cb376d4e8.png"></image>
							<view class="title">价格公示</view>
						</view>
					
					</view> 
					
					<view class="menu-box">
						<view class="menu"  >
							<image class="min-image" src="../../static/images/icon_service_center.png"></image>
							<view class="title">客服中心</view>
							<button class='contact-btn' open-type="contact">
							</button>
						</view>
						<view class="menu" @click="navTo('/pages_user/about')">
							<image class="min-image" src="../../static/images/icon_feedback.png"></image>
							<view class="title">关于我们</view>
						</view>
					</view>
					
					
				</view>
				<view class="menus">
					<view class="title-box">
						<view class="line"></view>
						<view class="title">管理端</view>
					</view>
					<view class="menu-box">
						<view class="menu" @click="loginNavTo('/pages_user/registerDoctor?type=1')">
							<image class="min-image" src="/static/images/doctor_reg.png"></image>
							<view class="title">医生入驻</view>
						</view>
						<view class="menu" @click="loginNavTo('/pages_user/registerDoctor?type=2')">
							<image class="min-image" src="/static/images/doctor_reg.png"></image>
							<view class="title">药师入驻</view>
						</view>
						<view class="menu" @click="toCompany()">
							<image class="min-image" src="/static/images/sales.png"></image>
							<view class="title">客服登录</view>
						</view>
						 
					</view>
				</view>
				<view class="btn-box" v-if="isLogin">
					<view class="sub-btn" @click="showLogout()">退出登录</view>
				</view>
 
				<view class="tip"  >
					<view class="title">技术支持 重庆云联融智科技有限公司 </view>
					<!-- <view class="tel">联系电话 18696558100 </view> -->
				</view>
			</view>
		</view>
		<u-popup bgColor="#f6f6f6" :safeAreaInsetBottom="false"  :round="15" mode="bottom" :show="wxShow" @close="wxAuthClose" @open="wxAuthOpen">
			 <view class="wxAuth">
				 <view class="title">编辑头像和昵称</view>
				 <wx-auth class="wx-box" @updateUser="updateUser()" ref="wxauth"   ></wx-auth>
			 </view>
			 
		</u-popup>
		<u-modal :show="show" title="提示" :showCancelButton="true"  @cancel="hideLogout()" @confirm="logout()" content='确认退出吗?'></u-modal>
	</view>
</template>
<script>
	import {getUserInfo} from '@/api/user.js'
	import store from "@/store";
	import {wxAuth} from '../components/wxAuth.vue';
	import {startDrugReport,getUserFollowDoctor} from '@/api/drugReport';
	export default {
		components:{
			wxAuth
		},
		data() {
			return {
				wxShow:false,
				isLogin:false,
				show:false,
				statusBarHeight: uni.getStorageSync('menuInfo').statusBarHeight,
				user:{
					nickName:"登录/注册",
					phone:"",
					integral:0,
					balance:0.00
				}
			}
		},
		onLoad() {
			var that=this;
		 
		},
		onShow() {
			var that=this;
			this.$isLogin().then(
				res => {
					if(res){
						uni.$emit('refreshIM')
						this.getUserInfo();
					}
				},
				rej => {}
			);
			
		},
		onReachBottom() {
		},
		onPageScroll(e){
			
		},
		methods: {
			updateUser(){
				this.wxShow=false;
				this.getUserInfo()
			},
			wxAuthOpen(){
				this.wxShow=true;
				var that=this;
				setTimeout(function(){
					that.$refs.wxauth.getUserInfo();
				})
				
			},
			wxAuthClose(){
				this.wxShow=false;
			},
			doIM(){
				console.log(110)
				uni.showLoading({
					title:"处理中..."
				})
				getUserFollowDoctor().then(
					res => {
						if(res.code==200){
							if(res.data!=null){
								var data=res.data;
								var param={followId:res.data.followId};
								startDrugReport(param).then(
									res => {
										uni.hideLoading()
										//
										// uni.switchTab({
										// 	url:"/pages/TUIKit/TUIPages/TUIConversation/index"
										// })
										var id="C2CD-"+data.followDoctorId;
										store.commit("timStore/setOrderId",data.orderId);
										store.commit("timStore/setFollowId",data.followId);
										store.commit("timStore/setImType", 2);
										store.commit("timStore/setConversationID", id);
										//uni.$TUIKit.TUIChatServer.updateStore(conversationId)
										uni.$TUIKit.TUIConversationServer.setMessageRead(id);
										uni.$TUIKit.TUIConversationServer.getConversationProfile(id)
										  .then((res) => {
												console.log(res)
												// uni.$TUIKit.TUIConversationServer.setConversationValue(id,that.orderId.toString())
												//   .then((res) => {
												// 	  console.log("更新order")
												// })
												// .catch((err) => {
												     
												// });
												//通知 TUIChat 关闭当前会话
												const { conversation } = res.data;
												store.commit("timStore/setConversation", conversation);
												let url = "/pages/TUIKit/TUIPages/TUIChat/index";
												const { nick: name } = conversation.userProfile;
												url = `${url}?conversationName=${
													conversation.userProfile.nick?.nick ||
													conversation.userProfile.userID
												}`;
												uni.redirectTo({ url });
												
											})
										.catch((err) => {
											uni.hideLoading()
										    console.warn("获取 group profile 异常 = ", err);
										});
									},
									rej => {}
								);
								
							}
							else{
								uni.hideLoading()
								uni.showToast({
									icon:'none',
									title: "当前没有药师为您服务",
								});
							}
						}else{
							uni.hideLoading()
							uni.showToast({
								icon:'none',
								title: "请求失败",
							});
						}
					},
					rej => {}
				);
			},
			toIM(){
				var that=this;
				this.$isLogin().then(
					res => {
						console.log(res)
						if(res){
							that.doIM()
						}
						else{
							uni.navigateTo({
								url:'/pages/auth/login'
							})
						}
					}
				);
			},
			callPhone(){
				uni.makePhoneCall({
					phoneNumber: "18696558100"
				})
			},
			toCompany(){
				this.$isLogin().then(
					res => {
						console.log(res)
						if(res){
							var token = uni.getStorageSync('CompanyUserToken');
							if (token ) {
								 uni.navigateTo({
								 	url: '/pages_company/index'
								 })
							} else {
								uni.navigateTo({
									url: '/pages_company/login'
								})
							}
						}
						else{
							uni.navigateTo({
								url:'/pages/auth/login'
							})
						}
					}
				);
			},
			showLogout(){
				this.show=true;
			},
			hideLogout(){
				this.show=false;	
			},
			logout(){
				this.$logout();
				uni.$TUIKit.logout();
				if(uni.$TUICallKit!=null){
					uni.$TUICallKit.logout();
				}
				uni.reLaunch({
					url: '/pages/index/index',
					animationType: 'pop-in',
					animationDuration: 100
				})
				this.isLogin=false;
			},
			getUserInfo(){
				getUserInfo().then(
					res => {
						if(res.code==200){
							if(res.user!=null){
								this.isLogin=true;
								this.user=res.user;
								console.log(this.user.nickName.substr(0,4));
								if(this.user.isWeixinAuth==0){
									this.wxAuthOpen()
								}
							}
						}else{
							uni.showToast({
								icon:'none',
								title: "请求失败",
							});
						}
					},
					rej => {}
				);
			},
			navTo(url){
				uni.navigateTo({
					url: url
				})
				 
			},
			loginNavTo(url){
				this.$isLogin().then(
					res => {
						console.log(res)
						if(res){
							uni.navigateTo({
								url: url
							})
						}
						else{
							uni.navigateTo({
								url:'/pages/auth/login'
							})
						}
					}
				);
				 
			}
		}
	}
</script>
<style scoped lang="scss">
	.content{
		height: 100%;
		.cont{
			position: relative;
			width: 100%;
			display: flex;
			flex-direction: column;
			.bg{
				width: 100%;
				height:450rpx;
				// background-color: #2BC7B9;
				// background: linear-gradient(#2BC7B9, #88e2da);
				
				position: fixed;
				image{
					border-radius: 0rpx 0rpx 50rpx 50rpx;
					width: 100%;
					height:100%;
				}
			}
			.cont-box{
				
				z-index: 1000;
				.top-title{
					height: 88upx;
					line-height: 88upx;
				}
				.user{
					margin: 0rpx 30rpx 30rpx;
					z-index: 101;
					display: flex;
					align-items: flex-start;
					justify-content: flex-start;
					.left{
						image{
							width:100rpx;
							height:100rpx;
							border-radius: 50%;
						}
					}
					.right{
						flex: 1;
						margin-left: 30rpx;
						display: flex;
						flex-direction: column;
						align-items: flex-start;
						justify-content: flex-start;
						.name-box{
							width: 100%;
							display: flex;
							align-items: center;
							justify-content: space-between;
							.name{
								font-size: 38rpx;
								font-family: PingFang SC;
								font-weight: bold;
								color: #111111;
							}
							.btns{
								image{
									width: 50rpx;
									height:50rpx;
								}
							}
							
						}
						.phone{
							margin-top:10rpx ;
							font-size: 32rpx;
							font-family: PingFang SC;
							color: #111111;
							
						}
						.counts{
							margin-top:10rpx ;
							display: flex;
							align-items: center;
							justify-content: flex-start;
							.count{
								margin-right: 10rpx;
								background-color: #E2C99E;
								padding: 5rpx 15rpx;
								border-radius: 20rpx;
								font-size: 24rpx;
								font-family: PingFang SC;
								color: #ffffff;
							}
							
						}
						
					}
					 
				}
				 
				.menus{
					z-index: 101;
					margin: 0rpx 30rpx 20rpx;
					padding: 15rpx;
					display: flex;
					flex-direction: column;
					box-shadow: 0px 0px 5px 2px rgba(0,0,0,0.05);
					background-color: #fff;
					border-radius: 15rpx;
					.title-box{
						display: flex;
						flex-direction: row;
						align-items: center;
						justify-content: flex-start;
						.title{
							font-size: 32upx;
							font-family: PingFang SC;
							font-weight: bold;
							color: #111111;
						}
						.line{
							margin-right: 15rpx;
							height: 30rpx;
							width: 6rpx;
							background-color: #C39A58;
							
						}
						 
					}
					.menu-box{
						display: flex;
						align-items: center;
						justify-content: flex-start;
						flex-wrap: wrap;
						.menu{
							padding: 15rpx;
							width:25%;
							min-width: 25%;
							display: flex;
							flex-direction: column;
							align-items: center;
							justify-content: center;
							position: relative;
							image{
								width:58rpx;
								height:58rpx;
							}
							.min-image{
								width:40rpx;
								height:40rpx;
							}
							.title{
								margin-top: 10rpx;
								font-size: 24upx;
								font-family: PingFang SC;
								font-weight: 500;
								color: #111111;
							}
							.contact-btn {
								position: absolute;
								width:80rpx;
								height:80rpx;
								display: flex;
								opacity: 0;
							}
						}
					}
					
				}
				.btn-box{
					margin: 30rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					.sub-btn{
						// box-shadow: 0px 0px 5px 2px rgba(0,0,0,0.05);
						border: 1rpx solid #f8f8f8;
						background: #FFFFFF;
						width: 100%;
						height: 88upx;
						line-height: 88upx;
						text-align: center;
						font-size: 30upx;
						font-family: PingFang SC;
						color: #111;
					}
				}
			}
			
			 
		}
		
	}
	.tip{
		margin: 30rpx 0rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		.title{
			font-size: 24rpx;
			color: #b5b5b5;
			font-weight: bold;
			
		}
		.tel{
			margin-top: 15rpx;
			font-size: 24rpx;
			color: #b5b5b5;
		}
		
		
	}
	.wxAuth{
		border-radius: 30rpx;
		width: 100%;
		padding: 15rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		background-color: #FFFFFF;
		.title{
			font-size: 32rpx;
			margin: 10rpx 0rpx;
			font-weight: bold;
			text-align: center;
		}
		.wx-box{
			width: 100%;
			padding: 0rpx 30rpx;
		}
	}
	
</style>
