<template>
	<view class="content">
		<view class="cont">
			<view class="bg">
				<image src="https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/app/image/userimg/mine_top_bg.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://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/app/newImages/my_heads_icon.png':user.avatar"></image>
					</view>
					<view class="right" >
						<view class="name-box" >
							<view class="x-f">
								<view class="name">{{user.nickName}}</view>
								<image class="rightimg" style="margin-left: 0rpx;" src="https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/app/newImages/userimg/right_arrow_black_icon.png" mode="aspectFill"></image>
							</view>
							<view class="btns" >
								<image src="https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/app/image/userimg/set_icon.png"></image>
							</view>
						</view>
						<view class="counts">
							<view class="count count-bg1 x-f" v-if="user.phone">
								<image style="width: 24rpx;height: 24rpx;margin-right: 10rpx;" src="https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/app/newImages/userimg/phone_icon.png" mode="aspectFill"></image>
								<text>{{$parsePhone(user.phone) || ''}}</text>
							</view>
							<view class="count count-bg2 x-f" @click.stop="loginNavTo('/pages_user/integral')">
								<image style="width: 24rpx;height: 24rpx;margin-right: 10rpx;" src="https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/app/image/userimg/lv_icon.png" mode="aspectFill"></image>
								<text>{{user.integral || 0}}积分</text>
							</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="https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/app/image/userimg/consultation_order.png"></image>
							<view class="title">咨询订单</view>
						</view>
						<view class="menu" @click="loginNavTo('/pages_order/storeOrderList')">
							<image src="https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/app/image/userimg/my_cforder.png"></image>
							<view class="title">处方订单</view>
						</view>
						<view class="menu" @click="loginNavTo('/pages_user/shopping/storeOrder')">
							<image src="https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/app/image/userimg/my_cforder.png"></image>
							<view class="title">药品订单</view>
						</view>
						<!-- <view class="menu" @click="loginNavTo('/pages_order/packageOrderList')">
							<image src="https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/app/image/userimg/service_pack.png"></image>
							<view class="title">服务包</view>
						</view> -->
						<view class="menu" @click="loginNavTo('/pages_order/storeOrderRefundList')">
							<image src="https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/app/image/userimg/my_service.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="https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/app/image/userimg/my_doctor.png"></image>
							<view class="title">我的医生</view>
						</view>
						<view class="menu" @click="loginNavTo('/pages_order/prescribeList')">
							<image src="https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/app/image/userimg/my_prescription.png"></image>
							<view class="title">我的处方</view>
						</view>
						<view class="menu" @click="loginNavTo('/pages_user/followList')">
							<image src="https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/app/image/userimg/medication_report.png"></image>
							<view class="title">我的随访</view>
						</view>
						<view class="menu"  @click="toIM()">
							<image src="https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/app/image/userimg/medical_consultation.png"></image>
							<view class="title">用药咨询</view>
						</view>
						<!-- <view class="menu"  @click="navTo('/pages_user/drugReportList')" >
							<image src="https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/app/image/userimg/my_service.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="https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/app/image/userimg/encounter_mgmt.png"></image>
							<view class="title">就诊管理</view>
						</view>
						
						<view class="menu" @click="loginNavTo('/pages_user/address')">
							<image class="min-image" src="https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/app/image/userimg/address_management.png"></image>
							<view class="title">地址管理</view>
						</view>
						<view class="menu"  @click="loginNavTo('/pages_user/healthRecords/index')">
							<image class="min-image" src="https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/app/image/userimg/health_records.png"></image>
							<view class="title">健康档案</view>
						</view>
						<!-- <view class="menu"  @click="loginNavTo('/pages_user/tongue/tongueList')">
							<image class="min-image" src="https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/shop/image/icon_company_3.png"></image>
							<view class="title">舌苔报告</view>
						</view> -->
						 
						<!-- <view class="menu"  @click="loginNavTo('/pages_user/addHealthButler')">
							<image class="min-image" src="https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/app/newImages/0eeec4b825534bfa8833eaf212cf803c.png"></image>
							<view class="title">健康管家</view>
						</view> -->
						<view class="menu" @click="loginNavTo('/pages_user/myCouponList')">
							<image class="min-image" src="https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/app/image/userimg/my_coupon.png"></image>
							<view class="title">我的优惠卷</view>
						</view>
						<view class="menu"  @click="loginNavTo('/pages_company/couponList?couponType=6')">
							<image class="min-image" src="https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/app/image/userimg/coupon_collection_center.png"></image>
							<view class="title">领券中心</view>
						</view>
						<!-- <view class="menu"  @click="loginNavTo('/pages_order/invoice/invoiceList')">
							<image class="min-image" src="https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/app/newImages/34eb8ee3f7b3471fb919c76e4d0735cf.png"></image>
							<view class="title">开票历史</view>
						</view>
						<view class="menu" @click="loginNavTo('/pages_user/cert')">
							<image class="min-image" src="https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/shop/image/cert.png"></image>
							<view class="title">资质证书</view>
						</view> -->
					<!-- 	<view class="menu"  @click="loginNavTo('/pages_user/price')">
							<image class="min-image" src="https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/app/newImages/415e1a6a58ec49ffaa1bb62cb376d4e8.png"></image>
							<view class="title">价格公示</view>
						</view> -->
						<view class="menu"  >
							<image class="min-image" src="https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/app/image/userimg/evaluate.png"></image>
							<view class="title">客服中心</view>
							<button class='contact-btn' open-type="contact">
							</button>
						</view>
						<view class="menu" @click="loginNavTo('/pages_user/complaint')">
							<image class="min-image" src="https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/app/image/userimg/consultation_complaints.png"></image>
							<view class="title">投诉建议</view>
						</view>
						<view class="menu" @click="navTo('/pages_user/about')">
							<image class="min-image" src="https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/app/newImages/userimg/about_us_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_shopping/registerMerchant')">
							<image class="min-image" src="https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/app/image/userimg/intelligent.png"></image>
							<view class="title">商家入驻</view>
						</view>
						<view class="menu" @click="loginNavTo('/pages_user/registerDoctor?type=1')">
							<image class="min-image" src="https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/app/image/userimg/doctor_entrance.png"></image>
							<view class="title">医生入驻</view>
						</view>
						<view class="menu" @click="loginNavTo('/pages_user/registerDoctor?type=2')">
							<image class="min-image" src="https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/app/image/userimg/pharmacist_entrance.png"></image>
							<view class="title">药师入驻</view>
						</view>
						<view class="menu" @click="toCompany()">
							<image class="min-image" src="https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/app/image/userimg/customer_service_login.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';
	import {navigateToDesignatedConversation,setConversation} from "@/pages_im/util/imCommon";
	import IMSDK, {SessionType} from "openim-uniapp-polyfill";
	export default {
		components:{
			wxAuth
		},
		data() {
			return {
				baseUrl:uni.getStorageSync('requestPath'),
				wxShow:false,
				isLogin:false,
				show:false,
				statusBarHeight: uni.getStorageSync('menuInfo').statusBarHeight,
				user:{
					nickName:"登录/注册",
					phone:"",
					integral:0,
					balance:0.00
				}
			}
		},
		onLoad() {
			var that=this;
		    this.getUserInfoCheck(true); 
			uni.$on('loginOut', function() {
				    //uni.removeStorage({key: "IMUserID"});
				    //uni.setStorageSync("AppToken",null);
					// uni.removeStorage({ key: 'IMToken' });
					// uni.removeStorageSync("imUnread",null);
					that.show=true;
					that.logout();
			});
			
		},
		onShow() {
			var that=this;
			this.getUserInfoCheck(false);
		},
		onReachBottom() {
		},
		onPageScroll(e){
			
		},
		methods: {
			getUserInfoCheck(needIm){
				this.$isLogin().then(res => {
						if(res){
							if(needIm){
								uni.$emit('refreshIM')
							}else{
								this.getUserInfo();
							}
						}
					},
					rej => {}
				);
			},
			updateUser(){
				this.wxShow=false;
				this.getUserInfo()
			},
			wxAuthOpen(){
				this.wxShow=true;
				var that=this;
				setTimeout(function(){
					that.$refs.wxauth.getUserInfo();
				})
				
			},
			wxAuthClose(){
				this.wxShow=false;
			},
			doIM(){
				var that=this;
				const userInfo = uni.getStorageSync('userInfo');
				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();
		                                var user = JSON.parse(userInfo);
		                                var uid = 'U' + user.userId;
		                                var did='D' + data.followDoctorId;
		                                var conversationID=`si_${did}_${uid}`;
		                                this.$store.commit("timStore/setOrderId",data.orderId);
		                                this.$store.commit("timStore/setFollowId",data.followId);
		                                this.$store.commit("timStore/setImType", 2);
		                                this.$store.commit("timStore/setConversationID", conversationID);
		                                navigateToDesignatedConversation(did,SessionType.Single,false).then((res) => {
											// setConversation(conversationID,JSON.stringify(ex)).then(() => {
											// 	console.log("qxj setConversation ex:"+JSON.stringify(ex));		
											// }).catch(() => {});
		                                }).catch(() => uni.$u.toast("操作失败") );
									},
									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 => {
						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();
				IMSDK.asyncApi(IMSDK.IMMethods.Logout, IMSDK.uuid()).then(() => {
						callingModule?.endCall();
						meetingModule?.endCall();
						uni.removeStorage({key: "IMToken",});
						uni.removeStorage({key: "IMUserID",});
						uni.removeStorage({key: "IMHasLogin"}); 
						
				  }).catch((err) => console.log(err))
				  .finally(() => {
				      
				  });
				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;
								uni.setStorageSync('userInfo',JSON.stringify(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: #2583EB;
				// background: linear-gradient(#2583EB, #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;
								}
							}
							.rightimg {
								width: 48rpx;
								height: 48rpx;
							}
						}
						.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: linear-gradient( 90deg, #F5CE3C 0%, #E39414 100%);
								padding: 5rpx 15rpx;
								border-radius: 20rpx;
								font-size: 24rpx;
								font-family: PingFang SC;
								color: #ffffff;
							}
							
						}
						
					}
					 
				}
				 
				.menus{
					z-index: 101;
					margin: 0rpx 30rpx 20rpx;
					padding: 32rpx;
					display: flex;
					flex-direction: column;
					background-color: #fff;
					border-radius: 15rpx;
					.title-box{
						display: flex;
						flex-direction: row;
						align-items: center;
						justify-content: flex-start;
						margin-bottom: 16rpx;
						.title{
							font-family: Source Han Serif CN, Source Han Serif CN;
							font-weight: bold;
							font-size: 36rpx;
							color: #222222;
						}
						.line{
							// margin-right: 15rpx;
							// height: 30rpx;
							// width: 6rpx;
							// background-color: #2583EB;
							
						}
						 
					}
					.menu-box{
						display: flex;
						align-items: center;
						justify-content: flex-start;
						flex-wrap: wrap;
						.menu{
							padding: 16rpx 0;
							width:25%;
							min-width: 25%;
							display: flex;
							flex-direction: column;
							align-items: center;
							justify-content: center;
							position: relative;
							image{
								width:48rpx;
								height:48rpx;
							}
							.min-image{
								width:48rpx;
								height:48rpx;
							}
							.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>
