<template>
  <div>
    <el-dialog
      v-loading="loading"
      :visible.sync="visible"
      :close-on-press-escape="true"
      :close-on-click-modal="false"
      lock-scroll
      append-to-body
      custom-class="home-more-dialog"
      title="消息列表"
      @close="close"
    >
      <div class="list-filter">
        <el-form ref="filterForm" :model="formData" inline label-width="100px">
          <el-form-item label="消息标题">
            <el-input v-model="formData.title" class="filter-item" clearable />
          </el-form-item>
          <el-form-item label="所属应用系统">
            <el-select v-model="formData.systemCode" clearable filterable placeholder="请输入">
              <el-option v-for="(item) in systemData" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="formData.status" clearable placeholder="请选择">
              <el-option v-for="(item) in statusData" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="searchTable">查询</el-button>
            <el-button type="danger" @click="resetTable('filterForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="list-table">
        <el-table :data="tableData" fit border stripe height="300" style="width: 100%;">
          <el-table-column type="index" label="序号" width="50" />
          <el-table-column prop="messageType" label="消息类型" :formatter="formatDictData" width="100" show-overflow-tooltip />
          <el-table-column prop="title" label="消息标题" show-overflow-tooltip />
          <el-table-column prop="system" label="所属应用系统" width="160" show-overflow-tooltip />
          <el-table-column prop="readStatus" label="状态" :formatter="formatDictData" width="60" />
          <el-table-column prop="createTime" label="消息接收时间" width="160" />
          <el-table-column prop="createUser" label="消息发送人" width="100" show-overflow-tooltip />
          <el-table-column label="操作" align="center" width="160" class-name="action-column">
            <template slot-scope="scope">
              <!-- <el-button type="text" @click="viewDetail(scope.row.id)">详情</el-button> -->
              <el-button type="text" @click="viewDetail(scope.row.content)">详情</el-button>
              <el-button v-if="scope.row.readStatus === 0" type="text" @click="markRead(scope.row)">不再提醒</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div v-if="total > 0" class="page">
        <el-pagination
          layout="total, sizes, prev, pager, next, jumper"
          :current-page="current"
          :total="total"
          :page-sizes="pageSizeAll"
          :page-size="size"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-dialog>
    <el-dialog
      :visible.sync="detailVisible"
      :close-on-press-escape="true"
      :close-on-click-modal="false"
      lock-scroll
      append-to-body
      title="消息内容"
      @close="closeDetail"
    >
      <pre>{{ content }}</pre>
    </el-dialog>
  </div>
</template>
<script>
import { isNull, formatDictData } from '@/utils/convert'
import { fetchTableList, pushMarkRead } from '@/api/message'

export default {
  props: {},
  data() {
    return {
      // dialog
      visible: false,
      // table
      current: 1,
      size: 20,
      total: 0,
      pageSizeAll: [10, 20, 50, 100, 200, 500],
      tableData: [],
      // filter
      formData: {
        title: '',
        systemCode: '',
        status: null
      },
      // select data
      typeData: [{ 'code': 4, 'name': '任务类' }, { 'code': 5, 'name': '通知类' }, { 'code': 6, 'name': '超期提醒类' }],
      systemData: [{ 'code': '1', 'name': 'System1' }, { 'code': '2', 'name': 'System2' }],
      statusData: [{ 'code': 0, 'name': '待办' }, { 'code': 1, 'name': '已办' }],
      // others
      loading: false,
      detailVisible: false,
      content: ''
    }
  },
  methods: {
    // 改变每页显示条数
    handleSizeChange(val) {
      this.current = 1
      this.size = val
      this.getTablelist()
    },
    // 切换第几页
    handleCurrentChange(val) {
      this.current = val
      this.getTablelist()
    },
    // 重置搜索项
    resetTable(formName) {
      this.$refs[formName].resetFields()
      this.getTablelist()
    },
    // 点击搜索按钮
    searchTable() {
      this.current = 1
      this.getTablelist()
    },
    // 获取table数据
    getTablelist() {
      this.loading = true
      const params = {
        current: this.current,
        size: this.size,
        title: this.formData.title,
        systemCode: this.formData.systemCode,
        readStatus: this.formData.status
      }
      fetchTableList(params).then(response => {
        this.loading = false
        if (!isNull(response.data)) {
          this.tableData = response.data.records
          this.total = response.data.total
        } else {
          this.tableData = []
          this.total = 0
        }
      }).catch(error => {
        console.log(error)
        this.loading = false
        this.$message.error({
          type: 'error',
          duration: 0,
          showClose: true,
          message: '获取消息列表出错: ' + error.message
        })
      })
    },
    /**
     * 加载附件界面
     */
    open() {
      this.tableData = []
      this.visible = true
      this.searchTable()
    },
    close() {
      this.visible = false
    },
    viewDetail(content) {
      this.content = content
      this.detailVisible = true
    },
    closeDetail() {
      this.content = ''
      this.detailVisible = false
    },
    markRead(row) {
      this.loading = true
      pushMarkRead(row.id).then(res => {
        row.readStatus = 1
        this.loading = false
      }).catch(error => {
        console.log(error)
        this.loading = false
        this.$message.error({
          type: 'error',
          duration: 0,
          showClose: true,
          message: '不再提醒操作出错: ' + error.message
        })
      })
    },
    formatDictData(row, column, cellValue) {
      let data
      if (column.property === 'messageType') {
        data = this.typeData
      } else if (column.property === 'readStatus') {
        data = this.statusData
      }
      return formatDictData(data, cellValue)
    }
  }
}
</script>

<style lang="scss" scoped>
pre {
  white-space: pre-wrap;
  word-wrap: break-word;
}
</style>
