<template>
	<div class="LoginMain">
		<LoadingD :status="dloading"></LoadingD>
		<div style="display: flex;justify-content: space-between;width: 100%;padding: 8px;margin-top: 10px;">
			<div></div>
			<div v-if="username" style="font-size: 16px;font-weight: 700; color: #277d7b;"><van-icon name="manager" size="18" style="margin-right: 4px;" />{{username}}</div>
		</div>
		<div class="top">
			<div class="biaoti"><van-icon name="todo-list-o" size="22" />体检预约.</div>
			<div class="button_group">
				<div class="button_new" @click="gotoYuyue" style="background-color: #85caf4;" v-if="JianKangZhengEnable==1">
					<div>健康证明</div>
					<div style="font-size: 12px;color: #277d7b;">预约健康证体检</div>
				</div>
				<div class="button_new" style="background-color: #5bc3c6;" v-if="LaoNianRenEnable==1" @click="gotoLaoNianRen()">
					<div> 65岁以上老年人</div>
					<div style="font-size: 12px;color: #277d7b;">老年人体检预约</div>
				</div>
				<div class="button_new" style="background-color: #ecb322;" v-if="JianKangTiJianEnable==1" @click="gotoJianKang()">
					<div>健康体检</div>
					<div style="font-size: 12px;color: #927220;">健康体检预约</div>
				</div>
				<div class="button_new" style="background-color: #309d99;" @click=" router.push('/myappointment')">
					<div><van-icon name="todo-list-o" size="16" style="margin-right: 4px;" />预约记录</div>
					<div style="font-size: 12px;color: #7dbfbe;"> 预约登记记录</div>
				</div>
			</div>
		</div>
		<div class="top">
			<div class="biaoti"><van-icon name="newspaper-o" size="22" />报告查询.</div>
			<div class="button_group">
				<div class="button_new2" @click="gotoReport(1)"  v-if="JianKangZhengEnable_baogao==1">
					<div>健康证明查询</div>
					<div style="font-size: 12px;color: #c3805f;">健康证明查询</div>
				</div>
				<div class="button_new2" @click="gotoReport(2)" v-if="LaoNianRenEnable_baogao==1">
					<div> 65岁以上老年人体检报告</div>
					<div style="font-size: 12px;color: #c3805f;">老年人体检报告查询</div>
				</div>
				<div class="button_new2" v-if="JianKangTiJianEnable_baogao==1">
					<div>健康体检报告</div>
					<div style="font-size: 12px;color: #c3805f;">健康体检报告查询</div>
				</div>
			</div>
		</div>
		<!-- 		<van-button class="button" round type="default" icon="todo-list-o" :loading="loading" @click="gotoYuyue">{{buttonText}}</van-button>
		<van-button class="button2" round type="warning" icon="newspaper-o"   :loading="loading" @click="gotoReport">查看体检结果</van-button>
	 -->
	</div>
</template>

