<template>
	<view class="tabs-content" :style="{ paddingBottom : `${props.paddingBottom || 0}rpx` }">
		<view v-for="(comment, index) in sortedCommentList" :key="index" class="comment-list-item">
			<view class="comment-list-left">
				<image :src="`${FILE_URL}/weixin.png`" class="comment-list-avator"></image>
			</view>
			<view class="comment-list-right">
				<view style="margin-bottom: 15rpx;">
					<text class="comment-list-username">{{ comment.username }}</text>
					<text class="comment-list-moment">{{ formatTime(comment.commentTime) }}</text>
				</view>
				<view>{{ comment.content }}</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		defineProps,
		computed
	} from 'vue'
	import configService from '@/utils/baseurl.js'
	const FILE_URL = configService.FILE_URL;
	const props = defineProps({
	  paddingBottom: {
	    type: Number,
	    required: false,
	    default: 10
	  },
	  commentList: {
	    type: Array,
	    required: false,
	    default: () => []
	  },
	});

	function formatDate(dateStr) {
		return dateStr.replace(" ", "T");
	}

	function formatTime(timeString) {
	    const commentDate = new Date(formatDate(timeString));
	    const now = new Date();
	    const diff = now - commentDate;

	    const minutes = Math.floor(diff / 60000);
	    const hours = Math.floor(diff / 3600000);
	    const days = Math.floor(diff / 86400000);

	    if (minutes < 1) { // 修改这里以处理0分钟
	        return "刚刚";
	    } else if (minutes < 60) {
	        return `${minutes}分钟前`;
	    } else if (hours < 24) {
	        return `${hours}小时前`;
	    } else {
	        return commentDate.toISOString().split('T')[0];
	    }
	}


	const sortedCommentList = computed(() => {
		return props.commentList.sort((a, b) =>
			new Date(formatDate(b.commentTime)) - new Date(formatDate(a.commentTime))
		);
	});
</script>

<style lang="scss" scoped>
	.tabs-content{
		padding-top: 10rpx;
	}
	.comment-list-item {
		display: flex;
		padding: 20rpx 0;
		font-size: 28rpx;
		.comment-list-left{
			flex: 0 0 auto;
			padding-right: 20rpx;
			padding-left: 10rpx;
			.comment-list-avator{
				width: 100rpx;
				height: 100rpx;
				border-radius: 50%;
			}
		}
		.comment-list-right{
			flex: 1;
			.comment-list-username{
				padding-right: 25rpx;
				font-size: 32rpx;
				font-weight: bold;
			}
		}
	}
</style>
