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

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

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

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

const commentDialogVisible = $ref(false)
const commentVisible = $ref(false)
const commentButtonText = $computed(() => (commentVisible ? '收起' : '展开'))
</script>
<template>
  <div class="discuss-item">
    <div class="discuss-box">
      <div class="discuss-box-user">
        <img :src="data.sso_user.avatar" />
        <p>{{ data.sso_user.real_name || data.sso_user.nickname || data.sso_user.username }}</p>
      </div>
      <div class="discuss-box-main">
        <div class="discuss-box-header">
          <p class="discuss-box-time">{{ data.created_time }}</p>
          <div class="tools">
            <el-button size="small" type="primary" plain round @click="commentVisible = !commentVisible">
              {{ commentButtonText }}
            </el-button>
            <el-icon><ChatLineRound></ChatLineRound></el-icon>{{ data.reply_count }}
          </div>
        </div>
        <h3>{{ data.title }}</h3>
        <div class="discuss-box-content" v-html="data.content"></div>
      </div>
    </div>
    <template v-if="commentVisible">
      <div class="discuss-box discuss-comment" v-for="item in data.replies" :key="item.id">
        <div class="discuss-box-user">
          <img :src="item.sso_user.avatar" />
          <p>{{ item.sso_user.real_name || item.sso_user.nickname || item.sso_user.username }}</p>
        </div>
        <div class="discuss-box-main">
          <p class="discuss-box-time">{{ item.created_time }}</p>
          <div class="discuss-box-content" v-html="item.content"></div>
        </div>
      </div>
    </template>
    <el-row justify="end" style="margin-top: 10px">
      <el-button round type="primary" size="small" @click="commentDialogVisible = true">我要评论</el-button>
    </el-row>
  </div>
  <!-- 我要评论 -->
  <DiscussCommentAddDialog
    :data="data"
    v-model="commentDialogVisible"
    @update="$emit('update')"
    v-if="commentDialogVisible"
  ></DiscussCommentAddDialog>
</template>

<style lang="scss" scoped>
.discuss-item {
  margin-top: 10px;
  padding: 10px 0;
  border-top: 1px dashed #ccc;
}
.discuss-box {
  display: flex;
  margin: 10px 0;
  padding: 10px;
  font-size: 12px;
  color: var(--main-color);
  border-radius: 10px;
  border: 1px solid var(--main-color);
  &.discuss-comment {
    margin-left: 40px;
    color: #016fa0;
    border: 1px solid #016fa0;
  }
}
.discuss-box-user {
  img {
    width: 50px;
    height: 50px;
    border-radius: 50%;
    overflow: hidden;
    object-fit: cover;
  }
  p {
    text-align: center;
  }
}
.discuss-box-main {
  flex: 1;
  padding-left: 10px;
  h3 {
    font-size: 14px;
    font-weight: 500;
  }
}
.discuss-box-content {
  padding: 5px 0;
  color: #555;
}
.discuss-box-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  .tools {
    display: flex;
    align-items: center;
    .el-icon {
      font-size: 16px;
      margin-left: 10px;
      margin-right: 5px;
      color: #333333;
    }
  }
}
</style>
