<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
          @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">
              <a v-if="item.detail" @click.stop="_detailModal(scope.row.id)">
                {{ scope.row[item.key] }}
              </a>
              <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>
          <template slot="col">
            <el-table-column
              label="操作"
              align="center"
              :width="90"
              fixed="right"
            >
              <template slot-scope="scope">
                <icon-list
                  :msg="scope.row.type === 1 ? iconMsgDis: iconMsg"
                  :row-data="scope.row"
                  @on-result-change="_iconClick"
                />
              </template>
            </el-table-column>
          </template>
        </element-table>
      </el-col>
    </el-row>
    <!-- 添加、编辑 -->
    <messageEdit ref="editModal" @on-result-change="_page" />
    <!-- 查看详情 -->
    <messageDetail ref="detailModal" />
    <!-- 发送消息 -->
    <sendMessage ref="sendModal" @on-result-change="_page" />
  </div>
</template>
<script>
import { message } from '../../../api'
import messageEdit from './messageEdit.vue'
import messageDetail from './messageDetail.vue'
import sendMessage from './sendMessage.vue'

export default {
  components: {
    messageEdit,
    messageDetail,
    sendMessage
  },
  data() {
    return {
      btn: [
        { type: 'success', id: 'sys-message-add', name: '添加' },
        { type: 'danger', id: 'sys-message-delall', name: '删除' }
      ],
      searchOpen: false,
      formObj: {
        type: '',
        title: ''
      },
      selectIds: [],
      typeList: [{ value: 0, label: '通知' }, { value: 1, label: '消息' }],
      pageColumns: [
        { title: '类型', key: 'type', width: 115, type: true },
        { title: '标题', key: 'title', detail: true },
        { title: '创建时间', key: 'ctime', width: 140, dateTime: true }
      ],
      getPage: {},
      iconMsg: [
        {
          type: 'pt-send',
          id: 'sys-message-send',
          name: '发送'
        },
        { type: 'pt-edit', id: 'sys-message-edit', name: '编辑' },
        { type: 'pt-trash', id: 'sys-message-del', name: '删除' }
      ],
      iconMsgDis: [
        {
          type: 'pt-send',
          id: 'sys-message-send',
          name: '发送',
          disabled: true
        },
        {
          type: 'pt-edit',
          id: 'sys-message-edit',
          name: '编辑',
          disabled: true
        },
        { type: 'pt-trash', id: 'sys-message-del', name: '删除' }
      ]
    }
  },
  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.page(this.$serializeForm(this.formObj))
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this._page()
          break
        case 'selectIds':
          this.selectIds = data
          break
      }
    },
    _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
      }
    },
    _deleteSelected() {
      if (this.selectIds.length === 0) {
        this.$message.warning('请选择一条或多条数据！')
      } else {
        this._deleteConfirm(
          this.selectIds,
          '确定删除 ' + this.selectIds.length + ' 条记录？'
        )
      }
    },
    _deleteConfirm(ids, content) {
      this.$confirm(content || '确定删除该记录？', '提示', {
        type: 'warning'
      })
        .then(() => {
          this._deleteByIds(ids)
        })
        .catch(() => {})
    },
    _deleteByIds: async function(ids) {
      const result = await message.deleteByIds(ids)
      this._resultChange(result, '删除成功!')
    },
    _resultChange(result, msg) {
      if (result) {
        this.$message.success(msg)
        this._page()
      }
    },
    _detailModal: async function(id) {
      const result = await message.getById(id)
      this.$refs.detailModal._open(result)
    },
    _editModal: async function(id) {
      const result = await message.getById(id)
      this.$refs.editModal._open(result)
    },
    _send(id) {
      this.$refs.sendModal._open(id)
    }
  }
}
</script>
