<template>
  <div>
    <el-dialog
      :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.deptCode" clearable filterable placeholder="请选择">
              <el-option v-for="(item) in systemData" :key="item.systemNumber" :label="item.systemName" :value="item.systemNumber" />
            </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.id" :label="item.label" :value="item.id" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="searchTable">查询</el-button>
            <!-- <el-button type="danger" @click="resetForm('filterForm')">重置</el-button> -->
          </el-form-item>
        </el-form>
      </div>
      <div class="list-table">
        <el-table v-loading="loading" :data="tableData" border stripe height="500" 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="createOrgName" 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 { hasValidRecords, formatDictData } from '@/utils/convert'
import { fetchTableList, pushMarkRead } from '@/api/message'
import { fetchAllSystemList } from '@/api/system'

export default {
  name: 'MessageList',
  props: {
    typeData: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      // dialog
      visible: false,
      // table
      current: 1,
      size: 20,
      total: 0,
      pageSizeAll: [10, 20, 50, 100, 200, 500],
      tableData: [],
      // filter
      formData: {
        title: '',
        deptCode: '',
        status: null
      },
      // dict
      // select data
      statusData: [{ 'id': 0, 'label': '待办' }, { 'id': 1, 'label': '已办' }],
      systemData: [],
      // others
      loading: false,
      detailVisible: false,
      content: ''
    }
  },
  created() {
    this.getSystemData()
  },
  methods: {
    // 改变每页显示条数
    handleSizeChange(val) {
      this.current = 1
      this.size = val
      this.getTablelist()
    },
    // 切换第几页
    handleCurrentChange(val) {
      this.current = val
      this.getTablelist()
    },
    // 重置搜索项
    resetForm(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,
        delFlag: 0,
        title: this.formData.title,
        deptCode: this.formData.deptCode,
        readStatus: this.formData.status
      }
      fetchTableList(params).then(response => {
        this.loading = false
        if (hasValidRecords(response)) {
          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({
          type: 'error',
          duration: 0,
          showClose: true,
          message: '获取消息列表出错: ' + error.message
        })
      })
    },
    /**
     * 加载附件界面
     */
    open() {
      this.tableData = []
      this.visible = true
      this.searchTable()
    },
    close() {
      this.visible = false
      this.$emit('refreshData')
    },
    viewDetail(content) {
      this.content = content
      this.detailVisible = true
    },
    closeDetail() {
      this.content = ''
      this.detailVisible = false
    },
    markRead(row) {
      this.loading = true
      pushMarkRead(row.targetId).then(res => {
        row.readStatus = 1
        this.$store.dispatch('message/readMessage', row.deptCode)
        this.loading = false
      }).catch(error => {
        console.log(error)
        this.loading = false
        this.$message({
          type: 'error',
          duration: 0,
          showClose: true,
          message: '不再提醒操作出错: ' + error.message
        })
      })
    },
    getSystemData() {
      const params = {
        page: 1,
        size: 100,
        order: 'update_time',
        params: {
          delFlag: 0
        }
      }
      fetchAllSystemList(params).then(response => {
        if (hasValidRecords(response)) {
          this.systemData = response.data.records
        } else {
          this.systemData = []
        }
      }).catch(error => {
        console.log(error)
        this.$message({
          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>
