<template>
  <div class="course-comment">
    <div v-show="comments.length" class="item" v-for="(item, index) in comments" :key="index">
      <img class="img" :src="item.imageUrl || 'https://file.yunqueyi.com/File/doctor_default.png'" alt="头像" @error="onAvatarError" />
      <div class="info-box">
        <div class="info-user">
          <div class="name">{{ item.doctorName }}</div>
          <div class="date">{{ item.createTime }}</div>
        </div>
        <div class="message">{{ item.message }}</div>
        <div class="reply-box" v-if="item.len">
          <div class="reply-item" v-for="child in item.cList" :key="child.qaId">
            <div class="reply-user">
              <div class="name">{{ child.doctorName }}</div>
              <div class="date">{{ child.createTime }}</div>
            </div>
            <div class="message">{{ child.message }}</div>
          </div>
          <div class="show-all" v-if="item.len > 2" @click="spreadAll(index)">
            {{ item.showAll ? '收起回复' : `查看全部${item.len}条回复` }}
            <img class="show-arrow" :class="{'show-arrow-up': item.showAll}" src="~@/images/course/arrow_down.png" alt="箭头" />
          </div>
        </div>
      </div>
    </div>
    <div v-show="!comments.length">
      <img class="img-big" src="~@/images/course/no_comment.png" alt="" />
    </div>    
    <div class="go-app" @click="download">
      <span>打开App，查看更多讨论内容</span>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    list: {
      type: Array,
      default() {
        return []
      }
    },
    download: {
      type: Function
    }
  },
  data() {
    return {
      comments: []
    };
  },
  watch: {
    list(val) {
      this.dealList(val);
    }
  },
  mounted() {
    this.dealList(this.list);
  },
  methods: {
    dealList(list) {
      this.comments = list.map(item => {
        const aList = item.aList || [];
        return {
          ...item,
          aList,
          len: aList.length,
          cList: aList.slice(0, 2),
          showAll: false,
        }
      })
    },
    spreadAll(index) {
      const { ...item } = this.comments[index];
      if (item.showAll) {
        item.showAll = false;
        item.cList = item.aList.slice(0, 2);
      } else {
        item.showAll = true;
        item.cList = item.aList;
      }
      this.comments.splice(index, 1, item);
    },
  }
};
</script>

<style  lang="less" scoped>
.course-comment {
  padding: 0 15px;
  .item{
    position: relative;
    display: flex;
    font-size: 14px;
    padding: 20px 0;
    &::after {
      position: absolute;
      content: "";
      left: 0;
      bottom: 0;
      width: 200%;
      height: 0;
      transform: scale(0.5);
      transform-origin: left center;
      border-bottom: 1px solid #F0F0F0;
    }
  }
  .img-big {
    display: block;
    width: 100px;
    margin: 40px auto 5px;
  }
  .img{
    display: block;
    width: 32px;
    height: 32px;
    margin-right: 10px;
  }
  .info-box{
    flex: 1;
    width: 100px;
  }
  .info-user, .reply-user{
    display: flex;
    padding-bottom: 10px;
    .name{
      color: #676869;
      flex: 1;
      width: 80px;
    }
    .date{
      color: #979899;
      font-size: 12px;
    }
  }
  .message{
    line-height: 22px;
    text-align: justify;
    word-break: break-all;
  }
  .reply-box{
    margin-top: 15px;
    padding: 12px 12px 0;
    background-color: #F8F9FA;
  }
  .reply-item{
    padding-bottom: 20px;
  }
  .show-all{
    position: relative;
    color: #676869;
    text-align: right;
    font-size: 12px;
    padding: 10px 18px 20px 0;
  }
  .show-arrow{
    position: absolute;
    right: 0;
    top: 12px;
    width: 12px;
    height: 12px;
    transition: all 0.3s;
  }
  .show-arrow-up{
    transform: rotate(180deg);
  }
  .go-app{
    color: #449284;
    font-size: 14px;
    line-height: 16px;
    text-align: center;
    padding: 20px 0;
    span{
      display: inline-block;
      padding-right: 20px;
      background: url('~@/images/course/array-right.png') no-repeat right center;
      background-size: 14px auto;
    }
  }
}
</style>