<script setup>
	import {
		AppLogin,
		AppGetUserInfo,
		mH5Register,
		GetConfigInArray,
		isAgeOver65
	} from "@/api/api.js";
	import {
		ref,
		onMounted,
		onActivated
	} from 'vue';
	import {
		useRoute,
		useRouter
	} from "vue-router"
	import {
		showToast
	} from 'vant';
	
	const route = useRoute()
	const router = useRouter();
	let openid = ''
	let username=ref('');
	const login = () => {
		if (!sessionStorage.getItem('sfz')) {
			showToast('查询用户信息出错！');
			return false
		}
		loading.value = true

		AppLogin({
			openid: openid
		}).then(res => {
			loading.value = false
			if (res.status == 'ok') {
				sessionStorage.setItem('token', res.token);
				sessionStorage.setItem('refreshToken', res.refresh_token);
				var token = sessionStorage.getItem('token');
				if (token == res.token) {

					if (res.isRegister) {
						//跳转到选择机构
						// router.push('/selectOrganization')
					} else {
						//未注册自动注册
						Reg()
					}
				} else {
					showToast('查询存储信息失败');
				}
			} else {
				showToast(res.msg);
			}
		})


	}
	let loading = ref(false)
	let buttonText = ref('开始预约')


	const gotoYuyue = () => {
		if (JianKangZhengEnable.value != 1) {
			showToast('暂未开放健康证预约');
			return false;
		}
		if (sessionStorage.getItem('sfz')) {
			sessionStorage.setItem('yuyueType','jiankangzheng_mf')
			router.push('/selectOrganization')
		} else {
			showToast('查询用户信息出错！');
		}
	}
	const gotoLaoNianRen=()=>{
		if (LaoNianRenEnable.value != 1) {
			showToast('暂未65岁老年人预约');
			return false;
		}
		if(sessionStorage.getItem('sfz')){
			isAgeOver65({
				sfz: sessionStorage.getItem('sfz')
			}).then(res => {
				if(res.status){
					if(!res.data){
						showToast('当前用户年龄不符合要求');
					}else{
						sessionStorage.setItem('yuyueType','laonianren_mf')
						router.push('/selectOrganization')
					}
				}else{
					showToast('年龄校验失败');
				}
			})
		}else {
			showToast('查询用户信息出错！');
		}
	}
	const gotoJianKang=()=>{
		if (JianKangTiJianEnable.value != 1) {
			showToast('暂未开放健康体检预约');
			return false;
		}
		if (sessionStorage.getItem('sfz')) {
			sessionStorage.setItem('yuyueType','jiankangzheng_mf')
			router.push('/selectchecktype')
		} else {
			showToast('查询用户信息出错！');
		}
	}
	const gotoReport = (type) => {
		let p = '?type=' + type
		if (sessionStorage.getItem('sfz')) {
			router.push('/showReport' + p)
		} else {
			showToast('查询用户信息出错！');
		}
	}
	let dloading = ref(false)
	const AppGetUserinfoAction = () => { //大兴APP获取登录用户信息
		dloading.value = true
		AppGetUserInfo({
			userId: openid
		}).then(res => {
			dloading.value = false
			console.log(res)
			if (res.status) {
				let info = res.data
				if (info[0] != undefined) {
					sessionStorage.setItem('sfz', info[0].idcardno);
					sessionStorage.setItem('name', info[0].xm);
					sessionStorage.setItem('tel', info[0].sj);
					sessionStorage.setItem('sex', info[0].xb);
					username.value=info[0].xm
					login()
				} else {
					showToast('查询用户信息出错！');
				}
			} else {
				showToast(res.msg);
			}
		})
	}

	//注册
	const Reg = () => {
		let info = {
			name: sessionStorage.getItem('name'),
			sex: sessionStorage.getItem('sex'),
			tel: sessionStorage.getItem('tel'),
			sfz: sessionStorage.getItem('sfz'),
		}
		loading.value = true
		mH5Register({
			info: info
		}).then(res => {
			loading.value = false
			if (res.status == true) {
				// showDialog({
				// 	title: '提示',
				// 	message: '添加人员信息成功',
				// }).then(() => {
				// 	//添加成功后，跳转选择体检机构
				// 	router.replace('/selectOrganization')
				// });
				showToast('注册成功！');
			} else {
				showToast(res.msg);
			}
		})
	}
	let configInfo = ref(null);
	let LaoNianRenEnable = ref(0);
	let JianKangZhengEnable = ref(0);
	let JianKangTiJianEnable = ref(0);
	let LaoNianRenEnable_baogao = ref(0);
	let JianKangZhengEnable_baogao = ref(0);
	let JianKangTiJianEnable_baogao = ref(0);
	//获取配置信息
	const GetConfigInArrayFunc = () => {
		GetConfigInArray({
			labels: ['是否开启H5老年人', '是否开启H5健康证', '是否开启H5健康体检', '是否开启H5老年人报告查询', '是否开启H5健康证明查询', '是否开启H5健康体检报告查询']
		}).then(res => {
			loading.value = false
			if (res.status) {
				configInfo.value = res.data
				configInfo.value.forEach(function(v, i) {
					if (v.label == '是否开启H5老年人') {
						if (v.value == 1) {
							LaoNianRenEnable.value = 1
						}
					}
					if (v.label == '是否开启H5健康证') {
						if (v.value == 1) {
							JianKangZhengEnable.value = 1
						}
					}
					if (v.label == '是否开启H5健康体检') {
						if (v.value == 1) {
							JianKangTiJianEnable.value = 1
						}
					}
					if (v.label == '是否开启H5老年人报告查询') {
						if (v.value == 1) {
							LaoNianRenEnable_baogao.value = 1
						}
					}
					if (v.label == '是否开启H5健康证明查询') {
						if (v.value == 1) {
							JianKangZhengEnable_baogao.value = 1
						}
					}
					if (v.label == '是否开启H5健康体检报告查询') {
						if (v.value == 1) {
							JianKangTiJianEnable_baogao.value = 1
						}
					}

				})
			} else {
				showToast(res.msg);
			}
		})
	}
	onMounted(() => {
		sessionStorage.setItem('is_zifei', '0');
		const script = document.createElement('script');
		  script.src = '/src/assets/sm4.js'; // 路径对应步骤1中存放的位置
		  script.onload = () => {
		    console.log('sm4.js加载完成，可使用SM4Util');
		    // 加载完成后再执行后续操作（如实例化）
			
			// 初始化SM4工具
			const sm4Util = new SM4Util();
			
			// 设置16字节密钥
			sm4Util.secretKey = "e23b1c3775b54b3dbe819eded11ebb9a";
			
			// URL解码函数
			function urlDecode(encoded) {
			  return encoded.replace(/%2B/g, '+').replace(/%2F/g, '/');
			}
			
			// 待解密的密文
			let encryptedText = "AmAYzZaAwbOMPUptaxWQeE1WSE85wt7TxejYjxRXua5%2BAPGSiw8dPM5LMW1ti2ex11";
			  console.log('55555555',encryptedText)
			//  console.log('jiami',sm4Util.encryptData_ECB('userid=13062619850808587X'));
			let hash1 = window.location.hash;
			encryptedText= hash1.split('?')[1];
			try {
			  // 1. URL解码
			  const decodedText = urlDecode(encryptedText);
			
			  
			  // 2. 执行解密
			  const decryptedText = sm4Util.decryptData_ECB(decodedText);
			   let params = new URLSearchParams(decryptedText);
				openid = params.get('userId')
			  console.log("解密结果：", openid);
			 sessionStorage.setItem('openid', openid);
			 AppGetUserinfoAction()
			 tiaoGuoAppYanzheng();
			 GetConfigInArrayFunc();
			} catch (e) {
			  console.error("解密失败：", e);
			}
			
		  };
		  document.body.appendChild(script);
		
 
		
		
		
	})
	//测试 跳过大兴app验证
	const tiaoGuoAppYanzheng = () => {
		//sessionStorage.setItem('sfz', "130302194510011112");
		sessionStorage.setItem('sfz', "110224195909090061");
		sessionStorage.setItem('name', '测试');
		sessionStorage.setItem('tel', "13333356789");
		sessionStorage.setItem('sex', 1);
		username.value= '测试'
		login()
	}
