<template>
	<div>
		<el-select v-model="val" clearable :style=" `width:${width}px;` " @change="change">
			<el-option v-for="(option,i) in optionList" :key="i" :value="option[optionVal?optionVal:'value']"
				:label="option[optionLab?optionLab:'label']" />
		</el-select>
	</div>
</template>

<script setup>
	import {
		ref,
		getCurrentInstance,
		onMounted,
		computed 
	} from 'vue'
	const {
		proxy
	} = getCurrentInstance()
	const emit = defineEmits(['change'])

	onMounted(() => {
		init()
	})
	const props = defineProps({
		options: {
			type: Array,
			default: () => []
		},
		value: {
			type: String,
			default: () => ''
		},
		dicType: {
			type: String,
			default: () => ''
		},
		optionValue: {
			type: String,
			default: () => ''
		},
		optionLabel: {
			type: String,
			default: () => ''
		},
		width: {
			type: Number,
			default: () => ''
		},
		url: {
			type: String,
			default: () => ''
		},
		param : {
			type: Object,
			default: () => {}
		},
	})
	const val = computed(() => {
		return props.value
	})
	// ref(props.value)
	const optionList = ref(props.options)
	const optionLab = ref(props.optionLabel)
	const optionVal = ref(props.optionValue)
	const init = () => {
		console.log('props.dicType' + props.dicType)
		if (props.dicType && props.dicType != null && props.dicType != '') {
			optionLab.value = 'dicName'
			optionVal.value = 'dicCode'
			getDic()
		} else if ( 
			props.url && props.url != null && props.url != '') {
			getOption()
		}

	}

	const getDic = () => {
		var url = 'framework/Common/querySelector'
		var param = {
			type: props.dicType
		}
		proxy.$axios.get(url, param).then(response => {
			if (response.data.code == '0') {
				optionList.value = response.data.data
			} else {
				ElMessage.error('操作失败！' + response.data.msg)
			}
		})
	}
	
	const getOption  = () => {
		var url = props.url
		var param = props.param
		proxy.$axios.get(url, param).then(response => {
			if (response.data.code == '0') {
				optionList.value = response.data.data
			} else {
				ElMessage.error('操作失败！' + response.data.msg)
			}
		})
	}

	const change = (val) => {
		emit('change', val)
	}
</script>

<style>
</style>