<template>
	<view class="container">
		<view class="page">
			<text class="num actived pass">1</text>
			<text class="line line_pass"></text>
			<text class="num actived pass">2</text>
			<text class="line line_pass"></text>
			<text class="num actived">3</text>
			<text class="line"></text>
			<text class="num">4</text>
			<text class="line"></text>
			<text class="num">5</text>
			<text class="line"></text>
			<text class="num">6</text>
			<text class="line"></text>
			<text class="num">7</text>
			<text class="line"></text>
			<text class="num">8</text>
		</view>
		<view class="wrapper">
			<view class="title">
				<view>
					<text class="line"></text>个人证件资料
				</view>
				<text class="page_mark">3/8</text>
			</view>
			<view class="content" style="padding-bottom: 150rpx;">
				<view class="content_wrapper">
					<view class="photo" v-if="!cardForm.idFrontPageOssPath" @click="upLoadPhoto('front')">
						<image src="../../static/front.png" alt="身份证正面" mode="widthFix"></image>
						<text class="iconfont icon-weibiaoti553"></text>
						<view class="choseBtn">点击添加身份证正面</view>
					</view>
					<!-- <view class="content_w" v-if="cardForm.idFrontPageOssPath"> -->
						<image class="picImg" v-if="cardForm.idFrontPageOssPath" :src="cardForm.idFrontPageOssPath" @click="upLoadPhoto('front')" mode="widthFix"></image>
					<!-- </view> -->
					<view class="tips">
						<view>（正确示例：身份证正面，字体清晰）</view>
						<view>（jpg,png 文件大小不大于1mb）</view>
					</view>
				</view>
				<view class="content_wrapper" style="margin-top: 25px;">
					<view class="photo" v-if="!cardForm.idBackPageOssPath" @click="upLoadPhoto('back')">
						<image src="../../static/back.png" alt="身份证反面" mode="widthFix"></image>
						<text class="iconfont icon-weibiaoti553"></text>
						<view class="choseBtn">点击添加身份证反面</view>
					</view>
					<!-- <view class="content_w" v-if="cardForm.idBackPageOssPath"> -->
						<image class="picImg" v-if="cardForm.idBackPageOssPath" :src="cardForm.idBackPageOssPath" @click="upLoadPhoto('back')" mode="widthFix"></image>
					<!-- </view> -->
					<view class="tips">
						<view>（正确示例：身份证反面，字体清晰）</view>
						<view>（jpg,png 文件大小不大于1mb）</view>
					</view>
				</view>
			</view>
		</view>
		<view class="fixed" @click="saveInfo()">
			{{(this.cardForm.approvalStatus== 0 || this.cardForm.approvalStatus == 2)?'保存并下一步':'下一步'}}
		</view>
	</view>
</template>

