<template>
	<view class="container">
		<view class="title" v-if="type === '1'">申请人</view>

		<view class="box" v-if="type === '1'">
			<img src="../../static/images/repairsImg/people.png" />
			<view class="box_info">{{ info.userName }}</view>
		</view>

		<view class="title">协作对象</view>

		<view class="box" @click="handleHelpPeople">
			<img src="../../static/images/repairsImg/peoples.png" />
			<view class="box_info" :class="{ color: !peoples }">{{ peoples ? peoples : '请选择协作对象' }}</view>
			<img v-if="type !== '1'" class="img" src="../../static/images/repairsImg/right.png" />
		</view>

		<view class="title">转单语音说明</view>

		<!-- 录音区域 -->
		<view class="voice">
			<view class="voice_box" v-if="!info.voice" @click="handleRecording">
				<img src="../../static/images/repairsImg/voice.png" />
			</view>
			<view v-if="!info.voice" @click="handleRecording">点击录音</view>

			<view class="item_recording" v-if="info.voice" @click="handlePlayRecording">
				<img :src="recordingImg" />
				{{ info.voiceLength || 0 }}″
			</view>
			<view class="recording_icon" v-if="info.voice && type !== '1'" @click="handleDeleteRecording">×</view>
		</view>

		<!-- 录音弹窗区域 -->
		<uni-popup :safe-area="true" background-color="#fff" ref="popup_recording">
			<view class="popup_recording">
				<recording @getTempFilePath="getTempFilePath" />
			</view>
		</uni-popup>

		<view class="title">备注</view>

		<view class="textarea">
			<textarea placeholder-style="color:#CCCCCC" placeholder="请输入协作说明" :disabled="type === '1'" v-model="info.remark"></textarea>
		</view>

		<view class="btn2" v-if="type === '1'">
			<view class="btn_box type" @click="handleDispose(0)">拒绝</view>
			<view class="btn_box type2" @click="handleDispose(1)">派单</view>
		</view>

		<view class="btn" v-else @click="handleAffirm">确认</view>
	</view>
</template>

