<template>
	<view class="content">
		<view class="box1">
			<view class="steps hflex acenter jcenter">
				<u-steps :current="current">
					<u-steps-item title="个人信息"></u-steps-item>
					<u-steps-item title="工作信息"></u-steps-item>
					<u-steps-item title="审核"></u-steps-item>
				</u-steps>
			</view>
			<view v-if="current == 0">
				<view class="title">个人信息</view>
				<view class="hflex acenter jbetween cell">
					<view class="left">姓名</view>
					<u-input v-model="userInfo.name" placeholder="请输入您的姓名" inputAlign="right" border="none"></u-input>
				</view>
				<view class="hflex acenter jbetween cell">
					<view class="left">身份证号</view>
					<u-input v-model="userInfo.idNumber" placeholder="请输入您的身份证号" inputAlign="right" border="none"></u-input>
				</view>
				<view class="hflex acenter jbetween cell">
					<view class="left">邮箱</view>
					<u-input v-model="userInfo.eamil" placeholder="请输入您的邮箱" inputAlign="right" border="none"></u-input>
				</view>
				<picker mode="region" @change="changeAddress" :value="index" >
					<view class="hflex acenter jbetween cell">
						<view class="left">所在地区</view>
						<u-input border="none" inputAlign="right" disabled disabledColor="#fff" v-model="userInfo.region" placeholder="请选择省市区" suffixIcon="arrow-right" suffixIconStyle="color: #b7b7b7"></u-input>
					</view>
				</picker>
				<view class="hflex acenter jbetween cell">
					<view class="left">地址</view>
					<u-input v-model="userInfo.address" placeholder="请输入您的地址" inputAlign="right" border="none"></u-input>
				</view>
				<view class="line"></view>
				<view class="cell">
					<view class="img_title">身份证人像面照片</view>
					<view class="upload_img hflex acenter jcenter">
						<u-upload :fileList="userInfo.fileList1" @afterRead="afterRead" @delete="deletePic" :maxCount="1" name="1" multiple :previewFullImage="true" width="550rpx" height="324rpx">
							<image src="/static/images/comment/upload-just.png" style="width: 550rpx;height: 324rpx;"></image>
						</u-upload>
					</view>
				</view>
				<view class="cell">
					<view class="img_title">身份证国徽面照片</view>
					<view class="upload_img hflex acenter jcenter">
						<u-upload :fileList="userInfo.fileList2" @afterRead="afterRead" @delete="deletePic" :maxCount="1" name="2" multiple :previewFullImage="true" width="550rpx" height="324rpx">
							<image src="/static/images/comment/upload-back.png" style="width: 550rpx;height: 324rpx;"></image>
						</u-upload>
					</view>
				</view>
				<view class="bottom">
					<view class="btn" @click="next">下一步</view>
				</view>
			</view>
			<view v-if="current == 1">
				<view class="title">工作信息</view>
				<view class="cell">
					<view class="img_title">营业执照（必填）</view>
					<view class="upload_img hflex acenter jcenter">
						<u-upload :fileList="userInfo.fileList3" @afterRead="afterRead" @delete="deletePic" :maxCount="1" name="3" multiple :previewFullImage="true" width="550rpx" height="324rpx">
							<image src="/static/images/comment/upload-business.png" style="width: 550rpx;height: 324rpx;"></image>
						</u-upload>
					</view>
				</view>
				<view class="cell">
					<view class="img_title">厂家授权（非必填）</view>
					<view class="upload_img hflex acenter jcenter">
						<u-upload :fileList="userInfo.fileList4" @afterRead="afterRead" @delete="deletePic" :maxCount="1" name="4" multiple :previewFullImage="true" width="550rpx" height="324rpx">
							<image src="/static/images/comment/upload-business.png" style="width: 550rpx;height: 324rpx;"></image>
						</u-upload>
					</view>
				</view>
				<view class="cell">
					<view class="img_title">以往业绩</view>
					<view class="img_subtitle">请详细描述您的以往业绩</view>
					<u-input v-model="userInfo.achievement" border="bottom"></u-input>
				</view>
				<view class="bottom">
					<view class="btn" @click="submit">提交</view>
				</view>
			</view>
			<view v-if="current == 2" class="box vflex acenter jcenter">
				<view v-if="state == 1" class="vflex acenter jcenter">
					<image src="/static/images/comment/wait.png" class="box1_img"></image>
					<view class="box1_title">审核中</view>
					<view class="box1_subTitle">预计审核时间3个工作日</view>
					<view class="box1_subTitle">请留意当前页面变动</view>
					<view class="btn" @click="result">返回</view>
				</view>
				<view v-if="state == 3" class="vflex acenter jcenter">
					<image src="/static/images/comment/error.png" class="box1_img"></image>
					<view class="box1_title">审核失败</view>
					<view class="box1_subTitle">驳回原因</view>
					<view class="box1_subTitle">{{reason}}<span style="color: #506dff;margin-left: 8rpx;" @click="edit">立即修改></span></view>
					<view class="btn" @click="result">返回</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import $api from '@/static/js/api.js'
	var that = ''
	export default {
		data() {
			return {
				current: 0,
				userInfo: {
					id: '',
					name: '',
					idNumber: '',
					eamil: '',
					region: '',
					address: '',
					province: '',
					city:'',
					area: '',
					fileList1: [],
					fileList2: [],
					fileList3: [],
					fileList4: [],
					achievement: '',
				},
				fileList1: '',
				fileList2: '',
				fileList3: '',
				fileList4: '',
				state: 1,
				reason: '',
			}
		},
		onLoad() {
			that = this
			that.getPreData()
		},
		methods: {
			getPreData() {
				var pages = getCurrentPages()
				var prePage = pages[pages.length - 2]
				var maintenance = prePage.$vm.user.maintenance
				if(maintenance) {
					that.userInfo = {
						name: maintenance.name,
						idNumber: maintenance.id_card,
						eamil: maintenance.eamil,
						region: maintenance.province + maintenance.city + maintenance.area,
						address: maintenance.results,
						province: maintenance.province,
						city:maintenance.city,
						area: maintenance.area,
						// fileList1: maintenance.id_card_img1.split(','),
						// fileList2: maintenance.id_card_img2.split(','),
						// fileList3: maintenance.business_img.split(','),
						// fileList4: maintenance.authorization_img.split(','),
						achievement: maintenance.results,
						id: maintenance.id
					}
					var imgs = maintenance.id_card_img1.split(',')
					let obj = {}
					for(let key in imgs) {
						obj[key] = imgs[key]
					}
					var fileList1 = Object.keys(obj).map(item => ({
						url: obj[item]
					}))
					that.userInfo.fileList1 = fileList1
					imgs = []
					imgs = maintenance.id_card_img1.split(',')
					obj = {}
					for(let key in imgs) {
						obj[key] = imgs[key]
					}
					var fileList2 = Object.keys(obj).map(item => ({
						url: obj[item]
					}))
					that.userInfo.fileList2 = fileList2
					imgs = []
					imgs = maintenance.id_card_img1.split(',')
					obj = {}
					for(let key in imgs) {
						obj[key] = imgs[key]
					}
					var fileList3 = Object.keys(obj).map(item => ({
						url: obj[item]
					}))
					that.userInfo.fileList3 = fileList3
					imgs = []
					imgs = maintenance.id_card_img1.split(',')
					obj = {}
					for(let key in imgs) {
						obj[key] = imgs[key]
					}
					var fileList4 = Object.keys(obj).map(item => ({
						url: obj[item]
					}))
					that.userInfo.fileList4 = fileList4
					that.state = maintenance.audit
					that.reason = maintenance.why
					if(that.state == 1 || that.state == 3) {
						that.current = 2
					}
				}
			},
			// 选择所在地区
			changeAddress(e) {
				console.log(e);
				that.userInfo.region = e.detail.value[0] + '-' + e.detail.value[1] + '-' + e.detail.value[2]
				that.userInfo.province = e.detail.value[0]
				that.userInfo.city = e.detail.value[1]
				that.userInfo.area = e.detail.value[2]
			},
			// 删除图片
			deletePic(event) {
				// that.userInfo.imgList.splice(event.index, 1)
				console.log(event)
				this.userInfo[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				console.log(`fileList${event.name}`);
				console.log(this.userInfo[`fileList${event.name}`]);
				let lists = [].concat(event.file)
				let fileListLen = this.userInfo[`fileList${event.name}`].length
				lists.map((item) => {
					this.userInfo[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this.userInfo[`fileList${event.name}`][fileListLen]
					this.userInfo[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result.url
					}))
					
					fileListLen++
				}
				if(that.userInfo[`fileList${event.name}`].length > 0) {
					for(var i=0;i<that.userInfo[`fileList${event.name}`].length;i++) {
						if(i == that.userInfo[`fileList${event.name}`].length -1) {
							that[`fileList${event.name}`] += that.userInfo[`fileList${event.name}`][i].url
						} else {
							that[`fileList${event.name}`] += that.userInfo[`fileList${event.name}`][i].url + ','
						}
					}
				}
			},
			// 上传图片
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: $api.config.baseUrl + '/data/api.auth.Center/upload',
						filePath: url,
						name: 'file',
						header: {
							'content-type': 'application/x-www-form-urlencoded',
							'api-token': uni.getStorageSync('token').token?uni.getStorageSync('token').token:'',
							'api-name': 'wxapp'
						},
						formData: {
							user: 'test'
						},
						success: (res) => {
							console.log(res);
							setTimeout(() => {
								var data = JSON.parse(res.data)
								resolve(data.data)
							}, 1000)
						}
					});
				})
			},
			next() {
				// that.current = that.current + 1
				if ($api.formCheck(that.userInfo.name,'truename') && $api.formCheck(that.userInfo.idNumber,'identity') && $api.formCheck(that.userInfo.eamil,'email') && $api.formCheck(that.userInfo.address,'required') && $api.formCheck(that.fileList1,'required') && $api.formCheck(that.fileList2,'required')) {
					that.current = that.current + 1
				}
			},
			submit() {
				var u = that.userInfo
				// that.current = that.current + 1
				if ($api.formCheck(that.fileList3,'required') && $api.formCheck(that.userInfo.achievement,'required')) {
					$api.req({
						url: '/data/api.auth.Center/addupmaintenance',
						method: 'POST',
						data: {
							id: u.id?u.id: '',
							name: u.name,
							id_card: u.idNumber,
							email: u.eamil,
							province: u.province,
							city: u.city,
							area: u.area,
							address: u.address,
							id_card_img1: that.fileList1,
							id_card_img2: that.fileList2,
							business_img: that.fileList3,
							authorization_img: that.fileList4,
							results: u.achievement,
						}
					}, function(res) {
						if(res.code == 1) {
							$api.info(res.info)
							that.current = that.current + 1
						}
					})
					
				}
			},
			edit() {
				that.state = 0
			},
			result() {
				$api.jump('/pages/tabbar/mine/mine',3)
			}
		},
	}
