<template>
	<!-- 实名认证 -->
	<view class="page">
		<navbar title="实名认证"
		leftClick 
		@leftClick="$utils.navigateBack"/>
		
		<view class="info-tips">
			
			完成实名认证，<text
			@click="$refs.configPopup.open('permission_authentic_person')">您将获得《个人实名认证平台特权》</text>
			
		</view>
		<view style="color: red;" v-if="sub ==2">
			已驳回，原因：{{form.remark}}
		</view>
		
		<view class="form">
			<view class="form-item">
				<view class="label">
					姓名
				</view>
				<input type="text" class="form-input" 
				placeholder="请输入姓名"
				v-model="form.name"/>
			</view>
			
			<view class="form-item">
				<view class="label">
					身份证号码
				</view>
				<input type="text" class="form-input" 
				placeholder="请输入身份证号码"
				v-model="form.cerNo"/>
			</view>
			
			<view class="form-item">
				<view class="label">
					联系方式
				</view>
				<input type="text" class="form-input" 
				placeholder="请输入联系方式"
				v-model="form.phone"/>
			</view>
			
			<view class="form-item">
				<view class="title">
					上岗操作证或技能等级证书（必填）
				</view>
			</view>
			
			<view class="form-item">
				<uv-upload 
				:fileList="skillBook" 
				:maxCount="9" 
				width="280rpx"
				height="280rpx" 
				multiple 
				name="skillBook"
				@afterRead="afterRead" 
				@delete="deleteImage">
				</uv-upload>
			</view>
			
			<view class="form-item">
				<view class="title">
					请上传身份证人像面照片（必填）
				</view>
				<view class="tips">
					信息仅用身份核实，上传后可增加曝光机会
				</view>
			</view>
			
			
			<view class="form-item">
				<uv-upload 
				:fileList="fileList" 
				:maxCount="1" 
				width="690rpx"
				height="280rpx" 
				multiple 
				name="fileList"
				@afterRead="afterRead" 
				@delete="deleteImage">
					<view class="upload">
						<image src="../static/auth/cart.png"
						mode="aspectFit"
						style="width: 390rpx;height: 280rpx;" />
						<view class="btn-add">
							点击上传
						</view>
					</view>
				</uv-upload>
			</view>
			<view class="form-item">
				<view class="tips"
				style="text-align: center;padding: 20rpx 0;">
					(确保文字清晰、可辨、避免遮挡、不全、反光)
				</view>
			</view>
			
			<view class="form-item">
				<view class="title">
					授权书（选填）
				</view>
			</view>
			
			<view class="form-item">
				<uv-upload 
				:fileList="authFileList" 
				:maxCount="1" 
				width="280rpx"
				height="280rpx" 
				multiple 
				name="authFileList"
				@afterRead="afterRead" 
				@delete="deleteImage">
				</uv-upload>
			</view>
			
		</view>
		
		
		
		<view v-if="sub==0"  class="uni-color-btn" @click="sumbit">
			认证
		</view>
		
		<view v-if="sub==1 && !isExpiringSoon"  class="uni-uncolor-btn">
			已审核通过
		</view>
		
		<view v-if="sub==1 && isExpiringSoon"  class="uni-color-btn" @click="sumbit">
			{{expireText}}
		</view>
		
		<view v-if="sub==2"  class="uni-redcolor-btn" @click="sumbit">
			已驳回,请重新提交
		</view>
		
		
		
		
		<view class="config">
			<uv-checkbox-group
				v-model="checkboxValue"
				shape="circle">
				<view class="content">
					<view
					style="display: flex;">
						<uv-checkbox
							size="40rpx"
							icon-size="30rpx"
							activeColor="#3796F8"
							:name="1"
						></uv-checkbox>
						阅读并同意我们的<text @click="$refs.configPopup.open('config_service_authentic')">《个人实名认证服务协议》</text>
					</view>
				</view>
			</uv-checkbox-group>
		</view>
		
		<configPopup ref="configPopup"/>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				checkboxValue : [],
				form : {},
				fileList: [],
				authFileList : [],
				skillBook : [],
				sub : 0,
				isExpiringSoon: false, // 是否即将过期或已过期
				expireText: '认证即将过期，重新认证', // 过期状态文案
			}
		},
		onLoad() {
			this.getAuthenticationPerson();
		},
		methods: {
			//获取个人实名信息
			getAuthenticationPerson(){
				this.$api('getAuthenticationPerson',{}, res =>{
					if(res.code == 200 && res.result){
						
						this.form = res.result || {};
						
						this.sub = this.form.status || 0;
						
						// 检查validTime是否即将过期或已过期
						if(this.form.validTime && this.sub == 1) {
							const validTime = new Date(this.form.validTime);
							const currentTime = new Date();
							const sevenDaysLater = new Date();
							sevenDaysLater.setDate(currentTime.getDate() + 7);
							
							// 如果已过期或7天内过期，允许重新认证
							if(validTime <= sevenDaysLater) {
								this.isExpiringSoon = true;
								// 设置过期状态文案
								if(validTime <= currentTime) {
									this.expireText = '认证已过期，重新认证';
								} else {
									this.expireText = '认证即将过期，重新认证';
								}
							}
						}
						
						this.fileList = this.form.image ? this.form.image.split(',').map(url => {
							return {
								url
							}
						}) : []
						
						this.authFileList = this.form.authorizationBook ? this.form.authorizationBook.split(',').map(url => {
						return {
							url
						}
					}) : []
					
					this.skillBook = this.form.skillBook ? this.form.skillBook.split(',').map(url => {
						return {
							url
						}
					}) : []
					}
				})
			},
			deleteImage(e){
				this[e.name].splice(e.index, 1)
			},
			afterRead(e){
				let self = this
				e.file.forEach(file => {
					self.$Oss.ossUpload(file.url).then(url => {
						console.log(url);
						self[e.name].push({
							url
						})
					})
				})
			},
			//提交实名认证信息 :lzx
			sumbit(){
				if(!this.checkboxValue.length){
					return uni.showToast({
						title: '请先同意个人实名认证服务协议',
						icon:'none'
					})
				}
				this.form.image = this.fileList.map((item) => item.url).join(",")
				this.form.authorizationBook = this.authFileList.map((item) => item.url).join(",")
				this.form.skillBook = this.skillBook.map((item) => item.url).join(",")
				if(this.$utils.verificationAll(this.form,{
					name:'请输入姓名',
					cerNo:'请输入身份证号码',
					phone:'请输入电话号码',
					skillBook:'请上传上岗操作证或技能等级证书',
					image:'身份证照片不能为空',
				})) {
					return
				}
				
				// 清理不需要给后端的字段
				delete this.form.createBy
				delete this.form.createTime
				delete this.form.updateBy
				delete this.form.updateTime
				delete this.form.userId
				delete this.form.validTime
				
				this.$api('addAuthenticationPerson',this.form, res =>{
					if(res.code == 200){
						uni.showToast({
							title:'提交成功!等待审核',
							icon: 'none'
						})	
						setTimeout(uni.navigateBack,1000,-1)
					}
				})
			}
			
		}
	}
