<script setup>
	import * as all from './data'
	import { getEnumListByEnumId } from '@/utils/systemEnum'
	const props = defineProps({
		type: {
			type: String,
			required: true
		},
		value: {
			type: [String, Number],
			required: true
		},
		readonly: {
			type: Boolean,
			required: false,
			default: false
		},
		loadType: {
			type: String,
			required: false,
			default: 'static'
		},
		multiple: {
			type: Boolean,
			required: false,
			default: false
		}
	})

	const selected = ref(null)
	if (props.multiple) {
		selected.value = []
	}

	watch(
		() => props.value,
		(n, o) => {
			selected.value = n
		}
	)

	const emit = defineEmits(['update:value', 'on-change'])

	const handleChange = (n) => {
		emit('update:value', n)
		if (props.multiple) {
			console.log('nnnnnn', n)
		} else {
			const o = options.value.find((d) => d.value === n)
			emit('on-change', n, o)
		}
	}

	const options = ref([])

	onMounted(() => {
		selected.value = props.value
		if (props.loadType === 'static') {
			options.value = all[props.type]
		} else if (props.loadType === 'enum') {
			getEnumListByEnumId(props.type).then((list) => {
				options.value = (list || []).map((d) => {
					return {
						label: d.enumname,
						value: d.enumid,
						raw: d
					}
				})
			})
		}
	})
</script>

<template>
	<a-select v-if="!props.multiple" :disabled="readonly" v-model:value="selected" @change="handleChange">
		<a-select-option v-for="item in options" :value="item.value" :key="item.value">{{ item.label }}</a-select-option>
	</a-select>
	<a-select v-else mode="multiple" :disabled="readonly" v-model:value="selected" @change="handleChange">
		<a-select-option v-for="item in options" :value="item.value" :key="item.value">{{ item.label }}</a-select-option>
	</a-select>
</template>

<style scoped></style>
