<template>
	<view class="container" v-if="total !== null">
		<!-- 顶部评分区域 -->
		<view class="header">
			<view class="header_box">
				<!-- 总评分区域 -->
				<view class="box_left">{{ score }}</view>
				<view class="box_right">
					<!-- 位置评分区域 -->
					<view class="right_item">
						<view class="item_info">位置 {{ scoreWz }}</view>
						<view class="item_progress">
							<progress activeColor="#0BAD8B" backgroundColor="#CCCCCC" stroke-width="10" border-radius="92" :percent="((scoreWz * 1) / 5) * 100" />
						</view>
					</view>
					<!-- 设施评分区域 -->
					<view class="right_item">
						<view class="item_info">设施 {{ scoreSs }}</view>
						<view class="item_progress">
							<progress activeColor="#0BAD8B" backgroundColor="#CCCCCC" stroke-width="10" border-radius="92" :percent="((scoreSs * 1) / 5) * 100" />
						</view>
					</view>
					<!-- 服务评分区域 -->
					<view class="right_item">
						<view class="item_info">服务 {{ scoreFw }}</view>
						<view class="item_progress">
							<progress activeColor="#0BAD8B" backgroundColor="#CCCCCC" stroke-width="10" border-radius="92" :percent="((scoreFw * 1) / 5) * 100" />
						</view>
					</view>
					<!-- 卫生评分区域 -->
					<view class="right_item">
						<view class="item_info">卫生 {{ scoreWs }}</view>
						<view class="item_progress">
							<progress activeColor="#0BAD8B" backgroundColor="#CCCCCC" stroke-width="10" border-radius="92" :percent="((scoreWs * 1) / 5) * 100" />
						</view>
					</view>
				</view>
			</view>
		</view>

		<!-- 分段器区域 -->
		<view class="segmented">
			<uni-segmented-control styleType="text" activeColor="#096562" :current="activeCurrent" :values="headerList" @clickItem="onClickItem" />
		</view>

		<!-- 评价列表 -->
		<view class="body">
			<!-- 每一个评价区域 -->
			<view class="body_box" v-for="item in commentList" :key="item.id" @click="handleGoDetail(item)">
				<!-- 用户信息区域 -->
				<view class="box_userInfo">
					<img mode="aspectFill" :src="item.headPhoto" />
					<view class="userInfo_msg">
						<view class="msg_name">{{ item.userName }}</view>
						<view class="msg_star">
							<uni-rate readonly activeColor="#FFC300" :size="16" :value="item.score" />
						</view>
					</view>
				</view>

				<!-- 入住时间区域 -->
				<view class="box_time" v-if="item.liveTime && item.commentTime">
					{{ item.liveTime.slice(0, 10) }}入住，{{ item.commentTime.slice(0, 10) }}发表 | {{ item.houseName }}
				</view>

				<!-- 评价内容区域 -->
				<uv-read-more show-height="85rpx" closeText="全文" color="#096663" fontSize="24rpx" textIndent="0" :toggle="true" :shadowStyle="shadowStyle">
					<view class="box_content">
						{{ item.content }}
					</view>
				</uv-read-more>

				<!-- 图片区域 -->
				<view class="box_img">
					<img
						v-for="(ele, index) in item.url"
						:key="index"
						mode="aspectFill"
						v-if="ele.indexOf('jpg') !== -1 || ele.indexOf('png') !== -1"
						:src="ele"
						@click.stop="handleClickImg(ele, index)"
					/>
					<video
						:id="item.id + index2"
						class="video"
						:show-fullscreen-btn="false"
						:show-play-btn="false"
						v-for="(video, index2) in item.url"
						:key="index2"
						v-if="video.indexOf('mp4') !== -1"
						:src="video"
						@fullscreenchange="fullscreenchange"
						@click.stop="handleClickVideo(item.id + index2)"
					></video>
				</view>

				<!-- 评论按钮区域 -->
				<view class="box_comment">
					<img src="../../static/index/comment.png" />
					<view>评论（{{ item.commentCount }}）</view>
				</view>
			</view>
		</view>

		<view class="noData" v-if="commentList.length === 0">
			<img src="../../static/images/noData.png" />
			暂无评论
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			// 分段器分类数组
			headerList: ['全部', '有图/视频', '商家回复'],
			// 当前激活的分段器索引
			activeCurrent: 0,
			// 评价内容区域阴影样式
			shadowStyle: {
				backgroundImage: 'none'
			},
			// 评价列表数据
			commentList: [],
			// 民宿id
			hotelId: '',
			// 当前页
			page: 1,
			// 每页多少条
			rows: 6,
			// 总条数
			total: null,
			// 总评分
			score: '',
			// 服务评分
			scoreFw: '',
			// 设施评分
			scoreSs: '',
			// 卫生评分
			scoreWs: '',
			// 位置评分
			scoreWz: '',
			// video 上下文 videoContext 对象
			videoContext: null,
			// 是否是全屏状态
			isFullScreen: false
		}
	},
	onLoad(options) {
		this.hotelId = options.hotelId
		this.getData()
	},
	onReachBottom() {
		if (this.commentList.length < this.total) {
			this.page++
			this.getData()
		} else {
			uni.showToast({
				title: '没有更多数据了',
				icon: 'none'
			})
		}
	},
	methods: {
		// 进入全屏和退出全屏时触发的回调
		fullscreenchange(e) {
			this.isFullScreen = e.detail.fullScreen
		},
		// 点击视频控件时触发的回调
		handleClickVideo(id) {
			this.videoContext = uni.createVideoContext(id)
			if (this.isFullScreen) {
				this.videoContext.stop()
				this.videoContext.exitFullScreen()
			} else {
				this.videoContext.requestFullScreen()
				this.videoContext.play()
			}
		},
		async getData() {
			const res = await this.$myRequest({
				url: '/mhotel/abcaevaluatePage.action',
				data: {
					hotelId: this.hotelId,
					page: this.page,
					rows: this.rows,
					status: this.activeCurrent
				}
			})
			// console.log(res);
			if (res.code === 200) {
				this.headerList = [`全部（${res.score.totalCount}）`, `有图/视频（${res.score.pictureCount}）`, `商家回复（${res.score.commentCount}）`]
				this.score = res.score.score.toFixed(1)
				this.scoreFw = res.score.scoreFw.toFixed(1)
				this.scoreSs = res.score.scoreSs.toFixed(1)
				this.scoreWs = res.score.scoreWs.toFixed(1)
				this.scoreWz = res.score.scoreWz.toFixed(1)
				this.commentList = [...this.commentList, ...(res.page.pageList || [])]
				this.total = res.page.total
			}
		},
		// 点击每一个评价回调
		handleGoDetail(item) {
			uni.navigateTo({
				url: `/pages/appraiseDetail/appraiseDetail?id=${item.id}`
			})
		},
		// 切换分段器时的回调
		onClickItem(e) {
			this.activeCurrent = e.currentIndex
			this.page = 1
			this.commentList = []
			this.getData()
		},
		// 点击评价图片回调
		handleClickImg(url, index) {
			if (this.videoContext) {
				this.videoContext.stop()
			}
			// 预览图片
			uni.previewImage({
				current: index,
				urls: [url]
			})
		}
	}
}
</script>

