<template>
	<view class="page">
		<u-alert-tips
			v-if="cObj.report_subscr_state === '3'"
			type="warning"
			title="当前状态:"
			description="当前资料审核中，修改提交后将重新进入审核流程"></u-alert-tips>
			<u-alert-tips
				v-if="cObj.report_subscr_state === '2'"
				type="error"
				title="当前状态:"
				description="申请被拒绝，请联系客服咨询"></u-alert-tips>
			<u-alert-tips
				v-if="cObj.report_subscr_state === '1'"
				type="success"
				title="当前状态:"
				description="当前资料已审核通过，修改提交将重新进入审核流程"></u-alert-tips>
		<view class="form">
			<u-form :model="form" ref="uForm">
				<!-- <u-form-item label-width="150" label="姓名" prop="real_name"><u-input placeholder="请输入您的真实姓名" v-model="form.real_name" type="text"></u-input></u-form-item>
				<u-form-item label-width="150" label="身份证号" prop="id_number">
					<u-input placeholder="请输入您的身份证号" v-model="form.id_number" type="text"></u-input>
				</u-form-item> -->
				<u-form-item label-width="150" label="到访备注信息" prop="visit_des" label-position="top">
					<u-input placeholder="请输入到访备注信息" v-model="form.visit_des" type="textarea"></u-input>
				</u-form-item>
				<u-form-item label-position="top" label-width="150" label="第三方水印" prop="img1">
					<view class="id_card" @click="uploadOneImage">
						<u-icon v-if="form.img1 == null" name="plus" size="32" color="#606266"></u-icon>
						<text v-if="form.img1 == null">请先上传第三方水印照片</text>
						<image v-if="form.img1 != null" :src="idCardFront" mode="aspectFill"></image>
					</view>
				</u-form-item>
				<u-form-item label-position="top" label-width="150" label="带看单" prop="img2">
					<view class="id_card" @click="uploadTwoImage">
						<u-icon v-if="form.img2 == null" name="plus" size="32" color="#606266"></u-icon>
						<text v-if="form.img2 == null">请先上传带看单照片</text>
						<image v-if="form.img2 != null" :src="idCardRevers" mode="aspectFill"></image>
					</view>
				</u-form-item>
				<u-form-item label-position="top" label-width="150" :label="defineName" prop="img2" v-for="(defineName, dIndex) in ercArr" :key="dIndex">
					<view class="id_card" @click="uploadDefineImage(dIndex)">
						<u-icon v-if="!form['key'+dIndex]" name="plus" size="32" color="#606266"></u-icon>
						<text v-if="!form['key'+dIndex]">请先上传{{defineName}}</text>
						<image v-if="form['key'+dIndex]" :src="form['key'+dIndex]" mode="aspectFill"></image>
					</view>
				</u-form-item>
			</u-form>
			<u-gap height="60"></u-gap>
			<u-button type="primary" :disabled="submitButtonDisabled" @click="submitModalShow = true">提交</u-button>
			<u-gap></u-gap>
		</view>

		<!-- modal -->
		<u-modal v-model="submitModalShow" content="请务必仔细确认各项信息是否正确" :show-cancel-button="true" @confirm="submitHandle()"></u-modal>
		
		<!-- mask -->
		<u-mask :show="maskShow" @click="maskShow = false">
			<view class="warp">
				<!-- <u-image src="https://zdcdn.2bwin.cn/uploads/20220513/90abe6fe85d6c17f6bdc069955fbb878.png" mode="widthFix" width="400rpx" border-radius="20rpx"></u-image> -->
				<u-gap></u-gap>
				<u-button size="medium" type="primary" @click="maskShow = false">确定</u-button>
			</view>
		</u-mask>
		<!-- utoast -->
		<u-toast ref="uToast" />
	</view>
