<template>
  <el-select
    @change="changeFun"
    v-model="nodeUuid"
    placeholder="阶段"
    :options="nodeLevel2ListOptions"
  >
  </el-select>
</template>
<script lang="ts" setup>
import { getChildrenNodeListApi } from '@/api/project/node';
import { NODE_TYPE } from '@/utils/enum/node';
import { computed, onMounted, ref, watch } from 'vue';

const props = defineProps({
  projectUuid: {
    type: String,
    default: '',
  },
  phaseUuid: {
    type: String,
    default: '',
  },
  currentPhase: {
    type: String,
    default: '',
  },
});

const nodeLevel2ListOptions = ref();

const emits = defineEmits(['change']);
const nodeUuid = computed({
  get() {
    return props.phaseUuid;
  },
  set(val) {
    // emits('update:phaseUuid', val);
    emits('change', { phaseUuid: val, type: 'change' });
  },
});
// const nodeInfo = ref();

const changeFun = () => {
  // emits('update:phaseUuid', nodeUuid.value);
  // nodeInfo.value = nodeLevel2ListOptions.value.find((item: { value: string; }) => item.value === nodeUuid.value)?.info;
  // emits('change', nodeUuid.value);
  // emits('update:updateNodeLevel2Info',
  //   {
  //   // 反馈对应的项目信息
  //     ...(nodeLevel2ListOptions.value.find((item: { value: string; }) => {return item.value === nodeUuid.value;}).info),
  //   });
};

// 将选中数据回滚到上一个版本
const rollBackValue = (value: string) => {
  // emits('update:phaseUuid', value);
  emits('change', { phaseUuid: value, type: 'rollBack' });
};

const nodeLevel2ListApi = async () => {
  const res: any = await getChildrenNodeListApi({
    current: 1,
    size: 999,
    nodeType: NODE_TYPE.PHASE,
    nodeId: props.projectUuid,
  });
  if (res && res.code === 200) {
    nodeLevel2ListOptions.value = res.data.map((item: { nodeName: string; uuid: string }) => {
      return { label: item.nodeName, value: item.uuid, info: item };
    });
    if (nodeLevel2ListOptions.value.length) {
      let phaseUuid = '';
      // changeFun();
      if (props.currentPhase) {
        const phase = nodeLevel2ListOptions.value.find(
          (item: { label: string }) => item.label === props.currentPhase
        )?.value;
        if (phase) {
          phaseUuid = phase;
        }
      } else {
        phaseUuid = nodeLevel2ListOptions.value[0].value;
      }
      emits('change', { phaseUuid: phaseUuid, type: 'change' });
    }
  } else {
    nodeLevel2ListOptions.value = [];
  }
};
onMounted(() => {});
watch(
  () => props.projectUuid,
  () => {
    if (props.projectUuid) {
      nodeLevel2ListApi();
    }
  },
  {
    immediate: true,
  }
);
defineExpose({ rollBackValue });
</script>
<style lang="scss" scoped></style>
