<script lang="ts" setup name="OcCatSelector">
import { getOcCatListById } from '@/api/system/ocCatApi'
const props = withDefaults(defineProps<{
  ocTypeId: number
  modelValue: number
}>(), {
  ocTypeId: 0,
  modelValue: 0,
})
const emit = defineEmits(['update:modelValue'])
interface OcCat {
  ocCatId: number
  ocCatTitle: string
}
const ocCatId = computed({
  get: () => {
    return props.modelValue
  },
  set: (val) => {
    emit('update:modelValue', val)
  },
})
const state = reactive({
  loading: false,
  dataList: [] as OcCat[],
})

const getData = async () => {
  state.loading = true
  const ocTypeId = props.ocTypeId
  const { data } = await getOcCatListById(ocTypeId)
  state.loading = false
  state.dataList = data
}
onMounted(() => {
  getData ()
})
</script>

<template>
  <el-select v-model="ocCatId" v-loading="state.loading" style="width: 100%" filterable placeholder="请选择分类" value-key="ocCatId">
    <el-option key="0" label="请选择" :value="0" disabled />
    <el-option
      v-for="item in state.dataList"
      :key="item.ocCatId"
      :label="item.ocCatTitle"
      :value="item.ocCatId"
    />
  </el-select>
</template>
