<template>
  <view class="comment-item">
    <image class="c-avatar" :src="comment.avatar" mode="aspectFill" />
    <view class="c-content-wrap">
      <view class="c-header">
        <view class="c-user-info">
          <text class="c-name">{{ comment.name }}</text>
          <!-- 用户态度徽章 -->
          <ActionBadge v-if="comment.userAction" :action="comment.userAction" />
          <view class="c-badge" v-if="comment.badge">
            <text class="b-icon">{{ comment.badgeIcon }}</text>
            <text class="b-text">{{ comment.badge }}</text>
          </view>
        </view>
        <view class="c-like" @tap="handleLike">
          <text class="like-icon" :class="{ 'is-liked': isLiked }"></text>
          <text class="like-count" :class="{ 'liked-text': isLiked }">{{ currentLikes }}</text>
        </view>
      </view>
      <text class="c-time">{{ comment.time }}</text>
      <text class="c-text">{{ comment.content }}</text>
    </view>
  </view>
</template>

<script setup>
import { ref, watch } from 'vue';
import ActionBadge from "@/components/ActionBadge/ActionBadge.vue";
import { topicItemCommentLike } from "@/api/topicItem";

const props = defineProps({
  comment: {
    type: Object,
    required: true,
    default: () => ({})
  }
});

const isLiked = ref(props.comment.isLiked || false);
const currentLikes = ref(props.comment.likes || 0);
let isLiking = false; // 防抖标志

watch(() => props.comment.likes, (newVal) => {
  currentLikes.value = newVal;
});

watch(() => props.comment.isLiked, (newVal) => {
  isLiked.value = newVal || false;
});

const handleLike = async () => {
  if (isLiking) return;
  isLiking = true;

  // 乐观更新 UI
  isLiked.value = !isLiked.value;
  if (isLiked.value) {
    currentLikes.value += 1;
    uni.vibrateShort({ type: 'light' });
  } else {
    currentLikes.value -= 1;
  }

  try {
    // 调用点赞接口 (点赞和取消点赞为同一个接口)
    await topicItemCommentLike({ commentId: props.comment.id });
    // 记录点赞事件
    uni.$trackRecord({
      eventName: 'like_comment',
      eventType: 'like',
      elementId: `comment_${props.comment.id}`,
      elementContent: `评论项_${props.comment.name}`,
      customParams: {
        comment: props.comment.content,
        page: 'rating_detail'
      }
    });
    uni.showToast({ title: '操作成功', icon: 'success' });
  } catch (error) {
    // 接口调用失败，回滚 UI 状态
    isLiked.value = !isLiked.value;
    if (isLiked.value) {
      currentLikes.value += 1;
    } else {
      currentLikes.value -= 1;
    }
    uni.showToast({ title: '操作失败', icon: 'none' });
  } finally {
    isLiking = false;
  }
};
</script>

<style lang="scss" scoped>
.comment-item {
  display: flex;
  padding: 32rpx 0;
  position: relative;
  transition: background-color 0.2s;
}

.comment-item:active {
  background-color: #fafbfe;
}

.comment-item::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 104rpx; /* 头像宽度(80) + 间距(24) */
  right: 0;
  height: 2rpx;
  background-color: #f0f2f7;
}

.c-avatar {
  width: 80rpx;
  height: 80rpx;
  border-radius: 50%;
  margin-right: 24rpx;
  background-color: #f5f6fa;
  flex-shrink: 0;
  border: 2rpx solid #f0f2f7;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.03);
}

.c-content-wrap {
  flex: 1;
}

.c-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 8rpx;
}

.c-user-info {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
}

.c-name {
  font-size: 30rpx;
  font-weight: 700; /* 减轻字重，显得更精致 */
  color: #1a1a1a;
  margin-right: 12rpx;
  font-family: -apple-system, BlinkMacSystemFont, 'Helvetica Neue', Helvetica, Segoe UI, Arial, Roboto, sans-serif;
}

.c-badge {
  display: flex;
  align-items: center;
  background: linear-gradient(90deg, #f5f6fa, #f0f2f7);
  padding: 4rpx 16rpx;
  border-radius: 100rpx;
  margin-left: 12rpx;
}

.b-icon { font-size: 20rpx; margin-right: 6rpx; }
.b-text { font-size: 20rpx; color: #666; font-weight: bold; }

.c-like {
  display: flex;
  align-items: center;
  color: #999;
  font-size: 24rpx;
  font-weight: 600;
  padding: 8rpx;
  margin: -8rpx; /* 增加点击区域 */
}

.like-icon {
  width: 32rpx;
  height: 32rpx;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23999' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3'%3E%3C/path%3E%3C/svg%3E");
  background-size: cover;
  margin-right: 8rpx;
  transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.like-icon.is-liked {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%23ff4d4f' stroke='%23ff4d4f' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3'%3E%3C/path%3E%3C/svg%3E");
  transform: scale(1.15);
}

.liked-text {
  color: #ff4d4f;
}

.c-like:active .like-icon {
  transform: scale(0.9);
}

.c-time {
  font-size: 22rpx;
  color: #a1a1a1;
  display: block;
  margin-bottom: 16rpx;
}

.c-text {
  font-size: 30rpx;
  color: #2c2c2c;
  line-height: 1.65;
  word-break: break-all;
}
</style>