<template>
	<view class="content" v-if="!loding">
		<block v-if="!eSign">
			<!-- 上传身份证 -->
			<view class="identity-from">
				<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>
			</view>
			<view class="identity-from">
				<!-- 经营省份 -->
				<view class="identity-inpus" v-if="provinces.length > 0">
					<label>经营省份</label>
					<picker class="city-picker" :value="provincesVal" :range="provinces" range-key="name" @change="onPickerChange($event, 'provincesVal')">
						<view class="city-picker-text" :class="{'text-gray': provincesVal === 0}">{{provinces[provincesVal].name}}<uni-icons class="city-icon" type="bottom" color="gray"></uni-icons></view>
					</picker>
				</view>
				<view class="identity-inpus" v-if="citys.length > 0">
					<label>经营城市</label>
					<picker class="city-picker" :value="citysVal" :range="citys" range-key="name" @change="onPickerChange($event, 'citysVal')">
						<view class="city-picker-text" :class="{'text-gray': citysVal === 0}">{{citys[citysVal].name}}<uni-icons class="city-icon" type="bottom" color="gray"></uni-icons></view>
					</picker>
				</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="identity-from" v-if="eSign">
			<view class="identity-inpus">
				<label>经营城市</label>
				<view class="identity-inpus-text">{{province}} {{city || '-'}}</view>
				<!-- <input type="text" placeholder="-" v-model="city" 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, getRegion } from '@/apis/interfaces/user'
	import { uploads } from '@/apis/interfaces/uploading'
	export default {
		data() {
			return {
				loding		: true,
				idCard		: false,
				eSign		: false,
				isPathChange: false,
				frontCard	: { showpath: '', path	: '' }, // 身份证正面
				backCard	: { showpath: '', path	: '' }, // 身份证反面
				realData	: {
					name	: '',
					id_card	: '',
					organ	: ''
				}, // 个人信息
				signUrl		: '',
				isRefresh	: false,
				province	: '',
				city		: '',
				// 省市区选择
				provinces	: [],
				provincesVal: 0,
				citys		: [],
				citysVal	: 0
			}
		},
		onShow(){
			if(this.isRefresh){
				this.getCertified()
				this.isRefresh = false
			}
		},
		created() {
			getRegion().then(res => {
				this.provinces = [{
					id	: '',
					name: '请选择经营省份'
				}, ...res]
				this.getCertified()
				this.getCity()
			}).catch(err => {
				uni.showToast({
					title: err.message,
					icon : 'none'
				})
			})
		},
		methods: {
			// 检查实名结果
			getCertified(){
				uni.showLoading({
					title: '加载中...',
					mask : true
				})
				certified().then(res => {
					let { id_card, e_sign } = res;
					this.idCard = id_card;
					this.eSign 	= e_sign;
					this.loding = false
					if(this.idCard){
						this.getInfo()
						return
					}
					uni.hideLoading()
				}).catch(err => {
					uni.showToast({
						title: err.message,
						icon: "none"
					})
				})
			},
			// 选择省市
			onPickerChange(e, key){
				let index = e.detail.value
				this[key] = index
				this.isPathChange = true
				if(key === 'provincesVal'){
					this.getCity()
				}
			},
			// 获取城市
			getCity(id){
				uni.showLoading({
					title: '获取城市',
					mask : true
				})
				getRegion(this.provinces[this.provincesVal].id).then(res => {
					this.citys = [
						{
							id	: '',
							name: '请选择经营城市'
						},
						...res
					]
					if(id){
						this.citysVal = this.citys.findIndex(val => val.id === id)
					}else{
						this.citysVal = 0
					}
					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,
						path	: res.front_card_path
					}
					this.backCard = {
						showpath: res.back_card,
						path	: res.back_card_path
					}
					
					if(res.province.id != 0){
						this.province = res.province.name
						this.provincesVal = this.provinces.findIndex(val => val.id === res.province.id)
					}
					if(res.city.id != 0){
						this.getCity(res.city.id)
						this.city = res.city.name
					}
					this.realData = res;
					uni.hideLoading()
				}).catch(err => {
					uni.showToast({
						title: err.message,
						icon: "none"
					})
				})
			},
			
			// 提交照片认证信息
			realInfo() {
				uni.showLoading({
					title: '提交中信息...',
					mask : true
				})
			    certification({
					province_id: this.provinces[this.provincesVal].id,
					city_id: this.citys[this.citysVal].id,
					front_card: this.frontCard.path,
					back_card : this.backCard.path,
					redirect_url: getApp().globalData.certificationUrl,
					app_scheme: 'doufire://',
					channel: 'app'
				}).then(res => {
					uni.hideLoading()
					let certificationData = res;
					this.idCard	= true
					this.isPathChange = false
					this.realData = certificationData.certification
					this.signUrl  = certificationData.sign.authShortUrl
					wx.showModal({
						title		: '提示',
						content		: '您的身份信息已上传，请尽快完成人脸识别以确认您是本人',
						cancelText	: '稍后认证',
						confirmText	: '立即认证',
						success		: ModalRes => {
							if(ModalRes.confirm){
								this.getSignUrl()
							}
						}
					})
			    }).catch(err => {
					uni.showToast({
						title: err.message,
						icon: "none"
					})
				})
			},
			
			// 人脸识别
			getSignUrl(){
				if(this.signUrl != ''){
					this.isRefresh = true
					this.$Router.push({
						name: 'ESign',
						params: {
							url: this.signUrl
						}
					})
					// plus.runtime.openURL(this.signUrl)
					return
				}
				uni.showLoading({
					title: '加载中...',
					mask : true
				})
				eSigns({
					redirect_url: getApp().globalData.certificationUrl,
					app_scheme: 'doufire://'
				}).then(res => {
					this.isRefresh = true
					this.$Router.push({
						name: 'ESign',
						params: {
							url: 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-top: 30rpx solid #f8f8f8;
		background-color: white;
		.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-inpus-text{
				font-size: 32rpx;
				width: calc(100% - 180rpx);
				text-align: right;
			}
			.city-picker{
				width: calc(100% - 180rpx);
				.city-picker-text{
					padding-right: 50rpx;
					position: relative;
					text-align: right;
					.city-icon{
						position: absolute;
						right: 0;
					}
					&.text-gray{
						color: gray;
					}
				}
			}
		}
	}
	// 身份证上传
	.identity-title{
		padding: 0 30rpx;
		line-height: 90rpx;
		font-size: 30rpx;
		color: gray;
	}
	.identity-flex{
		background: white;
		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;
			}
		}
	}
	// 身份认证按钮
	.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>