<template>
	<view class="container">
		<!-- 公告区域 -->
		<view class="notice">
			<uni-notice-bar show-icon single speed="50" :scrollable="true" background-color="#fff" color="#000" :text="repairsNotice" />
		</view>
		<view class="gap"></view>

		<!-- 报修区域 -->
		<view class="box_item">
			<view class="item_title">
				报修区域
				<text>*</text>
			</view>
			<view class="item_info" :class="{ item_color: repairsArea }" @click="handleSelectArea">
				{{ repairsArea ? repairsArea : '请选择报修区域' }}
			</view>
			<view class="item_icon" @click="handleSelectArea">
				<img src="../../static/images/repairsImg/right.png" />
			</view>
		</view>

		<!-- 详细地址区域 -->
		<view class="box_item">
			<view class="item_title">
				详细地址
				<text>*</text>
			</view>
			<view class="item_info">
				<view class="item_input">
					<input placeholder-style="color:#CCCCCC" type="text" placeholder="请输入详细地址" v-model="repairsAddress" />
				</view>
			</view>
		</view>

		<!-- 报修物品区域 -->
		<view class="box_item">
			<view class="item_title">
				报修物品
				<text>*</text>
			</view>
			<view class="item_info" :class="{ item_color: repairsGoods }" @click="handleSelectGoods">
				{{ repairsGoods ? repairsGoods : '请选择报修物品' }}
			</view>
			<view class="item_icon" @click="handleSelectGoods">
				<img src="../../static/images/repairsImg/right.png" />
			</view>
		</view>

		<!-- 故障描述 -->
		<view class="box_item2">
			<view class="item2_title">故障描述</view>
			<view class="item2_info">
				<view class="item2_textarea">
					<textarea placeholder-style="color:#CCCCCC" placeholder="请输入故障描述" v-model="repairsDescription"></textarea>
				</view>
			</view>
		</view>

		<!-- 报修录音区域 -->
		<view class="box_item">
			<view class="item_title">报修录音</view>
			<view class="item_info" v-if="!recordingPath">
				<view class="info_icon" @click="handleRecording">
					<img src="../../static/images/repairsImg/voice.png" />
				</view>
				<view class="info_tips" @click="handleRecording">点击录音</view>
			</view>
			<view class="item_recording" v-else @click="handlePlayRecording">
				<img :src="recordingImg" />
				{{ recordingTime }}″
			</view>
			<view class="recording_icon" v-if="recordingPath" @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="box_item3">
			<view class="item3_title">
				上传照片
				<span>*</span>
				<text>（可上传三张，请上传局部图和区域图）</text>
			</view>

			<uni-file-picker
				ref="filePicker"
				limit="3"
				v-model="imgList"
				fileMediatype="image"
				:image-styles="imageStyles"
				mode="grid"
				@select="select"
				@delete="handleDelete"
			></uni-file-picker>
		</view>

		<!-- 报修姓名区域 -->
		<view class="box_item">
			<view class="item_title">
				报修姓名
				<text>*</text>
			</view>
			<view class="item_info">
				<view class="item_input">
					<input placeholder-style="color:#CCCCCC" type="text" placeholder="请输入报修姓名" v-model="repairsName" />
				</view>
			</view>
		</view>

		<!-- 联系电话区域 -->
		<view class="box_item">
			<view class="item_title">
				联系电话
				<text>*</text>
			</view>
			<view class="item_info">
				<view class="item_input">
					<input placeholder-style="color:#CCCCCC" type="number" placeholder="请输入联系电话" v-model="repairsPhone" />
				</view>
			</view>
		</view>

		<!-- 底部区域 -->
		<view class="foot">
			<view class="foot_btn" @click="handleAffirmSub">确认提交</view>
			<view class="foot_phone" @click="handleCallPhone(urgencyPhone)">紧急电话 {{ urgencyPhone }}</view>
			<view class="foot_phone" @click="handleCallPhone(searchPhone)">服务监督电话 {{ searchPhone }}</view>
		</view>

		<!-- 消息通知按钮区域 -->
		<view class="fixed" @tap="handleClickFixed">
			<view class="fixed_box">
				<img src="../../static/images/repairsImg/message.png" />
				消息通知
			</view>
		</view>

		<!-- 用于图片压缩的canvas画布 -->
		<canvas
			:style="{
				width: cw + 'px',
				height: cw + 'px',
				position: 'absolute',
				zIndex: -1,
				left: '-10000rpx',
				top: '-10000rpx'
			}"
			canvas-id="zipCanvas"
		></canvas>
		<!--画布结束-->
	</view>
</template>

<script>
import recording from '../components/recording.vue'
// 图片压缩方法
import getLessLimitSizeImage from '../util/imageCompress.js'

