<template>
	<view class="container">
		<view class="title">工单信息</view>
		<view class="box">
			<view class="box_key">工单编号：</view>
			<view class="box_value">{{ detailInfo.recordNo }}</view>
		</view>
		<view class="box_time">
			<view class="time_msg">
				报修时间：
				<text>{{ detailInfo.reportTime }}</text>
			</view>

			<view class="time_type" v-if="detailInfo.state === '待接单'">待接单</view>
			<view class="time_type color_type" v-if="detailInfo.state === '维修中'">维修中</view>
			<view class="time_type" v-if="detailInfo.state === '待确认'">待确认</view>
			<view class="time_type color_type2" v-if="detailInfo.state === '已完成'">已完成</view>
			<view class="time_type" v-if="detailInfo.state === '已取消'">已取消</view>
			<view class="time_type color_type3" v-if="detailInfo.state === '已关单'">已关单</view>
			<view class="time_type color_type" v-if="detailInfo.state === '转单审核'">转单审核</view>
			<view class="time_type color_type" v-if="detailInfo.state === '协作审核'">协作审核</view>
		</view>

		<view class="box">
			<view class="box_key">报修姓名：</view>
			<view class="box_value">{{ detailInfo.userName }}</view>
		</view>
		<view class="box">
			<view class="box_key">报修电话：</view>
			<view class="box_value phone" @click="handleCallPhone(detailInfo.userPhone)">
				{{ detailInfo.userPhone }}
				<img src="../../static/images/repairsImg/phone.png" />
			</view>
		</view>
		<view class="box">
			<view class="box_key">报修区域：</view>
			<view class="box_value">{{ detailInfo.areaName }}</view>
		</view>
		<view class="box">
			<view class="box_key">详细地址：</view>
			<view class="box_value">{{ detailInfo.address }}</view>
		</view>
		<view class="box">
			<view class="box_key">报修物品：</view>
			<view class="box_value">{{ detailInfo.articleName }}</view>
		</view>
		<view class="box" v-if="detailInfo.description">
			<view class="box_key">故障描述：</view>
			<view class="box_value">{{ detailInfo.description }}</view>
		</view>

		<view class="box" v-if="detailInfo.voice">
			<view class="box_key">报修录音：</view>
			<view class="box_value">
				<view class="recording" @click="handlePlayRecording(detailInfo)">
					<img class="voiceImg" :src="detailInfo.status ? '../../static/images/repairsImg/play_active.png' : '../../static/images/repairsImg/play.png'" />
					{{ detailInfo.voiceLength }}″
				</view>
			</view>
		</view>

		<view class="box_img">
			<view class="img_key">上传图片：</view>
			<img class="img_value" mode="aspectFill" :src="detailInfo.images[0]" @click="handleLookImgs(detailInfo.images)" />
		</view>

		<view class="box top" v-if="detailInfo.maintenancerName">
			<view class="box_key">维修师傅：</view>
			<view class="box_value">{{ detailInfo.maintenancerName }}</view>
		</view>

		<view class="box" v-if="detailInfo.maintenancerPhone">
			<view class="box_key">师傅电话：</view>
			<view class="box_value phone" @click="handleCallPhone(detailInfo.maintenancerPhone)">
				{{ detailInfo.maintenancerPhone }}
				<img src="../../static/images/repairsImg/phone.png" />
			</view>
		</view>

		<view class="box" v-if="detailInfo.price">
			<view class="box_key">维修费用：</view>
			<view class="box_value2 phone" @click="checkFeeDetail(detailInfo)">
				{{ detailInfo.price }}元
				<img src="../../static/images/repairsImg/eye.png" />
			</view>
		</view>

		<view class="title top" v-if="detailInfo.tracks">报修跟踪</view>

		<!-- 步骤条区域 -->
		<uv-steps activeColor="#6FB6B8" direction="column" v-if="detailInfo.tracks" :current="detailInfo.tracks.length">
			<uv-steps-item :customStyle="customStyle" v-for="item in detailInfo.tracks" :key="item.id">
				<template v-slot:title>
					<view class="steps_title" :class="{ active: true }">{{ item.state }}</view>
				</template>
				<template v-slot:desc>
					<view class="steps_desc">{{ item.createTime }}</view>
					<view class="steps_desc" v-if="item.userName">操作人：{{ item.userName }}({{ item.userZzstr }})</view>
					<view class="steps_desc" v-if="item.voice">
						上传录音
						<view class="desc_recording" @click="handlePlayRecording(item)">
							<img class="voiceImg" :src="item.status ? '../../static/images/repairsImg/play_active.png' : '../../static/images/repairsImg/play.png'" />
							{{ item.voiceLength }}″
						</view>
					</view>
					<view class="steps_desc" v-if="item.content">{{ item.content }}</view>
				</template>
			</uv-steps-item>
		</uv-steps>

		<view class="gap"></view>

		<!-- 维修费用弹窗 -->
		<uni-popup :is-mask-click="false" ref="popup_fee">
			<view class="pop_fee">
				<view class="fee_title">
					维修费用
					<text @click="$refs.popup_fee.close()">×</text>
				</view>
				<view class="fee_list">
					<view class="fee_item" v-for="ele in consumables" :key="ele.id">
						<view class="fee_box">
							耗材：
							<text>{{ ele.consumeName }}</text>
						</view>
						<view class="fee_box">
							耗材单价：
							<text>{{ ele.price }}元</text>
						</view>
						<view class="fee_box">
							耗材数量：
							<text>{{ ele.number }}</text>
						</view>
						<view class="fee_box">
							耗材费用：
							<text>{{ ele.totalPrice }}元</text>
						</view>
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
const innerAudioContext = uni.createInnerAudioContext()
export default {
	data() {
		return {
			// 报修详情信息
			detailInfo: {},
			// 步骤条样式
			customStyle: {
				marginBottom: '5px'
			},
			// 订单id
			recordId: '',
			// 维修费用耗材明细列表
			consumables: [],
			// 定时器标识
			timer: null
		}
	},
	onLoad(options) {
		this.recordId = options.id
		this.getData()
	},
	methods: {
		// 获取报修详情信息
		async getData() {
			const res = await this.$myRequest_repairs({
				url: '/repairRecord/details',
				data: {
					recordId: this.recordId
				}
			})
			// console.log(res)
			if (res.code === '200') {
				if (res.data.tracks.length) {
					res.data.tracks.forEach((ele) => {
						return (ele.status = false)
					})
				}
				this.detailInfo = res.data
				this.$set(this.detailInfo, 'status', false)
			}
		},
		// 查看维修费用回调
		checkFeeDetail(item) {
			this.$refs.popup_fee.open('center')
			this.consumables = item.consumables
		},
		// 点击电话号码回调
		handleCallPhone(phone) {
			uni.makePhoneCall({
				phoneNumber: phone
			})
		},
		// 点击图片回调
		handleLookImgs(img) {
			uni.previewImage({
				urls: img
			})
		},
		// 点击录音播放回调
		handlePlayRecording(item) {
			// 先重置播放状态
			if (this.detailInfo.id === item.id) {
				this.detailInfo.tracks.forEach((ele) => {
					ele.status = false
				})
			} else {
				this.detailInfo.status = false
				this.detailInfo.tracks.forEach((ele) => {
					if (ele.id !== item.id) {
						ele.status = false
					}
				})
			}
			// 赋值音频地址
			innerAudioContext.src = item.voice
			if (!item.status) {
				item.status = true
				innerAudioContext.play()

				//播放结束
				innerAudioContext.onEnded(() => {
					item.status = false
				})
			} else {
				item.status = false
				innerAudioContext.stop()
			}
		}
	}
}
</script>

