<template>
	<view class="container">
		<view class="page" v-if="formModel">
			<view class="housing">
				<view class="audit">
					<view class="uni-icons">
						<uni-icons v-if="statusType === 1" type="more-filled" size="25" color="#F6C864"></uni-icons>
						<uni-icons v-if="statusType === 2" type="closeempty" size="25" color="#F42E2E"></uni-icons>
						<uni-icons v-if="statusType === 3" type="checkmarkempty" size="25" color="#1BDA1B"></uni-icons>
					</view>
					<view class="name">{{statusName}}</view>
				</view>
				<view class="steps-landscape">
					<view class="title">维修流程</view>
					<uni-steps :options="stepList" active-color="#6A59F2" :active="stepActive" direction="column" />
				</view>
				<view class="information">
					<view class="title">报修信息</view>
					<view class="content">
						<view class="item">
							<view class="name">报修地址</view>
							<view class="detail">{{formModel.repairAddress}}</view>
						</view>
						<view class="item">
							<view class="name">维修物品</view>
							<view class="detail">{{formModel.repairArticle}}</view>
						</view>
						<view class="item">
							<view class="name">标题</view>
							<view class="detail">{{formModel.repairTitle}}</view>
						</view>
						<view class="item">
							<view class="name">手机号码</view>
							<view class="detail">{{formModel.ownerPhone}}</view>
						</view>
						<view class="item">
							<view class="name">预约上门时间</view>
							<view class="detail">{{formModel.reservationTime}}</view>
						</view>
						<view class="item text">
							<view class="name">问题描述</view>
							<view class="detail">{{formModel.repairDesc}}</view>
						</view>
						<view class="item text" v-if="imageList.length">
							<view class="name">附件</view>
							<view class="image">
								<image @click="onPreviewImage(url, imageList)" v-for="(url, index) in imageList" :key="index" :src="url" class="icon"></image>
							</view>
						</view>
						<view class="item text" v-if="formModel.repairStatus === 'finishAuditPass' && maintenancePhotos.length">
							<view class="name">维修照片</view>
							<view class="image">
								<image @click="onPreviewImage(url, maintenancePhotos)" v-for="(url, index) in maintenancePhotos" :key="index" :src="url" class="icon"></image>
							</view>
						</view>
						<view class="item text" v-if="formModel.evaluateCount">
							<view class="name">评价</view>
							<view class="detail">
								<view>
									<uni-rate :readonly="true" v-model="formModel.evaluateCount" :size="20" />
								</view>
								<view>{{formModel.repairEvaluate}}</view>
							</view>
						</view>
					</view>
				</view>
				<template v-if="formModel.repairStatus === 'initiateRefuse'">
					<view class="form-submit" @click="toEditor()">修改</view>
					<view class="form-delete" @click="deleteDialog">删除</view>
				</template>
				<template v-if="formModel.repairStatus === 'finishAuditPass' && !formModel.evaluateCount">
					<view class="form-submit" @click="toEvaluate()">评价</view>
				</template>
			</view>
		</view>
		<uni-popup ref="popup" type="dialog">
			<uni-popup-dialog type="error" cancelText="关闭" confirmText="同意" title="删除信息" content="确定要删除此信息吗？!"
				@confirm="dialogConfirm" @close="dialogClose"></uni-popup-dialog>
		</uni-popup>
		<no-data :show="formModel === null" text="此报修已被删除"></no-data>
	</view>
</template>

