<template>
	<view class="content">

		<!-- 评价标签筛选 -->
		<view class="evaluate-filter">
			<view class="filter-item" :class="{active: activeFilter === 'all'}" @click="activeFilter = 'all'">全部</view>
			<view class="filter-item" :class="{active: activeFilter === 'video'}" @click="activeFilter = 'video'">视频/图片(5)</view>
			<view class="filter-item" :class="{active: activeFilter === 'sort'}" @click="showSort = !showSort">
				<text>排序</text>
				<image class="w24 h24" src="https://cdn.his.cdwjyyh.com/images/more_arrow.png"></image>
			</view>
		</view>

		<!-- 评价统计 -->
		<view class="evaluation-stats">
			<view class="stats-header">
				<view class="amazingness">
					<text class="num">{{stunningRate}}%</text>
					<view class="text">
						<text>惊艳度</text>
						<image class="w24 h24 ml10" src="https://cdn.his.cdwjyyh.com/images/stunning.png"></image>
					</view>
				</view>
				<view class="rating">
					<view class="stars">
						<image v-for="i in 5" :key="i" :src="i <= rating ? 'https://cdn.his.cdwjyyh.com/images/star_active.png' : 'https://cdn.his.cdwjyyh.com/images/star_inactive.png'" mode="aspectFit"></image>
					</view>
					<text class="satisfaction">满意度{{satisfactionRate}}%</text>
				</view>
			</view>

			<view class="tag-list">
				<view class="tag" v-for="(tag, index) in tags" :key="index">{{tag.name}} {{tag.count}}</view>
			</view>

			<view class="disclaimer">
				<text>本平台商品评价仅为用户个人的真实的产品体验分享，不能完整代表产品的实际情况，请以产品实际为准。平台无默认好评规则。</text>
			</view>
		</view>

		<!-- 评价列表 -->
		<view class="evaluation-list">
			<view class="evaluate-main" v-for="(evaluation, index) in evaluations" :key="evaluation.commentId">
				<view class="main-user">
					<image class="avatar" :src="evaluation.avatar || 'https://cdn.his.cdwjyyh.com/images/img.png'" mode="aspectFit"></image>
					<view class="right">
						<view class="name">{{evaluation.userName || '匿名用户'}}</view>
						<view class="evaluation-time">{{evaluation.createTime}}</view>
					</view>
				</view>
				<view class="rating-info">
					<view class="stars">
						<image v-for="i in 5" :key="i" :src="i <= evaluation.score ? 'https://cdn.his.cdwjyyh.com/images/star_active.png' : 'https://cdn.his.cdwjyyh.com/images/star_inactive.png'" mode="aspectFit"></image>
					</view>
					<view class="evaluation-label">{{evaluation.evaluationLabel}}</view>
				</view>
				<view class="tags">
					<view class="tag" v-for="(tag, tagIndex) in evaluation.tags" :key="tagIndex">{{tag}}</view>
				</view>
				<view class="content-text">{{evaluation.content}}</view>
				<view class="purchased-info">
					<text>已购：{{evaluation.productName}}</text>
				</view>
				<scroll-view class="image-list" v-if="evaluation.commentImageList && evaluation.commentImageList.length > 0" scroll-x="true" :show-scrollbar="false">
					<view class="image-container">
						<image v-for="(image, imgIndex) in evaluation.commentImageList" :key="imgIndex" :src="image" mode="aspectFit"></image>
					</view>
				</scroll-view>
				<view class="merchant-reply" v-if="evaluation.replyList && evaluation.replyList.length > 0">
					<view class="reply-header">商家回复</view>
					<view class="reply-content">{{evaluation.replyList[0].content}}</view>
				</view>
				<view class="evaluate-text">
					<view class="left">
						<view class="stars">
							<image v-for="i in 5" :key="i" :src="i <= evaluation.score ? 'https://cdn.his.cdwjyyh.com/images/star_active.png' : 'https://cdn.his.cdwjyyh.com/images/star_inactive.png'" mode="aspectFit"></image>
						</view>
					</view>
					<view class="day">{{calculateRelativeTime(evaluation.createTime)}}</view>
				</view>
				<view class="action-buttons">
					<view class="reply-btn" @click="showReplyDialog(evaluation)">
						<image src="https://cdn.his.cdwjyyh.com/images/reply.png" mode="aspectFit"></image>
						<text>回复</text>
					</view>
					<view class="like-btn" @click="toggleLike(evaluation)">
						<image :src="evaluation.isLiked ? 'https://cdn.his.cdwjyyh.com/images/like_active.png' : 'https://cdn.his.cdwjyyh.com/images/like_inactive.png'" mode="aspectFit"></image>
						<text>{{evaluation.likeCount}}</text>
					</view>
				</view>
			</view>
		</view>

		<!-- 回复对话框 -->
		<view class="reply-dialog" v-if="showReply">
			<view class="dialog-content">
				<view class="dialog-header">
					<text>回复评论</text>
					<view class="close-btn" @click="showReply = false">
						<image src="https://cdn.his.cdwjyyh.com/images/close.png" mode="aspectFit"></image>
					</view>
				</view>
				<textarea v-model="replyContent" placeholder="请输入回复内容"></textarea>
				<view class="dialog-footer">
					<view class="cancel-btn" @click="showReply = false">取消</view>
					<view class="confirm-btn" @click="submitReply">确定</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { productCommentPage, productCommentAdd, productLike, productCommentReply } from '@/api/product'

	export default {
		data() {
			return {
				productId: '',
				evals: [],
				imageVideoCount: 5,
				stunningRate: 96.4,
				rating: 4.5,
				satisfactionRate: 99.6,
				tags: [
					{name: '无防腐更健康', count: 101},
					{name: '买了还想买', count: 84},
					{name: '健康安全', count: 138},
					{name: '彰显档次', count: 64},
					{name: '有机的吃起来放心', count: 170}
				],
				evaluations: [],
				showReply: false,
				replyContent: '',
				currentEvaluation: null,
				activeFilter: 'all',
				showSort: false,
				amazingness: 96.4,
				satisfaction: 99.6
			}
		},
		onLoad(options) {
			this.productId = options.productId
			this.getEvaluations()
		},
		methods: {
			calculateRelativeTime(time) {
				// 简单的时间格式化逻辑
				return time
			},
			getEvaluations() {
				let data = {
					productId: this.productId
				}
				productCommentPage(data).then(res => {
					if (res.code == 200) {
						this.evaluations = res.data.records.map(record => ({
							...record,
							rating: record.score,
							evaluationLabel: this.getEvaluationLabel(record.score),
							tags: [],
							isLiked: false
						}))
					}
				})
			},
			getEvaluationLabel(score) {
				if (score >= 5) return '惊艳，列为远方惊艳商品'
				if (score >= 4) return '满意'
				if (score >= 3) return '一般'
				if (score >= 2) return '不满意'
				return '很差'
			},
			showReplyDialog(evaluation) {
				this.currentEvaluation = evaluation
				this.showReply = true
			},
			submitReply() {
				if (!this.replyContent) return

				let data = {
					commentId: this.currentEvaluation.commentId,
					content: this.replyContent
				}
				productCommentReply(data).then(res => {
					if (res.code == 200) {
						this.showReply = false
						this.replyContent = ''
						// 刷新评论列表
						this.getEvaluations()
					}
				})
			},
			toggleLike(evaluation) {
				let data = {
					commentId: evaluation.commentId
				}
				productLike(data).then(res => {
					if (res.code == 200) {
						evaluation.isLiked = !evaluation.isLiked
						evaluation.likeCount += evaluation.isLiked ? 1 : -1
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.content {
		background-color: #f5f5f5;
		min-height: 100vh;
	}



	/* 评价标签筛选 */
	.evaluate-filter {
		display: flex;
		align-items: center;
		background-color: #fff;
		padding: 20rpx;
		margin-bottom: 10rpx;

		.filter-item {
			display: flex;
			align-items: center;
			padding: 10rpx 20rpx;
			margin-right: 30rpx;
			font-size: 28rpx;
			color: #666;

			&.active {
				color: #333;
				font-weight: bold;
			}

			image {
				margin-left: 10rpx;
			}
		}
	}

	/* 评价统计 */
	.evaluation-stats {
		background-color: #fff;
		padding: 20rpx;
		margin-bottom: 10rpx;

		.stats-header {
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin-bottom: 20rpx;

			.amazingness {
				display: flex;
				flex-direction: column;

				.num {
					font-size: 36rpx;
					font-weight: bold;
					color: #333;
					margin-bottom: 10rpx;
				}

				.text {
					display: flex;
					align-items: center;
					font-size: 28rpx;
					color: #666;
				}
			}

			.rating {
				display: flex;
				flex-direction: column;
				align-items: center;

				.stars {
					display: flex;
					margin-bottom: 10rpx;

					image {
						width: 40rpx;
						height: 40rpx;
						margin-right: 10rpx;
					}
				}

				.satisfaction {
					font-size: 28rpx;
					color: #666;
				}
			}
		}

		.tag-list {
			display: flex;
			flex-wrap: wrap;
			margin-bottom: 20rpx;

			.tag {
				padding: 10rpx 20rpx;
				margin-right: 20rpx;
				margin-bottom: 10rpx;
				border-radius: 20rpx;
				background-color: #f0f0f0;
				font-size: 28rpx;
				color: #666;
			}
		}

		.disclaimer {
			font-size: 24rpx;
			color: #999;
			line-height: 36rpx;
		}
	}

	/* 评价列表 */
	.evaluation-list {
		background-color: #fff;
		padding: 20rpx;

		.evaluate-main {
			margin-bottom: 40rpx;

			.main-user {
				display: flex;
				align-items: center;
				margin-bottom: 20rpx;

				.avatar {
					width: 60rpx;
					height: 60rpx;
					border-radius: 50%;
					margin-right: 15rpx;
				}

				.right {
					.name {
						font-size: 28rpx;
						color: #333;
						margin-bottom: 5rpx;
					}

					.evaluation-time {
						font-size: 22rpx;
						color: #999;
					}
				}
			}

			.rating-info {
				display: flex;
				align-items: center;
				margin-bottom: 15rpx;

				.stars {
					display: flex;
					margin-right: 15rpx;

					image {
						width: 32rpx;
						height: 32rpx;
						margin-right: 5rpx;
					}
				}

				.evaluation-label {
					font-size: 24rpx;
					color: #666;
				}
			}

			.tags {
				display: flex;
				flex-wrap: wrap;
				margin-bottom: 15rpx;

				.tag {
					padding: 5rpx 15rpx;
					background-color: #f5f5f5;
					border-radius: 15rpx;
					margin-right: 10rpx;
					margin-bottom: 10rpx;
					font-size: 22rpx;
					color: #666;
				}
			}

			.content-text {
				font-size: 26rpx;
				color: #333;
				line-height: 40rpx;
				margin-bottom: 15rpx;
			}

			.purchased-info {
				font-size: 22rpx;
				color: #999;
				margin-bottom: 15rpx;
			}

			.image-list {
				margin-bottom: 15rpx;
				width: 100%;

				.image-container {
					display: flex;
					flex-wrap: nowrap;

					image {
						width: 180rpx;
						height: 180rpx;
						margin-right: 10rpx;
						border-radius: 10rpx;
					}
				}
			}

			.merchant-reply {
				padding: 15rpx;
				background-color: #f9f9f9;
				border-radius: 10rpx;
				margin-bottom: 15rpx;

				.reply-header {
					font-size: 24rpx;
					font-weight: bold;
					color: #666;
					margin-bottom: 10rpx;
				}

				.reply-content {
					font-size: 24rpx;
					color: #333;
					line-height: 36rpx;
				}
			}

			.evaluate-text {
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-bottom: 15rpx;

				.left {
					.stars {
						display: flex;

						image {
							width: 32rpx;
							height: 32rpx;
							margin-right: 5rpx;
						}
					}
				}

				.day {
					font-size: 22rpx;
					color: #999;
				}
			}

			.action-buttons {
				display: flex;
				align-items: center;
				justify-content: flex-end;

				.reply-btn,
				.like-btn {
					display: flex;
					align-items: center;
					margin-left: 30rpx;

					image {
						width: 32rpx;
						height: 32rpx;
						margin-right: 5rpx;
					}

					text {
						font-size: 24rpx;
						color: #666;
					}
				}
			}
		}
	}

	/* 回复对话框 */
	.reply-dialog {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		background-color: rgba(0, 0, 0, 0.5);
		display: flex;
		align-items: center;
		justify-content: center;
		z-index: 999;

		.dialog-content {
			background-color: #fff;
			width: 90%;
			border-radius: 20rpx;
			padding: 30rpx;

			.dialog-header {
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-bottom: 30rpx;

				text {
					font-size: 32rpx;
					font-weight: bold;
					color: #333;
				}

				.close-btn {
					width: 40rpx;
					height: 40rpx;

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

			textarea {
				width: 100%;
				height: 200rpx;
				padding: 20rpx;
				border: 2rpx solid #f0f0f0;
				border-radius: 10rpx;
				font-size: 26rpx;
				color: #333;
				margin-bottom: 30rpx;
			}

			.dialog-footer {
				display: flex;
				align-items: center;
				justify-content: space-between;

				.cancel-btn,
				.confirm-btn {
					padding: 15rpx 0;
					width: 48%;
					text-align: center;
					font-size: 28rpx;
					border-radius: 10rpx;
				}

				.cancel-btn {
					background-color: #f0f0f0;
					color: #666;
				}

				.confirm-btn {
					background-color: #38D97D;
					color: #fff;
				}
			}
		}
	}
</style>