<template>
	<app-layout :overflow='false'>
		<view class="identify">
			<!-- 输入信息 -->
			<view class="info">
				<text class="title">身份信息</text>
				<view class="input-grow first">
					<text class="input-label">您的姓名</text>
					<input class="input" placeholder="请输入您的姓名" v-model="card_name"></input>
				</view>
				
				<view class="input-grow">
					<text class="input-label">证件号码</text>
					<input class="input" placeholder="请输入证件号码" v-model="idcard"></input>
				</view>
			</view>
			<!-- 身份证图片 -->
			<view class="info picture">
				<text class="title">证件照片</text>
				<!-- 正面 -->
				<view class="picture-grow first">
					<view class="picture-box" @click="loadPicture(0)">
						<image v-if="!front_url" class="picture-img" src="/static/image/idcard_front.png" mode=""></image>
						<image v-if="front_url" class="picture-img" :src="front_url" mode=""></image>
					</view>
					<text class="tips">{{front_url ? '点击重新拍摄身份证人像面' : '点击拍摄身份证人像面'}}</text>
				</view>
				<!-- 反面 -->
				<view class="picture-grow first">
					<view class="picture-box" @click="loadPicture(1)">
						<image v-if="!back_url" class="picture-img" src="/static/image/idcard_back.png" mode=""></image>
						<image v-if="back_url" class="picture-img" :src="back_url" mode=""></image>
					</view>
					<text class="tips">{{back_url ? '点击重新拍摄身份证国徽像' : '点击拍摄身份证国徽面'}}</text>
				</view>
			</view>
			<!-- 按钮 -->
			<view class="btn" :style="{'backgroundColor':getTheme.background}" @click="doAuth">
				<text class="submit">立即认证</text>
			</view>
			<!-- tips -->
			<view class="bottom-tips">
				<text class="txt">*依据中华人民共和国法律规定，为保证电子合同的合法有效性，请您配合上传您的有效二代身份证正反面照片，并保证图片文字清晰，无反光、无遮挡。</text>
			</view>
		</view>
	</app-layout>
</template>

<script>
	import { mapState, mapGetters } from 'vuex';
	export default {
		data() {
			return {
				card_name: '',
				idcard: '',
				front_url: '',
				back_url: ''
			}
		},
		methods: {
			// 选择照片
			loadPicture(type) {
				const _this = this
				uni.chooseImage({
				    count: 1,
				    sizeType: ['original', 'compressed'],
				    success: function (file) {
						console.log(file)
						uni.uploadFile({
						    url: _this.$api.upload.file,
						    filePath: file.tempFilePaths[0],
						    name: 'file',
							formData: { file },
						    success: (res) => {
								const data = JSON.parse(res.data)
						        if (data.code === 0) {
						        	uni.showToast({
										icon: 'success',
						        	    title: '上传成功',
						        	    duration: 1000
						        	});
						        	if (type === 0) {
						        		_this.front_url = data.data.url
						        	} else if (type === 1) {
						        		_this.back_url = data.data.url
						        	}
						        }
						    },
							fail: () => {
								console.log('上传失败')
							}
						});
				    },
					fail: function (err) {
						console.log(err)
					}
				});
			},
			
			doAuth () {
				// 是否输入，是否上传了照片
				if (!this.card_name || !this.idcard) {
					uni.showToast({
						icon: 'error',
					    title: '请检查输入内容',
					    duration: 1000
					});
					return
				} else if(!this.front_url || !this.back_url) {
					uni.showToast({
						icon: 'error',
					    title: '请上传照片',
					    duration: 1000
					});
					return
				}
				const data = {
					card_name: this.card_name,
					idcard: this.idcard,
					card_front_url: this.front_url,
					card_back_url: this.back_url
				}
				
				// 发送认证请求
				this.$showLoading({ text: '正在认证，请稍后' })
				const res = this.$request({
				    url: this.$api.user.doAuth,
					method: 'post',
					data: data,
				}).then(res => {
					// 状态 1，验证不通过
					if (res.code === 1) {
						uni.showModal({
						    title: '提示：验证未通过',
						    content: res.msg,
							showCancel: false,
						    success: function (res) {
						    }
						})
						return
					}
					// 通过的操作：返回结算页面
					uni.showModal({
					    title: '提示：验证通过',
					    content: "点击确定立即返回结算页面",
						showCancel: false,
					    success: function (res) {
							if (res.confirm) {
								uni.navigateBack({ delta: 1 })
							}
					    }
					})
				}).catch(err => {
					console.log(err)
				}).finally(() => {
					this.$hideLoading() 
				});
			}
		},
		computed:{
		    ...mapState({
		        tabBarHeight: (state) => {
		          return state.gConfig.tabBarHeight;
		        },
		        iphoneHeight: (state) =>{
		            return  state.gConfig.iphoneHeight;
		        },
		        iphone: (state) => {
		            return state.gConfig.iphone
		        }
		    }),
		    ...mapGetters('iPhoneX', {
		        BotHeight: 'getBotHeight',
		        getEmpty: 'getEmpty',
			}),
			...mapGetters('mallConfig', {
				getTheme: 'getTheme',
			}),
			...mapGetters({
			    userInfo: 'user/info',
			})
		},
	}
</script>

<style lang="scss" scoped>
	.identify {
		display: flex;
		flex-direction: column;
		align-items: center;
		.info {
			box-sizing: border-box;
			width: 100%;
			margin-top: 30rpx;
			padding: 20rpx 24rpx 10rpx;
			background-color: white;
			.title {
				font-size: 28rpx;
				font-weight: bold;
			}
			.input-grow {
				display: flex;
				align-items: center;
				padding: 10rpx 0;
				.input-label {
					font-size: 26rpx;
					line-height: 26rpx;
				}
				.input {
					flex: 1;
					height: 48rpx;
					margin-left: 20rpx;
					font-size: 26rpx;
				}
				&.first {
					margin-top: 16rpx;
					border-bottom: 1px solid #f6f5f7;
				}
			}
		}
		.picture {
			padding-bottom: 30rpx;
			.picture-grow {
				display: flex;
				flex-direction: column;
				align-items: center;
				.picture-box {
					width: 580rpx;
					height: 320rpx;
					background-color: transparent;
					border-radius: 20rpx;
					overflow: hidden;
					box-shadow: 0 0 20rpx #dddddd;
					.picture-img {
						width: 100%;
						height: 100%;
					}
				}
				.tips {
					font-size: 26rpx;
					margin-top: 10rpx;
				}
				&.first {
					margin-top: 30rpx;
				}
			}
		}
		.btn {
			position: fixed;
			bottom: 0;
			width: 100%;
			height: 80rpx;
			background-color: skyblue;
			text-align: center;
			.submit {
				width: 100%;
				height: 100%;
				margin-top: 30rpx;
				font-size: 30rpx;
				border-radius: 0;
				color: white;
				line-height: 80rpx;
			}
		}
		.bottom-tips {
			padding: 0 24rpx;
			margin-top: 40rpx;
			margin-bottom: 120rpx;
			.txt {
				font-size: 20rpx;
				color: #C2C2C2;
				line-height: 22rpx;
			}
		}
	}
</style>
