<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="content">
              <el-input
                v-model="queryParams.content"
                placeholder="请输入消息内容"
                clearable
                style="width: 240px"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="状态" prop="states">
              <el-select v-model="queryParams.states" placeholder="状态" clearable>
                <el-option
                  v-for="dict in dict.type.msg_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </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="rt">
        <div class="operate">
          <el-button type="primary" v-if="tableList && tableList.length > 0" @click="setAllMarkedRead" icon="el-icon-check">全部标记已读</el-button>
        </div><!--operate 操作按钮-->
        <el-table v-loading="loading" :data="tableList">
          <el-table-column type="index" label="序号" width="50" />
          <el-table-column label="消息内容" prop="content" min-width="120" />
          <el-table-column label="接收时间" prop="createTime" :show-overflow-tooltip="true" min-width="120"/>
          <el-table-column label="消息状态" prop="states" :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.states == dict.value" :key="dict.value">{{ dict.label }}</el-tag>
              </template>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" min-width="100" fixed="right">
            <template v-slot="{ row }">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-check"
                v-if="row.states == '1'"
                @click="markRead(row)"
              >标记已读</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 { listMessage, getMessage, delMessage, addMessage, updateMessage,allMarkedRead } 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,
        content: '',
        states: '',
      },
    };
  },
  created() {
    this.getList();
    this.getDeptTree();
  },
  methods: {
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data
      })
    },
    getList() {
      this.loading = true
      this.$set(this.queryParams,"receiverId", this.$store.getters.userId)
      listMessage(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(row) {
      let self = this
      self.$confirm('确认标记已读吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let formData = JSON.parse(JSON.stringify(row))
        self.$set(formData, "states", 2)
          updateMessage(formData).then(response => {
            self.$modal.msgSuccess(response?.msg || '操作成功')
            self.getList();
          })
      }).catch((err) => {console.error(err)});
    },

    /**
     * 标记已读：id为null时表示标记全部已读
     * @param id
     */
    setAllMarkedRead() {
      let self = this
      self.$confirm('确认全部标记已读吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let formData = {
          "states": 2
        }
        allMarkedRead(formData).then(response => {
          self.$modal.msgSuccess(response?.msg || '操作成功')
          self.getList();
        })
      }).catch((err) => {console.error(err)});
    },
  }
};
</script>

