<template>
  <el-row :gutter="20">
    <el-col :span="6">
      <el-card class="box-card tree-wrapper" shadow="always">
        <el-tree
          ref="tree"
          :data="treeOptions"
          node-key="id"
          empty-text="加载中，请稍后"
          :props="defaultProps"
          default-expand-all
          :expand-on-click-node="false"
          @node-click="handleNodeClick"
        >
          <span slot-scope="{ node, data }" class="custom-tree-node">
            <i v-if="node.level === 1" class="iconfont icon-zuzhi tree-folder" />
            <span>{{ node.label }}</span>
          </span>
        </el-tree>
      </el-card>
    </el-col>
    <el-col :span="18">
      <el-card class="box-card" shadow="always">
        <el-form ref="queryForm" :model="queryParams" :inline="true">
          <el-form-item v-for="(item, index) in queryParams.conditions" :label="item.columnName" :prop="item.column" :key="index">
            <el-input
              v-model.trim="queryParams.conditions[index].value"
              clearable
              size="small"
            ></el-input>
          </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="primary"
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
              >新增</el-button>
              <el-button
                type="success"
                icon="el-icon-edit-outline"
                size="mini"
                :disabled="single"
                @click="handleEdit"
              >修改</el-button>
              <el-button
                type="info"
                icon="el-icon-view"
                size="mini"
                :disabled="single"
                @click="handleDetail"
              >详情</el-button>
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                :disabled="multiple"
                @click="handleBatchDelete"
              >删除</el-button>
            </el-button-group>
          </el-col>
        </el-row>

        <el-table
          v-loading="loading"
          :data="tableDataList"
          border
          tooltip-effect="dark"
          :height="tableHeight"
          style="width: 100%;margin: 15px 0;"
          @selection-change="handleSelectionChange"
        >
          <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
              :key="index"
              :prop="item.prop"
              :label="item.label"
              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-edit-outline"
                  @click="handleEdit(scope.row)"
                >修改</el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-view"
                  @click="handleDetail(scope.row)"
                >详情</el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(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"
          :current-page.sync="queryParams.pageNum"
          :page-size.sync="queryParams.pageSize"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-card>
    </el-col>
  </el-row>
</template>

<script>
import { listDataModel, getTableParam } from '@/api/masterdata/datamodel'
import { pageData, delData } from '@/api/masterdata/datamanage'

export default {
  name: 'DataList',
  data() {
    return {
      tableHeight: document.body.offsetHeight - 310 + 'px',
      // 展示切换
      showOptions: {
        data: {},
        showList: true,
        showDetail: false
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总数据条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        tableName: '',
        columns: [],
        conditions: []
      },
      // 左侧树
      treeOptions: [],
      defaultProps: {
        children: 'children',
        label: 'modelName'
      },
      tableName: '',
      // 表格头
      tableColumns: [],
      // 表格数据
      tableDataList: [],
      // 查询参数
      tableParamList: []
    }
  },
  created() {
    this.getTree()
  },
  methods: {
    /** 查询树结构 */
    getTree() {
      listDataModel().then(response => {
        if (response.success) {
          const { data } = response
          const tree = {}
          tree.modelName = '数据模型'
          tree.children = data
          this.treeOptions = []
          this.treeOptions.push(tree)
        }
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 20,
        tableName: this.tableName,
        columns: this.tableColumns.map(column => column.prop),
        conditions: this.tableParamList
      }
      this.handleQuery()
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.showOptions.data = {}
      this.showOptions.showList = false
      this.showOptions.showAdd = true
      this.showOptions.showEdit = false
      this.showOptions.showDetail = false
      this.$emit('showCard', this.showOptions)
    },
    /** 修改按钮操作 */
    handleEdit(row) {
      this.showOptions.data.id = row.id || this.ids[0]
      this.showOptions.showList = false
      this.showOptions.showAdd = false
      this.showOptions.showEdit = true
      this.showOptions.showDetail = false
      this.$emit('showCard', this.showOptions)
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      this.showOptions.data.id = row.id || this.ids[0]
      this.showOptions.showList = false
      this.showOptions.showAdd = false
      this.showOptions.showEdit = false
      this.showOptions.showDetail = true
      this.$emit('showCard', this.showOptions)
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('选中数据将被永久删除, 是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delData({ id: row.id, tableName: this.tableName, datas: { id: row.id }}).then(response => {
          if (response.success) {
            this.$message.success('删除成功')
            this.getList()
          }
        })
      }).catch(() => {
      })
    },
    /** 批量删除按钮操作 */
    handleBatchDelete() {
      if (!this.ids.length) {
        this.$message({
          message: '请先选择需要操作的数据',
          type: 'warning'
        })
      }
      this.$message.warning('不支持批量删除')
    },
    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()
    },
    /** 节点单击事件 */
    handleNodeClick(data) {
      if (data.id) {
        getTableParam(data.id).then(response => {
          if (response.success) {
            const { data } = response
            this.tableName = data.tableName
            this.tableColumns = data.columnList
            this.tableParamList = data.queryList
            this.queryParams.tableName = data.tableName
            this.queryParams.columns = data.columnList.map(column => column.prop)
            this.queryParams.conditions = data.queryList
            this.$nextTick(() => {
              // 获取表格数据
              this.getList()
            })
          }
        })
      }
    },
    /** 查询列表 */
    getList() {
      this.loading = true
      pageData(this.queryParams).then(response => {
        this.loading = false
        if (response.success) {
          const { data } = response
          this.tableDataList = data.data
          this.total = data.total
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.right-toolbar {
  float: right;
}
.el-card ::v-deep .el-card__body {
  height: calc(100vh - 170px);
}
.tree-wrapper {
  overflow-y: auto;
  ::v-deep .custom-tree-node {
    flex: 1;
    display: flex;
    font-size: 14px;
    .tree-folder {
      margin-right: 5px;
      color: rgb(246 207 7);
    }
  }
}

</style>
