<template>
  <div>
    <el-card class="box-card" shadow="always">
      <el-row :gutter="20">
        <el-col :span="4">
          <el-tree
            :data="treeOptions"
            ref="tree"
            node-key="id"
            empty-text="加载中，请稍后"
            :props="defaultProps"
            default-expand-all
            :expand-on-click-node="false"
            @node-click="handleNodeClick"
            :render-content="renderContent"
          ></el-tree>
        </el-col>
        <el-col :span="20" style="border-left: 2px solid #aaa;">
          <el-form :model="queryParams" ref="queryForm" :inline="true">
            <el-form-item label="字段名称" prop="columnName">
              <el-input
                v-model="queryParams.columnName"
                placeholder="请输入字段名称"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>

          <el-row type="flex" justify="space-between">
            <el-col :span="12">
              <el-button-group>
                <el-button
                  type="info"
                  icon="el-icon-view"
                  size="mini"
                  :disabled="single"
                  @click="handleDetail"
                >详情</el-button>
              </el-button-group>
            </el-col>
            <el-col :span="12">
              <div class="right-toolbar">
                <el-tooltip content="密度" effect="dark" placement="top">
                  <el-dropdown trigger="click" @command="handleCommand">
                    <el-button circle size="mini">
                      <svg-icon class-name="size-icon" icon-class="colum-height" />
                    </el-button>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item command="medium">正常</el-dropdown-item>
                      <el-dropdown-item command="small">中等</el-dropdown-item>
                      <el-dropdown-item command="mini">紧凑</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-tooltip>
                <el-tooltip content="刷新" effect="dark" placement="top">
                  <el-button circle size="mini" @click="handleRefresh">
                    <svg-icon class-name="size-icon" icon-class="shuaxin" />
                  </el-button>
                </el-tooltip>
                <el-tooltip content="列设置" effect="dark" placement="top">
                  <el-popover placement="bottom" width="100" trigger="click">
                    <el-checkbox-group
                      v-model="checkedTableColumns"
                      @change="handleCheckedColsChange"
                    >
                      <el-checkbox
                        v-for="(item, index) in tableColumns"
                        :key="index"
                        :label="item.prop"
                      >{{ item.label }}</el-checkbox>
                    </el-checkbox-group>
                    <span slot="reference">
                      <el-button circle size="mini">
                        <svg-icon class-name="size-icon" icon-class="shezhi" />
                      </el-button>
                    </span>
                  </el-popover>
                </el-tooltip>
              </div>
            </el-col>
          </el-row>

          <el-table
            v-loading="loading"
            :data="columnList"
            @selection-change="handleSelectionChange"
            border
            tooltip-effect="dark"
            :size="tableSize"
            :height="tableHeight"
            style="width: 100%;margin: 15px 0;"
          >
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="序号" width="55" align="center">
              <template slot-scope="scope">
                <span>{{ scope.$index + 1 }}</span>
              </template>
            </el-table-column>
            <template v-for="(item, index) in tableColumns">
              <el-table-column
                v-if="item.show"
                :prop="item.prop"
                :label="item.label"
                :key="index"
                :formatter="item.formatter"
                align="center"
                show-overflow-tooltip
              />
            </template>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-popover
                  placement="left"
                  trigger="click">
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-view"
                    @click="handleDetail(scope.row)"
                  >详情</el-button>
                  <el-button slot="reference">操作</el-button>
                </el-popover>
              </template>
            </el-table-column>
          </el-table>

          <el-pagination
            :page-sizes="[10, 20, 50, 100]"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page.sync="queryParams.pageNum"
            :page-size.sync="queryParams.pageSize"
            :total="total"
          ></el-pagination>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script>
import { pageDataColumn, getDataMetadataTree } from '@/api/metadata/datacolumn'

