<template>
  <div class="comp-content">
    <el-select-v2
      v-model="choseList"
      :options="listData"
      :placeholder="disabled ? '' : '请选择'"
      filterable
      clearable
      :disabled="disabled"
      @change="changeFun"
    />
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted, watch } from 'vue';
import { listQuantitiesApi } from '@/api/project/run';

interface Props {
  modelValue: string;
  disabled?: boolean;
}

const props = withDefaults(defineProps<Props>(), {
  modelValue: '',
  disabled: false,
});
const listData = ref<any>([]);
const choseList = ref<any>([]);

const emit = defineEmits(['update:modelValue', 'change']);

watch(
  () => props.modelValue,
  (val: any) => {
    choseList.value = val;
  },
  { deep: true, immediate: true }
);

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

const getlistDataFun = () => {
  listQuantitiesApi().then((res: any) => {
    if (res.code === 200) {
      const build = res.data.map((item: any) => {
        return {
          ...item,
          value: item.name,
        };
      });
      listData.value = build;
    }
  });
};

const changeFun = () => {
  emit('update:modelValue', choseList.value);
  let changeData: any = {};
  listData.value.some((item: any) => {
    if (choseList.value === item.name) {
      changeData = item;
      return true;
    }
  });
  emit('change', changeData);
};
</script>

<style lang="scss" scoped>
.comp-content {
  width: 100%;
}
</style>