<style lang="scss" scoped>
.container {
	display: flex;
	flex-direction: column;
	min-height: 100vh;
	background-color: #f2f3f5;

	.header {
		height: 150rpx;
		background-color: #fff;

		.header_box {
			display: flex;
			margin: 43rpx 0 36rpx;
			height: 70rpx;

			.box_left {
				display: flex;
				justify-content: center;
				align-items: center;
				width: 140rpx;
				color: #096562;
				font-size: 60rpx;
				font-weight: 900;
				border-right: 1rpx solid #cccccc;
			}

			.box_right {
				flex: 1;
				display: flex;
				flex-wrap: wrap;
				color: #808080;
				font-size: 24rpx;

				.right_item {
					display: flex;
					align-items: center;
					width: 50%;

					.item_info {
						margin-left: 33rpx;
					}

					.item_progress {
						margin-right: 20rpx;
						padding: 0 13rpx;
						flex: 1;
					}
				}
			}
		}
	}

	.segmented {
		padding-bottom: 18rpx;
		margin-bottom: 20rpx;
		background-color: #fff;
	}

	.body {
		.body_box {
			box-sizing: border-box;
			padding: 0 20rpx;
			margin: 0 auto 20rpx;
			width: 710rpx;
			border-radius: 18rpx;
			background-color: #fff;

			.box_userInfo {
				display: flex;
				align-items: center;
				height: 115rpx;

				img {
					width: 70rpx;
					height: 70rpx;
					border-radius: 50%;
				}

				.userInfo_msg {
					margin-left: 18rpx;

					.msg_name {
						font-size: 28rpx;
					}

					.msg_star {
						margin-left: -5rpx;
						margin-top: 5rpx;
					}
				}
			}

			.box_time {
				display: flex;
				align-items: center;
				color: #a6a6a6;
				font-size: 24rpx;
			}

			.box_content {
				margin-top: 15rpx;
				font-size: 24rpx;
				color: #000;
			}

			.box_img {
				display: grid;
				grid-template-columns: 1fr 1fr 1fr;
				grid-auto-rows: auto;
				gap: 10rpx;
				margin-top: 20rpx;

				img {
					width: 216rpx;
					height: 216rpx;
					border-radius: 20rpx;
				}

				.video {
					width: 216rpx;
					height: 216rpx;
					border-radius: 20rpx;
				}
			}

			.box_comment {
				display: flex;
				justify-content: flex-end;
				align-items: center;
				padding: 20rpx 0 30rpx;
				font-size: 24rpx;

				img {
					margin-right: 12rpx;
					width: 25rpx;
					height: 22rpx;
				}
			}
		}
	}

	.noData {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		padding-bottom: 20rpx;

		img {
			margin-top: 60rpx;
			width: 600rpx;
			height: 600rpx;
		}
	}
}
</style>
