<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="carBrand" borderBottom>
						<u-input v-model="form.carBrand" maxlength="20" :disabled="isEdit" placeholder="车辆品牌" />
					</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/94d1dba25b104bc89373fc3503049aba.png"
							style="width: 700rpx;" 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'

	const userId = uni.getStorageSync('userId')

	export default {
		data() {
			return {
				form: {
					certificateType: 'car',
					userId
				},
				fileList1: [],
				disabled: false,
				// 临时图片 (用于上传)
				tempFile: null,
				isEdit: false,
				submitText: '提交认证信息'
			}
		},
		onLoad() {
			this.getData()
		},
		methods: {
			getData() {
				const app = this
				const {
					form
				} = this
				CertificateApi.listCertificate({
					userId,
					certificateType: 'car',
				}).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() {
				this.$refs.area.open()
			},
			//地址选择成功
			chooseSuccess(e) {
				const data = e.value
				this.form.city = 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
				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>
