<template>
	<view class="content">
		<view class="yz-wrap" v-if="stap == 1">
		<!-- <view class="yz-wrap"> -->
			<view class="top-wrap flex">
				<view class="top-left">
					实名认证<text>*</text>
				</view>
			</view>
			<view class="sr-wrap flex">
				<view class="sr-tit">
					姓名
				</view>
				<input type="text" class="sr-inp" placeholder="请输入姓名" v-model="name">
			</view>
			<view class="sr-wrap flex">
				<view class="sr-tit">
					手机号
				</view>
				<input type="number" class="sr-inp" placeholder="请输入手机号" v-model="phone">
			</view>
			<view class="sr-wrap flex">
				<view class="sr-tit">
					身份证号
				</view>
				<input type="idcard" class="sr-inp" placeholder="请输入身份证号" v-model="idcard">
			</view>
			<view class="sr-wrap flex">
				<view class="sr-tit">
					推荐渠道
				</view>
				<input type="text" class="sr-inp" placeholder="请输入推荐渠道" v-model="code">
				<view class="sr-btn" @click="smget()">
					扫码获取
				</view>
			</view>
		</view>
		<view class="yz-wrap" v-if="stap == 0">
			<view class="top-wrap flex">
				<view class="top-left">
					上传身份证照片<text>*</text>
				</view>
			</view>
			<view class="sfz flex">
				<!-- #ifdef H5 -->
				<image class="" :src="sfzz||this.urlFile + '/static/img/sfzz.png'" mode="widthFix"
					@click="navCroper(664,414,'sfzz')"></image>
				<image class="" :src="sfzf||this.urlFile + '/static/img/sfzf.png'" mode="widthFix"
					@click="navCroper(664,414,'sfzf')"></image>
				<!-- #endif -->
				<!-- #ifndef H5 -->
				<image class="" :src="sfzz||'/static/img/sfzz.png'" mode="widthFix"
					@click="navTo('/pages/shop/upimg?type=front')">
				</image>
				<image class="" :src="sfzf||'/static/img/sfzf.png'" mode="widthFix"
					@click="navTo('/pages/shop/upimg?type=side')">
				</image>
				<!-- #endif -->
				<view class="">
					个人信息面
				</view>
				<view class="">
					国徽面
				</view>
			</view>

		</view>
		<!-- <view class="yz-wrap">
			<view class="sr-wrap flex">
				<view class="sr-tit">
					绑定邀请码
				</view>
				<input type="text" class="sr-inp" placeholder="请输入绑定邀请码(非必填)">
			</view>
		</view> -->

		<view class="base-buttom" @click="subsfz" v-if="stap == 0">
			下一步
		</view>
		<template v-if="stap == 1">
			<view class="base-buttom" @click="stap = 0">
				上一步
			</view>
			<view class="base-buttom" @click="sub">
				提交认证
			</view>
		</template>

	</view>
</template>

