<!--
 * @Descripttion: 表列表
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2020-11-09 11:15:55
 * @LastEditors: 无尘
 * @LastEditTime: 2020-11-13 11:11:54
-->
<template>
  <div class="common-right">
    <div class="m-b-20">
      <el-input placeholder="请输入表名称"  maxlength="50" v-model="searchInput" class="w-279" @keyup.native="value => toInput(value, searchInput)" clearable @clear="clearInput"></el-input>
    </div>
    <el-table class="select-table" ref="multipleTable" v-loading="loading" :data="tableData" tooltip-effect="dark" :style="{ width: '100%'}">
      <el-table-column label="表名称" show-overflow-tooltip>
        <template slot-scope="scope">{{ scope.row.sqlCheckTableName || '--' }}</template>
      </el-table-column>
      <el-table-column prop="" label="创建时间" show-overflow-tooltip>
        <template slot-scope="scope">
          <div>{{ scope.row.createTime | timeStampToYmd }} {{ !scope.row.createTime ? '--': '' }}</div>
          <div>{{ scope.row.createTime | timeStampToHms }} {{ !scope.row.createTime ? '--': '' }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="" label="操作" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-button type="text" @click="toSqlPage(scope.row)">SQL列表</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block common-wrap__page text-right m-t-24" v-if="tableData.length != 0">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[20, 40, 60, 80]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
    </div>
  </div>
</template>

<script>
import { _debounce } from '@/common/js/public';
import fetch from '@/api/dictionary';

const { getTableList } = fetch;
export default {
  name: 'TableSql',
  components: {},
  data() {
    return {
      searchInput: '',
      // 分页参数
      currentPage: 1,
      pageSize: 20,
      total: 0,
      loading: false,
      tableData: []
    };
  },
  // 生命周期 - 挂载完成（访问DOM元素）
  mounted() {
    const that = this;
    that.getTableData();
  },

  // methods
  methods: {
    /**
     * @description: 输入
     * @param {Object} e
     * @param {String} value
     * @author: 无尘
     */
    toInput: _debounce(function(e, value) {
      const that = this;
      that.currentPage = 1;
      that.getTableData();
    }, 500),
    /**
     * @description: 搜索清除
     * @author: 无尘
     */
    clearInput() {
      const that = this;
      that.currentPage = 1;
      that.getTableData();
    },
    /**
     * @description: 分页---页码变化
     * @param {Number} val
     * @author: 无尘
     */
    handleSizeChange(val) {
      const that = this;
      that.currentPage = 1;
      that.pageSize = val;
      that.getTableData();
    },
    /**
     * @description: 分页---当前页变化
     * @param {Number} val
     * @author: 无尘
     */
    handleCurrentChange(val) {
      const that = this;
      that.currentPage = val;
      that.getTableData();
    },
    /**
     * @description: 获取表列表
     * @author: 无尘
     */
    getTableData() {
      const that = this;
      that.loading = true;
      const param = {
        table: that.searchInput,
        pageNum: that.currentPage,
        pageSize: that.pageSize
      };
      getTableList(param).then(res => {
        that.loading = false;
        that.tableData = res.result.result || [];
        that.total = res.result.totalCount || 0;
      })
        .catch(function(error) {
          that.loading = false;
        });
    },
    /**
     * @description: 跳转 sql 列表
     * @param {Object} row
     * @author: 无尘
     */
    toSqlPage(row) {
      const that = this;
      that.$router.push(`/sql-list?sqlCheckTableId=${row.sqlCheckTableId}`);
    }
  },
  watch: {}
};
</script>
<style lang='less' scoped>
  .w-279 {
    width: 279px;
  }
</style>