<style lang="scss" scoped>
.container {
	padding: 0 30rpx;
	height: 100vh;
	overflow-y: auto;

	.title {
		height: 90rpx;
		line-height: 90rpx;
		font-size: 32rpx;
		font-weight: bold;
	}

	.top {
		margin-top: 20rpx;
	}

	.box {
		display: flex;
		line-height: 60rpx;
		font-size: 28rpx;

		.box_key {
			color: #808080;
		}

		.box_value {
			flex: 1;
			display: flex;
			align-items: center;

			img {
				margin-left: 10rpx;
				width: 28rpx;
				height: 28rpx;
			}

			.recording {
				display: flex;
				align-items: center;
				width: 130rpx;
				height: 40rpx;
				color: #000;
				border-radius: 100rpx;
				border: 1rpx solid #cccccc;

				.voiceImg {
					margin: 0 12rpx;
					width: 30rpx;
					height: 30rpx;
				}
			}
		}

		.box_value2 {
			display: flex;

			img {
				margin-top: -2rpx;
				margin-left: 14rpx;
				width: 46rpx;
				height: 46rpx;
			}
		}

		.phone {
			color: #6fb6b8;
		}
	}

	.box_time {
		display: flex;
		height: 60rpx;

		.time_msg {
			font-size: 28rpx;
			color: #808080;

			text {
				color: #000000;
			}
		}

		.time_type {
			margin-left: 123rpx;
			margin-top: -5rpx;
			font-size: 32rpx;
			color: #ff5733;
		}

		.color_type {
			color: #1e7dfb;
		}
		.color_type2 {
			color: #6fb6b8;
		}
		.color_type3 {
			color: #cccccc;
		}
	}

	.box_img {
		display: flex;
		align-items: center;
		height: 120rpx;
		color: #808080;
		font-size: 28rpx;

		.img_key {
		}

		.img_value {
			width: 120rpx;
			height: 120rpx;
			border-radius: 14rpx;
		}
	}

	.steps_title {
		color: #969799;
		font-size: 28rpx;
	}

	.active {
		color: #6fb6b8;
	}

	.steps_desc {
		display: flex;
		align-items: center;
		line-height: 45rpx;
		font-size: 24rpx;

		img {
			margin: 10rpx 0;
			width: 80rpx;
			height: 80rpx;
			border-radius: 14rpx;
		}

		.desc_recording {
			display: flex;
			align-items: center;
			margin-left: 10rpx;
			width: 130rpx;
			height: 40rpx;
			color: #000;
			border-radius: 100rpx;
			border: 1rpx solid #cccccc;

			.voiceImg {
				margin: 0 12rpx;
				width: 30rpx;
				height: 30rpx;
			}
		}
	}

	.gap {
		height: 20rpx;
	}

	.pop_fee {
		padding-bottom: 50rpx;
		border-radius: 19rpx;
		background-color: #fff;

		.fee_title {
			display: flex;
			justify-content: space-between;
			align-items: center;
			box-sizing: border-box;
			padding: 0 31rpx 0 42rpx;
			width: 690rpx;
			height: 110rpx;
			font-size: 32rpx;
			font-weight: bold;
			border-radius: 19rpx 19rpx 0 0;
			border-bottom: 1rpx solid #e6e6e6;

			text {
				font-size: 45rpx;
				font-weight: 400;
				color: #808080;
			}
		}

		.fee_list {
			max-height: 50vh;
			overflow-y: auto;

			.fee_item {
				border-bottom: 1rpx solid #e6e6e6;

				.fee_box {
					display: flex;
					align-items: center;
					padding-left: 42rpx;
					height: 80rpx;
					font-size: 28rpx;
					color: #808080;

					text {
						color: #000000;
					}
				}
			}
		}
	}
}
</style>
