<template>
	<view class="container-page">
		<view class="information">
			<view class="form-item-text" v-if="repairDetail.repairStatus === 'inRepair' || repairDetail.repairStatus === 'finishRefuse'">
				<view class="title">问题图片(最多传三张)</view>
				<view class="upload">
					<uni-file-picker file-mediatype="image" limit="3" @select="onSelectImage" @delete="onDeleteImage"></uni-file-picker>
				</view>
			</view>
			<view class="content">
				<view class="item">
					<view class="name">报修主题</view>
					<view class="detail">{{repairDetail.repairTitle || ''}}</view>
				</view>
				<view class="item">
					<view class="name">报修位置</view>
					<view class="detail">{{repairDetail.repairAddress || ''}}</view>
				</view>
				<view class="item">
					<view class="name">报修物品</view>
					<view class="detail">{{repairDetail.repairArticle || ''}}</view>
				</view>
				<view class="item">
					<view class="name">报修人</view>
					<view class="detail">{{repairDetail.ownerName || ''}}</view>
				</view>
				<view class="item text">
					<view class="name">联系方式</view>
					<view class="detail">{{repairDetail.ownerPhone || ''}}</view>
				</view>
				<view class="item text">
					<view class="name">预约上门时间</view>
					<view class="detail">{{repairDetail.reservationTime || ''}}</view>
				</view>
				<view class="item">
					<view class="name">状态</view>
					<view class="detail" :class="repairDetail.color"><view class="tag">{{repairDetail.statusName}}</view></view>
				</view>
				<view class="item" v-if="repairDetail.repairStatus === 'finishRefuse'">
					<view class="name">驳回理由</view>
					<view class="detail">{{repairDetail.auditOpinion || '无'}}</view>
				</view>
				<view class="item text">
					<view class="name">问题描述</view>
					<view class="detail">{{repairDetail.repairDesc || '无'}}</view>
				</view>
				<view class="item text" v-if="repairDetail.complaintImg && repairDetail.complaintImg.length">
					<view class="name">附件</view>
					<view class="image">
						<image @click="onPreviewImage(item)" v-for="(item,index) in repairDetail.complaintImg" :key="index" :src="item" class="icon"></image>
					</view>
				</view>
				<view class="item text" v-if="repairDetail.repairStatus === 'finishWaitAudit' || repairDetail.repairStatus === 'finishRefuse' || repairDetail.repairStatus === 'finishAuditPass'">
					<view class="name">维修照片</view>
					<view class="image">
						<image @click="onPreviewPhotos(item)" v-for="(item,index) in repairDetail.maintenancePhotos" :key="index" :src="item" class="icon"></image>
					</view>
				</view>
			</view>
		</view>

		<view class="button-box" v-if="repairDetail.repairStatus === 'waitReceive'">
			<view class="btn receiving" @click="onSubmit">转单</view>
			<view class="btn ransfer" @click="onSubmit">接单</view>
		</view>
		<view class="button-box" v-if="repairDetail.repairStatus === 'inRepair' || repairDetail.repairStatus === 'finishRefuse'">
			<view class="btn receiving" @click="onSubmit">完成</view>
		</view>
	</view>
</template>