<script>
	import {
		getCommunityRepairDetailApi,
		deleteCommunityRepairApi
	} from '@/config/api.js'
	import serverConfig from "@/config/server_config.js";
	import noData from '@/components/no-data/no-data'
	export default {
		components: {noData},
		data() {
			return {
				statusType: 0,
				statusName: '',
				formModel: {},
				imageList: [],
				stepActive: 1,
				stepList: [],
				maintenancePhotos: []
			}
		},
		watch: {
			formModel: {
				handler(newV) {
					if(!newV) return
					let status = newV.repairStatus
					if(['initiateWaitAudit', 'waitDispatch', 'waitReceive'].includes(status)) {
						this.statusType = 1
						this.stepActive = 1
						this.statusName = '您的维修提交成功，请耐心等待'
						this.stepList.push({
							title: '提交维修申请',
							desc: '提交成功 ' + newV.reservationTime
						},{
							title: '待维修',
							desc: '派单中'
						})
					}
					if(['initiateRefuse'].includes(status)) {
						this.statusType = 2
						this.stepActive = 1
						this.statusName = '您的维修被驳回，请重新提交'
						this.stepList.push({
							title: '提交维修申请',
							desc: '提交成功 ' + newV.reservationTime
						},{
							title: '审核驳回',
							desc: newV.auditOpinion + ' ' + newV.updateTime
						})
					}
					if(['finishRefuse', 'inRepair', 'finishWaitAudit'].includes(status)) {
						this.statusType = 1
						this.stepActive = 2
						this.statusName = '维修员正在维修中，请耐心等待'
						this.stepList.push({
							title: '提交维修申请',
							desc: '提交成功 ' + newV.reservationTime
						},{
							title: '派单中',
							desc: '派单成功 ' + newV.dispatchTime
						},{
							title: '维修中',
							desc: '维修员' + newV.maintenanceName + '已接单 ' + newV.updateTime
						})
					}
					if(status === 'finishAuditPass') {
						this.statusType = 3
						this.stepActive = 3
						this.statusName = '您的维修已完成'
						this.stepList.push({
							title: '提交维修申请',
							desc: '提交成功 ' + newV.reservationTime
						},{
							title: '派单中',
							desc: '派单成功 ' + newV.dispatchTime
						},{
							title: '维修中',
							desc: '维修员' + newV.maintenanceName + '已接单'
						},{
							title: '已完成',
							desc: '维修员完成维修 ' + newV.updateTime
						})
					}
				},
				immediate: true,
				deep: true
			}
		},
		methods: {
			async getDetail(id) {
				try {
					let {result} = await getCommunityRepairDetailApi({id})
					this.formModel = {...result}
					let maintenanceImg = result.repairPhoto ? result.repairPhoto.split(',') : []
					this.imageList = maintenanceImg.map(item=> `${serverConfig.imageURL}${item}`)
					if(this.formModel.repairStatus === 'finishAuditPass') {
						let maintenancePhotos = result.maintenancePhoto ? result.maintenancePhoto.split(',') : []
						this.maintenancePhotos = maintenancePhotos.map(item=> `${serverConfig.imageURL}${item}`)
					}
				} catch(err) {
					this.formModel = null
				}
			},
			deleteDialog() {
				this.$refs.popup.open()
			},
			async dialogConfirm() {
				await deleteCommunityRepairApi({id: this.formModel.id})
				setTimeout(()=> {
					uni.showToast({
						title: '删除成功！',
						icon: 'none'
					});
					// 页面重载
					// const pages = getCurrentPages()
					// 声明一个pages使用getCurrentPages方法
					// const curPage = pages[pages.length - 1]
					// 声明一个当前页面
					// curPage.onLoad(curPage.options) // 传入参数
					setTimeout(()=> {
						uni.navigateBack();
					}, 1500)
					uni.$emit('change-maintenance');
				}, 30)
			},
			dialogClose() {
				this.$refs.popup.close()
			},
			toEditor() {
				getApp().globalData.maintenanceInfo = {
					...this.formModel,
					// repairPhoto: this.imageList.map(url=>{
					// 	let chunk = url.split('.')
					// 	return {
					// 		url: serverConfig.baseURL + '/sys/common/static/' + url,
					// 		name: chunk[0],
					// 		extname: chunk[1],
					// 		initUrl: url
					// 	}
					// })
				}
				uni.navigateTo({
					url: '/pages/maintenanceAdd/index?type=edit',
				});
			},
			toEvaluate() {
				uni.navigateTo({
					url: '/pages/maintenanceDetails/index?id=' + this.formModel.id
				});
			},
			onPreviewImage(currentUrl, list) {
				uni.previewImage({
					current: currentUrl, // 图片的地址url
					urls: list // 预览的地址url
				})
			}
		},
		onLoad(option) {
			if(option.id) {
				this.getDetail(option.id)
			} else {
				let result = getApp().globalData.maintenanceInfo
				this.formModel = {...result}
				let maintenanceImg = result.repairPhoto ? result.repairPhoto.split(',') : []
				this.imageList = maintenanceImg.map(item=> `${serverConfig.imageURL}${item}`)
			}
		}
	}
</script>
<style>
	page {
		background-color: #F8F6F9;
	}
</style>
<style lang="scss" scoped>
	.page {
		padding: 30rpx;
	}

	.housing {
		.audit {
			display: flex;
			flex-wrap: wrap;
			justify-content: center;
			background-color: #ffffff;
			border-radius: 16rpx;
			padding: 30rpx;
			margin-bottom: 30rpx;

			.uni-icons {
				background-color: #F8F6F9;
				text-align: center;
				width: 100rpx;
				height: 100rpx;
				border-radius: 100rpx;
				border: 2px dashed #6A59F2;
				line-height: 100rpx;
			}

			.name {
				margin-top: 20rpx;
				font-size: 28rpx;
				color: #373737;
				width: 100%;
				text-align: center;
			}

		}

		.steps-landscape {
			background-color: #ffffff;
			border-radius: 16rpx;
			padding: 30rpx;
			margin-bottom: 30rpx;

			.title {
				height: 40rpx;
				line-height: 40rpx;
				padding-left: 20rpx;
				border-left: 3px solid #6852F0;
				font-size: 32rpx;
				color: #373737;
				margin-bottom: 20rpx;
			}
			/deep/ .uni-steps__column-desc {
				word-break: break-all;
			}
		}
	}

	.information {
		background-color: #ffffff;
		border-radius: 16rpx;
		padding: 30rpx;
		margin-bottom: 30rpx;

		.title {
			height: 40rpx;
			line-height: 40rpx;
			padding-left: 20rpx;
			border-left: 3px solid #6852F0;
			font-size: 32rpx;
			color: #373737;
		}

		.content {
			margin-top: 20rpx;

			.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;
				}
			}

			.text {
				height: auto;

				.detail {
					line-height: 52rpx;
				}

				.image {
					display: flex;

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

	}

	.form-submit {
		height: 80rpx;
		line-height: 80rpx;
		background-color: #6A59F2;
		border-radius: 80rpx;
		width: 100%;
		text-align: center;
		font-size: 28rpx;
		color: #FFFFFF;
		margin-bottom: 30rpx;
	}

	.form-delete {
		height: 80rpx;
		line-height: 80rpx;
		background-color: #ffffff;
		border: 1px solid #6A59F2;
		border-radius: 80rpx;
		width: 100%;
		text-align: center;
		font-size: 28rpx;
		color: #6A59F2;
	}
</style>