<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.msgType" placeholder="请选择类型" clearable style="width: 100%;">
              <el-option v-for="item in msgTypeList" :key="item.value" :value="item.value" :label="item.display" />
            </el-select>
          </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"
          :table-height="tableHeight"
          :get-page="getPage"
          show-check-box
          :icon-msg="iconMsg"
          :opt-col-width="80"
          @on-result-change="_tableResultChange"
        >
          <el-table-column
            v-for="item in pageColumns"
            :key="item.key"
            show-overflow-tooltip
            :prop="item.key"
            :label="item.title"
            :min-width="item.width"
            :fixed="item.fixed?item.fixed:undefined"
          >
            <template slot-scope="scope">
              <div v-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>

    <!-- 添加、编辑 -->
    <messageTypeEdit ref="editModal" @on-result-change="_page" />
  </div>
</template>
<script>
import { resEnum, messageType } from '../../../api'
import messageTypeEdit from './messageTypeEdit'

export default {
  components: {
    messageTypeEdit
  },
  data() {
    return {
      searchOpen: false,
      msgTypeList: [],
      formObj: {
        msgType: ''
      },
      btn: [
        { type: 'success', id: 'sys-message-type-add', name: '添加' },
        { type: 'danger', id: 'sys-message-type-batch-del', name: '删除' }
      ],
      selectIds: [],
      pageColumns: [
        { title: '消息类型', key: 'msgType' },
        { title: '跳转链接', key: 'link' }
      ],
      getPage: {},
      iconMsg: [
        { type: 'pt-edit', id: 'sys-message-type-edit', name: '编辑' },
        { type: 'pt-trash', id: 'sys-message-type-del', name: '删除' }
      ]
    }
  },
  computed: {
    tableHeight: function() {
      if (this.searchOpen) {
        return this.$tableHeight('search')
      } else {
        return this.$tableHeight('noSearch')
      }
    }
  },
  mounted() {
    this._getMsgType()
    this._formSearch()
  },
  methods: {
    _getMsgType: async function() {
      const result = await resEnum.getByType('EnvMessageType')
      this.msgTypeList = result
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await messageType.page(this.$serializeForm(this.formObj))
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _deleteByIds: async function(ids) {
      const result = await messageType.deleteByIds(ids)
      this._resultChange(result, '删除成功!')
    },
    _resultChange(result, msg) {
      if (result) {
        this.$message.success(msg)
        this._page()
      }
    },
    _deleteSelected() {
      if (this.selectIds.length === 0) {
        this.$message.warning('请选择一条或多条数据！')
      } else {
        this._deleteConfirm(
          this.selectIds,
          '确定删除 ' + this.selectIds.length + ' 条记录？'
        )
      }
    },
    _deleteConfirm(ids, content) {
      this.$Modal.confirm({
        title: '提示',
        content: content || '确定删除该记录？',
        onOk: () => {
          this._deleteByIds(ids)
        }
      })
    },
    _editModal: async function(id) {
      const result = await messageType.getById(id)
      this.$refs.editModal._open(result)
    },
    _btnClick(msg) {
      switch (msg) {
        case '添加':
          this.$refs.editModal._open()
          break
        case '删除':
          this._deleteSelected()
          break
        case 'search':
          this.searchOpen = !this.searchOpen
          break
      }
    },
    _iconClick(res, data) {
      switch (res) {
        case '编辑':
          this._editModal(data.id)
          break
        case '删除':
          this._deleteConfirm([data.id])
          break
        case '发送':
          this._send(data.id)
          break
      }
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this._page()
          break
        case 'selectIds':
          this.selectIds = data
          break
        case 'iconClick':
          this._iconClick(data.name, data.rowData)
          break
      }
    }
  }
}
</script>
