<template>
  <div>
    <el-row>
      <!--查询-->
      <el-col :span="24">
        <Form
          v-show="searchOpen"
          inline
          onsubmit="return false"
          :label-width="50"
        >
          <label class="label-sign" />
          <Form-item class="search-item" label="内容:">
            <el-input
              v-model="formObj.content"
              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>
      <!--table-->
      <el-col :span="24">
        <element-table
          ref="pageTable"
          :page-columns="pageColumns"
          :table-height="tableHeight"
          :get-page="getPage"
          :opt-col-width="80"
          :icon-msg="iconMsg"
          show-check-box
          @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.datetime">
                {{ scope.row[item.key]?$dateformat(scope.row[item.key],'yyyy-mm-dd HH:MM:ss'):'' }}
              </div>
              <div v-else>
                {{ scope.row[item.key] }}
              </div>
            </template>
          </el-table-column>
        </element-table>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import { sysLog } from '../../../api'
export default {
  data() {
    return {
      searchOpen: true,
      formObj: {
        content: ''
      },
      btn: [{ type: 'danger', id: 'sys-log-delall', name: '删除' }],
      pageColumns: [
        { title: '内容', key: 'content', width: 120 },
        { title: '请求URI', key: 'uri' },
        { title: '请求参数', key: 'params' },
        { title: '操作人', key: 'realName', width: 90 },
        { title: 'IP', key: 'ip' },
        { title: '创建时间', key: 'ctime', width: 140, datetime: true }
      ],
      getPage: {},
      iconMsg: [{ type: 'pt-trash', id: 'sys-log-del', name: '删除' }],
      selectIds: []
    }
  },
  computed: {
    tableHeight: function() {
      if (this.searchOpen) {
        return this.$tableHeight('search')
      } else {
        return this.$tableHeight('noSearch')
      }
    }
  },
  mounted() {
    this._formSearch()
  },
  methods: {
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await sysLog.page(this.$serializeForm(this.formObj))
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _btnClick(msg, data) {
      switch (msg) {
        case '删除':
          this._deleteSelected()
          break
        case 'search':
          this.searchOpen = !this.searchOpen
          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
      }
    },
    _iconClick(res, data) {
      switch (res) {
        case '删除':
          this._deleteById(data.id)
          break
      }
    },
    // 批量删除
    _deleteSelected() {
      // 批量删除
      const ids = this.selectIds
      if (ids.length === 0) {
        this.$message.warning('请选择一条或多条数据！')
      } else {
        this._deleteByIds(ids, '确定删除 ' + ids.length + ' 条记录？')
      }
    },
    _deleteByIds(ids, content) {
      const cont = content || '确定删除该记录？'
      this.$confirm(cont, '提示', { type: 'warning' }).then(() => {
        this._delOk(ids)
      })
    },
    _deleteById(id) {
      // 删除一条记录
      this._deleteByIds([id])
    },
    _delOk: async function(ids) {
      const result = await sysLog.deleteByIds(ids)
      if (result) {
        this.$message.success('删除成功')
        this._page()
      }
    }
  }
}
</script>