<script>
import recording from '../components/recording.vue'
const innerAudioContext = uni.createInnerAudioContext()
export default {
	components: {
		recording
	},
	data() {
		return {
			// 页面类型
			type: null,
			// 录音图片地址
			recordingImg: '../../static/images/repairsImg/recording.jpg',
			// 播放状态
			playStatus: false,
			// 定时器标识
			timer: null,
			// 订单id
			recordId: '',
			info: {
				// 申请人
				userName: '',
				// 协作对象数组
				collaboratorDatas: [],
				// 语音地址
				voice: '',
				// 备注
				remark: '',
				// 录音时长
				voiceLength: 0
			}
		}
	},
	computed: {
		// 协作对象
		peoples() {
			let temList = []
			temList = this.info.collaboratorDatas.map((ele) => ele.userName || ele.name)
			let res = temList.toString()
			return res
		},
		idList() {
			let temList = []
			this.info.collaboratorDatas.forEach((ele) => {
				temList.push(ele.id)
			})
			return temList
		}
	},
	mounted() {
		//在ios下静音时播放没有声音，默认为true，改为false就好了。
		uni.setInnerAudioOption({
			obeyMuteSwitch: false
		})
	},
	onLoad(options) {
		this.recordId = options.id
		if (options.type) {
			this.type = options.type
			this.getHelpData()
		}
		uni.$on('addCheckList', this.addCheckList)
	},
	methods: {
		// 拒绝 派单 按钮回调
		async handleDispose(type) {
			const res = await this.$myRequest_repairs({
				url: '/repairRecord/cooperationOrders',
				method: 'post',
				data: {
					id: this.info.id,
					collaborator: this.idList,
					approverStatu: type
				}
			})
			// console.log(res)
			if (res.code === '200') {
				uni.showToast({
					title: `${type === 0 ? '已拒绝该审核' : '派单成功'}`,
					icon: 'none'
				})
				setTimeout(() => {
					uni.reLaunch({
						url: '/pagesRepairs/box/box'
					})
				}, 1500)
			}
		},
		// 获取审核订单详细数据
		async getHelpData() {
			const res = await this.$myRequest_repairs({
				url: '/repairRecord/cooperationDetail',
				data: {
					recordId: this.recordId
				}
			})
			// console.log(res)
			if (res.code === '200') {
				this.info = res.data
			}
		},
		addCheckList(e) {
			this.info.collaboratorDatas = e.data
		},
		// 确认按钮回调
		handleAffirm() {
			if (!this.peoples) {
				uni.showToast({
					title: '请选择协作对象',
					icon: 'none'
				})
				return
			}
			if (!this.info.voice) {
				uni.showToast({
					title: '请录入转单语音说明',
					icon: 'none'
				})
				return
			}
			uni.showModal({
				title: '提示',
				content: '确认提交协作申请吗？',
				success: async (res) => {
					if (res.confirm) {
						let temList = this.info.collaboratorDatas.map((ele) => ele.id)
						const res = await this.$myRequest_repairs({
							url: '/repairRecord/collaborateApply',
							method: 'post',
							data: {
								recordId: this.recordId,
								userId: uni.getStorageSync('repairsUserInfo').userId,
								voice: this.info.voice,
								voiceLength: this.info.voiceLength,
								collaborator: temList,
								remark: this.info.remark
							}
						})
						// console.log(res)
						if (res.code === '200') {
							uni.showToast({
								title: '提交协作申请成功，请等待管理员审核',
								icon: 'none',
								duration: 3000
							})
							setTimeout(() => {
								uni.reLaunch({
									url: '/pagesRepairs/box/box'
								})
							}, 3000)
						}
					}
				}
			})
		},
		handleHelpPeople() {
			if (this.type !== '1') {
				uni.navigateTo({
					url: `/pagesRepairs/helpPeople/helpPeople?id=${this.recordId}`
				})
			}
		},
		// 点击录音按钮回调
		handleRecording() {
			uni.getSetting({
				success: (res) => {
					if (!res.authSetting['scope.record']) {
						uni.authorize({
							scope: 'scope.record',
							success(res) {
								// 授权成功
								uni.showToast({
									title: '授权成功',
									icon: 'none'
								})
							},
							fail() {
								uni.showModal({
									content: '检测到您没打开麦克风权限，是否去设置打开？',
									confirmText: '确认',
									cancelText: '取消',
									success: (res) => {
										if (res.confirm) {
											uni.openSetting({
												success: (res) => {}
											})
										} else {
											uni.showToast({
												title: '获取麦克风权限失败',
												icon: 'none'
											})
										}
									}
								})
							}
						})
					} else {
						this.$refs.popup_recording.open('bottom')
					}
				},
				fail() {
					uni.showToast({
						title: '获取麦克风权限失败',
						icon: 'none'
					})
				}
			})
		},
		// 点击录音播放回调
		handlePlayRecording() {
			innerAudioContext.src = this.info.voice
			if (!this.playStatus) {
				this.playStatus = true
				innerAudioContext.play()

				this.timer = setInterval(() => {
					if (this.recordingImg == '../../static/images/repairsImg/recording.jpg') {
						this.recordingImg = '../../static/images/repairsImg/recording2.jpg'
					} else if (this.recordingImg == '../../static/images/repairsImg/recording2.jpg') {
						this.recordingImg = '../../static/images/repairsImg/recording3.jpg'
					} else if (this.recordingImg == '../../static/images/repairsImg/recording3.jpg') {
						this.recordingImg = '../../static/images/repairsImg/recording.jpg'
					}
				}, 300)

				//播放结束
				innerAudioContext.onEnded(() => {
					clearInterval(this.timer)
					this.timer = null
					this.recordingImg = '../../static/images/repairsImg/recording.jpg'
					this.playStatus = false
				})
			} else {
				clearInterval(this.timer)
				this.recordingImg = '../../static/images/repairsImg/recording.jpg'
				this.playStatus = false
				innerAudioContext.stop()
			}
		},
		// 删除录音回调
		handleDeleteRecording() {
			if (this.playStatus) {
				uni.showToast({
					title: '播放中不能删除',
					icon: 'none',
					mask: true
				})
			} else {
				uni.showModal({
					title: '提示',
					content: '确定删除录音吗？',
					success: (res) => {
						if (res.confirm) {
							this.info.voice = ''
							this.info.voiceLength = 0
						}
					}
				})
			}
		},
		// 自定义事件回调,获取录音文件路径
		getTempFilePath(path, time) {
			this.info.voice = path
			this.info.voiceLength = time
			this.$refs.popup_recording.close()
		}
	}
}
</script>

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

	.title {
		display: flex;
		align-items: center;
		height: 107rpx;
		font-size: 36rpx;
		font-weight: bold;
	}

	.box {
		display: flex;
		align-items: center;
		height: 94rpx;
		font-size: 32rpx;
		border-radius: 10rpx;
		border: 1rpx solid #cccccc;

		.box_info {
			flex: 1;
			overflow: hidden;
			text-overflow: ellipsis;
			white-space: nowrap;
		}

		.color {
			color: #ccc;
		}

		img {
			margin: 0 14rpx 0 30rpx;
			width: 48rpx;
			height: 48rpx;
		}

		.img {
			width: 40rpx;
			height: 40rpx;
		}
	}

	.voice {
		display: flex;
		align-items: center;
		height: 94rpx;
		font-size: 32rpx;
		color: #cccccc;
		border-radius: 10rpx;
		border: 1rpx solid #cccccc;

		.voice_box {
			display: flex;
			justify-content: center;
			align-items: center;
			margin: 0 38rpx 0 33rpx;
			width: 101rpx;
			height: 47rpx;
			border-radius: 33rpx;
			box-shadow: 0px 0px 4rpx rgba(0, 0, 0, 0.25);

			img {
				width: 33rpx;
				height: 33rpx;
			}
		}

		.item_recording {
			margin-left: 35rpx;
			display: flex;
			align-items: center;
			width: 230rpx;
			height: 65rpx;
			color: #000;
			border-radius: 100rpx;
			border: 1rpx solid #cccccc;

			img {
				margin: 0 12rpx;
				width: 40rpx;
				height: 40rpx;
			}
		}

		.recording_icon {
			margin-left: auto;
			margin-right: 35rpx;
			font-size: 40rpx;
		}
	}

	.popup_recording {
		width: 100%;
		height: 460rpx;
		background-color: #fff;
	}

	.textarea {
		height: 310rpx;
		border-radius: 10rpx;
		border: 1rpx solid #cccccc;

		textarea {
			box-sizing: border-box;
			padding: 25rpx 35rpx;
			width: 100%;
			font-size: 32rpx;
		}
	}

	.btn {
		position: absolute;
		bottom: 66rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		margin: auto;
		width: 690rpx;
		height: 100rpx;
		color: #fff;
		font-size: 32rpx;
		border-radius: 12rpx;
		background-color: #6fb6b8;
	}

	.btn2 {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin: 200rpx auto 70rpx;
		width: 690rpx;
		height: 100rpx;
		font-size: 32rpx;

		.btn_box {
			display: flex;
			justify-content: center;
			align-items: center;
			width: 300rpx;
			height: 100rpx;
			border-radius: 12rpx;
		}

		.type {
			color: #fff;
			background-color: #6fb6b8;
		}

		.type2 {
			color: #6fb6b8;
			border: 1rpx solid #6fb6b8;
		}
	}
}
</style>