<script>
	import {
		certification,
		idcards
	} from '@/api/index.js'
	import {
		mapState
	} from "vuex"
	export default {
		data() {
			return {
				code: '', //推荐渠道
				stap: 0,
				sfzz: '',
				sfzf: '',
				name: '',
				phone: '',
				idcard: '',
				loading: false,
			}
		},
		onLoad() {

		},
		onShow() {
			this.phone = this.userInfo.phone || ''
		},
		computed: {
			// #ifdef H5
			...mapState(['urlFile']),
			// #endif
			...mapState('user', ['userInfo'])
		},
		onReachBottom() {

		},
		onReady() {

		},
		methods: {
			smget() {
				let that = this
				// #ifdef MP-WEIXIN
				uni.scanCode({
					success: (res) => {
						that.code = res.result
					}
				})
				// #endif
			},
			upimgg(path, name) {
				let that = this
				that.upLoad(path).then((urldata) => {
					console.log(urldata, '这里')
					that[name] = urldata.url;
				})
			},
			navTo(url) {
				uni.navigateTo({
					url
				})
			},
			back() {
				uni.navigateBack()
			},
			getImage(res) {
				console.log(res)
			},
			subsfz() {
				let that = this

				if (that.sfzz == '' || that.sfzf == '') {
					return that.$api.msg('请上传身份证信息')
				}
				idcards({
					card_front: that.sfzz,
					card_back: that.sfzf
				}).then(res => {
					console.log(res)
					that.name = res.data.name
					that.idcard = res.data.number
					that.stap = 1
				})


			},
			sub() {
				let that = this
				if (that.loading) {
					return
				}
				if (that.name == '') {
					that.$api.msg('请输入姓名')
				}
				if (that.phone == '') {
					that.$api.msg('请输入手机号码')
				}
				if (that.idcard == '') {
					that.$api.msg('请输入身份证号')
				}
				that.loading = true
				certification({
					name: that.name,
					id_number: that.idcard,
					phone_number: that.phone,
					code: that.code
				}).then(res => {
					uni.showToast({
						title: '提交成功',
						duration: 2000
					});
					setTimeout(() => {
						uni.switchTab({
							url: '/pages/user/user'
						})
					})
					console.log(res)
					that.loading = false
				}).catch(err => {
					that.loading = false
				})
			},
			upLoad(path) {
				uni.showLoading({
					title: '图片上传中',
					mask: true
				});
				console.log('图片上传中')
				return new Promise((resolve, error) => {
					uni.uploadFile({
						url: this.$store.state.baseURL + '/api/upload/image', //仅为示例，非真实的接口地址
						filePath: path,
						name: 'file',
						header: {
							"Authori-zation": 'Bearer ' + uni.getStorageSync('token')
						},
						success: (uploadFileRes) => {
							if ("string" === typeof uploadFileRes.data) {
								resolve(JSON.parse(uploadFileRes.data).data)
							} else {
								resolve(uploadFileRes.data.data)
							}
						},
						complete() {
							uni.hideLoading()
						}
					});
				})
			},
			chooseImage: function(index) {
				const _this = this
				return new Promise((ok, error) => {
					// 从相册/相机选择
					// 如需直接开相机或直接选相册，请只使用一个选项
					const sourceType = index === 0 ? ['camera'] : ['album']
					uni.chooseImage({
						count: 1, //默认9
						sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
						sourceType: sourceType,
						success: function(res) {
							ok(res.tempFilePaths[0])
						},
						fail(e) {
							uni.showModal({
								title: '文件打开错误',
								content: '请设置授权文件存储权限',
								showCancel: false,
							});
							error(e)
						}
					});
				})

			},
			onImg(type) {
				const _this = this
				return new Promise((ok, erro) => {
					// 判断是否需要选择
					if (type == 1) {
						uni.showActionSheet({
							itemList: ['拍照', '选择一张照片'],
							success: function(res) {
								console.log(res, '上传')
								_this.chooseImage(res.tapIndex).then((url) => {
									ok(url)
								}).catch((res) => {
									erro(res)
								})
							},
							fail: function(res) {
								erro(res)
								console.log(res.errMsg);
							}
						});
					}
					// 判断是否只需要拍照
					if (type == 2) {
						_this.chooseImage(0).then((url) => {
							ok(url)
						}).catch((res) => {
							erro(res)
						})
					}
				})

			},
			// 图片裁切
			/**
			 * @param {Number} w 裁切宽度比例
			 * @param {Number} h 裁切高度比例
			 * @param {Number} mw 图片最小宽度
			 * @param {Number} mh 图片最小高度
			 * @param {String} url url修改
			 */
			navCroper(w, h, type) {
				let that = this;
				let tt = (type == 'upimg' ? 2 : 1)
				this.onImg(tt).then((url) => {
					uni.navigateTo({
						url: `/pages/user_home/realName/cropper?width=${w}&height=${h}`,
						events: {
							uploadSuccess(res) {
								console.log(res, '这里')
								that.upLoad(res).then((urldata) => {
									console.log(urldata, '这里')
									that[type] = urldata.url;

								})
							}
						},
						success: function(res) {
							console.log(res, 'success');
							// 通过eventChannel向被打开页面传送数据
							res.eventChannel.emit('urlNext', {
								url
							})
						}
					})
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	page {
		padding-top: 1rpx;
	}

	.yz-wrap {
		background-color: #fff;
		margin: 20rpx 0;
	}

	.top-wrap {
		justify-content: space-between;
		padding: 0 25rpx;
		font-size: 32rpx;
		font-weight: bold;
		color: #00001A;
		height: 85rpx;

		.top-left {
			text {
				color: #FF6B2E;
				margin-left: 10rpx;
			}
		}

		.top-right {
			text {
				font-size: 25rpx;
				font-weight: 500;
				color: #999999;
			}

			image {
				height: 22rpx;
				margin-left: 10rpx;
			}
		}
	}

	.sr-wrap {
		border-top: #fafafa 1rpx solid;
		font-size: 32rpx;
		font-weight: 500;
		width: 690rpx;
		height: 114rpx;
		line-height: 114rpx;
		margin: auto;

		.sr-tit {
			width: 170rpx;
			flex-shrink: 0;
			color: #00001A;
		}

		.sr-inp {
			flex-grow: 1;

		}

		.sr-btn {
			color: $uni-color-primary;
		}
	}

	.sfz {
		padding: 30rpx;
		justify-content: space-between;
		flex-wrap: wrap;

		image {
			width: 332rpx;
			height: 207rpx;
		}

		view {
			width: 332rpx;
			text-align: center;
			font-size: 32rpx;
			color: #5C5C5C;
		}
	}

	.base-buttom {
		margin-top: 25rpx;
	}
</style>