<template>
	<view class="container">
	  <view class="content" v-if="showCon">
	    <view class="contentTop backgroundCustom">
	      <view class="contentTop_title">已获取医保电子凭证授权信息</view>
	      <view class="contentTop_tip">请点击授权获取手机号码</view>
	    </view>
	    <view class="contentBox">
	      <view class="contentBox_item border_bottom displayFlexLeft">
	        <view class="contentBoxItem_left">姓名</view>
	        <view class="contentBoxItem_right">{{codeInfo.memberName}}</view>
	      </view>
	      <view class="contentBox_item border_bottom displayFlexLeft">
	        <view class="contentBoxItem_left">身份证号</view>
	        <view class="contentBoxItem_right">{{codeInfo.certNum}}</view>
	      </view>
	      <view class="contentBox_item displayFlexLeft">
	        <view class="contentBoxItem_left">医保卡号</view>
	        <view class="contentBoxItem_right">{{codeInfo.cardNo}}</view>
	      </view>
	    </view>
    <!-- #ifdef MP-WEIXIN -->
    <button class="backgroundCustom_F08 btn" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">授权获取手机号码</button>
    <!-- #endif -->
    <!-- #ifndef MP-WEIXIN -->
    <view class="backgroundCustom_F08 btn" style="text-align:center;color:#fff;" @click="getPhoneNumber">授权获取手机号码(模拟)</view>
    <!-- #endif -->
  </view>
  <view class="dialogContiner mask" v-show="showHealth">
	    <view class="dialog">
	      <!-- <view class="title">微信授权</view> -->
	      <view class="info">获取你的公开信息（昵称，头像，地区，及性别）</view>
		  <!-- #ifdef MP-WEIXIN -->
	      <healthCardLogin :encrypt="true" :authLogin="false" @authFail="authFail" @authSucess="authSuccess" :wechatcode="true" :healthcode="true">
	        <view @click="auth" class="btn-agree">前往授权</view>
	      </healthCardLogin>
		  <!-- #endif -->
		  <!-- #ifndef MP-WEIXIN -->
		  <view @click="auth" class="btn-agree">前往授权(模拟)</view>
		  <!-- #endif -->
	    </view>
	  </view>
	</view>
	
</template>

<script lang="ts" setup>
	import { ref } from 'vue';
	import { onLoad } from '@dcloudio/uni-app';
	import { common } from '@/utils';
	import { 
		getYbzfMemberUserInfo, 
		getPhoneNumber as getPhoneNumberApi, 
		addBaseMember_V3New 
	} from '@/pagesPersonal/service/patientManagement';
	import { usePreserMember, useGetDefaultMember } from '@/hook';

	const app = getApp();
	
	const codeInfo = ref<any>({}); // 授权后得到的本人参数
	const showCon = ref(false); //页面加载完参数
	const showHealth = ref(true); // 健康卡授权弹窗
	
	onLoad((options: any) => {
		main(options);
	});
	
	const main = async (options: any) => {
		if(options.medicalCode){
			let { resData } = await getYbzfMemberUserInfo({
				openId : uni.getStorageSync("openid"),
				hosId: app.globalData.hosId,
				code: options.medicalCode
			});
			if(common.isNotEmpty(resData)){
				codeInfo.value = resData[0];
				showCon.value = true;
			}
		}
	};
	
	/**点击授权按钮 */
	const auth = () => {
		showHealth.value = false;
	};
	
	/**
	 * 电子健康卡授权成功后返回执行
	 */
	const authSuccess = async (data: any) => {
		console.log(data);
		// 原逻辑被注释，保持原样
	};
	
	const authFail = (err: any) => {
		console.log(err);
	};
	
	const getPhoneNumber = async (e: any) => {
        // #ifdef MP-WEIXIN
		let { resData } = await getPhoneNumberApi({
			ConfigKey: app.globalData.configKey,
			Code: e.detail.code
		});
        // #endif
        // #ifndef MP-WEIXIN
        // 模拟数据
        let resData = [{ phone_info: { phoneNumber: '13800138000' } }];
        common.showToast('模拟获取手机号成功');
        // #endif

		if(common.isNotEmpty(resData)){
			let info = codeInfo.value;
			let quer = {
				memberName: info.MemberName,
				certNum : info.IdCardNo,
				mobile: resData[0].phone_info.phoneNumber,
				userMobile: resData[0].phone_info.phoneNumber,
				certType: "01",
				memberType: 1, //医保添加都为本人
			};
			let { resData: addMember } = await addBaseMember_V3New(quer);
			if(addMember.RespCode == 10000){
				await usePreserMember();
				// 判断是否有存储跳转地址
				if (app.globalData.toUrl) {
					// 判断为（selecteCardOrHos）选择患者界面进入添加
					if(app.globalData.isSelectAdd){
						app.globalData.isSelectAdd = false;
						common.navigateBack(1);
						return;
					}
					let member = await useGetDefaultMember({cardType: app.globalData.cardType, url: app.globalData.toUrl});
					if(common.isNotEmpty(member)){
						common.goToUrl(app.globalData.toUrl, {skipWay: "redirectTo"});
					}
				} else {
					common.navigateBack(1);
				}
			} 
		}
	};