</script>

<style scoped>
	.LoginMain {
		display: flex;
		align-items: center;
		justify-content: ;
		flex-direction: column;
		height: 100vh;
		/* background-image: url('../assets/image/login.jpg'); */
		background-size: 100%;
		background-repeat: no-repeat;
		background-color: #fff;
		background-position: top center;
		padding: 10px;


	}

	.top {
		width: 100%;
		padding: 20px;
		background-color: rgba(255, 255, 255, 0.5);
		margin-top: 10px;
		border-radius: 10px;
	}

	.biaoti {
		font-size: 18px;
		margin-bottom: 10px;
		color: #333;
		font-weight: 700;
	}

	.button_group {
		display: flex;
		width: 100%;
		justify-content: space-between;
		flex-wrap: wrap;
	}

	.button_new {
		background-color: #33cdc9;
		border: 3px solid #fff;
		padding: 10px;
		font-size: 16px;
		border-radius: 10px;
		width: 48%;
		margin-bottom: 20px;
		color: #fff;
	}

	.button_new2 {
		background-color: #ffaa7f;
		border: 3px solid #fff;
		padding: 10px;
		font-size: 16px;
		border-radius: 10px;
		width: 48%;
		margin-bottom: 20px;
		color: #fff;
	}

	.button {
		width: 60%;
		margin-top: 50%;
		background-color: #33cdc9;
		color: #fff;
		border: 3px solid #e6f5fd;
	}

	.button2 {
		width: 60%;
		margin-top: 20px;
		background-color: #ffaa7f;
		color: #fff;
		border: 3px solid #e6f5fd;
	}
</style>