<template>
    <!-- 测试devkind = 'SYS' -->
  <div style="width: 100%;;display: flex;">
    <div style="width: 15%;;background-color: #F2F6FC;max-height: 85vh;overflow: auto;">
      <div style="display: flex;padding-top: 1%;padding-top: 5%;">
        <el-input
          v-model="inputNode"
          style="width:90%;margin-left: 5%;background-color: #ebeef5;"
          class="w-50 m-2"
          :prefix-icon="Search" 
        />
        <!-- <el-button style="margin-left: 2%;width: 10%;background-color: #F7F7F7" :icon="Filter"/> -->
        <!-- <el-button type="primary" @click="showAdd" style="margin-left: 5%;width: 10%;" :icon="Plus"/> -->
      </div>
      <!-- <Plus style="width: 1em; height: 1em; margin-left:90%;cursor: pointer;color: #337ecc;" @click="showAddTree"/> -->
      <el-tree :expand-on-click-node="false" ref="treeRef" :filter-node-method="filterNode" :current-node-key="currentNodeKey" class="treeLeft" :data="data" @node-click="handleNodeClick" node-key="id" style="margin-left: 5%;margin-top: 5%;width: 90%;background-color: transparent;" default-expand-all :key="valueKet">
          <template #default="{ node, data }">
            <span  style="justify-content: space-between;display: flex;width: 100%;align-items: center;">
              <div class="custom-tree-node">
                   <svg-icon icon-class="model2" style="color: #eebe77;" v-if="data.nodeType=='MODEL'"/>
                  <svg-icon icon-class="model" dstyle="color: #13E03B;" v-if="data.nodeType=='SERVICE'"/>
                  <svg-icon svg-icon icon-class="cate" style="color: red;" v-if="data.nodeType=='TREE'"/>
                <span>{{ node.label }}</span>
              </div>
              <!-- <div style="margin-right: 1%;position: absolute;margin-left: 75%;">
                <el-dropdown trigger="hover" @click.stop v-if="currentNodeKey === data.id&&data.nodeType!=='SERVICE'">
                    <el-icon class="el-icon--right" style="color: black;">
                      <plus />
                    </el-icon>
                  <template #dropdown>
                    <el-dropdown-menu>
                      <el-dropdown-item style="display: flex;" @click="add1Level">
                        <el-icon class="el-icon--right" style="color: black;">
                          <CirclePlus />
                        </el-icon>
                        <div>
                          添加同级
                        </div>
                      </el-dropdown-item>
                      <el-dropdown-item style="display: flex;"  @click="addNextLevel">
                        <el-icon class="el-icon--right" style="color: black;">
                          <Connection />
                        </el-icon>
                        <div>
                          新建下级
                        </div>
                      </el-dropdown-item>
                      <divider/>
                      <el-dropdown-item style="display: flex;"  @click="delAll" divided>
                        <el-icon class="el-icon--right" style="color: black;">
                          <CircleClose />
                        </el-icon>
                        <div>
                          删除节点
                        </div>
                      </el-dropdown-item>
                    </el-dropdown-menu>
                  </template>
                </el-dropdown>
              </div> -->
            </span>
          </template>
        </el-tree>
    </div>
    <div style="width: 84%;margin-left: 1%;padding-top: 0.5%;" class="tab-container">
      <div>
        <el-table 
          :data="tableData" 
          style="width: 98%;margin-left: 1%;margin-top: 0.5%;"
          :cell-style="{ padding:'5px' }"
          :header-cell-style="{height: heightAll*0.01+'px',}"
          :row-style="{ fontSize: '16px',textAlign:'center'}"
          border >
          <el-table-column type="index" label="序号" width="80">
            <template #default="{ $index }">
              <div style="text-align: center;">
                {{ $index + 1 }}
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="服务名称"  width="200">
            <template #default="scope">
              <div style="color: #409EFF;cursor: pointer;" @click="showDe(scope.row)">
                {{scope.row.name}}
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="type" label="接口类型"  width="200">
          </el-table-column>
          <el-table-column prop="url" label="接口地址"  show-overflow-tooltip/>
          <el-table-column prop="rqtype" label="请求方式" width="200"/>
          <el-table-column prop="rptype" label="响应类型" width="150">
            <template #default="scope">
              <div style="text-align: center;display: flex;" v-if="scope.row.rptype=='1'">
                JSON
              </div>
              <div style="text-align: center;display: flex;" v-if="scope.row.rptype=='2'">
                XML
              </div>
              <div style="text-align: center;display: flex;" v-if="scope.row.rptype=='3'">
                HTML
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="cateCode" label="服务分类" width="140" show-overflow-tooltip>
          </el-table-column>
          <el-table-column prop="audit" label="测试状态" width="140">
            <template #default="scope">
              <div style="text-align: center;display: flex;" v-if="scope.row.audit=='3'">
                <el-tag class="ml-2" type="success">已测试</el-tag>
              </div>
              <div style="text-align: center;display: flex;" v-if="scope.row.audit=='2'">
                <el-tag class="ml-2" type="danger">未测试</el-tag>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="address" label="操作" width="150">
              <template #default="scope">
                  <!-- <div style="display: flex;justify-content: space-between;width: 100%;">
                      <el-button type="danger" text size="mini" style="margin-left: -5%;" @click="handleDelete(scope.row)">注销</el-button>
                      <el-button @click="showDe(scope.row)" type="primary" text size="mini" style="margin-left: 0%;">查看</el-button>
                      <el-button @click="testSer(scope.row)" type="primary" text size="mini" style="margin-left: 0%;">测试</el-button>
                      <el-button @click="testSer(scope.row)" type="primary" text size="mini" style="margin-left: 0%;">审核</el-button>
                  </div> -->
                  <div style="display: flex;">
                    <el-button @click="testSer(scope.row)" type="primary" text size="mini" style="margin-left: 0%;">测试</el-button>
                    <el-button @click="showLog(scope.row)" type="primary" text size="mini" style="margin-left: 0%;">日志</el-button>
                  </div>
              </template>
          </el-table-column>
        </el-table>
      </div>
      
        <el-dialog @close="clearAdd" v-model="dialogVisible" title="" width="60%" destroy-on-close :key="tableKey">
          <div style="display: flex;align-items: center;">
            <div v-if="detailJson.rqtype==='GET'">
              <el-tag class="ml-2" type="warning">GET</el-tag>
            </div>
            <div v-if="detailJson.rqtype==='POST'">
              <el-tag class="ml-2" type="warning">POST</el-tag>
            </div>
            <div style="margin-left: 1%;">
              {{ detailJson.name }}:
            </div>
            <div style="margin-left: 1%;">
              {{ detailJson.url }}
            </div>
          </div>
          <el-descriptions style="margin-top: 1%;" :column="3" border>
            <el-descriptions-item label="接口类型">{{ detailJson.type }}</el-descriptions-item>
            <el-descriptions-item label="请求方式">{{ detailJson.rqtype }}</el-descriptions-item>
            <el-descriptions-item label="响应类型">{{ detailJson.rptype }}</el-descriptions-item>
            <el-descriptions-item label="服务分类">{{ detailJson.cateCode }}</el-descriptions-item>
          </el-descriptions>
          <div style="margin-top:4%;font-size: 18px;">
            请求参数
          </div>
           <el-table 
            style="margin-top: 1%;width: 98%;"
            :data="tableDataCan" 
            :cell-style="{ textAlign: 'center',padding:'2px 0' }"
            :header-cell-style="{ textAlign: 'center'}"
            :row-style="{ height: heightAll*0.01+'px',fontSize: '16px',textAlign:'center'  }"
            border >
                <el-table-column prop="paramCode" label="参数字段">
                </el-table-column>
                <el-table-column prop="paramName" label="参数名称">
                </el-table-column>
                <el-table-column prop="paramType" label="参数类型" width="200">
                </el-table-column>
                <el-table-column prop="paramValue" label="参数示例">
                  
                </el-table-column>
                <el-table-column prop="paramNote" label="参数说明" show-overflow-tooltip>
                </el-table-column>
            </el-table>
            <div style="margin-top:4%;font-size: 18px;">
              返回参数
            </div>
            <el-input readonly  placeholder="" :rows="8" type="textarea" v-model="dataJsonXiang" size="mini" text style="margin-top: 1%;width: 98%;" ></el-input>
          <template #footer>
            <span class="dialog-footer">
              <el-button type="primary" @click="dialogVisible = false" size="mini">
                确定
              </el-button>
            </span>
          </template>
        </el-dialog>
        <el-dialog @close="clearAdd" v-model="dialogVisibleTest" title="" width="60%" destroy-on-close :key="tableKey">
          <div style="display: flex;align-items: center;width: 98%;">
            <div v-if="detailJson.rqtype==='GET'">
              <el-tag class="ml-2" type="warning">GET</el-tag>
            </div>
            <div v-if="detailJson.rqtype==='POST'">
              <el-tag class="ml-2" type="warning">POST</el-tag>
            </div>
            <div style="margin-left: 1%;">
              {{ detailJson.name }}:
            </div>
            <div style="margin-left: 1%;">
              {{ detailJson.url }}
            </div>
            <svg-icon @click="test" icon-class="startTest" style="margin-left: 1%;width: 50px;height: 25px;cursor: pointer;"/>
          </div>
          <div style="margin-top:2%;font-size: 18px;">
            请求参数
          </div>
           <el-table 
            style="margin-top: 1%;width: 98%;"
            :data="tableDataCan" 
            :cell-style="{ textAlign: 'center',padding:'2px 0' }"
            :header-cell-style="{ textAlign: 'center'}"
            :row-style="{ height: heightAll*0.01+'px',fontSize: '16px',textAlign:'center'  }"
            border >
                <el-table-column prop="paramCode" label="参数字段">
                </el-table-column>
                <el-table-column prop="paramName" label="参数名称">
                </el-table-column>
                <el-table-column prop="paramType" label="参数类型" width="200">
                </el-table-column>
                <el-table-column prop="paramValue" label="测试值">
                  <template #default="scope">
                      <div style="width: 100%;">
                          <el-input placeholder="请填写" type="primary" class="noBor" v-model="scope.row.paramValue" size="mini" text style="margin-left: 0%;"></el-input>
                      </div>
                  </template>
                </el-table-column>
                <el-table-column prop="paramNote" label="参数说明" show-overflow-tooltip>
                </el-table-column>
            </el-table>
            <div style="margin-top:4%;font-size: 18px;">
              返回参数
            </div>
            <el-input  placeholder="" :rows="8" type="textarea" v-model="dataReturn" size="mini" text style="margin-top: 1%;width: 98%;" ></el-input>
          <template #footer>
            <span class="dialog-footer">
            </span>
          </template>
        </el-dialog>
        <el-dialog @close="clearFromLev" title="添加目录" v-model="dialogVisibleLevel" width="50%" destroy-on-close :key="tableKey">
          <el-table 
            style="margin-top: 1%;width: 98%;"
            :data="tableDataLog" 
            :cell-style="{ textAlign: 'center',padding:'2px 0' }"
            :header-cell-style="{ textAlign: 'center'}"
            :row-style="{ height: heightAll*0.01+'px',fontSize: '16px',textAlign:'center'  }"
            border >
                <el-table-column prop="createBy" label="测试发起人">
                </el-table-column>
                <el-table-column prop="runTm" label="测试时间">
                </el-table-column>
                <el-table-column prop="execTm" label="测试耗时" show-overflow-tooltip>
                </el-table-column>
                <el-table-column prop="returnData" label="测试返回结果" show-overflow-tooltip>
                </el-table-column>
            </el-table>
          <template #footer>
              <span class="dialog-footer">
                <el-button type="primary" @click="dialogVisibleLevel = false" size="mini">
                  确定
                </el-button>
            </span>
          </template>
        </el-dialog>
        <el-dialog  v-model="dialogVisibleDe" title="" width="50%" @close="" destroy-on-close class="custom-dialog-bg">
          <el-form size="mini" :key="tableKey" style="margin-top:1%;width: 100%;" :model="formJi" label-position="right"
                 ref="formRefJi" label-width="120px" :rules="rulesJi">
            <el-row :gutter="48">
              <el-col :span="10">
                <el-form-item label="服务名称:" prop="name" style="">
                  {{formJi.name}}
                </el-form-item>
              </el-col>
              <el-col :span="10">
                <el-form-item label="接口类型:" prop="" style="">
                  <div style="display: flex;width: 100%;justify-content: space-between;">
                    {{formJi.type}}
                  </div>

                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="48">
              <el-col :span="20">
                <el-form-item label="接口地址:" prop="url" style="">
                  {{formJi.url}}
                </el-form-item>
              </el-col>
              <el-col :span="10">
                <el-form-item label="请求方式:" prop="" style="">
                  {{formJi.rqtype}}
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="48">
              <el-col :span="10">
                <el-form-item label="响应类型:" prop="" style="display: flex; align-items: center;">
                  {{formJi.rptype}}
                </el-form-item>
              </el-col>
              <el-col :span="10">
                <el-form-item label="服务分类:" prop="cateCode" style="">
                  {{formJi.cateCode}}
                  <!-- <el-cascader :disabled="isEdit" v-model="formJi.cateCode" :options="cascaderOptions" clearable
                              style="width: 100%;"></el-cascader> -->
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="48">
              <el-col :span="13">
                <el-form-item label="服务说明:">
                  {{formJi.intro}}
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-dialog>
    </div>
  </div>
