<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">
				<view class="card-header">
					<view class="user-info">
						<image v-if="item.userIcon" class="avatar" :src="item.userIcon" mode="aspectFill"></image>
					</view>
					<view style="flex: 1;display: flex;flex-direction: column;justify-content: space-between;">
						<view style="display: flex;align-items: center;justify-content: space-between;">
							<text class="nickname">{{ item.nickName }}666</text>
							<text class="review-time">{{ item.commentTime }}</text>
						</view>
						<view class="star-row">
							<u-rate :value="item.score" :count="5" :allowHalf="true" readonly size="23rpx" gutter="4"
								activeColor="#F21144" inactiveColor="#e5e5e5"></u-rate>
							<text class="score-text">{{ formatScore(item.score) }}分</text>
						</view>
					</view>

				</view>



				<view class="content-wrap">
					<text class="review-content" :class="{ collapsed: item.needExpand && !isExpanded(item.id) }">{{
						item.text }}</text>
					<view v-if="item.needExpand" class="toggle" @click="toggleExpand(item.id)">
						<view class="toggle-text">
							{{ isExpanded(item.id) ? '收起' : '展开' }}
							<image v-if="isExpanded(item.id)" src="/static/workbench/upIcon.png" mode="aspectFill"
								style="width: 32rpx;height: 32rpx;margin-top: 5rpx;" />

							<image v-else src="/static/workbench/downIcon.png" mode="aspectFill"
								style="width: 32rpx;height: 32rpx;margin-top: 5rpx;" />

						</view>
					</view>
				</view>

				<view class="service-tag">
					<text class="tag-hash">#</text>
					<text class="tag-text">{{ item.projectName }}</text>
				</view>
			</view>
		</view>
		<view class="empty" v-else>暂无评价</view>
	</view>
</template>

<script>
import { getCommentList } from '@/api/workbench.js'
import {
	MOCK_RATING_LIST,
	normalizeRatingItem,
	sortByTimeDesc,
	formatScore,
} from './mock.js'

export default {
	data() {
		return {
			reviewList: [],
			expandedIds: {},
		}
	},
	onShow() {
		this.loadList()
	},
	methods: {
		formatScore,
		loadList() {
			getCommentList({
				pageNum: 1,
				pageSize: 10,
			})
				.then(res => {
					if (res.data.code == 200) {
						this.reviewList = res.data.data.records || []
						this.reviewList.forEach((item, index) => {
							item.needExpand = normalizeRatingItem(item, index)
						})

					}
				})
		},
		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: 16rpx;
	padding: 28rpx 24rpx;
}

.card-header {
	display: flex;
	align-items: center;
	justify-content: space-between;
}

.user-info {
	width: 68rpx;
	height: 68rpx;

	overflow: hidden;
	margin-right: 16rpx;
}

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

	&.placeholder {
		background: #eee;
	}
}

.nickname {
	font-weight: 500;
	font-size: 28rpx;
	color: #1D2129;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

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

.star-row {
	margin-top: 12rpx;
	margin-left: -8rpx;
	display: flex;
	align-items: center;
}

.score-text {
	font-weight: 600;
	font-size: 24rpx;
	color: #F21144;
	line-height: 24rpx;
}

.content-wrap {
	margin-bottom: 20rpx;
	margin-left: 80rpx;

}

.review-content {
	margin-top: 20rpx;
	display: block;
	font-weight: 400;
	font-size: 28rpx;
	color: #1D2129;
	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-flex;
	align-items: center;
	margin-top: 8rpx;
}

.toggle-text {
	display: flex;
	align-items: center;
	font-weight: 400;
	font-size: 28rpx;
	color: #19D29B;
	line-height: 32rpx;
	margin-right: 4rpx;

}


.service-tag {
	display: flex;
	align-items: center;
	line-height: 1.4;
	margin-left: 80rpx;
}

.tag-hash {
	height: 28rpx;
	width: 28rpx;
	background: linear-gradient(263deg, #45FFD7 0%, #84FF94 100%);
	font-size: 26rpx;
	font-weight: bold;
	color: #1D2129;
	margin-right: 4rpx;
	flex-shrink: 0;
	display: flex;
	align-items: center;
	justify-content: center;
	margin-right: 8rpx;
}

.tag-text {
	font-size: 26rpx;
	color: #666;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

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