<template>
  <view v-if="message && user" class="message-box">
    <view v-if="message.messageType === MessageType.text" class="message">
      {{ user.name }}:{{ message.content }}
    </view>
    <view v-if="message.messageType === MessageType.image" class="message-img">
      <text>{{ user.name }}:</text>
      <image alt="图片" :src="message.extend?.url" style="height: 60rpx;width: 60rpx" />
    </view>
    <view v-if="message.messageType === MessageType.file" class="message">
      {{ user.name }}:{{ getFileName(message.extend?.fileName) }}
    </view>
    <view v-if="message.messageType === MessageType.video" class="message">
      {{ user.name }}:<text class="cuIcon cuIcon-video"></text>{{getFileName(message.extend?.url)}}
    </view>
    <view v-if="message.messageType === MessageType.voice" class="message">
      {{ user.name }}:<text class="cuIcon-sound"></text>{{getFileName(message.extend?.time)}}
    </view>
    <text class="cuIcon cuIcon-close" @tap="closeQuote"></text>
  </view>
</template>
<script setup lang="ts">
import { computed } from 'vue'
import MessageType from '../utils/MessageType'
import { storeToRefs } from 'pinia'
import { useUserStore } from '@/store/userStore'
import {getFileName}  from '@/utils/FileUtils'
import type Message from "@/mode/Message";
import {useChatStore} from "@/store/chatStore";

const { userMap } = storeToRefs(useUserStore())
interface IProps{
  message:Message
}
const props = defineProps<IProps>();

const user = computed(() => {
  if(props.message){
    return userMap.value.get(props.message.fromId)
  }
  return null;
})

const closeQuote = () => {
  useChatStore().setQuoteMessage(undefined)
}
</script>
<style scoped lang="scss">
.message-box {
  cursor: pointer;
  flex: 1;
  overflow: hidden;
  background-color: #e9e9e9;
  margin-top: 10px;
  padding: 5px 11px;
  color: #999999;
  font-size: 24rpx;
  width: 60vw;
  display: flex;
  align-items: center;
  .message {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    display: inline-block;
    max-width: 100%;
    flex: 1;
  }
  .message-img {
    line-height: 2;
    white-space: nowrap;
    display: inline-flex;
    padding: 0 11px;
    max-width: 620px;
  }
}
</style>
