<template>
	<view class="rating-page">
		<view class="rating-list" v-if="reviewList.length">
			<view class="rating-card" v-for="item in reviewList" :key="item.id">
				<text class="service-title">{{ item.serviceName }}</text>

				<view class="star-row">
					<u-rate
						:value="item.score"
						:count="5"
						readonly
						size="18"
						gutter="4"
						activeColor="#ffca28"
						inactiveColor="#e5e5e5"
					></u-rate>
					<text class="score-text">{{ formatScore(item.score) }}分</text>
				</view>

				<view class="content-wrap">
					<text
						class="review-content"
						:class="{ collapsed: item.needExpand && !isExpanded(item.id), empty: !item.hasContent }"
					>{{ item.displayContent }}</text>
					<text
						v-if="item.needExpand"
						class="toggle"
						@click="toggleExpand(item.id)"
					>{{ isExpanded(item.id) ? '收起' : '展开' }}</text>
				</view>

				<view class="card-footer">
					<view class="user-info">
						<image
							v-if="item.avatar"
							class="avatar"
							:src="item.avatar"
							mode="aspectFill"
						></image>
						<view v-else class="avatar placeholder"></view>
						<text class="nickname">{{ item.nickName }}</text>
					</view>
					<text class="review-time">{{ formatReviewTime(item.createTime) }}</text>
				</view>
			</view>
		</view>
		<view class="empty" v-else>暂无评价</view>
	</view>
</template>

<script>
import { userComment } from '@/api/index'
import {
	MOCK_RATING_LIST,
	normalizeRatingItem,
	sortByTimeDesc,
	formatReviewTime,
	formatScore,
} from './mock.js'

export default {
	data() {
		return {
			reviewList: [],
			expandedIds: {},
		}
	},
	onShow() {
		this.loadList()
	},
	methods: {
		formatReviewTime,
		formatScore,
		loadList() {
			const openId = uni.getStorageSync('wx_copenid')
			userComment({ openId })
				.then(res => {
					if (res.data.code == 200) {
						const data = res.data.data
						const raw = Array.isArray(data) ? data : data?.records || []
						if (raw.length) {
							this.reviewList = sortByTimeDesc(
								raw.map((item, index) => normalizeRatingItem(item, index))
							)
							return
						}
					}
					this.useMock()
				})
				.catch(() => this.useMock())
		},
		useMock() {
			this.reviewList = sortByTimeDesc(
				MOCK_RATING_LIST.map((item, index) => normalizeRatingItem(item, index))
			)
		},
		isExpanded(id) {
			return !!this.expandedIds[id]
		},
		toggleExpand(id) {
			this.$set(this.expandedIds, id, !this.expandedIds[id])
		},
	},
}
</script>

<style lang="scss" scoped>
.rating-page {
	min-height: 100vh;
	background: #f5f5f5;
	padding: 16rpx 24rpx 32rpx;
	box-sizing: border-box;
}

.rating-list {
	display: flex;
	flex-direction: column;
	gap: 16rpx;
}

.rating-card {
	background: #fff;
	border-radius: 12rpx;
	padding: 28rpx 24rpx;
}

.service-title {
	display: block;
	font-size: 30rpx;
	font-weight: 600;
	color: #333;
	line-height: 1.4;
	margin-bottom: 16rpx;
}

.star-row {
	display: flex;
	align-items: center;
	margin-bottom: 16rpx;
}

.score-text {
	font-size: 26rpx;
	color: #666;
	margin-left: 12rpx;
}

.content-wrap {
	margin-bottom: 20rpx;
}

.review-content {
	display: block;
	font-size: 28rpx;
	color: #666;
	line-height: 1.6;
	word-break: break-all;

	&.empty {
		color: #999;
	}

	&.collapsed {
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 3;
		overflow: hidden;
	}
}

.toggle {
	display: inline-block;
	font-size: 26rpx;
	color: #0879ff;
	margin-top: 8rpx;
}

.card-footer {
	display: flex;
	align-items: center;
	justify-content: space-between;
	padding-top: 16rpx;
	border-top: 1rpx solid #f5f5f5;
}

.user-info {
	display: flex;
	align-items: center;
	flex: 1;
	overflow: hidden;
	margin-right: 16rpx;
}

.avatar {
	width: 48rpx;
	height: 48rpx;
	border-radius: 50%;
	flex-shrink: 0;
	margin-right: 12rpx;

	&.placeholder {
		background: #eee;
	}
}

.nickname {
	font-size: 24rpx;
	color: #999;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

.review-time {
	font-size: 24rpx;
	color: #999;
	flex-shrink: 0;
}

.empty {
	text-align: center;
	padding: 120rpx 0;
	font-size: 28rpx;
	color: #999;
}
</style>
