<template>
  <div class="app-container">
    <!-- 页面说明 -->
    <el-alert
      title="消息管理说明"
      type="info"
      :closable="false"
      style="margin-bottom: 16px"
    >
      <div style="line-height: 1.8; font-size: 13px;">
        此页面仅用于<strong>管理员发布系统通知</strong>。<br/>
        • 用户之间的<strong>对话功能</strong>请使用右上角<el-tag size="small" type="primary" style="margin: 0 4px">💬 消息中心</el-tag><br/>
        • 发布通知后，所有选中用户将<strong>实时收到WebSocket推送</strong>
      </div>
    </el-alert>

    <!-- 搜索栏 -->
    <el-form :model="queryParams" ref="queryFormRef" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="消息标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入消息标题"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="优先级" prop="priority">
        <el-select v-model="queryParams.priority" placeholder="请选择优先级" clearable style="width: 120px">
          <el-option label="普通" value="0" />
          <el-option label="重要" value="1" />
          <el-option label="紧急" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="Promotion"
          @click="handleSend"
          v-hasPermi="['system:message:send']"
        >发布通知</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:message:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 数据表格 -->
    <el-table v-loading="loading" :data="messageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="消息ID" align="center" prop="messageId" width="80" />
      <el-table-column label="优先级" align="center" width="80">
        <template #default="scope">
          <el-tag v-if="scope.row.priority === '0'" type="info" size="small">普通</el-tag>
          <el-tag v-else-if="scope.row.priority === '1'" type="warning" size="small">重要</el-tag>
          <el-tag v-else-if="scope.row.priority === '2'" type="danger" size="small">紧急</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="消息标题" align="left" prop="title" :show-overflow-tooltip="true" min-width="200" />
      <el-table-column label="发送者" align="center" prop="senderName" width="120">
        <template #default="scope">
          {{ scope.row.senderName || '系统' }}
        </template>
      </el-table-column>
      <el-table-column label="接收人数" align="center" width="100">
        <template #default="scope">
          <el-tag type="info" size="small">{{ scope.row.receiverCount || 0 }}人</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="已读/未读" align="center" width="100">
        <template #default="scope">
          <span style="color: #67C23A;">{{ scope.row.readCount || 0 }}</span>
          /
          <span style="color: #F56C6C;">{{ scope.row.unreadCount || 0 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发送时间" align="center" prop="createTime" width="160">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150" fixed="right">
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="View"
            @click="handleView(scope.row)"
            v-hasPermi="['system:message:query']"
          >查看</el-button>
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:message:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查看消息详情对话框 -->
    <el-dialog title="消息详情" v-model="viewOpen" width="800px" append-to-body class="message-detail-dialog">
      <el-descriptions :column="2" border>
        <el-descriptions-item label="消息标题" :span="2">
          <span style="font-weight: bold; font-size: 16px;">{{ form.title }}</span>
        </el-descriptions-item>
        <el-descriptions-item label="消息类型">
          <el-tag v-if="form.messageType === '1'" type="primary">对话消息</el-tag>
          <el-tag v-else-if="form.messageType === '2'" type="success">系统通知</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="优先级">
          <el-tag v-if="form.priority === '0'" type="info">普通</el-tag>
          <el-tag v-else-if="form.priority === '1'" type="warning">重要</el-tag>
          <el-tag v-else-if="form.priority === '2'" type="danger">紧急</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="发送者">
          {{ form.senderName || '系统' }}
        </el-descriptions-item>
        <el-descriptions-item label="发送时间">
          {{ parseTime(form.createTime) }}
        </el-descriptions-item>
        <el-descriptions-item label="接收人数">
          {{ form.receiverCount || 0 }}人
        </el-descriptions-item>
        <el-descriptions-item label="已读/未读">
          <span style="color: #67C23A;">{{ form.readCount || 0 }}</span>
          /
          <span style="color: #F56C6C;">{{ form.unreadCount || 0 }}</span>
        </el-descriptions-item>
        <el-descriptions-item label="消息内容" :span="2">
          <div class="message-content">{{ form.content }}</div>
        </el-descriptions-item>
        <el-descriptions-item label="接收人列表" :span="2" v-if="receiverList.length > 0">
          <el-table :data="receiverList" size="small" max-height="300">
            <el-table-column label="用户名" prop="userName" width="120" />
            <el-table-column label="阅读状态" align="center" width="100">
              <template #default="scope">
                <el-tag v-if="scope.row.isRead === '0'" type="danger" size="small">未读</el-tag>
                <el-tag v-else type="success" size="small">已读</el-tag>
              </template>
            </el-table-column>
            <el-table-column label="阅读时间" prop="readTime" width="160">
              <template #default="scope">
                <span v-if="scope.row.readTime">{{ parseTime(scope.row.readTime, '{y}-{m}-{d} {h}:{i}') }}</span>
                <span v-else style="color: #909399;">未读</span>
              </template>
            </el-table-column>
            <el-table-column label="接收时间" prop="createTime" width="160">
              <template #default="scope">
                {{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}') }}
              </template>
            </el-table-column>
          </el-table>
        </el-descriptions-item>
      </el-descriptions>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="viewOpen = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 发送消息对话框 -->
    <send-message-dialog ref="sendDialogRef" @success="handleSendSuccess" />
  </div>
</template>

<script setup name="MessageManagement">
import { listNotification, getNotification, delNotification } from "@/api/system/message"
import SendMessageDialog from './components/SendMessageDialog.vue'

const { proxy } = getCurrentInstance()

const loading = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const showSearch = ref(true)
const total = ref(0)
const messageList = ref([])
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  title: null,
  messageType: null,
  priority: null,
  status: null
})
const form = ref({})
const receiverList = ref([])
const viewOpen = ref(false)
const sendDialogRef = ref(null)
const queryFormRef = ref(null)

/** 查询系统消息列表 */
function getList() {
  loading.value = true
  listNotification(queryParams.value).then(response => {
    messageList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryFormRef")
  handleQuery()
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.messageId)
  single.value = selection.length !== 1
  multiple.value = !selection.length
}

/** 发送消息按钮操作 */
function handleSend() {
  sendDialogRef.value.open()
}

/** 发送成功回调 */
function handleSendSuccess() {
  getList()
}

/** 查看消息详情 */
function handleView(row) {
  loading.value = true
  getNotification(row.messageId).then(response => {
    form.value = response.data
    receiverList.value = response.data.receivers || []
    viewOpen.value = true
    loading.value = false
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const messageIds = row.messageId || ids.value
  proxy.$modal.confirm('是否确认删除消息ID为"' + messageIds + '"的数据项？').then(() => {
    return delNotification(messageIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

getList()
</script>

<style lang="scss" scoped>
.message-content {
  padding: 15px;
  background: #f8fafc;
  border-radius: 8px;
  line-height: 1.8;
  max-height: 400px;
  overflow-y: auto;
  white-space: pre-wrap;
  word-break: break-word;
}

:deep(.message-detail-dialog) {
  .el-descriptions {
    .el-descriptions__label {
      background: #fafafa;
      font-weight: 500;
    }
  }
}
</style>