<script>
	import {
		uploadFile,
		getCommunityRepairDetailApi,
		editCommunityComplaintApi
	} from '@/config/api.js'
	import serverConfig from "@/config/server_config.js";
	const statusList = {
		initiateWaitAudit: { name: '待维修', color: 'green' },	//	待审核
		waitDispatch: { name: '待维修', color: 'green' },		// 带派单
		waitReceive: { name: '待维修', color: 'green' },		// 待接单
		initiateRefuse: { name: '已驳回', color: 'red' },		// 提单驳回
		finishRefuse: { name: '回单驳回', color: 'red' },			// 回单驳回
		inRepair: { name: '维修中', color: 'blue' },			// 维修中
		finishWaitAudit: { name: '回单待审核', color: 'blue' },		// 回单待审核
		finishAuditPass: { name: '已完成', color: 'gray' }		// 完成
	}
	export default {
		data() {
			return {
				repairDetail: {},
				repairPhoto: []
			}
		},
		methods: {
			async getDetail(id) {
				let {result} = await getCommunityRepairDetailApi({id})
				let complaintImgList = result.repairPhoto ? result.repairPhoto.split(',') : []
				let complaintImg = complaintImgList.map(item=>`${serverConfig.imageURL}${item}`)

				
				let maintenanceList = result.maintenancePhoto ? result.maintenancePhoto.split(',') : []
				let maintenancePhotos = maintenanceList.map(item=>`${serverConfig.imageURL}${item}`)
				this.repairDetail = {
					...result,
					statusName: statusList[result.repairStatus].name,
					color: statusList[result.repairStatus].color,
					complaintImg,
					maintenancePhotos
				}
			},
			uploadImage(path) {
				return new Promise((resolve, reject)=> {
					uploadFile({
						filePath: path,
						name: 'file',
						formData: {                                  //其他需要携带的参数
							'biz': 'scott/pic'
						}
					}).then(({message})=> {
						let res = {
							url: message,
							tempFilePath: path
						}
						resolve(res)
					}).catch(err=> {
						reject(err)
					})
				})
			},
			// // 选择上传触发函数
			onSelectImage(e) {
				let serveList = []
				e.tempFilePaths.forEach(url=> {
					serveList.push(this.uploadImage(url))
				})
				Promise.all(serveList).then(data=> {
					this.repairPhoto = this.repairPhoto.concat(data)
				}).catch(err=> {
					setTimeout(()=> {
						uni.showToast({
							title: '上传失败，请重试！',
							icon: 'none'
						});
					}, 30)
				})
			},
			onDeleteImage(err) {
				const num = this.repairPhoto.findIndex(v => v.tempFilePath === err.tempFilePath);
				this.repairPhoto.splice(num, 1);
			},
			async onSubmit() {
				// if(!this.repairPhoto.length) {
				// 	uni.showToast({
				// 		title: '请上传维修成功照片！',
				// 		icon: 'none'
				// 	});
				// 	return
				// }
				let maintenancePhoto = this.repairPhoto.map(item=>item.url).join(',')
				await editCommunityComplaintApi({
					maintenancePhoto,
					id: this.repairDetail.id,
					repairStatus: 'finishWaitAudit'
				})
				setTimeout(()=> {
					uni.showToast({
						title: '提交成功，正在审核！',
						icon: 'none'
					});
					setTimeout(()=> {
						uni.switchTab({
							url: '/pages/home/index'
						});
					}, 1500)
				}, 30)
			},
			onPreviewImage(currentUrl) {
				uni.previewImage({
					current: currentUrl, // 图片的地址url
					urls: this.repairDetail.complaintImg // 预览的地址url
				})
			},
			onPreviewPhotos(currentUrl) {
				uni.previewImage({
					current: currentUrl, // 图片的地址url
					urls: this.repairDetail.maintenancePhotos // 预览的地址url
				})
			}
		},
		onLoad(options) {
			this.getDetail(options.id)
		}
	}
</script>
<style lang="scss" scoped>
	.container-page {
		box-sizing: border-box;
		min-height: 100vh;
		background-color: #F8F6F9;
		padding: 30rpx;
	}

	.information {
		background-color: #ffffff;
		border-radius: 16rpx;
		padding: 0 30rpx;
		box-shadow: 0rpx 0rpx 25rpx 0rpx rgba(81,89,234,0.06);

		.form-item-text {
			padding-top: 30rpx;
			.title {
				font-size: 28rpx;
				font-weight: 400;
				color: #373737;
			}
			.upload {
				padding: 30rpx 0;
				border-bottom: 1rpx solid #F0F0F2;
			}
		}

		.content {
			padding: 30rpx 0;
			.item {
				display: flex;
				&:not(:last-child) {
					margin-bottom: 26rpx;
				}

				.name {
					width: 35%;
					font-size: 28rpx;
					color: #CECED3;
				}

				.detail {
					width: 65%;
					font-size: 28rpx;
					color: #373737;
					word-break: break-all;
					&.green .tag {
						color: #F1BD4D;
						border: 1px solid #F1BD4D;
						background-color: #FFFCF6;
					}
					&.red .tag {
						color: #F42E2E;
						border: 1px solid #F42E2E;
						background-color: #FFF5F5;
					}
					&.blue .tag {
						color: #6A59F2;
						border: 1px solid #6A59F2;
						background-color: #F6F5FF;
					}
					&.gray .tag {
						color: #B6B6BA;
						border: 1px solid #B6B6BA;
						background-color: #F8F8F8;
					}
					.tag {
						height: 40rpx;
						line-height: 40rpx;
						font-size: 22rpx;
						padding: 2rpx 20rpx;
						border-radius: 30rpx;
						display: inline-block;
					}
				}
			}

			.text {
				height: auto;

				.detail {
					line-height: 52rpx;
				}

				.image {
					display: flex;

					.icon {
						width: 120rpx;
						height: 120rpx;
						border-radius: 10rpx;
						margin-right: 10rpx;
					}
				}
			}
		}

	}

	.button-box {
		margin-top: 60rpx;
		.btn {
			height: 80rpx;
			border-radius: 40rpx;
			text-align: center;
			line-height: 80rpx;
			font-size: 28rpx;
			&.receiving {
				background-color: #6A59F2;
				color: #FFFFFF;
			}
			&.ransfer {
				background-color: #FFFFFF;
				border: 1rpx solid #B5B8ED;
				box-shadow: 0rpx 0rpx 25rpx 0rpx rgba(81,89,234,0.06);
				color: #6A59F2;
				margin-top: 30rpx;
			}
		}
	}

</style>