<template>
  <div>
    <el-row>
      <!--查询-->
      <el-col :span="24">
        <Form v-show="searchOpen" inline onsubmit="return false" :label-width="60">
          <label class="label-sign" />
          <Form-item class="search-item" label="类型:">
            <el-select v-model="formObj.type" placeholder="请选择类型" clearable style="width: 100%;">
              <el-option v-for="item in typeList" :key="item.value" :value="item.value" :label="item.label" />
            </el-select>
          </Form-item>
          <Form-item class="search-item" label="标题:">
            <el-input v-model="formObj.title" placeholder="请输入标题" clearable @keyup.enter.native="_formSearch" />
          </Form-item>
          <Form-item class="search-btn">
            <el-button type="primary" @click="_formSearch">
              搜索
            </el-button>
          </Form-item>
        </Form>
      </el-col>
      <!--操作-->
      <el-col :span="24">
        <btn-list :msg="btn" :open="searchOpen" :show-search-btn="true" @on-result-change="_btnClick" />
      </el-col>
      <!-- 表格 -->
      <el-col :span="24">
        <element-table
          ref="pageTable"
          :page-columns="pageColumns"
          :table-height="tableHeight"
          :get-page="getPage"
          :show-check-box="true"
          select-data
          @on-result-change="_tableResultChange"
        >
          <el-table-column
            v-for="item in pageColumns"
            :key="item.key"
            show-overflow-tooltip
            sortable
            :prop="item.key"
            :label="item.title"
            :width="item.width"
            :min-width="200"
            :fixed="item.fixed?item.fixed:undefined"
          >
            <template slot-scope="scope">
              <div v-if="item.detail">
                <span v-if="scope.row['read'] === 0">
                  <a
                    @click.stop="_detailModal(scope.row)"
                  >
                    {{ scope.row[item.key] }}
                  </a>
                </span>
                <span v-else class="gray-color">
                  {{ scope.row[item.key] }}
                </span>
              </div>
              <div v-else-if="item.type">
                <span v-if="scope.row['type'] === 1" class="blue-color">
                  消息
                </span>
                <span v-else class="green-color">
                  通知
                </span>
              </div>
              <span v-else-if="item.dateTime">
                {{ $dateformat(scope.row[item.key],'yyyy-mm-dd HH:MM') }}
              </span>
              <span v-else>
                {{ scope.row[item.key] }}
              </span>
            </template>
          </el-table-column>
        </element-table>
      </el-col>
    </el-row>
    <!-- 我的消息详情 -->
    <myMessageDetail ref="detailModal" />
  </div>
</template>
<script>
import { message } from '../../../api'
import myMessageDetail from './myMessageDetail.vue'

export default {
  components: {
    myMessageDetail
  },
  data() {
    return {
      searchOpen: false,
      formObj: {
        type: '',
        title: ''
      },
      btn: [
        { type: 'success', id: 'sys-my-message-mark-all', name: '标记已读' },
        { type: '', id: '', name: '一键已读' },
        { type: '', id: '', name: '一键删除' }
      ],
      typeList: [{ value: 0, label: '通知' }, { value: 1, label: '消息' }],
      pageColumns: [
        { title: '类型', key: 'type', width: 115, type: true },
        { title: '标题', key: 'title' },
        { title: '发送人', key: 'sender', width: 120 },
        { title: '内容', key: 'content', detail: true },
        { title: '创建时间', key: 'ctime', width: 140, dateTime: true }
      ],
      getPage: {},
      selectIds: []
    }
  },
  computed: {
    tableHeight: function() {
      if (this.searchOpen) {
        return this.$tableHeight('search')
      } else {
        return this.$tableHeight('noSearch')
      }
    }
  },
  mounted() {
    this._formSearch()
  },
  methods: {
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await message.myPage(this.$serializeForm(this.formObj))
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _detailModal: async function(data) {
      const result = await message.getById(data.id)
      this.$refs.detailModal._open(result)
      this._read([data.relId])
    },
    // 批量阅读
    _readAll() {
      if (this.selectIds.length === 0) {
        this.$message.warning('请选择一条或多条数据！')
      } else {
        this.$confirm(
          '确定阅读这 ' + this.selectIds.length + ' 条记录？',
          '提示',
          { type: 'warning' }
        ).then(() => {
          this._read(this.selectIds)
        })
      }
    },
    _read: async function(ids) {
      const result = await message.read(ids)
      this._resultChange(result, '阅读成功')
    },
    // 监听事件(到头部去刷新消息)
    _bus() {
      this.$store.commit('messageNumber', this.getPage.records)
      // this.$bus.emit('read-my-message')
    },
    _btnClick(msg) {
      switch (msg) {
        case '标记已读':
          this._readAll()
          break
        case '一键已读':
          this._oneKeyOperate('确定要将【全部消息】标记为已读 ？', 1)
          break
        case '一键删除':
          this._oneKeyOperate('确定要将【全部消息】删除 ？', 2)
          break
        case 'search':
          this.searchOpen = !this.searchOpen
          break
      }
    },
    // 一键操作
    _oneKeyOperate(cont, type) {
      this.$confirm(cont, '提示', {
        type: 'warning'
      })
        .then(() => {
          if (type === 1) {
            this._oneKeyReadOk()
          } else if (type === 2) {
            this._oneKeyDelAllOk()
          }
        })
        .catch(() => {})
    },
    _oneKeyReadOk: async function() {
      const result = await message.readAll()
      this._resultChange(result, '消息全部标记为已读')
    },
    _oneKeyDelAllOk: async function() {
      const result = await message.deleteAll()
      this._resultChange(result, '消息已全部删除')
    },
    _resultChange(result, msg) {
      if (result) {
        this.$message.success(msg)
        this._page()
        this._bus()
      }
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this._page()
          break
        case 'selectData':
          // eslint-disable-next-line no-case-declarations
          const selectIds = []
          for (let i = 0; i < data.length; i++) {
            selectIds.push(data[i].relId)
          }
          this.selectIds = selectIds
          break
      }
    }
  }
}
</script>
