<template>
  <el-scrollbar class="chatContent">
    <div class="chat-wrapper" v-for="item in list" :key="item.id">
      <div class="chat-text" :class="{right:item.userType == '0'}">
        <img class="avatar" :src="avatar(item)" alt=""/>
        <div class="content" v-text="item.content"/>
      </div>
    </div>
  </el-scrollbar>
</template>

<script>

export default {
  name: "chatContent",
  props: {
    list: {default: () => []}
  },
  computed: {
    lastMessage: v => v.list.at(-1)?.content
  },
  watch: {
    lastMessage() {
      this.scrollBottom()
    }
  },
  methods: {
    scrollBottom() {
      const content = this.$el.querySelector(".el-scrollbar__wrap")
      if (content) {
        content.scrollTop = content.scrollHeight - content.clientHeight
      }
    },
    avatar(item) {
      return item.avatar || (item.userType == '0' ? 'https://cdn.sinoecare.com/i/2024/06/17/666fdb275be82.png' :
          'https://cdn.sinoecare.com/i/2024/06/04/665ec6f5ef213.png')
    }
  },
  mounted() {
    this.scrollBottom()
  }
}
</script>

<style lang="scss" scoped>
.chatContent {
  :deep(.el-scrollbar__wrap) {
    overflow-x: hidden;
  }

  .chat-text {
    display: flex;
    gap: 12px;
    font-size: 14px;
    color: #333333;
    margin-bottom: 8px;
    padding-right: 14px;

    .content {
      position: relative;
      max-width: 220px;
      padding: 8px;
      border-radius: 4px;
      background: #F3F5F7;
      word-break: break-all;

      &:before {
        content: " ";
        width: 0;
        height: 0;
        border-top: 6px solid transparent;
        border-bottom: 6px solid transparent;
        border-right: 6px solid #F3F5F7;
        position: absolute;
        left: -6px;
        top: 6px;
      }
    }

    &.right {
      flex-direction: row-reverse;

      .content {
        background: #CCE2FF;

        &:before {
          left: unset;
          right: -6px;
          border-left: 6px solid #CCE2FF;
          border-right: none;
        }
      }
    }
  }

  .avatar {
    width: 36px;
    height: auto;
    align-self: flex-start;
  }

  .chat-friend {
    width: 100%;
    float: left;
    margin-bottom: 20px;
    position: relative;
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    align-items: flex-start;
  }

  .chat-me {
    width: 100%;
    float: right;
    margin-bottom: 20px;
    position: relative;
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    align-items: flex-end;
  }
}
</style>
