<template>
	<view>
		<block v-if="!eSign">
			<!-- 上传身份证 -->
			<view class="identity-title">请拍摄并上传你的身份证照片</view>
			<view class="identity-flex">
				<view class="identity-item" @click="updImg('frontCard')">
					<view class="upd" style="background-image: url('/static/imgs/card_front.png');">
						<image v-if="frontCard.showpath != ''" :src="frontCard.showpath" class="idcardImg-img" mode="aspectFit"></image>
					</view>
					<view class="text">拍摄正面</view>
				</view>
				<view class="identity-item" @click="updImg('backCard')">
					<view class="upd" style="background-image: url('/static/imgs/card_verso.png');">
						<image v-if="backCard.showpath != ''" :src="backCard.showpath" class="idcardImg-img" mode="aspectFit"></image>
					</view>
					<view class="text">拍摄反面</view>
				</view>
			</view>
		</block>
		<!-- 身份证信息 -->
		<view class="identity-from" v-if="realData.name != ''">
			<view class="identity-inpus">
				<label>真实姓名</label>
				<input type="text" placeholder="-" v-model="realData.name" disabled>
			</view>
			<view class="identity-inpus">
				<label>身份证号</label>
				<input type="idcard" placeholder="-" v-model="realData.id_card" disabled>
			</view>
			<view class="identity-inpus">
				<label>户籍地</label>
				<input type="idcard" placeholder="-" v-model="realData.organ" disabled>
			</view>
		</view>
		<!-- 按钮 -->
		<view class="btn" v-if="!eSign">
			<button size="default" @click="idCard && !isPathChange ? getSignUrl(): realInfo()">{{idCard && !isPathChange ? '人脸认证': '提交身份信息'}}</button>
		</view>
	</view>
</template>

<script>
	import { certification, identityOcr, certified, eSigns } from '@/apis/interfaces/user'
	import { uploads } from '@/apis/interfaces/uploading'
	export default {
		data() {
			return {
				idCard		: false,
				eSign		: false,
				isPathChange: false,
				frontCard	: { showpath: '', path	: '' }, // 身份证正面
				backCard	: { showpath: '', path	: '' }, // 身份证反面
				realData	: {
					name	: '',
					id_card	: '',
					organ	: ''
				}, // 个人信息
				signUrl		: '',
				isRefresh	: false
			}
		},
		onShow(){
			if(this.isRefresh){
				this.getCertified()
				this.isRefresh = false
			}
		},
		created() {
			this.getCertified()
		},
		methods: {
			// 检查实名结果
			getCertified(){
				uni.showLoading({
					title: '加载中...',
					mask : true
				})
				certified().then(res => {
					let { id_card, e_sign } = res;
					this.idCard = id_card;
					this.eSign = e_sign;
					if(this.idCard){
						this.getInfo()
						return
					}
					uni.hideLoading()
				}).catch(err => {
					uni.showToast({
						title: err.message,
						icon: "none"
					})
				})
			},
			// 提交真实信息
			getInfo(){
				uni.showLoading({
					title: '加载中...',
					mask : true
				})
				identityOcr().then(res => {
					this.frontCard.showpath = res.front_card
					this.backCard.showpath = res.back_card
					this.realData = res;
					uni.hideLoading()
				}).catch(err => {
					uni.showToast({
						title: err.message,
						icon: "none"
					})
				})
			},
			
			// 提交照片认证信息
			realInfo() {
				uni.showLoading({
					title: '提交中信息...',
					mask : true
				})
			    certification({
					front_card: this.frontCard.path,
					back_card : this.backCard.path,
					redirect_url: 'http://web.douhuofalv.com/app/e-sign?sign_type=personal',
					app_scheme: 'doufire://'
				}).then(res => {
					console.log(res)
					let certificationData = res;
					wx.showModal({
						title		: '提示',
						content		: '您的身份信息已上传，请完成人脸识别以确认您是本人',
						showCancel	: false,
						success		: () => {
							this.isPathChange = false
							this.realData = certificationData.certification
							this.signUrl  = certificationData.sign.authShortUrl
							this.getSignUrl()
						}
					})
			    }).catch(err => {
					uni.showToast({
						title: err.message,
						icon: "none"
					})
				})
			},
			
			// 人脸识别
			getSignUrl(){
				if(this.signUrl != ''){
					this.isRefresh = true
					plus.runtime.openURL(this.signUrl)
					return
				}
				uni.showLoading({
					title: '加载中...',
					mask : true
				})
				eSigns({
					redirect_url: 'http://web.douhuofalv.com/app/e-sign?sign_type=personal',
					app_scheme: 'doufire://'
				}).then(res => {
					this.isRefresh = true
					plus.runtime.openURL(res.authShortUrl)
					uni.hideLoading()
				}).catch(err => {
					uni.showToast({
						title: err.message,
						icon: "none"
					})
				})
			},
			
			// 上传图片
			updImg(type){
				uni.chooseImage({
					count	: 1,
					success	: path => {
						uploads([{
							uri   : path.tempFilePaths[0]
						}], {
							driver: "private-oss"
						}).then(res => {
							this.isPathChange = true
			                this[type].showpath = res.url[0]
			                this[type].path = res.path[0]
						}).catch(err => {
							uni.showToast({
								title: err.message,
								icon : 'none'
							})
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	// 身份信息
	.identity-from{
		border-bottom: 30rpx solid #f8f8f8;
		.identity-inpus{
			display: flex;
			border-bottom: solid 1rpx #F6F6F6;
			padding: 0 30rpx;
			height: 100rpx;
			line-height: 100rpx;
			font-size: 32rpx;
			&:last-child{
				border-bottom: none;
			}
			label{
				width: 180rpx;
				color: gray;
			}
			input{
				height: 100rpx;
				font-size: 32rpx;
				width: calc(100% - 180rpx);
				text-align: right;
			}
		}
	}
	// 身份证上传
	.identity-title{
		padding: 0 30rpx;
		line-height: 90rpx;
		font-size: 30rpx;
		color: #111111;
		border-top: 30rpx solid #f8f8f8;
		color: gray;
	}
	.identity-flex{
		display: flex;
		padding: 0 20rpx 30rpx;
		.identity-item{
			width: calc(50% - 20rpx);
			margin: 0 10rpx;
			background-color: rgba(68, 110, 254, .1);
			border-radius: 10rpx;
			overflow: hidden;
			.upd{
				padding-top: 63%;
				position: relative;
				background-size: 56%;
				background-position: center;
				background-repeat: no-repeat;
				.idcardImg-img{
					position: absolute;
					top: 0;
					left: 0;
					width: 100%;
					height: 100%;
				}
			}
			.text{
				text-align: center;
				line-height: 60rpx;
				background: $main-color;
				color: white;
				font-size: 28rpx;
			}
		}
		border-bottom: 30rpx solid #f8f8f8;
	}
	// 身份认证按钮
	.btn{
		padding: 30rpx;
		button[size="default"]{
			background: $main-color;
			color: white;
			height: 90rpx;
			line-height: 90rpx;
			padding: 0;
			font-size: 32rpx;
			border-radius: 10rpx;
			font-weight: bold;
			&::after{
				display: none;
			}
		}
	}
</style>