<template>
  <div class="folder-info">
    <div class="left-part" :class="include_mock_service? '' : 'padding-reset'">
      <bg-btns v-model="type" :options="types" v-if="!mockDetail && include_mock_service" />
      <el-tree
        :data="data"
        node-key="code"
        default-expand-all
        @node-click="handleNodeClick"
      >
        <template #default="{ data }">
          <div class="custom-tree-node" pointer-events="none">
            <template v-if="data.node_type === 0">
              <bg-icon
                icon="#bg-ic-s-folder-opened"
                v-if="fileOpenState[data.code] !== false"
              />
              <bg-icon icon="#bg-ic-s-folder" v-else />
            </template>
            <template v-else>
              <el-tooltip
                effect="dark"
                :content="testStateTexts[data.test_state]"
                placement="top"
              >
                <bg-icon
                  v-if="data.service_type != 'cda56bb2-d00d-436d-9fc2-0508737fe503'"
                  :class="`state-${data.test_state}`"
                  :icon="testStateIcons[data.test_state]"
                />
              </el-tooltip>
            </template>
            <span :style="data.service_type != 'cda56bb2-d00d-436d-9fc2-0508737fe503' ? {} : { marginLeft: '20px' }">{{ data.node_name }}</span>
          </div>
        </template>
      </el-tree>
    </div>
    <div class="right-part" v-if="nodeDetail">
      <folder
        class="folder-detail"
        :data="nodeDetail"
        :disabled="true"
        v-if="nodeDetail.node_type == 0"
        :showTip="false"
      />
      <file :data="nodeDetail" :disabled="true" :detailMock="type == 'configMock' && !mockDetail" v-else />
    </div>
  </div>
</template>
  
<script>
import { reactive, toRefs, watch, onBeforeMount } from "vue";
import Folder from "@/page/main/ability-register/step2/folder.vue";
import File from "@/page/main/ability-register/step2/file/index.vue";
import axios from "@/request/http.js";

export default {
  name: "BaseInfo",
  components: { Folder, File },
  props: {
    id: {
      type: String,
      required: true,
    },
    // 是否为mock服务详情
    mockDetail: {
      type: Boolean,
      default: false
    }
  },
  setup(props) {
    const state = reactive({
      type: "",
      data: [],
      types: [
        {
          name: "真实能力",
          value: "",
        },
        {
          name: "mock能力",
          value: "configMock",
        },
      ],
      fileOpenState: {},
      nodeDetail: null,
      testStateTexts: [
        "未测试",
        "测试成功",
        "测试失败",
        "其中有服务未测试",
        "其中有服务测试未通过",
      ],
      testStateIcons: [
        "#bg-ic-s-circle-warning",
        "#bg-ic-s-circle-check",
        "#bg-ic-s-circle-close",
        "#bg-ic-s-circle-question",
        "#bg-ic-s-circle-question",
      ],
      include_mock_service: false, // 真实能力中是否配置mock服务
    });

    const getTreeData = () => {
      state.data = [];
      state.nodeDetail = null;

      axios
        .get(`/apaas/service/v5/ability/info`, {
          params: {
            id: props.id,
            queryType: props.mockDetail ? "" : state.type,
          },
        })
        .then(({ data }) => {
          if (data.code == 200) {
            state.data = data.data.folder;
            state.include_mock_service = data.data.ability_info.include_mock_service || false
            console.log(state.include_mock_service)
          }
        });
    };

    const handleNodeClick = (data) => {
      state.nodeDetail = null;

      if (data.children) {
        let flag = state.fileOpenState[data.code] !== false;

        state.fileOpenState[data.code] = !flag;
      }

      if (data.node_type === 0) {
        setTimeout(() => {
          state.nodeDetail = { ...data, children: undefined };
        }, 50);
      } else {
        axios
          .get(`/apaas/service/v5/service/detail?treeCode=${data.code}`)
          .then(({ data }) => {
            if (data.code == 200) {
              state.nodeDetail = data.data;
            }
          });
      }
    };

    watch(
      () => state.type,
      () => {
        getTreeData();
      }
    );

    onBeforeMount(() => {
      // 如果是mock能力详情，给type赋值后会触发watch然后去获取树形 所以不需要初始化时请求该接口
      if (props.mockDetail) {
        state.type = "configMock"
      }else {
        getTreeData();
      }
    });

    return {
      ...toRefs(state),
      handleNodeClick,
    };
  },
};
</script>
  
<style lang="scss" scoped>
.folder-info {
  display: flex;
  justify-content: flex-start;
  align-items: stretch;
  overflow: hidden;

  > .left-part {
    flex: 0 0 281px;
    padding: 24px 0;
    border-right: 1px solid #ebedf2;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: center;

    > .bg-btns {
      margin: 0 auto;
    }

    > .el-tree {
      width: 100%;
      margin-top: 16px;

      :deep() {
        .el-icon.el-tree-node__expand-icon {
          display: none;
        }
      }
    }
  }
  > .padding-reset {
    padding: 8px 0 24px;
  }

  > .right-part {
    // flex: 1 1 calc(100% - 281px);
    flex: 1;
    // padding: 16px;
    box-sizing: border-box;

    .folder-detail {
      width: 100%;
      margin: 0;
      display: flex;

      :deep() {
        .left-span {
          width: 80px;
          padding-top: 68px !important;
        }

        .right-container {
          padding: 12px 0;
          // float: none;
          // width: 100%;

          // .tips {
          //   display: none;
          // }
        }
      }
    }
  }
}

.custom-tree-node {
  padding: 0 16px;

  > .bg-icon {
    font-size: 12px;
    margin-right: 8px;

    &.state-0 {
      color: #dd6955;
    }

    &.state-1 {
      color: #48ad97;
    }

    &.state-2 {
      color: #dd6955;
    }

    &.state-3 {
      color: #dd6955;
    }

    &.state-4 {
      color: #dd6955;
    }
  }
}
</style>