<script>
	import {
		CommonUpload
	} from '@/util/uploaderFile';
	import api from '../../api/api';
	export default {
		data() {
			return {
				cardForm: {
					idFrontPageOssPath: '',
					idBackPageOssPath: '',
					approvalStatus: ''
				},
				applyParam: {},
				shareId: null,
				dataForm: {
					loginId: uni.getStorageSync('cffp_userId'),
					targetType: "5",
					targetId: '',
					// uni.getStorageSync('cffp_userId')
					targetNo: "",
					targetUseFor: "12",
					targetSeq: "0"
				},
			}
		},
		components: {},
		onLoad(options) {
			// this.shareId = options.shareId
			if (uni.getStorageSync('applyId')) {
				this.queryById(uni.getStorageSync('applyId'), null)
			} else if (options.shareId) {
				this.queryById(options.shareId, null)
			} else {
				this.queryById(null, uni.getStorageSync('cffp_userId'))
			}
		},
		onShow() {
			if(uni.getStorageSync('cardForm')){
				this.cardForm = uni.getStorageSync('cardForm')
			}
			// if(uni.getStorageSync('Refresh_query')){
			// 	this.queryById(null, uni.getStorageSync('cffp_userId'))
			// }
			uni.$on("ckqueryById", res => {
				if (res == true) {
					this.queryById(null, uni.getStorageSync('cffp_userId'))
				}
			});
		},
		onUnload() {
			// 移除监听事件  
			uni.$off('ckqueryById');
		},
		methods: {
			queryById(id, userId) {
				uni.showLoading({
					title: '加载中...'
				});
				api.queryById({
					id: id,
					userId: userId
				}).then((res) => {
					if (res['success']) {
						this.applyParam = res['data']['data'];
						if(uni.getStorageSync('cardForm')){
							this.cardForm = uni.getStorageSync('cardForm')
						}else {
							this.cardForm.idFrontPageOssPath = this.applyParam.idFrontPageOssPath
							this.cardForm.idBackPageOssPath = this.applyParam.idBackPageOssPath
						}
						this.cardForm.approvalStatus = this.applyParam.approvalStatus
						this.cardForm.id = this.applyParam.id
						uni.setStorageSync('cardForm',this.cardForm)
					}
					uni.hideLoading()
				})
			},
			upLoadPhoto(type) {
				if (this.cardForm.approvalStatus == 0 || this.cardForm.approvalStatus == 2) {
					if (type == 'front') {
						this.dataForm.targetNo = 2;
					}
					if (type == 'back') {
						this.dataForm.targetNo = 3;
					}
					this.dataForm.targetId = this.cardForm.id
					CommonUpload(this.dataForm).then(res => {
						if (type == 'front') {
							this.cardForm.idFrontPageOssPath = res.data.filePath;
						}
						if (type == 'back') {
							this.cardForm.idBackPageOssPath = res.data.filePath;
						}
						uni.setStorageSync('cardForm',this.cardForm)
						console.log(this.cardForm.idBackPageOssPath, 151515)
					})
				}
			},
			saveInfo() {
				uni.showLoading({
					title: '加载中...'
				});
				if (this.cardForm.approvalStatus == 0 || this.cardForm.approvalStatus == 2) {
					if (!this.cardForm.idFrontPageOssPath) {
						uni.showToast({
							title: '请上传身份证正面照片',
							duration: 2000,
							icon: 'none'
						})
						return;
					}
					if (!this.cardForm.idBackPageOssPath) {
						uni.showToast({
							title: '请上传身份证反面照片',
							duration: 2000,
							icon: 'none'
						})
						return;
					}
					this.applyParam.idFrontPageOssPath = this.cardForm.idFrontPageOssPath
					this.applyParam.idBackPageOssPath = this.cardForm.idBackPageOssPath
					this.applyParam.approvalStatus = this.cardForm.approvalStatus
					this.applyParam = {
						...this.applyParam,
						operatStep: 3,
					}
					uni.setStorageSync('cardForm',this.cardForm)
					api.saveApplyInfo(this.applyParam).then((res) => {
						if (res['success']) {
							// this.queryById(null, uni.getStorageSync('cffp_userId'))
							uni.navigateTo({
								// url:`id-card?id=${res['data']['id']}`
								url: `education`
							});
						} else {
							uni.showToast({
								title: res['message'],
								duration: 2000,
								icon: 'none'
							})
							return;
						}
						uni.hideLoading()
					})
				} else {
					uni.hideLoading()
					uni.navigateTo({
						// url:`id-card?id=${res['data']['id']}`
						url: `education`
					});
				}
			}
		}
	}
</script>

<style lang="scss">
	@import 'applyCommon.scss';

	.content_wrapper {
		display: flex;
		justify-content: center;
		flex-wrap: wrap;
		margin-top: 10px;
		overflow: auto;

		.photo {
			text-align: center;
			border: 1px #8D8D8D dashed;
			border-radius: 4px;
			display: flex;
			justify-content: center;
			align-items: center;
			flex-direction: column;
			font-size: 12px;
			color: #333;
			width: 75%;
			background-size: contain;
			background-repeat: no-repeat;
			position: relative;

			.icon-weibiaoti553 {
				position: absolute;
			}

			.choseBtn {
				position: absolute;
				font-size: 13px;
				bottom: 19%;
			}
		}
		.picImg {
			width: 60%!important;
		}
	}

	
</style>
