<template>
  <el-select @change="changeFun" v-model="nodeLevel1Uuid" placeholder="项目" :options="nodeLevel1ListOptions">
  </el-select>
</template>
<script lang='ts' setup>
import { queryNodeListApi } from '@/api/project/node';
import { NODE_TYPE } from '@/utils/enum/node';
import { onMounted, ref } from 'vue';

const props = defineProps({
  nodeUuid: {
    type: String,
    default: '',
  },
});
const nodeLevel1ListOptions = ref();

const emits = defineEmits(['update:nodeLevel1Uuid', 'update:updateNodeLevel1Info', 'update:nodeLevel1List']);
const nodeLevel1Uuid = ref(props.nodeUuid);

const changeFun = () => {
  emits('update:nodeLevel1Uuid',
    nodeLevel1Uuid.value
  );
  emits('update:updateNodeLevel1Info',
    {
      // 反馈对应的项目信息
      ...(nodeLevel1ListOptions.value.find((item: { value: string; }) => {return item.value === nodeLevel1Uuid.value;}).info),
    }
  );
};

const nodeListApi = async() => {
  const res:any = await queryNodeListApi({ current: 1, size: 999, nodeType: NODE_TYPE.PROJECT });
  if (res && res.code === 200) {
    nodeLevel1ListOptions.value = res.data.data.map((item: { nodeName: string; uuid: string; }) => {
      if (!nodeLevel1Uuid.value) {
        nodeLevel1Uuid.value = item.uuid;
      }
      return { label: item.nodeName, info: { ...item }, value: item.uuid };
    });
    if (nodeLevel1ListOptions.value.length) {
      changeFun();
    }
    emits('update:nodeLevel1List', res.data.data);
  } else {
    nodeLevel1ListOptions.value = [];
  }
};
onMounted(() => {
  nodeListApi();
});

</script>
<style lang='scss' scoped></style>
