<template>
	<view class="page">
		<view class="head-box"></view>
		<uv-navbar autoBack 
		:title="$t('pages_zlx.zlx_form.title')"
		leftIconColor="#fff" 
		:bgColor="bgColor" 
		height="100rpx" 
		:titleStyle="{color:'#fff'}"></uv-navbar><!-- 主理人认证 -->
	
		<view class="content">
			<view class="user-box">
				<uv-avatar :src="userInfo.headImage" size="98rpx" shape="circle"></uv-avatar>
				<view class="user-msg">
					<view class="user-msg-top">
						<view>{{userInfo.nickName}}</view>
						<!-- <view class="level-box">普通用户</view> -->
					</view>
					<view class="id-box">
						<text>ID:{{userInfo.id}}</text>
						<text class="copy-text" @click.stop="copy">{{$t('pages_zlx.zlx_form.copy')}}</text><!-- 复制 -->
					</view>
				</view>
			</view>
			
			<view class="base-msg">
				<view class="title-box">{{$t('pages_zlx.zlx_form.basic_info')}}</view><!-- 基础信息 -->
				<view class="form-box">
					<view class="form-box-line">
						<view class="label-box">{{$t('pages_zlx.zlx_form.real_name')}}</view><!-- 真实姓名 -->
						<view class="value-box">
							<uv-input :placeholder="$t('pages_zlx.zlx_form.enter_real_name')" v-model="info.name" border="none" color="#fff" :disabled="info.state == 0"></uv-input><!-- 请输入真实姓名 -->
						</view>
					</view>
					<view class="form-box-line">
						<view class="label-box">{{$t('pages_zlx.zlx_form.contact_info')}}</view><!-- 联系方式 -->
						<view class="value-box">
							<uv-input :placeholder="$t('pages_zlx.zlx_form.enter_contact_info')" v-model="info.phone" type="number" border="none" color="#fff" :disabled="info.state == 0"></uv-input><!-- 请输入联系方式 -->
						</view>
					</view>
					<view class="form-box-line">
						<view class="label-box">{{$t('pages_zlx.zlx_form.id_card')}}</view><!-- 身份证号 -->
						<view class="value-box">
							<uv-input :placeholder="$t('pages_zlx.zlx_form.enter_id_card')" type="idcard" v-model="info.cardNo" border="none" color="#fff" :disabled="info.state == 0"></uv-input><!-- 请输入身份证号 -->
						</view>
					</view>
					<view class="form-box-line">
						<view class="label-box">{{$t('pages_zlx.zlx_form.resume_attachment')}}</view><!-- 简历附件 -->
						<view class="value-box">
							
							<!-- <uv-input placeholder=" " 
							v-model="info.image" 
							border="none" 
							color="#fff"></uv-input> -->
							
							<view class="docx-title"
							@click="openDocument(info.image)">
								{{ info.image || '' }}
							</view>
							
							<view class="upload-btn"
							@click="info.state != 0 ? uploadFile() : null"
							:class="{'disabled': info.state == 0}">{{$t('pages_zlx.zlx_form.upload_resume')}}</view><!-- 上传简历 -->
						</view>
					</view>
					<view class="form-box-line">
						<view class="label-box">{{$t('pages_zlx.zlx_form.wechat_qr_code')}}</view><!-- 微信二维码 -->
						<view class="value-box">
							<view style="width: 80rpx;height: 80rpx;">
								<uv-image v-if="info.img" :src="info.img" width="80rpx" height="80rpx" @click="bigImg"></uv-image>
							</view>
							<view class="upload-btn" 
							@click="info.state != 0 ? upImg() : null"
							:class="{'disabled': info.state == 0}">{{$t('pages_zlx.zlx_form.upload_image')}}</view><!-- 上传图片 -->
						</view>
					</view>
				</view>
			</view>
			
			<view class="xie-box">
				<view class="title-box">{{$t('pages_zlx.zlx_form.personal_photos_title')}}</view><!-- 个人生活照片（含全身照） -->
				<view class="xie-box-val">
					<uv-upload 
					:fileList="fileList" 
					multiple
					:maxCount="9" 
					width="180rpx"
					height="180rpx" 
					:disabled="info.state == 0"
					@afterRead="afterRead" 
					@delete="deleteImage">
					</uv-upload>
				</view>
			</view>
			
			<view class="xie-box">
				<view class="title-box">{{$t('pages_zlx.zlx_form.agreement_title')}}</view><!-- 阅读并同意协议 -->
				<view class="xie-box-val">
					<view class="val-text">
						<view>{{$t('pages_zlx.zlx_form.agreement_text')}}</view><!-- 我已经阅读并同意 -->
						<view class="xieyi-val"
						@click="$utils.navigateTo('/pages_login/yinsixieyi?key=vip_text')">{{$t('pages_zlx.zlx_form.agreement_link')}}</view><!-- 《主理人协议》 -->
					</view>
					<view class="choose-box"  
					@click="info.state != 0 ? (isAgree = !isAgree) : null"
					:class="{'disabled': info.state == 0}">
						<view class="normol-box" v-if="!isAgree"></view>
						<image src="./static/choose-icon.png" mode="widthFix" v-else></image>
					</view>
				</view>
			</view>
		</view>
	
		<!-- 审核中状态 -->
		<view class="btn-box" v-if="info.state == 0">
			<uv-button :text="$t('pages_zlx.zlx_form.reviewing')" color="#999999" shape="circle" :customStyle="reviewingBtnStyle"></uv-button><!-- 审核中 -->
		</view>
		
		<!-- 已审核通过状态 -->
		<view class="btn-box" v-else-if="info.state == 1">
			<uv-button :text="$t('pages_zlx.zlx_form.approved')" color="#381615" shape="circle" :customStyle="btnCustomStyle"></uv-button><!-- 已审核通过 -->
		</view>
		
		<!-- 驳回状态或初始状态，可以提交 -->
		<view class="btn-box" v-else>
			<uv-button :text="$t('pages_zlx.zlx_form.save')" color="#381615" shape="circle" :customStyle="btnCustomStyle" @click="confrim"></uv-button><!-- 保存 -->
		</view>
	</view>