const innerAudioContext = uni.createInnerAudioContext()
export default {
	components: {
		recording
	},
	data() {
		return {
			// 报修公告
			repairsNotice: '今日14:00系统更新请不要进行报修！',
			// 报修区域
			repairsArea: '',
			// 报修地址
			repairsAddress: '',
			// 报修物品
			repairsGoods: '',
			// 故障描述
			repairsDescription: '',
			// 报修姓名
			repairsName: '',
			// 联系电话
			repairsPhone: '',
			// 紧急电话
			urgencyPhone: '18320846714',
			// 服务电话
			searchPhone: '18320846714',
			// 播放状态
			playStatus: false,
			// 定时器标识
			timer: null,
			// 录音图片地址
			recordingImg: '../../static/images/repairsImg/recording.jpg',
			// 录音文件路径
			recordingPath: '',
			// 录音时长
			recordingTime: 0,
			// 显示的图片数据
			imgList: [],
			// 上传的图片数据
			subImgList: [],
			// 图片上传框的样式
			imageStyles: {
				width: 60,
				height: 60,
				border: {
					color: '#ccc',
					width: 1,
					style: 'dashed',
					radius: '9px'
				}
			},
			//画板边长默认是屏幕宽度，正方形画布
			cw: uni.getSystemInfoSync().windowWidth
		}
	},
	mounted() {
		uni.$on('addRepairsArea', this.addRepairsArea)
		uni.$on('addRepairsGoods', this.addRepairsGoods)
		//在ios下静音时播放没有声音，默认为true，改为false就好了。
		uni.setInnerAudioOption({
			obeyMuteSwitch: false
		})
	},
	methods: {
		// 确认提交按钮回调
		handleAffirmSub() {
			const flag = this.handleValidate()
			if (flag) {
				console.log(this.repairsArea)
				console.log(this.repairsAddress)
				console.log(this.repairsGoods)
				console.log(this.repairsDescription)
				console.log(this.recordingPath)
				console.log(this.subImgList)
				console.log(this.repairsName)
				console.log(this.repairsPhone)
				uni.showToast({
					title: '提交成功',
					icon: 'none'
				})
			}
		},
		// 验证信息是否符合规范
		handleValidate() {
			const reName = /^[\u4e00-\u9fa5]{2,4}$/
			const rePhone = /^[1][3,4,5,7,8,9][0-9]{9}$/
			if (!this.repairsArea) {
				uni.showToast({
					title: '请选择报修区域',
					icon: 'none'
				})
				return
			}
			if (!this.repairsAddress) {
				uni.showToast({
					title: '请输入详细地址',
					icon: 'none'
				})
				return
			}
			if (!this.repairsGoods) {
				uni.showToast({
					title: '请选择报修物品',
					icon: 'none'
				})
				return
			}
			if (this.subImgList.length == 0) {
				uni.showToast({
					title: '请上传报修照片',
					icon: 'none'
				})
				return
			}
			if (!this.repairsName) {
				uni.showToast({
					title: '请输入报修姓名',
					icon: 'none'
				})
				return false
			}
			if (!reName.test(this.repairsName)) {
				uni.showToast({
					title: '姓名格式有误',
					icon: 'none'
				})
				return false
			}
			if (!this.repairsPhone) {
				uni.showToast({
					title: '请输入联系电话',
					icon: 'none'
				})
				return false
			}
			if (!rePhone.test(this.repairsPhone)) {
				uni.showToast({
					title: '电话号码格式有误',
					icon: 'none'
				})
				return false
			}
			return true
		},
		// 全局自定义事件
		addRepairsArea(e) {
			console.log(e)
			this.repairsArea = e.data
		},
		// 全局自定义事件
		addRepairsGoods(e) {
			console.log(e)
			this.repairsGoods = e.data
		},
		// 删除录音回调
		handleDeleteRecording() {
			if (this.playStatus) {
				uni.showToast({
					title: '播放中不能删除',
					icon: 'none',
					mask: true
				})
			} else {
				uni.showModal({
					title: '提示',
					content: '确定删除录音吗？',
					success: (res) => {
						if (res.confirm) {
							this.recordingPath = ''
							this.recordingTime = 0
						}
					}
				})
			}
		},
		// 点击录音播放回调
		handlePlayRecording() {
			innerAudioContext.src = this.recordingPath
			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()
			}
		},
		// 自定义事件回调,获取录音文件路径
		getTempFilePath(path, time) {
			this.recordingPath = path
			this.recordingTime = time
			this.$refs.popup_recording.close()
		},
		// 选择图片回调
		select(e) {
			console.log(e)
			e.tempFiles.forEach((item) => {
				//这里的id和页面中写的html代码的canvas的id要一致
				let canvasId = 'zipCanvas'
				//原图的路径
				let imagePath = item.path
				//大小限制1024kb
				let limitSize = 1024
				//初始绘画区域是画布自身的宽度也就是屏幕宽度
				let drawWidth = uni.getSystemInfoSync().windowWidth

				getLessLimitSizeImage(canvasId, imagePath, limitSize, drawWidth, (resPath) => {
					uni.showLoading({
						title: '上传中'
					})
					uni.uploadFile({
						url: `https://jiangxih3cpartner.com/reporting/file/fileUpDown`,
						filePath: resPath,
						name: 'file',
						success: (uploadFileRes) => {
							this.subImgList.push(JSON.parse(uploadFileRes.data).data.join())
							this.imgList.push({
								url: item.path,
								name: ''
							})
							uni.hideLoading()
						},
						fail: () => {
							uni.showToast({
								title: '上传失败',
								icon: 'error'
							})
						}
					})
				})
			})
		},
		// 删除图片回调
		handleDelete(e) {
			// console.log(e);
			const num = this.imgList.findIndex((v) => v.url === e.tempFilePath)
			this.subImgList.splice(num, 1)
			this.imgList.splice(num, 1)
		},

		// 点击报修区域回调
		handleSelectArea() {
			uni.navigateTo({
				url: '/pagesRepairs/selectArea/selectArea'
			})
		},
		// 点击报修物品回调
		handleSelectGoods() {
			uni.navigateTo({
				url: '/pagesRepairs/selectGoods/selectGoods'
			})
		},
		// 点击消息通知按钮回调
		handleClickFixed() {
			uni.navigateTo({
				url: '/pagesRepairs/message/message'
			})
		},
		// 点击电话号码回调
		handleCallPhone(phone) {
			uni.makePhoneCall({
				phoneNumber: phone
			})
		},

		// 点击录音按钮回调
		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'
					})
				}
			})
		}
	}
}
</script>

