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

<script lang="ts" setup>
import { ElMessage, ElMessageBox } from 'element-plus'
import DSModel from './components/model.vue'
import DSEntity from './components/entity.vue'
import { deleteModelById, getModelByList } from '@/api/model/modelApi'
import { deleteEntityById, getEntityByList } from '@/api/model/entityApi'

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

interface Entity {
  modelId: number
  entityId: number
  entityTitle: string
  entityCode: string
  entityComment: string
}

const state = reactive({
  loading: false,
  keywords: '',
  models: [] as TreeNode[],
  curModelId: 0,
  entityList: [] as Entity[],
  dialogState: 1,
  search: {
    keywords: '',
  },
})

const leftTreeRef = ref()
const modelRef = ref()
const entityTableRef = ref()
const entityRef = ref()

const getModelList = async () => {
  const { data } = await getModelByList('')
  const children: TreeNode[] = []
  data.forEach((item: any) => {
    return children.push({
      label: item.modelTitle,
      modelId: item.modelId,
      isLeaf: true,
    })
  })
  state.models = [
    {
      label: '模型库',
      isLeaf: false,
      modelId: -1,
      children,
    },
  ]

  if (data && data.length > 0) {
    state.curModelId = data[0].modelId
    window.console.log(`curModelId:${data[0].modelId}`)
    const res = await getEntityByList({ modelId: state.curModelId })
    state.entityList = res.data
  }
}

const getEntityList = async () => {
  const curModelId = state.curModelId
  if (curModelId > 0) {
    state.loading = true
    const { data } = await getEntityByList({ modelId: curModelId })
    state.entityList = data
    state.loading = false
  }
}

onMounted(() => {
  getModelList()
})

const onModelAdd = () => {
  modelRef.value.showAddModel()
}

const onModelEdit = (data: { modelId: number }) => {
  const modelId = data.modelId
  modelRef.value.showEditModel(modelId)
}

const onModelDelete = (data: any) => {
  ElMessageBox.confirm(`确认删除「${data.modelTitle}」吗？`, '确认信息').then(async () => {
    await deleteModelById(data.modelId)
    await getModelList()
    ElMessage.success({
      message: '删除成功',
      center: true,
    })
  })
}

const onTreeNodeClick = (node: TreeNode) => {
  if (node.isLeaf) {
    state.curModelId = node.modelId
    getEntityList()
  }
}

const onSearch = () => {
  getEntityList()
}

const onEntityAdd = () => {
  if (state.curModelId > 0) {
    entityRef.value.showAddModel(state.curModelId)
  }
}

const onEntityEdit = (row: any) => {
  const { modelId, entityId } = row
  entityRef.value.showEditModel(modelId, entityId)
}

const onEntityDelete = (row: any) => {
  const { modelId, entityId, entityTitle } = row
  ElMessageBox.confirm(`确认删除「${entityTitle}」吗？`, '确认信息').then(async () => {
    await deleteEntityById(modelId, entityId)
    await getEntityList()
    ElMessage.success({
      message: '删除成功',
      center: true,
    })
  })
}
</script>

<template>
  <div class="absolute-container">
    <tool-header title="数据模型">
      <template #right>
        <el-button plain @click="onModelAdd">
          <template #icon>
            <el-icon>
              <svg-icon name="i-ep:plus" />
            </el-icon>
          </template>
          新增模型
        </el-button>
      </template>
    </tool-header>

    <div class="page-main">
      <LayoutContainer left-side-width="300px" hide-left-side-toggle>
        <template #leftSide>
          <el-scrollbar class="tree">
            <ElTree ref="leftTreeRef" :data="state.models" default-expand-all @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>
                    <div class="text">
                      {{ data.label }}
                    </div>
                  </div>
                  <div v-if="data.isLeaf" class="actions">
                    <el-button-group>
                      <el-button type="info" plain size="small" @click.stop="onModelEdit(data)">
                        <template #icon>
                          <el-icon>
                            <svg-icon name="i-ep:edit" />
                          </el-icon>
                        </template>
                      </el-button>
                      <el-button type="danger" plain size="small" @click.stop="onModelDelete(data)">
                        <template #icon>
                          <el-icon>
                            <svg-icon name="i-ep:delete" />
                          </el-icon>
                        </template>
                      </el-button>
                    </el-button-group>
                  </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>

              <el-button plain size="default" @click.stop="onEntityAdd">
                <template #icon>
                  <el-icon>
                    <svg-icon name="i-ep:plus" />
                  </el-icon>
                </template>
                新增实体
              </el-button>
            </template>
          </tool-bar>

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

            <el-table-column prop="entityTitle" label="名称" header-align="center" align="center" width="200" />

            <el-table-column prop="entityCode" label="编码" header-align="center" align="center" width="150" />

            <el-table-column prop="entityComment" label="说明" header-align="center" align="left" />

            <el-table-column label="操作" header-align="center" align="center" width="200">
              <template #default="scope">
                <el-button type="primary" size="small" plain @click="onEntityEdit(scope.row)">
                  编辑
                </el-button>
                <el-button type="danger" size="small" plain @click="onEntityDelete(scope.row)">
                  删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </LayoutContainer>
    </div>
    <DSEntity ref="entityRef" @success="getEntityList" />
    <DSModel ref="modelRef" @success="getModelList" />
  </div>
</template>

<style lang="scss" scoped>
.container {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.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>
