<template>
  <div class="flex radius-base overflow-hidden message-list-modal">
    <slot></slot>
    <!-- <div class="message-list p-base scroll-y-auto">
      <template v-if="!!(messageList || []).length">
        <div
          v-for="message in messageList"
          :key="message.id"
          class="radius-base fill-blank p-base m-b-mini relative message-item"
          :class="{ active: currentMessage?.sendUserId === message.sendUserId, 'un-read': message.unReadCount > 0 }"
          @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">{{ transHtmlContent(message.content) }}</pre>
        </div>
      </template>
      <div v-else class="none-msg-box">
        <div class="center-box">
          <svg-icon name="none_message" style="font-size: 120px"></svg-icon>
          <p class="tip">暂无消息</p>
        </div>
      </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 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 ref="historyMsgWrap" class="history-box scroll-auto">
            <div v-for="(message, index) in historyReverse" :key="index" class="m-b-base">
              <div class="m-b-mini message-header">
                <span class="m-r-base user-name">{{ message.sendUserName }}</span>
                <span class="message-time">{{ message.sendTime }}</span>
              </div>
              <div class="history-message-info-content" @click="onContentClick" v-html="message.content"></div>
            </div>
          </div>
        </div>
        <div class="p-base" style="padding-top: 0">
          <div class="p-base radius-base overflow-hidden msg-content-box">
            <div style="height: calc(100% - 18px)" class="scroll-y-auto">
              <content-edit-able
                v-model="messageContent"
                class="full content-edit-able"
                @click="onContentClick"
              ></content-edit-able>
            </div>

            <div class="limit-tip">{{ messageContent.length }} / 2000</div>
          </div>
        </div>
        <div class="p-base flex items-center justify-end">
          <!-- <el-button size="small" type="primary" :disabled="!currentMessage" @click="onReply">回复</el-button> -->
          <!-- <el-button size="small" plain :disabled="!currentMessage" @click="toggleHistory">历史消息</el-button> -->
          <el-button size="small" type="primary" :disabled="!allowSend" :loading="sendLoading" @click="onSendMessage"
            >发送</el-button
          >
        </div>
      </div>
      <!-- <message-history v-if="showHistory" :send-user-id="currentMessage?.sendUserId"></message-history> -->
    </div>
  </div>
  <image-preview
    v-model="previewModalVisible"
    resize-key="can-resize22"
    :url="paperPath"
    :is-big="true"
  ></image-preview>
</template>

<script setup lang="ts" name="MessageList">
/** 消息列表*/
import { ref, watch, onUnmounted, computed, nextTick, unref } from 'vue'
import { ElButton, ElIcon, ElMessage } from 'element-plus'
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 useMainStore from '@/store/main'
import { setLastMsgs } from '@/hooks/useMessageLoop'
import bus from '@/utils/bus'
import type { ExtractApiResponse } from '@/api/api'
import ContentEditAble from '@/components/common/ContentEditAble.vue'
type MessageType = ExtractArrayValue<ExtractApiResponse<'getMessageList'>>
const historyMsgWrap = ref()
const mainStore = useMainStore()
const emits = defineEmits(['close', 'change-type', 'reply'])
const { fetch: getMessageHistory, result: history, loading } = useFetch('getMessageHistory')
const historyReverse = computed(() => {
  return ((history.value as any) || []).reverse()
})
/** 图片预览 */
const previewModalVisible = ref<boolean>(false)
/** 消息内容 */
const messageContent = ref<string>('')
/** 图片路径 */
const paperPath = ref<string>('')

const showHistory = ref<boolean>(false)

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

const currentMessage = ref<MessageType>()
function scrollToBottom() {
  let scrollHeight = historyMsgWrap.value.scrollHeight
  if (scrollHeight > 200) {
    historyMsgWrap.value.scrollTop = scrollHeight - 200 + 100
  }
}
watch(historyReverse, (newVal: any, oldVal: any) => {
  let oldV = unref(oldVal)
  if ((oldV || []).length == 0) {
    nextTick(() => {
      scrollToBottom()
    })
  }
})
watch(currentMessage, (newVal: any, oldVal: any) => {
  if (currentMessage.value) {
    useFetch('handleReadMessage')
      .fetch({ id: currentMessage.value.id })
      .then(() => {
        if (currentMessage.value?.unReadCount) {
          // currentMessage.value.unReadCount--
          currentMessage.value.unReadCount = 0
        }
        if (currentMessage.value?.sendUserId) {
          getMessageHistory({ sendUserId: currentMessage.value.sendUserId }).then(() => {
            let newV = unref(newVal)
            let oldV = unref(oldVal)
            if (newV.id != oldV.id) {
              setTimeout(scrollToBottom, 1)
            }
          })
        }
      })
  }
})
const allowSend = computed<boolean>(() => {
  return !!(messageContent.value && currentMessage.value?.sendUserId)
})
const { fetch: sendMessage, loading: sendLoading } = useFetch('sendMessage')
/** 发送消息 */
const onSendMessage = async () => {
  try {
    if (messageContent.value.length > 2000) {
      return ElMessage.error('输入内容过长')
    }

    await sendMessage({
      content: messageContent.value,
      receiveUserIds: [currentMessage.value?.sendUserId as number],
    })
    ElMessage.success('发送成功')
    messageContent.value = ''
    if (currentMessage.value?.sendUserId) {
      getMessageHistory({ sendUserId: currentMessage.value.sendUserId }).then(() => {
        setTimeout(scrollToBottom, 1)
      })
    }
    // emit('close')
  } catch (error) {
    console.error(error)
  }
}
// const checkMessage = (message: MessageType) => {
//   currentMessage.value = message
// }
bus.on('clickChangeMsg', (msg: any) => {
  currentMessage.value = msg
})

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

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

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]
//   if (result[0]) {
//     bus.emit('getCurMsg', result[0])
//   }
// })
// watch(
//   () => mainStore.newMsgs,
//   () => {
//     getMessageList().then((res) => {
//       if (res && Array.isArray(res)) {
//         let find = res.find((item) => item.sendUserId == currentMessage.value?.sendUserId)
//         if (!!find) {
//           currentMessage.value = find
//         }
//       }
//     })
//   }
// )
onUnmounted(() => {
  useFetch('getUnReadMessage')
    .fetch()
    .then((res) => {
      setLastMsgs(res)
    })
})
</script>

<style scoped lang="scss">
.message-list-modal {
  width: 750px;
  background-color: transparent;
  border-top-left-radius: 0;
  border-bottom-left-radius: 0;
  .message-info-container {
    // width: 600px;
    width: calc(100% - 260px);
    position: relative;
    .history-box {
      height: 200px;
      border: 1px solid #eee;
      border-radius: 4px;
      padding: 15px;
      .message-header {
        color: $color--primary;
      }
      .history-message-info-content {
        white-space: pre-wrap;
      }
    }
    .msg-content-box {
      height: 100px;
      font-size: 12px;
      border: 1px solid #0091ff;
      .limit-tip {
        text-align: right;
        height: 18px;
        padding-top: 5px;
      }
    }
    .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: 0 12px;
            border: $OnePixelLine;
            height: 38px;
            line-height: 36px;
          }
        }
        .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;
        white-space: pre-wrap;
      }
    }
  }
}
</style>
