<template>
  <!-- 移动端使用抽屉 -->
  <el-drawer
    v-if="isMobile"
    v-model="dialogVisible"
    title="消息详情"
    direction="btt"
    size="70%"
  >
    <div class="mobile-detail">
      <div class="mobile-detail__header">
        <div class="mobile-detail__sender">{{ detailData.templateNickname }}</div>
        <div class="mobile-detail__time">{{ formatDate(detailData.createTime) }}</div>
      </div>

      <div class="mobile-detail__meta">
        <div class="meta-item">
          <span class="meta-item__label">消息类型</span>
          <dict-tag :type="DICT_TYPE.SYSTEM_NOTIFY_TEMPLATE_TYPE" :value="detailData.templateType" />
        </div>
        <div class="meta-item">
          <span class="meta-item__label">状态</span>
          <dict-tag :type="DICT_TYPE.INFRA_BOOLEAN_STRING" :value="detailData.readStatus" />
        </div>
        <div class="meta-item" v-if="detailData.readStatus">
          <span class="meta-item__label">阅读时间</span>
          <span class="meta-item__value">{{ formatDate(detailData.readTime) }}</span>
        </div>
      </div>

      <div class="mobile-detail__content">
        <div class="content-title">消息内容</div>
        <div class="content-body">{{ detailData.templateContent }}</div>
      </div>
    </div>

    <template #footer>
      <div class="mobile-drawer-footer">
        <el-button @click="dialogVisible = false" style="flex:1">关闭</el-button>
      </div>
    </template>
  </el-drawer>

  <!-- PC端使用对话框 -->
  <Dialog v-else v-model="dialogVisible" :max-height="500" :scroll="true" title="消息详情">
    <el-descriptions :column="1" border>
      <el-descriptions-item label="发送人">
        {{ detailData.templateNickname }}
      </el-descriptions-item>
      <el-descriptions-item label="发送时间">
        {{ formatDate(detailData.createTime) }}
      </el-descriptions-item>
      <el-descriptions-item label="消息类型">
        <dict-tag :type="DICT_TYPE.SYSTEM_NOTIFY_TEMPLATE_TYPE" :value="detailData.templateType" />
      </el-descriptions-item>
      <el-descriptions-item label="是否已读">
        <dict-tag :type="DICT_TYPE.INFRA_BOOLEAN_STRING" :value="detailData.readStatus" />
      </el-descriptions-item>
      <el-descriptions-item v-if="detailData.readStatus" label="阅读时间">
        {{ formatDate(detailData.readTime) }}
      </el-descriptions-item>
      <el-descriptions-item label="内容">
        {{ detailData.templateContent }}
      </el-descriptions-item>
    </el-descriptions>
  </Dialog>
</template>
<script lang="ts" setup>
import { computed } from 'vue'
import { DICT_TYPE } from '@/utils/dict'
import { formatDate } from '@/utils/formatTime'
import * as NotifyMessageApi from '@/api/system/notify/message'
import { useWindowSize } from '@vueuse/core'

defineOptions({ name: 'MyNotifyMessageDetailDetail' })

const { width } = useWindowSize()
const isMobile = computed(() => width.value < 768)

const dialogVisible = ref(false) // 弹窗的是否展示
const detailLoading = ref(false) // 表单的加载中
const detailData = ref({} as NotifyMessageApi.NotifyMessageVO) // 详情数据

/** 打开弹窗 */
const open = async (data: NotifyMessageApi.NotifyMessageVO) => {
  dialogVisible.value = true
  // 设置数据
  detailLoading.value = true
  try {
    detailData.value = data
  } finally {
    detailLoading.value = false
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗
</script>

<style lang="scss" scoped>
.mobile-detail {
  &__header {
    text-align: center;
    padding-bottom: 16px;
    border-bottom: 1px solid #f0f0f0;
    margin-bottom: 16px;
  }

  &__sender {
    font-size: 18px;
    font-weight: 600;
    color: #303133;
    margin-bottom: 4px;
  }

  &__time {
    font-size: 13px;
    color: #909399;
  }

  &__meta {
    display: flex;
    flex-direction: column;
    gap: 12px;
    padding: 12px;
    background: #f5f7fa;
    border-radius: 8px;
    margin-bottom: 16px;
  }

  &__content {
    .content-title {
      font-size: 14px;
      font-weight: 600;
      color: #303133;
      margin-bottom: 8px;
    }

    .content-body {
      font-size: 14px;
      color: #606266;
      line-height: 1.8;
      padding: 12px;
      background: #fafafa;
      border-radius: 8px;
    }
  }
}

.meta-item {
  display: flex;
  justify-content: space-between;
  align-items: center;

  &__label {
    font-size: 13px;
    color: #909399;
  }

  &__value {
    font-size: 13px;
    color: #606266;
  }
}

.mobile-drawer-footer {
  display: flex;
  gap: 12px;
  padding: 12px 16px;
  border-top: 1px solid #f0f0f0;
}
</style>
