<template>
  <div class="flex radius-base overflow-hidden message-list-modal">
    <div class="message-list p-base scroll-y-auto">
      <div
        v-for="message in messageList"
        :key="message.id"
        class="radius-base fill-blank p-base message-item"
        :class="{ active: currentMessage?.sendUserId === message.sendUserId }"
        @click="checkMessage(message)"
      >
        <div class="flex items-center m-b-base message-title">
          <div class="message-send-user">{{ message.sendUserName }}</div>
          <div class="m-l-auto message-send-time">{{ dayjs(message.sendTime).format('HH:mm') }}</div>
        </div>
        <pre class="message-content" v-html="message.content"></pre>
      </div>
    </div>
    <div class="radius-base message-info-container">
      <div class="flex direction-column message-info">
        <div class="flex items-center p-base message-info-header">
          <div class="flex items-center send-user">
            <span class="m-r-mini">发件人</span>
            <span class="radius-base user-name">{{ currentMessage?.sendUserName }}</span>
          </div>
          <div class="grid pointer m-l-auto close-icon" @click="$emit('close')">
            <el-icon><close /></el-icon>
          </div>
        </div>
        <div class="flex-1 overflow-hidden p-base">
          <pre
            class="full-h radius-base p-extra-base scroll-y-auto message-info-content"
            @click="onContentClick"
            v-html="currentMessage?.content"
          ></pre>
        </div>
        <div class="p-base flex items-center justify-end">
          <el-button size="small" plain :disabled="!currentMessage" @click="toggleHistory">历史消息</el-button>
          <el-button size="small" type="primary" :disabled="!currentMessage" @click="onReply">回复</el-button>
        </div>
      </div>
      <message-history v-if="showHistory" :send-user-id="currentMessage?.sendUserId"></message-history>
    </div>
  </div>
  <image-preview v-model="previewModalVisible" :url="paperPath"></image-preview>
</template>

<script setup lang="ts" name="MessageList">
/** 消息列表*/
import { ref, watch } from 'vue'
import { ElButton, ElIcon } from 'element-plus'
import dayjs from 'dayjs'
import { Close } from '@element-plus/icons-vue'
import useFetch from '@/hooks/useFetch'
import MessageHistory from '@/components/shared/message/MessageHistory.vue'
import ImagePreview from '../ImagePreview.vue'

import type { ExtractApiResponse } from '@/api/api'

type MessageType = ExtractArrayValue<ExtractApiResponse<'getMessageList'>>

const emits = defineEmits(['close', 'change-type', 'reply'])

/** 图片预览 */
const previewModalVisible = ref<boolean>(false)

/** 图片路径 */
const paperPath = ref<string>('')

const showHistory = ref<boolean>(false)

const { fetch: getMessageList, result: messageList } = useFetch('getMessageList')

const currentMessage = ref<MessageType>()

watch(currentMessage, () => {
  if (currentMessage.value) {
    useFetch('handleReadMessage').fetch({ id: currentMessage.value.id })
  }
})

const checkMessage = (message: MessageType) => {
  currentMessage.value = message
}

const toggleHistory = () => {
  showHistory.value = !showHistory.value
}

const onReply = () => {
  if (currentMessage.value) {
    emits('change-type', 'send')
    emits('reply', currentMessage.value.sendUserId)
  }
}

const onContentClick = (e: Event) => {
  const target = e.target as HTMLButtonElement
  const path = target.getAttribute('data-path')
  if (path) {
    previewModalVisible.value = true
    paperPath.value = path
  }
}

getMessageList().then((result) => (currentMessage.value = result?.[0]))
</script>

<style scoped lang="scss">
.message-list-modal {
  width: 880px;
  background-color: transparent;
  .message-list {
    width: 280px;
    height: 446px;
    background: #fafafa;
    box-shadow: 0px 6px 6px 0px rgba(0, 0, 0, 0.1);
    .message-item {
      &.active {
        background-color: $color--primary;
        color: $color--white;
      }
      &:not(.active) {
        .message-title {
          color: $NormalColor;
          .message-send-time {
            color: $RegularFontColor;
          }
        }
        .message-content {
          color: $RegularFontColor;
        }
      }
      .message-title {
        font-size: $BaseFont;
        .message-send-time {
          font-size: $SmallFont;
          font-weight: 400;
        }
      }
      .message-content {
        font-weight: 400;
        word-break: break-all;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        font-size: $SmallFont;
      }
    }
  }
  .message-info-container {
    width: 600px;
    .message-info {
      height: 446px;
      background-color: $color--white;
      .message-info-header {
        border-bottom: $OnePixelLine;
        .send-user {
          font-size: $SmallFont;
          color: $RegularFontColor;
          .user-name {
            display: inline-block;
            width: 160px;
            padding: 10px 12px;
            border: $OnePixelLine;
          }
        }
        .close-icon {
          width: 20px;
          height: 20px;
          place-items: center;
          font-size: 18px;
          color: $RegularFontColor;
          &:hover {
            color: $NormalColor;
          }
        }
      }
      .message-info-content {
        border: $OnePixelLine;
        font-size: $SmallFont;
      }
    }
  }
}
</style>
