<template>
	<!-- 实名认证 -->
	<view class="page">
		<navbar title="企业认证"
		leftClick 
		@leftClick="$utils.navigateBack"/>
		
		<view class="info-tips">
			完成企业认证，<text
			@click="$refs.configPopup.open('permission_authentic_company')">
				您将获得《企业认证平台特权》
			</text>
		</view>
		<view style="color: red;" v-if="status == 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.career"/>
			</view>
			
			<view class="form-item">
				<view class="label">
					企业名称
				</view>
				<input type="text" class="form-input" 
				placeholder="请输入企业名称"
				v-model="form.companyName"/>
			</view>
			
			<view class="form-item"
			@click="selectAddr">
				<view class="label">
					企业地址
				</view>
				<input type="text" class="form-input" 
				placeholder="请输入企业地址"
				disabled
				v-model="form.companyAddress"/>
			</view>
			
			<view class="form-item">
				<view class="label">
					法定代表人
				</view>
				<input type="text" class="form-input" 
				placeholder="请输入法定代表人"
				v-model="form.legalPerson"/>
			</view>
			
			<view class="form-item">
				<view class="label">
					社会统一新信用代码
				</view>
				<input type="text" class="form-input" 
				placeholder="请输入社会统一新信用代码"
				v-model="form.socialCode"/>
			</view>
			
			<view class="form-item">
				<view class="title">
					请上传工商营业执照
				</view>
				<view class="tips">
					（只能上传jpg、png,且不能超过1MB）
				</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="status==0"  class="uni-color-btn" @click="sumbit">
			认证
		</view>
		
		<view v-if="status==1"  class="uni-uncolor-btn" @click="sumbit">
			已审核通过
		</view>
		
		<view v-if="status==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('service_authentic_company')">
							《企业认证服务协议》
						</text>
					</view>
				</view>
			</uv-checkbox-group>
		</view>
		
		<configPopup ref="configPopup"/>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				checkboxValue : [],
				form : {
					companyAddress : '',
					career : '',//职位
				},
				fileList: [],
				status : 0,
				authFileList : [],
			}
		},
		onLoad() {
			this.getAuthenticationCompany();
		},
		methods: {
			//我的服务-查询企业实名认证信息
			getAuthenticationCompany(){
				this.$api('getAuthenticationCompany', {}, res =>{
					if(res.code == 200 && res.result){
						
						this.form = res.result;
						
						this.status = this.form.status || 0;
						
						this.fileList = this.form.businessLicense ? this.form.businessLicense
						.split(',').map(url => {
							return {
								url
							}
						}) : []
						
						this.authFileList = this.form.authorizationBook ? this.form.authorizationBook.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 => {
						self[e.name].push({
							url
						})
					})
				})
			},
			//提交企业认证信息 :lzx
			sumbit(){
				if(!this.checkboxValue.length){
					return uni.showToast({
						title: '请先同意企业认证服务协议',
						icon:'none'
					})
				}
				this.form.businessLicense = this.fileList.map((item) => item.url).join(",")
				if(this.$utils.verificationAll(this.form,{
					career : '请输入您的职位',
					companyName : '请输入企业名称',
					companyAddress : '请选择企业地址',
					legalPerson : '请输入企业法人',
					socialCode : '请输入社会统一信用代码',
					businessLicense : '工商营业执照不能为空',
				})) {
					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('addAuthenticationCompany', this.form, res =>{
					if(res.code == 200){
						uni.showToast({
							title:'提交成功!等待审核',
							icon: 'none'
						})
						setTimeout(uni.navigateBack,1000,-1)
					}
				})
			},
			//地图上选择地址
			selectAddr() {
				uni.chooseLocation({
					success: (res) => {
						//经纬度信息
						this.form.latitude = res.latitude
						this.form.longitude = res.longitude
						
						// if (res.name) { //用户直接选择城市的逻辑
						if (!res.address && res.name) { //用户直接选择城市的逻辑
							return this.form.companyAddress = res.name
						}
						if (res.address || res.name) {
							return this.form.companyAddress = res.address + res.name
						}
						this.form.companyAddress = '' //用户啥都没选就点击勾选
						this.form.latitude = ''
						this.form.longitude = ''
					}
				});
			},
			
		}
	}
</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>