</script>

<style lang="scss" scoped>
	.contentTop{
	  height: 230upx;
	  padding: 40upx 30upx;
	}
	
	.contentTop .contentTop_title{
	  font-size: 32upx;
	  font-weight: bold;
	  margin-bottom: 24upx;
	}
	.contentTop .contentTop_tip{
	  font-size: 26upx;
	  font-family: PingFang SC;
	}
	.contentBox{
	  background: #FFFFFF;
	  border-radius: 24upx;
	  padding: 0 30upx;
	  margin: -70upx 30upx 60upx;
	}
	.contentBox_item{
	  height: 110upx;
	  line-height: 110upx;
	  font-size: 32upx;
	  font-family: PingFang SC;
	  color: #222326;
	}
	.contentBoxItem_left{
	  color: #8A8A99;
	  width: 150upx;
	  margin-right: 60upx;
	  text-align: justify;
	  text-align-last: justify;
	}
	.btn{
	  width: 690upx !important;
	  height: 92upx;
	  line-height: 92upx;
	  border-radius: 46upx;
	  margin: 0 auto !important;
	  font-size: 32upx;
	}
	/* 健康卡授权 */
	.dialogContiner .dialog {
	  position: absolute;
	  top: 50%;
	  left: 50%;
	  width: 450upx;
	  transform: translate(-50%, -50%);
	  background-color: #fff;
	  box-shadow: 0 -2px 25px 0 rgba(0, 0, 0, 0.15), 0 13px 25px 0 rgba(0, 0, 0, 0.3);
	  z-index: 100000;
	  text-align: center;
	  border-radius: 10upx;
	}
	
	.dialogContiner .dialog .btn-agree {
	  position: relative;
	  line-height: 100upx;
	  font-size: 35upx;
	  color: #4DB371;
	}
	
	.dialogContiner .dialog .btn-agree::before {
	  content: '';
	  position: absolute;
	  left: 0;
	  right: 0;
	  top: 0;
	  height: 1px;
	  transform: scaleY(0.5);
	  background-color: rgba(234, 234, 234, 1);
	}
	
	.dialogContiner .dialog .btn-agree:hover {
	  background-color: #fff;
	}
	
	.dialogContiner .dialog view.title {
	  font-size: 40upx;
	  line-height: 100upx;
	  position: relative;
	}
	
	.dialogContiner .dialog view.title::after {
	  content: '';
	  position: absolute;
	  left: 0;
	  right: 0;
	  bottom: 0;
	  height: 1px;
	  transform: scaleY(0.5);
	  background-color: rgba(234, 234, 234, 1);
	}
	
	.dialogContiner .dialog view.info {
	  font-size: 26upx;
	  line-height: 30upx;
	  color: rgba(22, 23, 42, 0.5);
	  padding: 40upx;
	} 
	
</style>