</script>

<style lang="scss" scoped>
	.content::v-deep {
		padding: 0 40rpx;
		.box1 {
			.steps {
				width: 100%;
				height: 188rpx;
			}
			.title {
				font-size: 32rpx;
				color: #444;
				margin: 8rpx 0;
				position: relative;
				z-index: 9;
			}
			.title::before {
				width: 120rpx;
				height: 16rpx;
				background: linear-gradient(270deg, #506dff 0%, #fff 100%);
				content: "";
				position: absolute;
				left: 0;
				bottom: 0;
				z-index: 2;
				opacity: 0.8;
			}
			.u-input {
				width: 100%;
				// margin-top: 30rpx;
			}
			.cell {
				width: 100%;
				padding: 20rpx 0;
				.left {
					font-size: 28rpx;
					color: #222;
				}
				.u-input {
					font-size: 28rpx;
					color: #222;
				}
				.img_title {
					font-size: 28rpx;
					color: #444;
				}
				.upload_img {
					width: 670rpx;
					height: 404rpx;
					background-color: #f4f4f4;
					border-radius: 20rpx;
					margin: 28rpx 0 40rpx;
				}
				.u-upload__wrap {
					display: flex;
					align-items: center !important;
					justify-content: center !important;
				}
				.img_subtitle {
					font-size: 28rpx;
					color: #A3A3A3;
					padding: 12rpx 0;
				}
				.u-input {
					width: 100%;
					// margin-top: 30rpx;
				}
			}
			.line {
				width: 100%;
				height: 1rpx;
				background-color: #f4f4f4;
			}
			.bottom {
				width: 100%;
				margin: 40rpx 0 64rpx;
				.btn {
					width: 690rpx;
					height: 88rpx;
					text-align: center;
					line-height: 88rpx;
					border-radius: 44rpx;
					background-color: #506dff;
					color: #fff;
					font-size: 36rpx;
				}
			}
		}
		.box {
			width: 100%;
			// height: 100vh;
			background-color: #fff;
			.box1_img {
				width: 124rpx;
				height: 124rpx;
				margin: 108rpx 0 40rpx;
			}
			.box1_title {
				font-size: 32rpx;
				color: #222;
				margin-bottom: 24rpx;
			}
			.box1_subTitle {
				margin: 12rpx 0;
				font-size: 26rpx;
				color: #999999;
			}
			.btn {
				width: 530rpx;
				height: 88rpx;
				background-color: #506dff;
				color: #fff;
				text-align: center;
				line-height: 88rpx;
				border-radius: 44rpx;
				font-size: 36rpx;
				margin-top: 122rpx;
			}
		}
	}
</style>