<template>
	<view class="page-content">
		<scroll-view v-if="myReviewList.length > 0" style="height: 100%;" scroll-y="true" @scrolltolower="changePage">
		<view class="review-item" v-for="(item, index) in myReviewList" :key="index">
			<view class="flex margin-bottom-sm">
				<!-- <image src="" class="avatar"></image> -->
				<view class="flex flex-col">
					<text class="beerName">{{ item.userName}}</text>
					<text class="time">{{ item.createTime.slice(0,10)}}</text>
				</view>
			</view>
			<view class="margin-bottom flex ">
				<text class="word-all flex-1">
					{{ item.reviewContent }}
				</text>
				<image v-if="item.reviewImg" :src="item.reviewImg" style="width: 200rpx;height:200rpx;flex-shrink: 0;margin-left: 20rpx;"></image>
			</view>
			<view class="flex align-center justify-between">
				<view class="flex align-center justify-start">
					<image src="@/static/vector.png" class="icon"></image>
					<text style="color:#5F5F63;margin-right: 40rpx;">{{ item.overallRating }}</text>
					<!-- <image src="@/static/message.png" class="icon"></image>
					<text style="color:#979797">(100)</text> -->
				</view>


				<view class="cu-btn btn" @click="delReview(item)">删除</view>

			</view>
		</view>
		</scroll-view>
		<template v-else>
			<view class="flex align-center justify-center"
				style="height: 140rpx;width: 100%;color: #747783;font-size: 24rpx;">暂无评价</view>
		</template>
	</view>
</template>

<script>
	import {
		listMyReview,
		delReview
	} from '@/api/user.js'
	export default {
		data() {
			return {
				myReviewList: [],
				total: 0,
				queryForm: {
					pageNum: 1,
					pageSize: 10
				}
			}
		},
		onLoad() {
			// 测试接口
			console.log('开始测试接口...')
			
			// 测试获取我的酒评列表
			listMyReview({
				pageNum: 1,
				pageSize: 10
			}).then(res => {
				console.log('获取我的酒评列表测试成功:', res)
			}).catch(err => {
				console.error('获取我的酒评列表失败:', err)
			})
			
			// 测试删除酒评接口(需要有效的ID)
			// delReview('xxx').then(res => {
			//     console.log('删除酒评测试成功:', res)
			// }).catch(err => {
			//     console.error('删除酒评失败:', err)
			// })
			
			// 原有的数据加载
			this.getMyReviewList()
		},
		methods: {
			getMyReviewList() {
				listMyReview(this.queryForm).then(res => {
					this.total = res.total
					if(res.rows && res.rows.length > 0) {
						res.rows.forEach(item => {
							this.myReviewList.push(item)
						})
					}
					// this.myReviewList = res.rows
				})
			},
			delReview(item) {
				uni.showModal({
					title: '提示',
					content: '确定删除该评论吗？',
					success: (res) => {
						if (res.confirm) {
							delReview(item.id).then(res => {
								this.queryForm.pageNum = 1
								this.myReviewList = []
								this.getMyReviewList()
								uni.showToast({
									title: '删除成功',    
									icon: 'success'
								})
							})
							
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},
			changePage() {
				if(this.myReviewList.length < this.total) {
					this.queryForm.pageNum++
					this.getMyReviewList()
				}
				
			}
		}
	}
</script>

<style lang="scss" scoped>
	.page-content {
		padding: 32rpx;
		background: #FDFDFD;
		height: 100vh;

		.review-item {
			width: 100%;
			// height: 200rpx;
			padding: 24rpx 0;
			border-radius: 16rpx;
			border-bottom: 1px solid rgba(242, 242, 242, 0.9);

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

			.beerName {
				font-size: 24rpx;
				font-weight: bold;
				color: #32343E;
				margin-bottom: 8rpx;
			}

			.time {
				font-size: 24rpx;
				color: #9C9BA6;
			}

			.icon {
				width: 40rpx;
				height: 40rpx;
				margin-right: 10rpx;
			}
		}
		.btn {
			width: 118rpx;
			height: 42rpx;
			font-size: 20rpx;
			border-radius: 12rpx;
			box-sizing: border-box;
			border: 1px solid #979797;
			background: #fff;
		}
	}
</style>