<script lang="ts" setup name="ModelObjectSelector">
import { getObjectListById } from '@/api/model/modelObjectApi'
const props = withDefaults(defineProps<{
  modelValue: number
  modelId: number
}>(), {
  modelValue: 0,
  modelId: 0,
})
const emit = defineEmits(['update:modelValue', 'update:modelId'])
interface ModelObject {
  modelId: number
  objectId: number
  objectTitle: string
}
const objectId = computed({
  get: () => {
    return props.modelValue
  },
  set: (val) => {
    emit('update:modelValue', val)
  },
})
const state = reactive({
  loading: false,
  dataList: [] as ModelObject[],
})

const getData = async () => {
  const modelId = props.modelId
  if (modelId > 0) {
    state.loading = true
    const { data } = await getObjectListById(modelId)
    state.loading = false
    state.dataList = data
  }
}
watch(() => props.modelId, (value) => {
  getData()
}, {
  immediate: true,
})
onMounted(() => {
  getData ()
})
</script>

<template>
  <el-select v-model="objectId" v-loading="state.loading" style="width: 100%" filterable placeholder="请选择业务对象" value-key="objectId">
    <el-option key="0" label="请选择" :value="0" disabled />
    <el-option
      v-for="item in state.dataList"
      :key="item.objectId"
      :label="item.objectTitle"
      :value="item.objectId"
    />
  </el-select>
</template>