</template>
<script   setup>
import {getCatalog} from "@/api/service/catalog";
import { Plus,Search,Filter,Promotion,Check  } from '@element-plus/icons-vue'
import { reactive } from 'vue'
import { modelTreeSelect,getSerDe,addService,addServiceParam,delService,addTree,deTree,getServiceInfo,testService,testLog } from "@/api/service/info";
import { ref, onMounted, onUnmounted, nextTick,onBeforeMount } from 'vue';
import JsonViewer from 'vue-json-viewer'
import 'vue-json-viewer/style.css' 
import  JsonEditorVue from 'json-editor-vue3'
import { cloneDeep } from 'lodash'
import { useClipboard } from "@vueuse/core";

const { proxy } = getCurrentInstance();
const JsonAdd= ref(JSON.stringify({ data: "初始值1" }))
const exampleAdd = ref('')
const dialogVisibleLevel = ref(false)
const dialogVisibleDe = ref(false)
const detail = ref({
  name:'',
  rqtype:'',
  rptype:''
})
const tableDataLog = ref([])
const show1Lev = ref(true)
const inputNode =ref('')
const isEdit = ref(true)
const dataReturn = ref('')
const optionsCan = ref([
  {
    label:"string",
    value:'string'
  },
  {
    label:"int",
    value:'int'
  },
  {
    label:"boolean",
    value:'boolean'
  },
  {
    label:"array",
    value:'array'
  },
  {
    label:"object",
    value:'object'
  },
  {
    label:"number",
    value:'number'
  },
  {
    label:"null",
    value:'null'
  },
  {
    label:"any",
    value:'any'
  },
  
])
const currentNodeKey = ref('')
const options =  ref([
    {label:'开发中',
    value:'1'
    },
    {label:'运行中',
    value:'2'
    },
])
const dataJsonXiang = ref([])
const parTree = ref({})
const valueKet = ref(0)
const tableDataCan = ref([
    {
      itemName:'',
      itemCode:'',
      paramType:'',
      paramNote:''
    }
])
const dialogVisibleTest = ref(false)
const optionsMdid = ref([])
const optionsRqtype = ref([
    {label:'GET',
    value:'GET'
    },
    {label:'POST',
    value:'POST'
    },
])
const optionsType=  ref([
    {label:'RESTful',
    value:'RESTful'
    },
    {label:'WebService',
    value:'WebService'
    },
    {label:'HTTP',
    value:'HTTP'
    },
    {label:'WebSocket',
    value:'WebSocket'
    },
])
const data =  ref([])
const select =  ref('1')
const activeName =  ref('first')
const title =  ref('')
const titleTree =  ref('')
const currentPage =  ref(1)
const total =  ref(1)
const tableData = ref([])
const tableheight = window.innerHeight*0.7
const heightAll = window.innerHeight
const dialogVisible = ref(false)
const dialogVisibleTree = ref(false)
const isAdd = ref(false)
const isAddTa = ref(false)
const treeId = ref('');
const valueSta = ref('1');
const parMdid = ref('')
const example = ref('');
const tableDataCanAdd = ref([
  {
      itemName:'',
      itemCode:'',
      paramType:'',
      paramNote:''
    }
])
const formZu = ref({
  itemName:'',
  itemTp:'',
  itemEn:'',
  itemTp:'',
  itemDataTp:'',
  itemDefaultVal:'',
  itemUnit:'',
  itemNotes:'',
});
const parId= ref('')
const formJi = ref({
  name:'',
  cateCode:'',
  type:'',
  url:'',
  rqtype:'',
  rptype:'',
  intro:'',
});
const rulesJi = reactive({
  name: [{ required: true, message: '必填', trigger: 'blur' }],
  url: [{ required: true, message: '必填', trigger: 'blur' }],
  cateCode: [{ required: true, message: '必填', trigger: 'blur' }],
});
const formRefJi = ref();
const formAdd = ref({
  name:'',
  cateCode:'',
  type:'',
  url:'',
  rqtype:'',
  rptype:'',
  intro:'',
  mdid:''
});
const rulesAdd = reactive({
  mdid: [{ required: true, message: '必填', trigger: 'blur' }],
  name: [{ required: true, message: '必填', trigger: 'blur' }],
  url: [{ required: true, message: '必填', trigger: 'blur' }],
  cateCode: [{ required: true, message: '必填', trigger: 'blur' }],
});
const formRefAdd = ref();
const treeRef = ref(null);
const formTree = ref({
  itemName:'',
  catePid:'',
  itemNo:'',
  itemNotes:''
});
const rulesTree = reactive({
  itemNo: [{ required: true, message: '必填', trigger: 'blur' }],
  catePid: [{ required: true, message: '必填', trigger: 'blur' }],
  itemName: [{ required: true, message: '必填', trigger: 'blur' }],
});
const formRefTree = ref();
const formLev = ref({
  itemName:'',
  itemNo:'',
  itemNotes:'',
});
const rulesLev = reactive({
  itemNotes: [{ required: true, message: '必填', trigger: 'blur' }],
  itemNo: [{ required: true, message: '必填', trigger: 'blur' }],
  itemName: [{ required: true, message: '必填', trigger: 'blur' }],
});
const formRefLev = ref();
const cascaderOptions = ref([])
const props1 = ref({
  checkStrictly: true,
})
const detailJson = ref('')
const parOptions = ref([])
watch(inputNode, (val) => {
  treeRef.value?.filter(val); // 调用树的过滤方法
});
const filterNode = (value, data) => {
  if (!value) return true; // 空搜索时显示所有节点
  return data.label.includes(value); // 检查节点标签是否包含关键字
};

