<template>
  <div class="phone-frame">
    <n-h4 style="display: flex; justify-content: center">{{ title }}</n-h4>
    <n-scrollbar class="chat-container">
      <div class="load-more-trigger" ref="loadMoreTrigger"></div>
      <div class="button">
        <n-button size="tiny" secondary @click="loadingData('up')">
          <n-icon size="15">
            <ArrowUp />
          </n-icon>
          加载更多
        </n-button>
      </div>

      <div class="messages">
        <div v-for="message in messages" :key="message.id" :class="['message', message.sender]">
          <img :src="message.avatar" class="avatar" alt="avatar" />
          <div class="message-content">
            <div v-if="message.sender === 'other'" class="username">{{ message.username }}</div>
            <div v-if="message.sender === 'me'" class="username-right">{{ message.username }}</div>
            <div class="text">{{ message.text }}</div>
            <div class="time">{{ message.time }}</div>
          </div>
        </div>
      </div>
      <div class="button">
        <n-button size="tiny" secondary @click="loadingData('down')">
          <n-icon size="15">
            <ArrowDown />
          </n-icon>
          加载更多
        </n-button>
      </div>
    </n-scrollbar>
  </div>
</template>

<script setup>
  import { ref } from 'vue';
  import { NScrollbar } from 'naive-ui';
  import { ArrowDown, ArrowUp } from '@vicons/ionicons5';

  const props = defineProps({
    title: {
      type: String,
    },
    messages: {
      type: Array,
      required: true,
    },
  });

  const emit = defineEmits(['loadMoreMessages']);

  // 引用滚动容器和加载触发器
  const loadMoreTrigger = ref(null);
  const loading = ref(false);

  function loadingData(flag) {
    emit('loadMoreMessages', flag);
  }

  // 懒加载处理
  const handleScroll = () => {
    const element = event.target;
    if (!loading.value && element.scrollTop === 0) {
      loading.value = true;
      // 滚动到顶部时加载更多
      emit('loadMoreMessages', 'up');
      loading.value = false;
    } else if (
      !loading.value &&
      element.scrollHeight - element.scrollTop - element.clientHeight <= 50
    ) {
      loading.value = true;
      // 接近底部时加载更多
      emit('loadMoreMessages', 'down');
      loading.value = false;
    }
  };
</script>

<style scoped>
  /* 模拟简化的手机界面 */
  .phone-frame {
    width: 400px;
    height: 800px;
    padding: 10px;
    background-color: white;
    display: flex;
    flex-direction: column;
    border-radius: 20px;
  }

  /* 聊天容器样式 */
  .chat-container {
    height: 100%;
    overflow-y: auto;
    display: block !important;
  }

  .button {
    display: flex;
    justify-content: center;
  }

  /* 消息列表 */
  .messages {
    display: flex;
    flex-direction: column;
  }

  /* 单条消息 */
  .message {
    display: flex;
    align-items: flex-start;
    margin: 8px 0;
  }

  /* 头像 */
  .avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    margin-right: 10px;
  }

  /* 消息内容 */
  .message-content {
    max-width: 220px;
    display: flex;
    flex-direction: column;
  }

  /* 其他用户的用户名 */
  .username {
    font-weight: bold;
    font-size: 0.9rem;
    margin-bottom: 4px;
    color: #666;
  }

  /* 我方的用户名 */
  .username-right {
    font-weight: bold;
    font-size: 0.9rem;
    color: #666;
    align-self: flex-end;
    margin-left: 10px;
  }

  /* 文本气泡 */
  .text {
    background-color: #f1f1f1;
    border-radius: 10px;
    padding: 8px;
    color: #333;
  }

  /* 我方的消息样式 */
  .message.me {
    flex-direction: row-reverse;
  }

  .message.me .avatar {
    margin-left: 10px;
    margin-right: 0;
  }

  .message.me .text {
    background-color: #a0e7e5;
  }

  /* 时间戳 */
  .time {
    font-size: 0.75rem;
    color: #888;
    margin-top: 4px;
    align-self: flex-end;
  }
</style>
