<template>
  <editDrawer ref="editDrawerRef" />
  <editGroup ref="editGroupRef" />
  <editFormula ref="editFormulaRef" />

  <!-- <ns-modal ref="modalRef" title="新增" v-model:visible="visible">
    <ns-form ref="formRef" :schemas="formSchema" :model="formData" formLayout="formVertical" />
  </ns-modal> -->

  <NsModalFrom ref="modalFormRef" v-bind="nsModalFormConfig" />
  <div class="groupContainer">
    <div class="tree">
      <ns-tree-api ref="treeRef" v-bind="tConfig" @select="handleSelect">
        <template #title="data">
          <div class="treeRow">
            <div>
              <ns-icon :name="data.pointType !== '计算节点' ? 'fenzujiedian' : 'jisuanjiedian'" />
              <span style="padding-left: 8px">{{ data.pointName }}</span>
            </div>
            <a-dropdown>
              <ns-icon name="actionMore" size="14" class="actionMore" />
              <template #overlay>
                <a-menu>
                  <template v-for="(item, index) in actionList" :key="index">
                    <!-- 全部节点只需要新增子节点 -->
                    <a-menu-item
                      v-if="data.id !== 'all' || item.key === 'addNodeSon'"
                      @click="item.func(data)">
                      <span>{{ item.title }}</span>
                    </a-menu-item>
                  </template>
                </a-menu>
              </template>
            </a-dropdown>
          </div>
        </template>
      </ns-tree-api>
    </div>
    <ns-view-list-table v-show="defaultType" class="table" v-bind="config" />
    <ns-view-list-table v-show="!defaultType" class="table" v-bind="configCal" />
  </div>
</template>
<script lang="ts" setup>
  import { createVNode, nextTick, onMounted, ref } from 'vue';
  import { tableConfig, treeConfig, tableConfigCal, formSchema } from './config';
  import { useParams } from '/nerv-lib/use';
  import editDrawer from './edit.vue';
  import editGroup from './editGroup.vue';
  import editFormula from './editFormula.vue';
  import { NsMessage, NsModal } from '/nerv-lib/component';
  import NsModalFrom from '/@/components/ns-modal-form.vue';
  import { group } from '/@/api/deviceManage';
  import { http } from '/nerv-lib/util/http';

  type opType = 'up' | 'down';
  const { getParams } = useParams();
  const modalFormRef = ref();
  const editDrawerRef = ref();
  const editGroupRef = ref();
  const editFormulaRef = ref();
  const treeRef = ref();
  const defaultType = ref(true);
  const result = JSON.parse(sessionStorage.getItem('ORGID')!);
  const config = tableConfig(editDrawerRef, editGroupRef, editFormulaRef);
  const configCal = tableConfigCal(editDrawerRef, editGroupRef, editFormulaRef);
  const tConfig = treeConfig(result);
  const nsModalFormConfig = ref({
    api: group.creatOrUpdate,
    data: {},
    title: '新增',
    schemas: formSchema,
    extraModalConfig: {
      bodyStyle: { paddingBottom: 0 },
    },
    success: () => {
      treeRef.value?.treeReload();
    },
  });
  nextTick(() => {
    console.log(modalFormRef.value, 'modal');
  });

  const addNodeSon = (data) => {
    console.log(data);
    nsModalFormConfig.value.title = '新增';
    nsModalFormConfig.value.data = {
      isCreate: true,
      orgId: result,
    };
    if (data.id !== 'all') {
      nsModalFormConfig.value.data.pid = data.id;
      nsModalFormConfig.value.data.isCreatSon = true;
    }
    modalFormRef.value?.toggle();
  };
  const editNode = (data) => {
    console.log(data);
    nsModalFormConfig.value.title = '编辑';
    nsModalFormConfig.value.data = data;
    modalFormRef.value?.toggle();
  };
  const moveNode = (data, type: opType) => {
    console.log(data);
  };

  const deleteNode = (record) => {
    NsModal.confirm({
      centered: true,
      title: '提示',
      content: '确定删除吗?',
      onOk: () => {
        http.post(group.del, { id: record.id }).then(() => {
          treeRef.value?.treeReload();
          NsMessage.success('删除成功');
        });
      },
    });
  };
  const filterAction = (data) => {};
  const actionList = [
    { title: '新增子节点', key: 'addNodeSon', func: (data) => addNodeSon(data) },
    { title: '编辑', key: 'editNode', func: (data) => editNode(data) },
    { title: '上移', key: 'moveUp', func: (data) => moveNode(data, 'up') },
    { title: '下移', key: 'moveDown', func: (data) => moveNode(data, 'down') },
    { title: '删除', key: 'deleteNode', func: (data) => deleteNode(data) },
  ];
  const handleSelect = () => {
    defaultType.value = !defaultType.value;
  };
</script>
<style lang="less" scoped>
  .groupContainer {
    height: 100%;
    overflow-y: auto;
    background-color: @ns-content-bg;
    display: flex;
    // gap: @ns-gap;
    .tree,
    .table {
      background-color: @white;
      border-radius: @ns-border-radius;
      overflow-y: auto;
    }

    .tree {
      margin-right: @ns-gap;
      :deep(.ant-spin-nested-loading) {
        width: 300px;
        background-color: @white;
      }
    }
    .table {
      flex: 1;
      min-width: 0;
    }
  }

  .actionMore {
    display: none;
  }
  :deep(.ant-tree-node-content-wrapper) {
    &:hover {
      .actionMore {
        display: block;
      }
    }
  }

  .treeRow {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
</style>