<template>
  <div id="chat">
    <div id="contents">
      <div
        class="chat-item flex justify-start items-start"
        v-for="(i, index) in chatList"
        :key="index"
      >
        <div class="chat-msg">
          <!-- {{ user_info }} -->
          <!-- <span :class="{ my: i.UserId == user_info.UserId }" class="chat-name">
            {{ i.Nickname }} &nbsp;
          </span> -->
          <span class="chat-name" :class="{ my: isMe(i.UserId) }"> {{ i.Nickname }} &nbsp; </span>
          <span class="chat-content"> {{ i.text }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
  // import { propTypes } from "/@/utils/propTypes";

  import { computed, watch } from 'vue';
  import { ChatContentType } from '/@/store/modules/rtc';
  import { propTypes } from '/@/utils/propTypes';
  const props = defineProps({
    currentUser: propTypes.string.def(''),
    chatList: {
      type: Array as PropType<ChatContentType[]>,
      default: () => [],
    },
  });

  const isMe = computed(() => (currentUser: string) => props.currentUser === currentUser);

  const chatAutoScroll = () => {
    let el = document.getElementById('chat');
    let client_h = document.getElementById('chat')?.clientHeight;
    let all = document.getElementById('contents')?.clientHeight;
    // console.log("chatAutoScroll", client_h, all);
    el?.scrollTo(0, (client_h || 0) + (all || 0));
    console.log('拉底');
  };

  watch(
    () => props.chatList,
    (_) => {
      setTimeout(chatAutoScroll, 100);
    },
    {
      deep: true,
      immediate: true,
    },
  );
</script>

<style scoped lang="scss">
  #chat {
    // width: 7.03rem;
    width: 50%;
    max-width: 4rem;
    max-height: 4.58rem;
    overflow: auto;
    position: fixed;
    left: 0.44rem;
    bottom: 2.83rem;
    .chat-item {
      text-align: left;
    }
    .chat-msg {
      width: auto;
      border-radius: 0.44rem;
      background: rgba(0, 0, 0, 0.3);
      padding: 0.17rem 0.28rem;
      // padding: 0.07rem 0.28rem;
      box-sizing: border-box;
      overflow: hidden;
      display: inline-block;
      // margin-bottom: 0.17rem;
      margin-bottom: 0.1rem;
      .chat-name {
        color: #70bbff;
        font-size: 0.28rem;
        &.my {
          color: #ff9a6a;
        }
      }
      .chat-content {
        color: #fff;
        font-size: 0.28rem;
        // word-break: break-all;
        white-space: pre-wrap;
        word-wrap: break-word;
        text-align: justify;
      }
    }
    &::-webkit-scrollbar {
      display: none;
    }
  }
</style>
