<template>
  <!-- <view v-if="notifyText" class="notify-bar">
    <view class="notify-line"></view>
    <view class="notify-text">{{ notifyText }}</view>
    <view class="notify-line"></view>
  </view> -->
  <view class="system-message">
    <view class="system-text">{{ text }}</view>
  </view>
</template>

<script setup>
import { computed } from "vue";

const props = defineProps({
  message: {
    type: Object,
    default: () => ({}),
  },
});

const payload = computed(() => props.message?.payload || {});

// 解析系统消息内容
const systemMessageData = computed(() => {
  try {
    // 尝试从 payload.data 解析系统消息
    if (payload.value.data) {
      const data =
        typeof payload.value.data === "string"
          ? JSON.parse(payload.value.data)
          : payload.value.data;

      if (data.type === "system_message") {
        return data;
      }
    }
  } catch (e) {
    console.error("解析系统消息失败:", e);
  }
  return null;
});

// 解析扩展信息
const extension = computed(() => {
  try {
    if (payload.value.extension) {
      return typeof payload.value.extension === "string"
        ? JSON.parse(payload.value.extension)
        : payload.value.extension;
    }
  } catch (e) {
    console.error("解析扩展信息失败:", e);
  }
  return {};
});

// 显示的文本内容
const text = computed(() => {
  // 优先从系统消息数据中获取文本
  if (systemMessageData.value?.text) {
    return systemMessageData.value.text;
  }

  // 根据消息类型返回默认文本
  if (systemMessageData.value?.messageType) {
    const messageType = systemMessageData.value.messageType;
    switch (messageType) {
      case "consult_pending":
        return "患者已发起咨询申请，请及时接诊";
      case "consult_accepted":
        return "医生已接诊";
      case "consult_rejected":
        return "医生暂时无法接诊";
      case "consult_ended":
        return "问诊已结束";
      case "consult_timeout":
        return "问诊已超时";
      case "consult_reopened":
        return "会话已重新开启";
      default:
        return systemMessageData.value.content || "[系统消息]";
    }
  }

  // 兼容旧格式：从 extension 中获取
  if (extension.value.patient) {
    return extension.value.patient;
  }

  // 兼容旧格式：直接从 payload.data 获取
  if (payload.value.data && typeof payload.value.data === "string") {
    // 如果 data 不是 JSON 格式，直接显示
    try {
      JSON.parse(payload.value.data);
    } catch {
      return payload.value.data;
    }
  }

  return "[系统消息]";
});

// 通知文本（红色提示）
const notifyText = computed(() => {
  // 从扩展信息中获取通知文本
  if (extension.value.notifyText) {
    return extension.value.notifyText;
  }

  // 根据系统消息类型显示不同的通知
  if (systemMessageData.value) {
    const messageType = systemMessageData.value.messageType;
    switch (messageType) {
      case "consult_pending":
        return "待接诊";
      case "consult_rejected":
        return "已拒绝";
      case "consult_timeout":
        return "已超时";
      case "consult_accepted":
        return "已接诊";
      case "consult_ended":
        return "已结束";
      default:
        return "";
    }
  }

  return "";
});
</script>

<style scoped lang="scss">
@import "../chat.scss";

.notify-bar {
  display: flex;
  align-items: center;
  padding: 0 30rpx;
}

.notify-line {
  flex-grow: 1;
  min-width: 30rpx;
  height: 1px;
  background: #ddd;
}

.notify-text {
  margin: 0 20rpx;
  font-size: 24rpx;
  color: red;
  white-space: nowrap;
  flex-shrink: 0;
  max-width: 80%;
  text-overflow: ellipsis;
  overflow: hidden;
}
</style>