</script>

<style scoped lang="scss">
.page{
	background-color: #fff;
	min-height: 100vh;
	padding-bottom: 100rpx;
	.info-tips{
		width: 100%;
		padding: 30rpx 0;
		background-color: #f3f3f3;
		text-align: center;
		text{
			color: $uni-color;
		}
	}
	
	.form {
		padding: 30rpx;
		.form-item{
			.label{
				padding: 20rpx 0;
			}
			.form-input{
				border: 1px solid $uni-color;
				background: rgba($uni-color, 0.1);
				padding: 10rpx 20rpx;
				font-size: 28rpx;
			}
			.title{
				font-weight: 900;
				margin-top: 50rpx;
				padding: 10rpx 0;
			}
			.tips{
				font-size: 26rpx;
				color: #777;
				padding-bottom: 20rpx;
			}
		}
		.upload{
			display: flex;
			justify-content: center;
			align-items: center;
			width: 690rpx;
			background-color: #f3f3f3;
			border-radius: 10rpx;
			.btn-add{
				margin: auto;
				padding: 10rpx 20rpx;
				background-color: $uni-color;
				color: #fff;
				border-radius: 10rpx;
			}
		}
	}
	
	.config{
		font-size: 26rpx;
		line-height: 40rpx;
		width: 100%;
		display: flex;
		justify-content: center;
		.content{
			width: 100%;
			display: flex;
			flex-direction: column;
			align-items: center;
		}
		text{
			color: $uni-color;
		}
	}
}
</style>
