<script lang="ts" setup>
import { ElMessage, ElMessageBox } from 'element-plus'
import { deleteApiReqById, getApiReqByPage } from '@/api/paas/apiReqApi'
import { getModuleByList } from '@/api/paas/moduleApi'
const { pagination, getParams, onSizeChange, onCurrentChange } = usePagination()

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

const state = reactive({
  loading: false,
  curModuleId: 0,
  modules: [] as TreeNode[],
  reqList: [],
  search: {
    keywords: '',
  },
})

const leftTreeRef = ref()
const ModuleRef = ref()

// 查询日志列表
const getReqData = async () => {
  const params = getParams()
  params.moduleId = state.curModuleId
  params.keywords = state.search.keywords
  state.loading = true
  const { data } = await getApiReqByPage(params)
  state.reqList = data.data
  state.loading = false
}

const getModuleData = async () => {
  const { data } = await getModuleByList('')
  const children: TreeNode[] = []
  data.forEach((item: any) => {
    return children.push({
      label: item.moduleTitle,
      moduleId: item.moduleId,
      isLeaf: true,
    })
  })
  state.modules = [
    {
      label: 'API模块列表',
      isLeaf: false,
      moduleId: -1,
      children,
    },
  ]

  if (data && data.length > 0) {
    state.curModuleId = data[0].moduleId
    nextTick(() => {
      leftTreeRef.value.setCurrentKey(data[0].moduleId)
    })
    await getReqData()
  }
}

// 每页数量切换
function sizeChange(size: number) {
  onSizeChange(size).then(() => getReqData())
}

// 当前页码切换（翻页）
function currentChange(page = 1) {
  onCurrentChange(page).then(() => getReqData())
}
// 查询
const onSearch = () => {
  getReqData()
}

onMounted(() => {
  getModuleData()
})

// 模块选择
const onTreeNodeClick = (node: TreeNode) => {
  if (node.isLeaf) {
    state.curModuleId = node.moduleId
    getReqData()
  }
}

// 删除日志
const onReqDel = async (data: { apiTitle: any; reqId: number }) => {
  ElMessageBox.confirm('确认删除吗？', '确认信息').then(async () => {
    await deleteApiReqById(data.reqId)
    await getReqData()
    ElMessage.success({
      message: '删除成功',
      center: true,
    })
  })
}
</script>

<template>
  <div class="absolute-container">
    <page-header title="日志管理" content="日志调用管理" />
    <div class="page-main">
      <LayoutContainer left-side-width="300px" hide-left-side-toggle>
        <template #leftSide>
          <el-scrollbar class="tree">
            <ElTree ref="leftTreeRef" :data="state.modules" default-expand-all node-key="moduleId" @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" />
                </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="apiReqTableRef" v-loading="state.loading" class="list-table" :data="state.reqList" border stripe highlight-current-row>
            <el-table-column type="index" align="center" label="序号" width="60" />

            <el-table-column align="center" width="100" label="接口名称" prop="apiTitle" />

            <el-table-column align="center" label="调用者IP" prop="appTitle" />

            <el-table-column align="center" label="创建时间" prop="reqTime" />

            <el-table-column label="操作" header-align="center" align="center" width="220">
              <template #default="scope">
                <el-button v-if="scope.row.isFixed !== 1" size="small" type="danger" @click="onReqDel(scope.row)">
                  删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination :current-page="pagination.page" :total="pagination.total" :page-size="pagination.size" :page-sizes="pagination.sizes" :layout="pagination.layout" :hide-on-single-page="false" class="pagination" background @current-change="currentChange" @size-change="sizeChange" />
        </div>
      </LayoutContainer>
    </div>
  </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>