</template>
<script>
var that;
export default {
	data() {
		return {
			maskShow: false,
			idCardFront: '',
			idCardRevers: '',
			form: {
				visit_des: '',
				img1: null,
				img2: null,
			},
			curId: '',
			cObj: {},
			ercArr: [],
			submitButtonDisabled: false,
			rules: {
				visit_des: [
					{
						required: true,
						message: '备注不得为空',
						trigger: ['change', 'blur']
					},
				],
				img1: [
					{
						required: true,
						message: '第三方水印照片不得为空',
						trigger: ['change', 'blur']
					}
				],
				img2: [
					{
						required: true,
						message: '带看单照片不得为空',
						trigger: ['change', 'blur']
					}
				],
			},
			submitModalShow: false,
		};
	},
	onLoad(params) {
		this.curId = params.id
		this.getData()
	},
	// 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
	onReady() {
		this.$refs.uForm.setRules(this.rules);
	},
	methods: {
		getData () {
			uni.api.cust.apireportvisitdetail({report_id: this.curId}).then(res => {
				const cObj = res || {}
				let ercArr = cObj.estate_report_conf ? cObj.estate_report_conf.split(',') : []
				this.ercArr = [...ercArr]
				const ercMoreVal = cObj.more ? JSON.parse(cObj.more) : []
				let defineForm = {}
				ercArr.forEach((name, index) => {
					defineForm['key'+index] = ercMoreVal[index] || 'https://img.fangpiaovip.com/20221116045017-4379.png'
				})
				if (cObj.id) {
					this.idCardFront = cObj.img1
					this.idCardRevers = cObj.img2
					let f = {
						visit_des: cObj.visit_des,
						img1: cObj.img1,
						img2: cObj.img2,
					}
					this.form = {...defineForm, ...f}
					this.cObj = {...cObj}
				} else {
					this.form = {...defineForm}
				}
			})
		},
		uploadImgHandle (bc) {
			uni.chooseImage({
				count: 1,
				sizeType: ['compressed'],
				success: function(res) {
					const filePath = res.tempFilePaths[0];
					let token = uni.getStorageSync('MD_token') || ''
					uni.uploadFile({
						url: uni.baseUrl + 'api/upload/cloud',
						filePath,
						name: 'upload',
						formData: {
							'token': token
						},
						success: (f) => {
							const cData = JSON.parse(f.data)
							if (cData.errno === 0) {
								if (bc && typeof(bc) === 'function') bc(cData.data)
							} else {
								uni.$msg(cData.errmsg || `未知错误-${cData.errno}`)
							}
						}
					})
				}
			})
		},
		// 三方水印
		uploadOneImage() {
			this.uploadImgHandle((d) => {
				this.form.img1 = d.url
				this.idCardFront = d.url
			})
		},
		// 带看单
		uploadTwoImage() {
			this.uploadImgHandle((d) => {
				this.form.img2 = d.url
				this.idCardRevers = d.url
			})
		},
		// 楼盘自定义上传
		uploadDefineImage (i) {
			this.uploadImgHandle((d) => {
				this.form['key'+i] = d.url
			})
		},
		submitHandle() {
			const that = this
			this.$refs.uForm.validate(valid => {
				if (valid) {
					let params = {
						visit_des: that.form.visit_des,
						img1: that.form.img1,
						img2: that.form.img2,
					}
					let mustStr = ''
					let moreArr = []
					that.ercArr.forEach((eName, eIndex) => {
						if (that.form['key'+eIndex]) {
							moreArr.push(that.form['key'+eIndex])
						} else {
							mustStr = eName
						}
					})
					if (mustStr) {
						uni.$msg('请上传' + mustStr)
						return
					}
					if (moreArr.length > 0) params.more = JSON.stringify(moreArr)
					params.report_id = that.curId
					uni.api.cust.apireportvisitchange(params).then(res => {
						uni.$msgConfirm('编辑成功', () => {
							uni.reLaunch({
								url: `/pages/agent/recommend/detail?id=${that.curId}`
							})
						}, () => {
							uni.reLaunch({
								url: `/pages/agent/recommend/detail?id=${that.curId}`
							})
						})
					})
				}
			});
		},


		// 以下是工具函数
		// 格式化日期的月份或天数的显示（小于10，在前面增加0）
		getFormatDate(value) {
			if (value == undefined || value == '') {
				return '';
			}
			var str = value;
			if (parseInt(value) < 10) {
				str = '0' + value;
			}
			return str;
		}
	}
};
</script>
<style lang="scss">
.page {
	background-color: #ffffff;
}

.form {
	border-radius: 10rpx;
	padding: 0 40rpx;
}

.popup-body {
	.tips-title {
		font-size: $u-p;
		margin-bottom: 20rpx;
	}

	.tips-content {
		font-size: $u-p2;
		color: $u-tips-color;
		margin-bottom: 60rpx;
	}
}

.id_card {
	color: #606266;
	width: 100%;
	height: 350rpx;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	background-color: #f4f5f6;
	font-size: $u-p2;
	border-radius: 10rpx;

	image {
		border-radius: 10rpx;
	}
}

.footer {
	position: relative;
	text-align: center;
	font-size: $u-p2;
	left: 0;
	bottom: 20rpx;
	.agreement {
		color: $u-theme-color;
	}
}

.slot-content {
	font-size: 28rpx;
	color: $u-content-color;
	padding: 20rpx;
}

.warp {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	height: 100%;
}

.rect {
	width: 400rpx;
	height: 400rpx;
	background-color: #fff;
}
</style>
