<template>
  <div>
    <div class="chapter-work-comment-and-reply" v-if="showFirst">
      <show-more>
        <div v-html="data.comment"></div>
      </show-more>
    </div>
    <div class="chapter-work-comment-and-reply-list">
      <div
        class="chapter-work-comment-and-reply"
        :class="{ 'is-mine': studentId === reply.student_id, 'is-other': studentId !== reply.student_id }"
        v-for="reply in data.details"
      >
      <div class="chapter-work-comment-and-reply-title">
        <p v-if="studentId === reply.student_id">我的回复：</p>
        <p v-else>{{ title }}的回复：</p>
        <p class="time">{{ data.created_time }}</p>
      </div>
        <show-more :key="reply.id">
          <div v-html="reply.comment"></div>
        </show-more>
      </div>
    </div>
    <div class="chapter-work-comment-and-reply-tools">
      <el-button size="small" @click="showComment = !showComment">回复此评论</el-button>
      <template v-if="showComment">
        <div style="flex:1;width:100%;">
          <v-editor v-model="comment"></v-editor>
        </div>
        <el-button size="small" type="primary" @click="handleSubmit">提交回复</el-button>
      </template>
    </div>
  </div>
</template>

<script>
import showMore from '@/components/showMore'
import VEditor from '@/components/tinymce'
import * as api from '../../api'

export default {
  name: 'ChapterWorkCommentAndReply',
  props: {
    data: { type: Object, default: () => ({}) },
    showFirst: { type: Boolean, default: true },
    title: { type: String, default: '' }
  },
  emits: ['update'],
  components: { VEditor, showMore },
  data() {
    return {
      showComment: false,
      comment: ''
    }
  },
  computed: {
    studentId() {
      return window.G.UserInfo?.student_info?.id
    }
  },
  methods: {
    handleSubmit() {
      api.addChapterHomeworkCommentReply({ comment_id: this.data.id, comment: this.comment }).then((response) => {
        this.$message({ type: 'success', message: '回复成功' })
        this.showComment = false
        this.comment = ''
        this.$emit('update')
      })
    }
  }
}
</script>

<style lang="scss">
.chapter-work-comment-and-reply {
  margin-bottom: 10px;
  padding: 10px;
  border-radius: 8px;
  background-color: #fff;
  font-size: 12pt;
  line-height: initial;
  img {
    max-width: 100%;
  }
}
.chapter-work-comment-and-reply-title {
  font-size: 14px;
  margin-bottom: 10px;
  color: #333;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .time {
    font-size: 12px;
    color: #999;
  }
}
.chapter-work-comment-and-reply-tools {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 10px;
}
</style>
