<template>
	<view class="cardscanning">
		<!-- 摄像机 -->
		<view class="cardscanning-head">
			<view class="title item">请扫描你的有效身份证</view>
			<!-- 扫描身份证 -->
			 <ocr-navigator @onSuccess="scanIdcardFront" certificateType="idCard" :opposite="false">
			  <view class="title-box" >
				<img style="width: 100%; height: 100%;" :src="src" alt="" v-if="src" />
			   	<img src="/static/zhengmian.png" alt="" v-else />
			
			   	<view class="box" >
			   		扫描身份证正面
			   	</view>
				
			   </view>
			 </ocr-navigator>
		
		</view>
		<!-- 身份信息 -->
		<view class="cardscanning-box">
			<view class="box-item item">请确认你的身份信息</view>
			<view class="box-item">
				<view>姓名</view>
				<view class="box-item-info">{{userInfo.name}}</view>
			</view>
			<view class="box-item">
				<view>性别</view>
				<view class="box-item-info">{{userInfo.gender}}</view>
			</view>
			<view class="box-item">
				<view>民族</view>
				<view class="box-item-info">{{userInfo.nationality}}</view>
			</view>
		</view>
		<view class="cardscanning-box" style="height: 250rpx;">
			<view class="box-item">
				<view>出生日期</view>
				<view class="box-item-info">{{userInfo.mount}}</view>
			</view>
			<view class="box-item">
				<view>家庭住址</view>
				<view class="box-item-info">{{userInfo.address}}</view>
			</view>
			<view class="box-item">
				<view>身份证号</view>
				<view class="box-item-info">{{userInfo.id}}</view>
			</view>
		</view>
	
		<!-- 最后的提取信息 -->
		<view class="bottom">
			<button type="default" :disabled="!userInfo.id" class="button" @click="extractBtn">提取信息</button>
		</view>
	</view>
</template>

  


<script>
 import {request} from '../../request/request.js'
// 在 Vue 文件中调用原生插件
// import { NativeModules } from '@dcloudio/uni-app';
	export default {
			data(){
				return{
					id:'',
					patientForm:{
						
					},
					userInfo:{
						
					} ,//个人信息
					src:''
				}
			},
		mounted() {
			this.getPatient()
		},
		methods:{
			// 出生年月
			extractBirthDate(idNumber){
				const birthDate = new Date(idNumber);
				// 当前日期
				const currentDate = new Date();
				// 计算年龄
				let age = currentDate.getFullYear() - birthDate.getFullYear();
				// 如果当前月份小于出生月份，或者当前月份等于出生月份但是当前日期小于出生日期，则年龄减一
				if (currentDate.getMonth() < birthDate.getMonth() || 
				    (currentDate.getMonth() === birthDate.getMonth() && currentDate.getDate() < birthDate.getDate())) {
				  age--;
				}
				
				// 打印年龄
				return  age
			},
			// 获取患者信息
			async	getPatient	(){
				const res = await request({url:'/patient'})	
				console.log(res,'res');
				this.patientForm = res
			},
			// 扫描身份证
			scanIdcardFront(e){
				const data = e.detail;
				const birthDate = this.extractBirthDate(data.birth.text);		
				this.userInfo = {
					 name:data.name.text,
					 id:data.id.text,
					 address:data.address.text,
					 gender:data.gender.text,
					 nationality:data.nationality.text,
					 mount:data.birth.text,
					 age:birthDate,
					 id1:this.id,
					 img:this.patientForm.img,
					 id1:this.patientForm.id,
					 caseHistory:this.patientForm.caseHistory
				}
				this.src  = data.image_path	
			},
			// 提取信息
			async extractBtn(){
				uni.navigateTo({
				  url: `/pages/gentfiling/index?userInfo=${JSON.stringify(this.userInfo)}`
				});
			}
		}
	}
</script>

<style scoped lang="scss">
	.cardscanning{
		width: 100vw;
		height: 100vh;
		// padding: 23rpx;
		background-color: #eee;
		box-sizing: border-box;
		.item{
			font-family: SourceHanSansCN, SourceHanSansCN;
			font-weight: 400;
			font-size: 30rpx;
			color: #999999;
			line-height: 32rpx;
			text-align: left;
			font-style: normal;
			text-transform: none;
		}
		.cardscanning-head{
			width: 750rpx;
			height: 400rpx;
			background: #FFFFFF;
			padding: 30rpx;
			.title-box{
				margin-top: 32rpx;
				width: 685rpx;
				height: 279rpx;
				background: #F3F7FD;
				border-radius: 10rpx 10rpx 10rpx 10rpx;
				text-align: center;
				img{
					width: 220rpx;
					height: 180rpx;
				}
				.box{
					margin-top: 20rpx;
					width: 692rpx;
					height: 80rpx;
					background: #5581FF;
					border-radius: 0rpx 10rpx 10rpx 0rpx;
					line-height: 80rpx;
					font-family: SourceHanSansCN, SourceHanSansCN;
					font-weight: 400;
					font-size: 28rpx;
					color: #FFFFFF;
					font-style: normal;
					text-transform: none;
				}
			}
		}
		
		.cardscanning-box{
			margin-top: 30rpx;
			width: 750rpx;
			height: 362rpx;
			background-color: #FFFFFF;
			box-sizing: border-box;
			.box-item{
				// width: 750rpx;
				height: 88rpx;
				line-height: 88rpx;
				border-bottom:  2rpx solid #eee;
				background: #FFFFFF;
				padding:0 40rpx ;
				display: flex;
				justify-content: space-between;
				font-family: SourceHanSansCN, SourceHanSansCN;
				font-weight: 400;
				font-size: 30rpx;
				color: #1B1B20;
				.box-item-info{
					color: #656565;
				}
			}
		}
		
		.bottom{
			margin-top: 50rpx;
			height: 200rpx;
			background-color: #FFFFFF;
			display: flex;
			align-items: center;
			.button{
				width: 721rpx;
				height: 100rpx;
				background: #5581FF;
				box-shadow: 8rpx 0rpx 16rpx 0rpx #2385FF;
				border-radius: 44rpx 44rpx 44rpx 44rpx;
				color: #FFFFFF;
			}
		}
		
	}
</style>