<template>
  <view class="comment-list">
    <view 
      v-for="comment in comments" 
      :key="comment.id"
      class="comment-item"
    >
      <!-- 用户信息 -->
      <view class="user-info">
        <image 
          :src="comment.userEntity.avatar || defaultAvatar" 
          class="avatar" 
          mode="aspectFill"
        />
        <view class="user-detail">
          <text class="username">{{ getUserName(comment.userEntity) }}</text>
        </view>
      </view>
      
      <!-- 评论内容 -->
      <view class="content" v-html="comment.content" />
      
      <!-- 操作按钮 -->
      <view class="actions">
        <view class="action-btn" @click="handleLike(comment)">
          <wd-icon 
            :name="comment.isLike ? 'heart-filled' : 'heart'"
            :color="comment.isLike ? 'red' : 'gray'"
            size="18px"
          />
          <text>{{ comment.likeCount || 0 }}</text>
        </view>
        <view v-if="comment.pid === 0" class="action-btn" @click="handleReply(comment)">
          <wd-icon name="chat" size="16px" />
          <text>{{ comment.children?.length || 0 }}</text>
        </view>
        <view class="action-btn" @click="handleDelete(comment)">
          <text class="time">{{ comment.createTime }}</text>
        </view>
        <view 
          v-if="isMyComment(comment)"
          class="action-btn delete" 
          @click="handleDelete(comment)"
        >
          <wd-icon name="delete-outline" size="16px" />
          <text>{{ $t('common.delete') }}</text>
        </view>
      </view>
      
      <!-- 子评论 -->
      <view v-if="comment.children?.length" class="children-comments">
        <CommentList 
          :comments="comment.children"
          @like="handleLike"
          @reply="handleReply"
          @delete="handleDelete"
        />
      </view>
    </view>
    
    <!-- 加载更多 -->
    <view v-if="hasMore" class="load-more" @click="handleLoadMore">
      <text>{{ $t('common.loadMore') }}</text>
    </view>
  </view>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useUserStore } from '@/stores/user'
import type { IComment } from '@/types/book'
import { useI18n } from 'vue-i18n'

const { t } = useI18n()

interface Props {
  comments: IComment[]
  hasMore?: boolean
}

const props = withDefaults(defineProps<Props>(), {
  hasMore: false
})

const emit = defineEmits<{
  like: [comment: IComment]
  reply: [comment: IComment]
  delete: [comment: IComment]
  loadMore: []
}>()

const userStore = useUserStore()
const defaultAvatar = '/static/icon/morenAvavter.png'

const isMyComment = (comment: IComment) => {
  return comment.userEntity.id === userStore.id
}

const getUserName = (user: any) => {
  return user.nickname || user.name || 'TA'
}

const formatTime = (time: string) => {
  if (!time) return ''
  // 简单的时间格式化，可以根据需要优化
  try {
    const date = new Date(time)
    const now = new Date()
    const diff = now.getTime() - date.getTime()
    
    const minute = 60 * 1000
    const hour = 60 * minute
    const day = 24 * hour
    
    if (diff < minute) {
      return t('common.justNow')
    } else if (diff < hour) {
      return `${Math.floor(diff / minute)}${t('common.minutesAgo')}`
    } else if (diff < day) {
      return `${Math.floor(diff / hour)}${t('common.hoursAgo')}`
    } else if (diff < 7 * day) {
      return `${Math.floor(diff / day)}${t('common.daysAgo')}`
    } else {
      return `${date.getMonth() + 1}-${date.getDate()}`
    }
  } catch (e) {
    return time
  }
}

const handleLike = (comment: IComment) => emit('like', comment)
const handleReply = (comment: IComment) => emit('reply', comment)
const handleDelete = (comment: IComment) => emit('delete', comment)
const handleLoadMore = () => emit('loadMore')
</script>

<style lang="scss" scoped>
.comment-list {
  .comment-item {
    padding: 20rpx 0;
    border-bottom: 1rpx solid #f0f0f0;
    
    .user-info {
      display: flex;
      align-items: center;
      margin-bottom: 15rpx;
      
      .avatar {
        width: 60rpx;
        height: 60rpx;
        border-radius: 50%;
        margin-right: 15rpx;
      }
      
      .user-detail {
        flex: 1;
        
        .username {
          display: block;
          font-size: 28rpx;
          color: #333;
          font-weight: 500;
          line-height: 32rpx;
        }
        
        .time {
          display: block;
          font-size: 24rpx;
          color: #999;
          line-height: 28rpx;
          margin-top: 5rpx;
        }
      }
    }
    
    .content {
      font-size: 28rpx;
      color: #333;
      line-height: 40rpx;
      margin-bottom: 15rpx;
      word-break: break-all;
      
      ::v-deep img {
        width: 45rpx !important;
        height: 45rpx !important;
        vertical-align: middle;
      }
    }
    
    .actions {
      display: flex;
      align-items: center;
      gap: 30rpx;
      
      .action-btn {
        display: flex;
        align-items: center;
        gap: 8rpx;
        color: #666;
        
        text {
          font-size: 24rpx;
        }
        
        &.delete text {
          color: #ff4703;
        }
      }
    }
    
    .children-comments {
      margin-top: 20rpx;
      margin-left: 75rpx;
      padding-left: 20rpx;
      border-left: 2rpx solid #f0f0f0;
      
      .comment-item {
        border-bottom: none;
        padding: 15rpx 0;
        
        &:last-child {
          padding-bottom: 0;
        }
      }
    }
  }
  
  .load-more {
    padding: 30rpx 0;
    text-align: center;
    
    text {
      font-size: 28rpx;
      color: #999;
    }
  }
}
</style>