export default {
  name: 'DataColumnList',
  data () {
    return {
      tableHeight: document.body.offsetHeight - 340 + 'px',
      // 展示切换
      showOptions: {
        data: {},
        showList: true,
        showDetail: false
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 表格头
      tableColumns: [
        { prop: 'columnName', label: '字段名称', show: true },
        { prop: 'columnComment', label: '字段注释', show: true },
        { prop: 'columnKey', label: '是否主键', show: true, formatter: this.keyFormatter },
        { prop: 'columnNullable', label: '是否允许为空', show: true, formatter: this.nullableFormatter },
        { prop: 'dataType', label: '数据类型', show: true },
        { prop: 'dataLength', label: '数据长度', show: true },
        { prop: 'dataPrecision', label: '数据精度', show: true },
        { prop: 'dataScale', label: '数据小数位', show: true },
        { prop: 'dataDefault', label: '数据默认值', show: true }
      ],
      // 默认选择中表格头
      checkedTableColumns: [],
      tableSize: 'medium',
      // 表格数据
      columnList: [],
      // 总数据条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        columnName: '',
        sourceId: '',
        tableId: ''
      },
      // 左侧树
      treeOptions: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      }
    }
  },
  created () {
    this.getList()
    this.getTree()
  },
  mounted () {
    this.initCols()
  },
  methods: {
    /** 查询列表 */
    getList () {
      this.loading = true
      pageDataColumn(this.queryParams).then(response => {
        this.loading = false
        if (response.success) {
          const { data } = response
          this.columnList = data.data
          this.total = data.total
        }
      })
    },
    /** 查询树结构 */
    getTree () {
      getDataMetadataTree('table', {}).then(response => {
        if (response.success) {
          const { data } = response
          this.treeOptions = data
        }
      })
    },
    initCols () {
      this.checkedTableColumns = this.tableColumns.map(col => col.prop)
    },
    handleCheckedColsChange (val) {
      this.tableColumns.forEach(col => {
        if (!this.checkedTableColumns.includes(col.prop)) {
          col.show = false
        } else {
          col.show = true
        }
      })
    },
    handleCommand (command) {
      this.tableSize = command
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.$refs['queryForm'].resetFields()
      this.queryParams.sourceId = ''
      this.queryParams.tableId = ''
      this.handleQuery()
    },
    /** 刷新列表 */
    handleRefresh () {
      this.getList()
    },
    /** 多选框选中数据 */
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 详情按钮操作 */
    handleDetail (row) {
      this.showOptions.data.id = row.id || this.ids[0]
      this.showOptions.showList = false
      this.showOptions.showDetail = true
      this.$emit('showCard', this.showOptions)
    },
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
      this.queryParams.pageNum = 1
      this.queryParams.pageSize = val
      this.getList()
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`)
      this.queryParams.pageNum = val
      this.getList()
    },
    renderContent (h, { node, data, store }) {
      if (node.level === 1) {
        data.icon = 'icon iconfont icon-weidu'
      } else {
        data.icon = 'icon iconfont icon-zhibiao'
      }
      return (
        <span class="custom-tree-node">
          <i class={data.icon}></i>
          <span>{node.label}</span>
        </span>)
    },
    /** 节点单击事件 */
    handleNodeClick (data) {
      if (data.type === 'database') {
        this.queryParams.sourceId = data.id
        this.queryParams.tableId = ''
      } else if (data.type === 'table') {
        this.queryParams.tableId = data.id
        this.queryParams.sourceId = ''
      }
      this.getList()
    },
    keyFormatter (row, column, cellValue, index) {
      if (cellValue === '1') {
        return 'Y'
      } else {
        return 'N'
      }
    },
    nullableFormatter (row, column, cellValue, index) {
      if (cellValue === '1') {
        return 'Y'
      } else {
        return 'N'
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.right-toolbar {
  float: right;
}

::v-deep .custom-tree-node {
  flex: 1;
  display: flex;
  font-size: 14px;
  .icon {
    margin-right: 5px;
  }
}
</style>
