<template>
	<view class="phone">
		<view class="titlle">身份证：</view>
		<view class="phone-bd">
			<view class="item">
				<view class="txt">姓名</view>
				<input v-model="realName" placeholder="请输入本人姓名" type="text" />
			</view>
			<view class="item">
				<view class="txt">证件号</view>
				<input v-model="idCard" maxlength="18" placeholder="请输入18位居民身份证号码" type="text" />
			</view>
		</view>
		<view class="phone-fd">
			<view class="btn" @click="toNext">确定</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				realName: "",
				idCard: "",
			};
		},
		mounted() {
			// 监听从裁剪页发布的事件，获得裁剪结果
			uni.$on('uAvatarCropper', path => {
				const that = this;
				// 可以在此上传到服务端
				uni.uploadFile({
					url: that.$baseUrl+'/prod-api/common/upload',
					filePath: path,
					name: 'file',
					complete: (res) => {
						if(res.statusCode == 200){
							const {code, fileName, url, msg} = JSON.parse(res.data);
							if(code == 200){
								uni.showToast({
									title: "上传成功"
								})
								that.toReg(url)
							}else{
								uni.showToast({
									icon: "error",
									title: "上传失败"
								})
							}
						}else{
							uni.showToast({
								icon: "error",
								title: "上传失败"
							})
						}
						
					}
				});
			})
		},
		methods: {
			toNext() {
				const that = this;
				if(!that.realName){
					uni.showToast({
						icon: "none",
						position: "bottom",
						title: "姓名不能为空！",
					})
					return
				}
				if(!that.idCard){
					uni.showToast({
						icon: "none",
						position: "bottom",
						title: "身份证号码不能为空！",
					})
					return
				}
				that.$u.route({
					// 关于此路径，请见下方"注意事项"
					url: '/uni_modules/vk-uview-ui/components/u-avatar-cropper/u-avatar-shiming',
					// 内部已设置以下默认参数值，可不传这些参数
					params: {
						// 输出图片宽度，高等于宽，单位px
						destWidth: 700,
						// 裁剪框宽度，高等于宽，单位px
						rectWidth: 300,
						// 输出的图片类型，如果'png'类型发现裁剪的图片太大，改成"jpg"即可
						fileType: 'jpg',
					}
				})
			},
			//去登录
			async toReg(avatar) {
				const {code, data, msg} = await this.$api.realNameAuth({
					userId: uni.getStorageSync("userInfo").id,
					realName: this.realName,
					idCard: this.idCard,
					avatar
				})
				if(code == 200){
					uni.showToast({
						title: "提交成功"
					})
					setTimeout(()=>{
						uni.navigateBack({
							delta: 1
						})
					},2000)
				}else{
					uni.showToast({
						icon: "none",
						position: "bottom",
						title: msg
					})
				}
				
			}
		}
	}
</script>

<style scoped lang="scss">
.phone{
	padding: 25rpx;
	overflow: hidden;
	.title{
		color: #222222;
		font-weight: 500;
		font-size: 30rpx;
	}
	.tips{
		font-weight: 500;
		font-size: 24rpx;
		color: #999999;
		margin-top: 20rpx;
	}
	&-fd{
		width: 100%;
		position: fixed;
		left: 0;
		bottom: 50rpx;
		z-index: 22;
		.btn{
			width: 700rpx;
			line-height: 98rpx;
			background: linear-gradient(0deg, #000000, #3D3B38);
			border-radius: 98rpx;
			font-weight: 400;
			font-size: 34rpx;
			text-align: center;
			color: #FFFFFF;
			margin: 0 auto;
		}
	}
	&-bd{
		width: 100%;
		overflow: hidden;
		background: #FFFFFF;
		padding: 0 30rpx;
		box-sizing: border-box;
		border-radius: 20rpx;
		margin-top: 30rpx;
		.item{
			display: flex;
			align-items: center;
			border-top: 1px solid #EAEAEA;
			padding: 50rpx 0 30rpx;
			&:first-child{
				border-top-color: #FFFFFF;
			}
			input{
				flex: 1;
				font-weight: 400;
				font-size: 30rpx;
				color: #333;
				margin-left: 30rpx;
			}
			.code{
				width: 170rpx;
				line-height: 60rpx;
				background: #000000;
				border-radius: 60rpx;
				text-align: center;
				font-weight: 500;
				font-size: 26rpx;
				color: #FFFFFF;
			}
		}
	}
}
</style>