<template>
  <div class="tree-page">
    <versionPreview
      v-if="versionObjJson?.children?.length"
      @skip-run="skipRun"
      :treeData="versionObjJson"
      :versionNode="versionNode"
      :graphLoading="graphLoading"
      :currentVersion="currentVersion"
    >
    </versionPreview>
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue';
import versionPreview from '@/components/common/versionPreview/index.vue';
import { TasksStatusClass, TasksStatusColor } from '@/utils/enum/task';
import { getRunVersionApi, getTaskRunVersionApi } from '@/api/project/run';

const props = defineProps({
  currentTaskInfo: {
    type: Object,
    default: () => {},
  },
});
const emits = defineEmits(['skipToNewRun']);

const graphLoading = ref<any>(true);
const currentVersion = ref<any>('');

const versionNode = [
  {
    label: 'name',
    omitLength: 20,
    chineseLabel: '任务名',
  },
  {
    label: 'creator',
    chineseLabel: '创建人',
  },
  {
    label: 'createTime',
    chineseLabel: '创建时间',
  },
  {
    label: 'description',
    omitLength: 10,
    chineseLabel: '描述',
  },
  {
    label: 'taskStatus',
    chineseLabel: '作业状态',
  },
];
const versionObjJson = ref<any>({});

const visionTreeData = ref<any>([]);

const getTreeData = async (task: any) => {
  graphLoading.value = true;

  let res: any;
  if (task.nodeType === 'run' || task.type === '1') {
    res = await getRunVersionApi({
      runId: task.uuid,
    });
  } else {
    res = await getTaskRunVersionApi({
      taskId: task.uuid,
    });
  }

  if (res && res.code === 200) {
    const result = task.nodeType === 'run' || task.type === '1' ? [res.data] : res.data;
    deleteTypeFun(result);

    currentVersion.value = '';
    visionTreeData.value = result;
    formatVersionData(visionTreeData.value);

    versionObjJson.value.children = visionTreeData.value;
    versionObjJson.value.description = '';
    versionObjJson.value.name = 'root';
    versionObjJson.value.status = null;
    versionObjJson.value.createTime = '';
    versionObjJson.value.id = 'root';
    graphLoading.value = false;
  }
};

const formatVersionData = (arr: any, isCreate: boolean = false) => {
  arr.forEach((item: any) => {
    item.id = item.uuid + '';
    item.name = item.runName;
    item.createTime = item.createTime;
    item.description = item.description;
    item.taskStatus = getVersionStatusMap(item.status);
    item.creator = item.creatorName;
    item.resultLoadcaseId = item.taskId;

    if (item.children?.length > 0) {
      formatVersionData(item.children, isCreate);
    }
  });
};

// 删除数据本身type属性对组件的干扰
const deleteTypeFun = (list: any) => {
  for (let i = 0; i < list.length; i++) {
    for (const key in list[i]) {
      if (key === 'type') {
        delete list[i][key];
      }
    }
    if (list[i]?.children?.length) {
      deleteTypeFun(list[i].children);
    }
  }
};

/**
 *
 * @param key status
 * @returns 例{name:未开始,color:#999999}
 */
const getVersionStatusMap = (key: any) => {
  const status = versionStatus.find((item: any) => {
    return item.value === key;
  });
  return (
    status || {
      name: '未开始',
      color: '#999999',
      value: 'NoStart',
      class: 'tag-gray',
    }
  );
};

const versionStatus = [
  {
    name: '进行中',
    value: 1,
    class: TasksStatusClass.RUNNING,
    color: TasksStatusColor.RUNNING,
  },
  {
    name: '已取消',
    value: 2,
    class: TasksStatusClass.CANCELING,
    color: TasksStatusColor.CANCELING,
  },
  {
    name: '异常',
    value: 3,
    class: TasksStatusClass.FAILED,
    color: TasksStatusColor.FAILED,
  },
  {
    name: '已完成',
    value: 4,
    class: TasksStatusClass.FINISHED,
    color: TasksStatusColor.FINISHED,
  },
];

const skipRun = (info: any) => {
  emits('skipToNewRun', info);
};

watch(
  () => props.currentTaskInfo,
  async (newVal) => {
    if (newVal) {
      await getTreeData(newVal);
    }
  },
  {
    immediate: true,
    deep: true,
  }
);
</script>

<style lang="scss" scoped>
.tree-page {
  width: 100%;
  height: 100%;
}
</style>
