<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" v-for="reply in data.details">
      <show-more :key="reply.id">
        <div v-html="reply.comment"></div>
      </show-more>
    </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;">
          <v-editor v-model="comment"></v-editor>
        </div>
        <el-button 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 } },
  emits: ['update'],
  components: { VEditor, showMore },
  data() {
    return {
      showComment: false,
      comment: ''
    }
  },
  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-tools {
  display: flex;
  flex-direction: column;
  // align-items: flex-end;
  gap: 10px;
}
</style>
