<template>
  <div class="app-container">
    <el-card>
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
        <div class="search">
          <div class="sl">
            <el-form-item label="消息内容" prop="mesContent">
              <el-input
                v-model="queryParams.mesContent"
                placeholder="请输入消息内容"
                clearable
                style="width: 240px"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </div>
          <div class="sr">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </div>
        </div>
      </el-form>
    </el-card>
    <el-card class="lrtt">
      <div class="lt">
        <el-input
          v-model="deptName"
          placeholder="请输入部门名称"
          clearable
          size="small"
          prefix-icon="el-icon-search"
          style="margin-bottom: 20px"
        />
        <div class="divide"></div><!--divide 分隔-->
        <el-tree
          :data="deptOptions"
          :props="defaultProps"
          :expand-on-click-node="false"
          :filter-node-method="filterNode"
          ref="tree"
          node-key="id"
          default-expand-all
          highlight-current
          @node-click="handleNodeClick"
        />
      </div><!--lt 左-->
      <div class="rt">
        <div class="operate">
          <el-button type="primary" @click="markRead(null)">全部标记已读</el-button>
        </div><!--operate 操作按钮-->
        <el-table v-loading="loading" :data="tableList">
          <el-table-column label="消息内容" prop="msgContent" min-width="120" />
          <el-table-column label="接收时间" prop="msgRecTime" :show-overflow-tooltip="true" min-width="120"/>
          <el-table-column label="消息状态" prop="msgStatus" :show-overflow-tooltip="true" min-width="80" >
            <template  v-slot="{ row }">
              <template v-for="dict in dict.type.msg_status">
                <!-- {{ dict }} -->
                <el-tag :type="dict.raw.listClass" v-if="row.msgStatus == dict.value" :key="dict.value">{{ dict.label }}</el-tag>
              </template>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" min-width="100">
            <template v-slot="{ row }">
              <el-button
                size="mini"
                type="text"
                v-if="row.msgStatus === 'unread'"
                @click="markRead(row.id)"
              >标记已读</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </el-card>
  </div>
</template>

<script>
import { markAllRead, markReadById, getMessageList } from "@/api/message/message"
import { deptTreeSelect } from "@/api/system/user";

export default {
  name: "Document",
  dicts: ['msg_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 部门名称
      deptName: undefined,
      // 部门树选项
      deptOptions: undefined,
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 总条数
      total: 0,
      // 表格数据
      tableList: [],
      // 弹出层标题
      title: "",
      viewDialogTitle: "",
      viewDialogOpen: false,
      open: false,
      // 日期范围
      dateRange: [],
      previewUrl: '',
      progress: 0,
      fileList: [],
      websocket: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mesContent: ''
      },
    };
  },
  created() {
    this.getList();
    this.getDeptTree();
  },
  methods: {
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data
      })
    },
    getList() {
      this.loading = true
      getMessageList(this.queryParams).then(response => {
        this.total = response?.total
        this.tableList = response?.rows || []
        this.loading = false
      }).catch(() => { this.loading = false });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
     /** 重置按钮操作 */
     resetQuery() {
      this.tableList = []
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.toolRespDept = data.id;
      this.handleQuery();
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    /**
     * 标记已读：id为null时表示标记全部已读
     * @param id
     */
    markRead(id) {
      this.$confirm('确认标记已读吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        if(id) {
          markReadById(id).then(response => {
            this.$modal.msgSuccess(response?.msg || '操作成功')
            this.getList();
          })
        } else {
          markAllRead().then(response => {
            this.$modal.msgSuccess(response?.msg || '操作成功')
            this.getList();
          })
        }
      }).catch(() => {});
    },
  }
};
</script>