<style lang="scss" scoped>
.container {
	width: 100vw;
	height: calc(100vh - 152rpx);
	overflow-y: auto;

	.notice {
		height: 90rpx;
	}

	.gap {
		height: 12rpx;
		background-color: #f2f2f2;
	}

	.box_item {
		display: flex;
		align-items: center;
		margin: 0 30rpx;
		height: 105rpx;
		font-size: 28rpx;
		border-bottom: 1rpx solid #e6e6e6;

		.item_title {
			width: 190rpx;
			font-weight: bold;

			text {
				margin-left: 5rpx;
				color: #d43030;
			}
		}

		.item_info {
			flex: 1;
			display: flex;
			align-items: center;
			padding: 0 10rpx;
			height: 100%;
			color: #cccccc;

			.item_input {
				width: 100%;
				height: 105rpx;

				input {
					height: 105rpx;
					line-height: 105rpx;
					color: #000;
				}
			}

			.info_icon {
				display: flex;
				justify-content: center;
				align-items: center;
				width: 79rpx;
				height: 37rpx;
				border-radius: 33rpx;
				border: 1rpx solid #e6e6e6;

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

			.info_tips {
				margin-left: 33rpx;
				font-size: 28rpx;
				color: #cccccc;
			}
		}

		.item_color {
			color: #000;
		}

		.item_icon {
			width: 38rpx;
			height: 38rpx;

			img {
				width: 100%;
				height: 100%;
			}
		}

		.item_recording {
			display: flex;
			align-items: center;
			width: 200rpx;
			height: 50rpx;
			border-radius: 100rpx;
			border: 1rpx solid #cccccc;

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

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

	.box_item2 {
		display: flex;
		align-items: center;
		margin: 0 30rpx;
		height: 237rpx;
		font-size: 28rpx;
		border-bottom: 1rpx solid #e6e6e6;

		.item2_title {
			width: 190rpx;
			font-weight: bold;

			text {
				margin-left: 5rpx;
				color: #d43030;
			}
		}

		.item2_info {
			flex: 1;
			padding: 0 10rpx;

			.item2_textarea {
				display: flex;
				align-items: center;
				width: 100%;
				height: 237rpx;

				textarea {
					box-sizing: border-box;
					padding: 20rpx;
					width: 500rpx;
					height: 176rpx;
					border: 1rpx solid #e6e6e6;
					border-radius: 6rpx;
				}
			}
		}
	}

	.box_item3 {
		margin: 0 30rpx;
		height: 246rpx;
		border-bottom: 1rpx solid #e6e6e6;

		.item3_title {
			height: 95rpx;
			line-height: 95rpx;
			font-size: 28rpx;
			font-weight: bold;

			span {
				margin-left: 5rpx;
				color: #d43030;
			}

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

	.foot {
		display: flex;
		flex-direction: column;
		align-items: center;
		margin: 0 30rpx;
		height: 407rpx;

		.foot_btn {
			margin-top: 167rpx;
			width: 100%;
			height: 100rpx;
			line-height: 100rpx;
			text-align: center;
			color: #fff;
			font-size: 32rpx;
			border-radius: 12rpx;
			background-color: #6fb6b8;
		}

		.foot_phone {
			margin-top: 30rpx;
			color: #b3b3b3;
			font-size: 24rpx;
		}
	}

	.fixed {
		z-index: 2;
		position: fixed;
		right: 0;
		bottom: 200rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		width: 211rpx;
		height: 90rpx;
		border-radius: 150rpx 0 0 150rpx;
		border: 1rpx solid #d9d9d9;
		background-color: #fff;
		box-shadow: 0 0 7rpx #d9d9d9;

		.fixed_box {
			display: flex;
			align-items: center;
			font-size: 28rpx;

			img {
				margin-right: 10rpx;
				width: 38rpx;
				height: 38rpx;
			}
		}
	}

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