<template>
  <el-select-v2
    v-model="currentPool"
    :options="options"
    :placeholder="$t('工况库.请选择工况库')"
    :fit-input-width="true"
    @change="onChangeFun"
  />
</template>

<script lang="ts" setup>
import { ref, computed, onMounted } from 'vue';
import { getAllTaskPoolApi } from '@/api/task/taskpool';

const emit = defineEmits(['update:modelValue', 'change']);
const props = defineProps<{
  modelValue?: string;
}>();

const poolList = ref<{ poolName: string }[]>([]);
const currentPool = ref(props.modelValue || '');

const options = computed(() =>
  [...poolList.value].reverse().map((item) => ({
    value: item.poolName,
    label: item.poolName,
  }))
);

const queryPoolListFun = async () => {
  const req = { bCurrent: true };
  const res: any = await getAllTaskPoolApi(req);
  if (res.code === 200 && Array.isArray(res.data)) {
    poolList.value = res.data;
    if (!currentPool.value && poolList.value.length > 0) {
      currentPool.value = poolList.value[poolList.value.length - 1].poolName;
      emit('update:modelValue', currentPool.value);
      emit('change', currentPool.value);
    }
  }
};

const onChangeFun = (val: string) => {
  emit('update:modelValue', val);
  emit('change', val);
};

onMounted(() => {
  queryPoolListFun();
});

defineExpose({ poolList, currentPool, queryPoolListFun });
</script>
