<route lang="yaml">
meta:
  enabled: false
</route>

<script lang="ts" setup>
import { getModelTreeData } from '@/api/model/modelApi'
import { getTableDataByList, getTableFieldsByList } from '@/api/database/dbApi'

interface TreeNode {
  label: string
  isLeaf: boolean
  value: number
  objectCode: string
  children?: TreeNode[]
}

const state = reactive({
  loading: false,
  keywords: '',
  treeData: [] as TreeNode[],
  curModelId: 0,
  curObjectId: 0,
  curObjectCode: '',
  dialogState: 1,
  tableFields: [],
  tableDataList: [],
  search: {
    keywords: '',
  },
})

const leftTreeRef = ref()
const getTreeData = async () => {
  state.loading = true
  const { data } = await getModelTreeData()
  state.treeData = data
  state.loading = false

  if (data && data.length > 0) {
    const firstModel = data[0]
    state.curModelId = firstModel.value
    if (firstModel.children && firstModel.children.size > 0) {
      const firstObject = firstModel.children[0]
      nextTick(() => {
        leftTreeRef.value.setCurrentKey(firstObject.value)
      })
    }
  }
}

const getTableFields = async () => {
  const tableName = state.curObjectCode
  const res = await getTableFieldsByList(tableName)
  state.tableFields = res.data
}

const getTableData = async () => {
  const tableName = state.curObjectCode
  const res = await getTableDataByList(tableName)
  state.tableDataList = res.data
}

const getData = () => {
  if (state.curObjectCode !== '') {
    state.loading = true
    getTableFields()
    getTableData()
    state.loading = false
  }
}

onMounted(() => {
  getTreeData()
})

// 对象选择
const onTreeNodeClick = (node: TreeNode) => {
  if (node.isLeaf) {
    state.curObjectId = node.value
    state.curObjectCode = node.objectCode
    getTableFields()
    getTableData()
  }
  else {
    state.curModelId = node.value
  }
}

const tableDataRef = ref()

// 检索对象
const onSearch = () => {
  getData()
}
</script>

<template>
  <div class="absolute-container">
    <tool-header title="数据仓库">
      <template #right />
    </tool-header>
    <div class="page-main">
      <LayoutContainer left-side-width="250px" hide-left-side-toggle>
        <template #leftSide>
          <el-scrollbar class="tree">
            <ElTree ref="leftTreeRef" :data="state.treeData" default-expand-all node-key="modelId" @node-click="onTreeNodeClick">
              <template #default="{ node, data }">
                <div class="custom-tree-node">
                  <div class="label" :title="node.label">
                    <el-icon v-if="data.isLeaf" size="16px" style="margin-right: 5px;">
                      <svg-icon name="uim:box" />
                    </el-icon>
                    <el-icon v-else size="16px" style="margin-right: 5px;">
                      <svg-icon name="bx:bxs-component" />
                    </el-icon>
                    <div class="text">
                      {{ data.label }}
                    </div>
                  </div>
                </div>
              </template>
            </ElTree>
          </el-scrollbar>
        </template>

        <div class="container">
          <tool-bar>
            <template #right>
              <el-input v-model="state.search.keywords" placeholder="请输入名称，支持模糊查询" style="padding-right: 10px;" clearable />
              <el-button type="primary" @click="onSearch">
                <template #icon>
                  <el-icon>
                    <svg-icon name="i-ep:search" />
                  </el-icon>
                </template>
              </el-button>
            </template>
          </tool-bar>

          <el-table ref="tableDataRef" v-loading="state.loading" class="list-table" :data="state.tableDataList" size="small" border stripe highlight-current-row height="100%">
            <el-table-column type="index" label="序号" header-align="center" align="center" width="70" />

            <template v-for="(field, index) in state.tableFields" :key="index">
              <el-table-column :prop="field.attrCode" :label="field.attrCode" header-align="center" align="center" />
            </template>
          </el-table>
        </div>
      </LayoutContainer>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.absolute-container {
  :deep(.left-side) {
    display: flex;
    flex-direction: column;
    height: 100%;

    .btns {
      display: inline-flex;
      width: 100%;
      margin-bottom: var(--container-padding);

      .add {
        width: 100%;
      }
    }

    .tree {
      flex: 1;
      overflow-y: auto;

      .el-tree {
        .el-tree-node__content {
          height: 40px;
        }

        .is-current > .el-tree-node__content {
          background-color: var(--el-color-primary-light-9);
        }

        .custom-tree-node {
          flex: 1;
          position: relative;
          width: 0;
          height: 100%;
          display: flex;
          flex-direction: column;
          justify-content: center;

          .label {
            display: inline-flex;
            align-items: center;
            width: calc(100% - 10px);
            color: var(--el-text-color-primary);

            .text {
              @include text-overflow;
            }
          }

          &:hover {
            .actions {
              display: block;
            }
          }

          .actions {
            display: none;
            position: absolute;
            right: 10px;
            top: 50%;
            transform: translateY(-50%);

            .el-button {
              padding: 5px 8px;
            }
          }
        }
      }
    }
  }
}
</style>