</template>

<script>
	export default{
		data() {
			return {
				isAgree:false,
				btnCustomStyle:{
					color:'#FF5858'
				},
				reviewingBtnStyle:{
					color:'#999999'
				},
				info:{
					name:'',
					phone:'',
					cardNo:'',
					img:'',
					image : '',
				},
				bgColor : 'transparent',
				fileList : [],
			}
		},
		onPageScroll(e) {
			if(e.scrollTop > 50) {
				this.bgColor = '#49070c'
			}else{
				this.bgColor = 'transparent'
			}
		},
		computed: {
		},
		onLoad() {
			this.getjoinRecruitInfo()
			this.$store.commit('getUserInfo')
		},
		methods:{
			bigImg() {
				uni.previewImage({
					urls:[this.info.img],
					current:0
				})
			},
			openDocument(Path){
				if(!Path){
					return
				}
				 wx.openDocument({
					filePath: Path,
					showMenu: true,
					success: function (res) {
						// console.log('打开成功');
						// util.hideLoading()
					}
				})
			},
			uploadFile() {
				let that = this
				uni.chooseMessageFile({
					count: 1, //一次可以上传多少个
					type: 'file',
					extension: ['.doc', '.pdf', '.docx'], //文件类型
					success(res) {
						console.log(res);
						that.$Oss.ossUpload(res.tempFiles[0].path)
						.then(url => {
							that.info.image = url
						})
					},
					fail: (err) => {
						console.log(err, 'err');
					}
				});
			},
			upImg() {
				uni.chooseImage({
					count: 1, // 默认9, 设置图片的选择数量
					sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
					success: (res) => {
						// 返回选定照片的本地文件路径列表 tempFilePaths
						this.$Oss.ossUpload(res.tempFilePaths[0])
						.then(url => {
							this.info.img = url
							this.$api('updateInfo',this.info, res => {
								if (res.code == 200) {
									uni.showToast({
										title: this.$t('pages_zlx.zlx_form.save_success'), // 保存成功
										icon:'none'
									})
								}
							})
						})
						// 根据需要进行后续处理，例如上传图片等
					},
					fail: (err) => {
						// 处理错误情况
						console.log('选择图片失败', err);
					}
				});
				
			},
			getjoinRecruitInfo() {
				this.$api('joinRecruitInfo',res=>{
					if(res.code == 200 && res.result) {
						this.info = res.result
						
						this.fileList = this.info.images ? 
						this.info.images.split(',').map(url => {
							return {
								url
							}
						}) : []
					}
				})
			},
			copy() {
				uni.setClipboardData({
					data:this.userInfo.id,
					success: () => {
						uni.showToast({
							title: this.$t('pages_zlx.zlx_form.copy_success'), // 复制成功
							icon:'none'
						})
					}
				})
			},
			confrim() {
				if(!this.isAgree) return this.$Toast(this.$t('pages_zlx.zlx_form.please_agree_agreement')) // 请先阅读并同意《主理人协议》
				if(!this.info.name) return this.$Toast(this.$t('pages_zlx.zlx_form.please_enter_name')) // 请输入姓名
				if(!this.info.phone) return this.$Toast(this.$t('pages_zlx.zlx_form.please_enter_contact')) // 请输入联系方式
				if(!this.$utils.verificationPhone(this.info.phone)) return this.$Toast(this.$t('pages_zlx.zlx_form.please_enter_correct_contact')) // 请输入正确的联系方式
				if(!this.info.cardNo) return this.$Toast(this.$t('pages_zlx.zlx_form.please_enter_id_card')) // 请输入身份证号
				if(this.info.cardNo.length != 18) return this.$Toast(this.$t('pages_zlx.zlx_form.please_enter_correct_id_card')) // 请输入正确的身份证号
				if(!this.info.img) return this.$Toast(this.$t('pages_zlx.zlx_form.please_upload_wechat_qr_code')) // 请上传微信二维码
				let params = {
					name:this.info.name,
					phone:this.info.phone,
					cardNo:this.info.cardNo,
					img:this.info.img,
					image:this.info.image,
					id:this.info.id || '',
					images : this.fileList.map((item) => item.url).join(",")
				}
				// if(!this.info.image) return this.$Toast('请输入姓名')
				this.$api('joinRecruit',params,res=>{
					if(res.code == 200) {
						this.$Toast(this.$t('pages_zlx.zlx_form.submit_success')) // 提交成功
						setTimeout(uni.navigateBack, 800, -1)
					}
				})
			},
			deleteImage(e){
				this.fileList.splice(e.index, 1)
			},
			afterRead(e){
				let self = this
				e.file.forEach(file => {
					self.$Oss.ossUpload(file.url).then(url => {
						self.fileList.push({
							url
						})
					})
				})
			},
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #060504;
	}
</style>
<style lang="scss" scoped>
	.page{
	}
	.head-box {
		background: url('@/static/image/nav-bg.png') no-repeat;
		background-size: 100% 100%;
		width: 100%;
		height: 534rpx;
		position: absolute;
		z-index: -1;
	}
	.content {
		margin-top: 40rpx;
		padding-bottom: 100rpx;
		padding: 0 30rpx;
		padding-top: calc(var(--status-bar-height) + 110rpx);
		
		.user-box {
			display: flex;
			align-items: center;
			margin-bottom: 74rpx;
			.user-msg {
				margin-left: 20rpx;
				.user-msg-top {
					font-weight: 600;
					font-size: 32rpx;
					color: #E6E6E6;
					display: flex;
					align-items: center;
					.level-box {
						width: 108rpx;
						height: 31rpx;
						background: RGBA(40, 19, 4, 1);
						border-radius: 16rpx;
						font-weight: 400;
						font-size: 20rpx;
						color: #FF9C00;
						text-align: center;
						margin-left: 14rpx;
					}
				}
			
				.id-box {
					color: #999999;
					font-size: 22rpx;
					margin-top: 20rpx;
					.copy-text {
						font-weight: 400;
						font-size: 22rpx;
						color: #E6E6E6;
						margin-left: 18rpx;
					}
				}
			}
		}
		
		.title-box {
			display: inline-block;
			font-weight: bold;
			font-size: 29rpx;
			color: #FFFFFF;
			position: relative;
			z-index: 2;
			&::after{
				content: "";
				position: absolute;
				bottom: 0;
				left: 0;
				width: 100%;
				height: 9rpx;
				background: #FF2828;
				border-radius: 4rpx 4rpx 4rpx 4rpx;
				opacity: 0.2;
				z-index: 1;
			}
		}
	
		.base-msg {
			.form-box {
				background: #1B1713;
				border-radius: 27rpx;
				padding:0 40rpx;
				margin-top: 20rpx;
				.form-box-line {
					min-height: 112rpx;
					border-bottom: 1px solid #403D3A;
					display: flex;
					align-items: center;
					&:last-child {
						border: none;
					}
					.label-box {
						font-weight: 500;
						font-size: 29rpx;
						color: #FFFFFF;
						margin-right: 34rpx;
					}
					.value-box {
						display: flex;
						align-items: center;
						justify-content: space-between;
						flex:1;
						.upload-btn {
							font-weight: 400;
							font-size: 28rpx;
							color: #FF5858;
							&.disabled {
								color: #999999;
								cursor: not-allowed;
							}
						}
					}
					.docx-title{
						font-size: 20rpx;
						color: #999999;
						width: 340rpx;
						overflow:hidden; //超出的文本隐藏
						text-overflow:ellipsis; //溢出用省略号显示
						white-space:nowrap; //溢出不换行
					}
				}
			}
		}
		.xie-box {
			margin-top:37rpx;
			.xie-box-val {
				margin-top: 25rpx;
				// height: 116rpx;
				background: #1B1713;
				border-radius: 27rpx 27rpx 27rpx 27rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 36rpx 36rpx;
				.val-text {
					font-weight: 400;
					font-size: 25rpx;
					color: #FFFFFF;
					display: flex;
					align-items: center;
					.xieyi-val {
						color: #FF5858;
					}
				}
				.choose-box {
					.normol-box {
						width: 31rpx;
						height: 31rpx;
						border: 1px solid #fff;
						border-radius: 5rpx;
					}
					image {
						width: 31rpx;
						height: 31rpx;
					}
					&.disabled {
						opacity: 0.5;
						cursor: not-allowed;
					}
				}
			}
		}
	}

	.btn-box {
		// position: fixed;
		// bottom: 70rpx;
		// left: 0;
		// right: 0;
		padding: 0 40rpx;
		margin-top: 20rpx;
		height: 200rpx;
	}
</style>