const copied = ref(false);
function showLog(row){
  dialogVisibleLevel.value = true
  var par = {
    serId:row.srvId
  }
  testLog(par).then(res=>{
    tableDataLog.value = res.rows
  })
}
function test(){
  var par = detailJson.value
  par.params = tableDataCan.value
  testService(par).then(res=>{
    console.log(res)
    dataReturn.value = res.msg
    handleNodeClick(parMdid.value)
  })
}
function testSer(row){
  getSerDe(row.srvId).then(res=>{
    if(res.code===200){
      detailJson.value = res.data.ptService
      console.log(detailJson.value)
      dialogVisibleTest.value = true
      tableDataCan.value = res.data.list
    }
  })
}
function showDe(row){
  getSerDe(row.srvId).then(res=>{
    if(res.code===200){
      dialogVisibleDe.value = true
      parOptions.value.forEach(item=>{
        if(res.data.ptService.cateCode === item.cateCode){
          res.data.ptService.cateCode = item.cateName
        }
      })
      if(res.data.ptService.rptype === '1'){
        res.data.ptService.rptype = 'JSON'
      }
      if(res.data.ptService.rptype === '2'){
        res.data.ptService.rptype = 'XML'
      }
      if(res.data.ptService.rptype === '3'){
        res.data.ptService.rptype = 'HTML'
      }
      formJi.value = res.data.ptService
    }
  })
}
function filterModelNodes(nodes) {
  if (!Array.isArray(nodes)) return [];
  
  const result = [];
  for (const node of nodes) {
    // 递归处理子节点（如果有）
    const filteredChildren = node.children ? filterModelNodes(node.children) : [];
    
    if (node.nodeType === 'MODEL') {
      // 保留当前节点，并更新其子节点
      node.value = node.id
      result.push({
        ...node,
        children: filteredChildren
      });
    } else {
      // 删除当前节点，将其子节点提升到当前层级
      result.push(...filteredChildren);
    }
  }
  return result;
}
function clearAdd(){
  tableDataCan.value = [
  ]
  dataReturn.value = ''
}
function extractModelNodesDFSIterative(root) {
  const result = [];
  const stack = root;
  
  while (stack.length > 0) {
    const node = stack.pop();
    if (node.nodeType === 'MODEL') {
      result.push(node);
    }
    
    // 子节点逆序入栈（保证从左到右遍历）
    if (node.children) {
      for (let i = node.children.length - 1; i >= 0; i--) {
        stack.push(node.children[i]);
      }
    }
  }
  return result;
}
function delAll(){
  proxy.$modal.confirm('是否确认删除？').then(function () {
    return deTree(parTree.value.id);
  }).then(() => {
    getTreeLeft();
    proxy.$modal.msgSuccess("删除成功");
    dialogVisibleLevel.value = false
  }).catch(() => {});
}
function clearFromLev(){
  tableDataLog.value = []
}
function addNextLevel(){
  dialogVisibleLevel.value = true
  show1Lev.value = false
}
function saveAddNextLevel(){
   formRefLev.value.validate((valid) => {
    if(valid){
      var par = formLev.value
      par.treeType = 'MODEL'
      par.treePid = parTree.value.id
      addTree(par).then(res=>{
        if(res.code===200){
          proxy.$modal.msgSuccess("新增成功");
          getTreeLeft()
          dialogVisibleLevel.value = false
        }
      })
    }
  });
}
function add1Level(){
  dialogVisibleLevel.value = true
  show1Lev.value = true
}
function saveAdd1Level(){
  formRefLev.value.validate((valid) => {
    if(valid){
      var par = formLev.value
      par.treeType = 'MODEL'
      par.treePid = parTree.value.pid
      addTree(par).then(res=>{
        if(res.code===200){
          proxy.$modal.msgSuccess("新增成功");
          getTreeLeft()
          dialogVisibleLevel.value = false
        }
      })
    }
  });
}
async function delSer(){
  proxy.$modal.confirm('是否确认删除？').then(function () {
    return delService(parId.value.id);
  }).then(() => {
    getTreeLeft();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}
async function addSer(){
  console.log(JsonAdd.value)
  var valid
  await formRefAdd.value.validate((valid1) => {
    valid = valid1
  });
  console.log(valid) 
  if(valid){
    var par = formAdd.value
    par.example = exampleAdd.value
    par.rpcontent = JsonAdd.value
    par.cateCode = par.cateCode[0]
    console.log(par)

    await addService(par).then(res=>{
      if(res.code===200){
        var parCan = tableDataCanAdd.value
        parCan.srvId = res.data.srvId
        addServiceParam(parCan).then(res1=>{
          if(res1.code===200){
            proxy.$modal.msgSuccess("新增成功");
            getTreeLeft()
          }
        })
      }
    })
    
    
  }

}
function showAdd(){
  dialogVisible.value = true
}
async function handleNodeClick(node,data,event){
  // parTree.value = data.data
  // currentNodeKey.value = data.data.id
 
  // isEdit.value = false
  // parId.value = data.data
  // await getSerDe(data.data.id).then(res=>{
  //   if(res.data.ptService){
  //     detail.value = res.data.ptService
  //     tableDataCan.value = res.data.list
  //     dataReturn.value = res.data.returnList
  //     console.log(tableDataCan.value)
  //     dataJsonXiang.value = res.data.ptService.rpcontent
  //     formJi.value = res.data.ptService
  //     example.value = res.data.ptService.example
  //   }
    
  // })
  parMdid.value = data.data.id
  var par = {
    mdid:data.data.id
  }
  console.log(data.data.id)
  await getServiceInfo(par).then(res=>{
    if(res.data){
      tableData.value = res.data.serviceList
      
    }
  })
  parOptions.value.forEach(option => {
    option.label = option.cateName;
    option.value = option.cateCode;
  });
  console.log(parOptions.value)
  for(var i = 0; i < parOptions.value.length; i++){
    for(var i1 = 0; i1 < tableData.value.length; i1++){
      console.log(i1);
      if(parOptions.value[i].value === tableData.value[i1].cateCode){
         tableData.value[i1].cateCode = parOptions.value[i].label
      }
    }
  }
  var par = [
    {
      value:'1',
      label:'JSON'
    },
    {
      value:'2',
      label:'XML'
    },
    {
      value:'3',
      label:'HTML'
    }
  ]
  for(var i = 0; i < par.length; i++){
    for(var i1 = 0; i1 < tableData.value.length; i1++){
      if(par[i].value === tableData.value[i1].rptype){
         tableData.value[i1].rptype = par[i].label
      }
    }
  }
}
async function getTreeLeft(){
  var par = {
    params:{
      level:'2',
      devkind:'APP'
    }
  }
  await modelTreeSelect(par).then(res=>{
    par = res.data
    data.value = res.data
    
  })
  optionsMdid.value = filterModelNodes(par)
  console.log(optionsMdid.value)
}
function renameTreeProperties(tree) {
  // 深拷贝避免修改原始数据（可选，根据需求）
  const newTree = cloneDeep(tree); // 使用 lodash
  // 或：const newTree = JSON.parse(JSON.stringify(tree)); // 原生方法（不兼容函数等）

  // 递归处理函数
  const processNode = (node) => {
    // 重命名属性
    if ('cateName' in node) {
      node.label = node.cateName;
      delete node.cateName;
    }
    if ('cateCode' in node) {
      node.value = node.cateCode;
      delete node.cateCode;
    }

    // 递归处理子节点
    if (node.children && node.children.length > 0) {
      node.children.forEach(child => processNode(child));
    }
  };

  // 遍历根节点
  newTree.forEach(rootNode => processNode(rootNode));
  return newTree;
}
function fetchData() {
  getCatalog().then((r) => {
    parOptions.value = r.data
    cascaderOptions.value = renameTreeProperties(buildTree(r.data))
    console.log(parOptions.value)
  });
}
function buildTree(flatData, rootValue = '0') {
      const nodeMap = new Map();
      const tree = [];
      flatData.forEach(item => {
        nodeMap.set(item.cateCode, { ...item, children: [] });
      });
      for (const [code, node] of nodeMap) {
        const parentCode = node.catePcode;
        if (parentCode === rootValue || !parentCode) {
          tree.push(node);
          continue;
        }
        const parent = nodeMap.get(parentCode);
        if (parent) {
          parent.children.push(node);
        } else {
          console.warn(`Orphan node detected: ${code}. Parent ${parentCode} not found`);
        }
      }

      return tree;
    }
function addPa(){
    var par = {
        itemName:''
    }
    tableDataCan.value.push(par)
}
function delCan(index){
  tableDataCan.value.splice(index, 1)
}
function addCanAdd(){
    var par = {
        parName:''
    }
    tableDataCanAdd.value.push(par)
}
function delCanAdd(index){
  tableDataCanAdd.value.splice(index, 1)
}
onMounted(() => {
  fetchData()
  getTreeLeft()
});

</script>
<style scoped>

.noBorSel :deep(.el-select__wrapper){
  box-shadow: none !important; /* 去除阴影边框 */
  border: 1px solid transparent !important; /* 设置透明边框 */
  background-color: transparent !important; /* 可选：透明背景 */
}
.noBor :deep(.el-input__wrapper) {
  box-shadow: none !important; /* 去除阴影边框 */
  border: 1px solid transparent !important; /* 设置透明边框 */
  background-color: transparent !important; /* 可选：透明背景 */
}
/* 节点垂直间距 */
:deep(.el-tree-node) {
  margin-bottom:5px !important; /* 增大节点间距 */
}
.el-popover.custom-popover {
  min-width: 50px;
  padding: 8px; /* 可选：调整内边距 */
}
</style>
<style>
.dot {
  width: 20px;          /* 控制圆点直径 */
  height: 20px;
  background-color: #333; /* 实心颜色 */
  border-radius: 50%;    /* 关键属性 */
  display: inline-block; /* 确保与其他元素并排 */
}
/* 全局透明样式（不使用 scoped） */
.transparent-select .el-select__wrapper {
  background-color: transparent !important;
  border-color: transparent !important;
  box-shadow: none !important;
}

.transparent-select .el-select__selection {
  background-color: transparent;
}

/* 处理悬停和聚焦状态 */
.transparent-select .el-select__wrapper:hover,
.transparent-select .el-select__wrapper.is-focused {
  border-color: transparent !important;
  box-shadow: none !important;
}

/* 下拉箭头图标 */
.transparent-select .el-select__caret {
  color: rgba(0, 0, 0, 0.5); /* 半透明图标 */
}
</style>
<style scoped>
:deep(.treeLeft) .el-tree-node__content {
  display: flex !important;
  height: 28px;                  /* 按设计稿调整高度 */
  align-items: center;
  padding-top: 0 !important;
}
:deep(.treeLeft) .el-tree-node__content:hover {
  background-color: #e9e9eb;
}
:deep(.treeLeft) .el-tree-node__content:active {
    background-color: rgka(69,157,255,0.1) !important;
  }

  /* 选中态（Active） */
:deep(.treeLeft) .el-tree-node.is-current > .el-tree-node__content {
    background-color: #c6e2ff !important;
  }

.tab-container {
  height: 100vh; /* 关键：父容器明确高度 */
  display: flex;
  flex-direction: column;
}
.drag-handle {
  cursor: move;
}

.ghost {
  opacity: 0.5;
  background: #c8ebfb;
}

/* 防止文字选中 */
:deep(.el-table__row) {
  user-select: none;
  -webkit-user-select: none;
}
</style>
<style scoped lang="scss">

.custom-tree-node {
  display: flex;       /* 启用 Flex 布局 */
  align-items: center; /* 垂直居中 */
  gap: 8px;            /* 图标与文字间距 */
  justify-content: space-between;
}
.el-table .el-table__row td {
  height: 60px !important; /* 行高 */
}
.custom-tree-node {
  display: flex;       /* 启用 Flex 布局 */
  align-items: center; /* 垂直居中 */
  gap: 8px;            /* 图标与文字间距 */
}
</style>