<template>
	<view class="container">
		<view class="form">
			<u--form :model="form" ref="uForm" label-width="200rpx">
				<u-gap height="10" bgColor="#f3f3f3"></u-gap>
				<view class="block">
					<u-form-item label="房产所在城市" prop="cityByHouse" @click="onArea">
						<u-input v-model="form.cityByHouse" :disabled="true" :customStyle="{backgroundColor: '#ffffff'}" placeholder="选择房产所在城市" class="u-input" maxlength="20" />
						<!-- <view class="">
							<view style="border: solid 1px #eeeeee;height: 60rpx;border-radius: 10rpx;line-height: 60rpx;padding-left: 10rpx;margin:0 10rpx;">
								{{ form.cityByHouse }}
							</view>
						</view> -->
					</u-form-item>
				</view>
				<u-gap height="10" bgColor="#f3f3f3"></u-gap>
				<view class="block">
					<view class="upload">
						<u-upload :fileList="fileList1" :maxSize="3145728" width="250" height="150"
							@afterRead="afterRead" @delete="deletePic" :disabled="isEdit" name="1" multiple :maxCount="1">
							<view class="id-card-upload">
								<image class="photograph" src="../../../static/icon/photograph.png" mode="widthFix">
								</image>
								<text>上传证件照片</text>
							</view>
						</u-upload>
					</view>
				</view>
				<u-gap height="10" bgColor="#f3f3f3"></u-gap>
				<view class="block">
					<view class="desc-title">请上传房产证内页或房购房合同内页，需露出署名：</view>
					<view class="desc"><text>您上传的照片资料仅作为认证使用，并将加密处理</text></view>
					<view class="photo">
						<image src="https://file.wsdns.cn/20230619/432114c901dc499197c63e247fafdae6.png" style="width: 400rpx;" mode="widthFix"></image>
					</view>
				</view>
			</u--form>
		</view>
		<view class="btn-wrapper">
			<u-button :text="submitText" color="linear-gradient(to bottom, #010002, #681752)" :disabled="isEdit"
				shape="circle" @click="handleSubmit()"></u-button>
		</view>
		<!-- 地址选择器 -->
		<liu-customize-sel ref="area" @change="chooseSuccess"></liu-customize-sel>
	</view>
</template>

<script>
	import * as UploadApi from '@/api/upload'
	import * as CertificateApi from '@/api/love-certificate.js'
	import Vue from 'vue'
	const userId = uni.getStorageSync('userId')

	export default {
		data() {
			return {
				form: {
					userId,
					certificateType: 'house',
					cityByHouse: ''
				},
				fileList1: [],
				disabled: false,
				// 临时图片 (用于上传)
				tempFile: null,
				isEdit: false,
				submitText: '提交认证信息'
			}
		},
		onLoad() {
			this.getData()
		},
		methods: {
			getData() {
				const app = this
				const {
					form
				} = this
				CertificateApi.listCertificate({
					userId,
					certificateType: 'house',
				}).then(res => {
					if (res.data && res.data.length > 0) {
						app.form = res.data[0]
						app.fileList1 = JSON.parse(res.data[0].files)

						if(app.form.status == 10){
							app.submitText = '认证中'
							app.isEdit = true
						}
						if(app.form.status == 20){
							app.submitText = '提交认证信息'
							app.isEdit = false
						}
						if(app.form.status == 30){
							app.submitText = '已通过'
							app.isEdit = true
						}
					}
				})
			},
			onArea() {
				if(this.isEdit == true){
					return false;
				}
				this.$refs.area.open()
			},
			//地址选择成功
			chooseSuccess(e) {
				const app = this
				const data = e.value
				app.form.cityByHouse = data[1].label
				// Vue.set(this.form, 'cityByHouse', data[1].label)
			},
			// 新增图片
			async afterRead(event) {
				console.log("event: ", event);
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`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[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result.url,
						thumb: result.thumbUrl
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					UploadApi.uploadFile({
						filePath: url,
						fileType: 'image',
						name: 'file',
						header: {
							Authorization: uni.getStorageSync('AccessToken')
						},
					}).then(result => {
						setTimeout(() => {
							resolve(result.data)
						}, 1000)
					}).catch(err => {
						console.log("err: ", err);
					})
				})
			},
			// 删除图片
			deletePic(event) {
				if(this.isEdit == true){
					return false;
				}
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 确认修改
			async handleSubmit() {
				const app = this
				if(app.fileList1.findIndex(item => item.status != 'success') >= 0){
					return
				}
				if (app.disabled === true) return
				console.log("app.tempFile: ", app.tempFile);
				app.form.files = JSON.stringify(app.fileList1)
				const saveCertificate = app.isEdit ? CertificateApi.updateCertificate : CertificateApi.addCertificate
				saveCertificate(app.form).then(result => {
					app.$success('提交成功')
					this.getData()
				}).catch(err => {
					app.$error(err.message)
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #f3f3f3;
	}

	.container {

		.form{
			background-color: #ffffff;
		}

		.block {
			margin: auto;
			width: 700rpx;
			font-size: 28rpx;
			.u-input{
				background-color: #ffffff !important;
			}

			.upload {
				width: 360rpx;
				margin: 0 auto;
				padding: 20rpx 0;
				justify-content: center;

				.id-card-upload {
					display: flex;
					flex-direction: column;
					justify-content: center;
					align-items: center;
					text-align: center;
					margin: 10rpx auto;
					color: #cccccc;
					border: 5rpx dashed #d9d9d9;
					border-radius: 20rpx;
					width: 360rpx;
					height: 220rpx;

					.photograph {
						width: 60rpx;
					}
				}
			}

			.desc-title {
				line-height: 3rem;
			}
			.desc{
				color: #cccccc;
				text{
					color: #7f006f;
				}
			}

		}
	}

	.btn-wrapper {
		background: none;
		width: 360rpx;
		margin: 50rpx auto;
		// display: flex;
		// align-items: center;
		padding: 0 20rpx;
	}
</style>
