<script setup lang="ts">
import type { ExperimentDiscussType } from '../types'
import { ChatLineRound } from '@element-plus/icons-vue'
import Avatar from '@/components/Avatar.vue'

const DiscussCommentPublish = defineAsyncComponent(() => import('./DiscussCommentPublish.vue'))

interface Props {
  data: ExperimentDiscussType
}
defineProps<Props>()

defineEmits<{
  (e: 'update'): void
}>()

const commentVisible = $ref(false)
</script>
<template>
  <div class="discuss-item">
    <div class="discuss-box">
      <div class="discuss-box-user">
        <div class="discuss-box-user__avatar"><Avatar :src="data.sso_user.avatar" /></div>
        <div class="discuss-box-user__content">
          <h3>{{ data.sso_user.real_name || data.sso_user.nickname || data.sso_user.username }}</h3>
          <p>{{ data.created_time }}</p>
        </div>
      </div>
      <div class="discuss-box-main">
        <h3>{{ data.title }}</h3>
        <div class="discuss-box-content" v-html="data.content"></div>
      </div>
      <div class="discuss-box-footer">
        <div class="button-comment" :class="{ 'is-active': commentVisible }" @click="commentVisible = !commentVisible">
          <el-icon><ChatLineRound></ChatLineRound></el-icon>{{ data.reply_count }}
        </div>
      </div>
    </div>
    <template v-if="commentVisible">
      <!-- 我要评论 -->
      <DiscussCommentPublish :data="data" @update="$emit('update')"></DiscussCommentPublish>
      <div class="discuss-comment" v-for="item in data.replies" :key="item.id">
        <div class="discuss-comment__avatar">
          <Avatar :src="item.sso_user.avatar" />
        </div>
        <div class="discuss-comment-content">
          <span class="discuss-comment__username">{{
            item.sso_user.real_name || item.sso_user.nickname || item.sso_user.username
          }}</span
          >：
          <span v-html="item.content"></span>
          <p class="discuss-comment-time">{{ item.created_time }}</p>
        </div>
      </div>
    </template>
  </div>
</template>

<style lang="scss" scoped>
.discuss-item {
  margin-top: 10px;
  padding-right: 15px;
  border-top: 1px solid #e6e6e6;
}
.discuss-box {
  padding: 20px 0 10px;
}
.discuss-box-user {
  display: flex;
  align-items: center;
}
.discuss-box-user__avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  overflow: hidden;
  img {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
}
.discuss-box-user__content {
  flex: 1;
  margin-left: 10px;
  overflow: hidden;
  h3 {
    font-size: 16px;
    font-weight: 500;
    line-height: 20px;
    color: #333333;
  }
  p {
    font-size: 14px;
    font-weight: 400;
    line-height: 20px;
    color: #999999;
  }
}
.discuss-box-main {
  margin-left: 50px;
  h3 {
    padding: 16px 0;
    font-size: 16px;
    font-weight: 500;
    line-height: 20px;
    color: #333333;
  }
}
.discuss-box-content {
  font-size: 16px;
  font-weight: 400;
  line-height: 24px;
  color: #333333;
}
.discuss-box-footer {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  margin-top: 10px;
  .button-comment {
    display: flex;
    align-items: center;
    justify-content: center;
    color: #333333;
    cursor: pointer;
    &.is-active {
      color: var(--main-color);
    }
  }
  .el-icon {
    font-size: 16px;
    margin-left: 10px;
    margin-right: 5px;
  }
}
.discuss-comment {
  display: flex;
  margin-top: 20px;
}
.discuss-comment__avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  overflow: hidden;
  img {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
}
.discuss-comment-content {
  flex: 1;
  margin-left: 10px;
  color: #333;
  overflow: hidden;
}
.discuss-comment__username {
  font-size: 16px;
  line-height: 20px;
  color: var(--main-color);
}
.discuss-comment-time {
  margin-top: 10px;
  font-size: 14px;
  line-height: 20px;
  color: #999999;
  text-align: right;
}
</style>
