<template>
	<!-- <uni-data-select v-model="select" :localdata="options" :disabled="readonly" @change="change"></uni-data-select> -->
	<zxz-uni-data-select v-model="select" :localdata="options" :disabled="readonly" :multiple="multiple" @change="change"></zxz-uni-data-select>
</template>

<script>
	import {
		getAllChildren
	} from '@/api/system'
	import * as staticAll from './options.js'
	import { getDict2 } from '@/utils/dict.js'
	export default {
		name: "simpleSelect",
		props: {
			value: {
				type: [String, Number, Array],
				required: false,
				default: null,
			},
			data: {
				type: [String, Number, Array],
				required: true
			},
			loadType: {
				type: String,
				required: false,
				default: "local"
			},
			field: {
				type: String,
				required: false,
				default: "default"
			},
			multiple: {
				type: Boolean,
				required: false,
				default: false,
			},
			ext: {
				type: Object,
				required: false,
				default: () => ({}),
			},
			readonly: {
				type: Boolean,
				required: false,
				default: false,
			}
		},
		computed: {
			isLocal() {
				return this.loadType === 'local'
			},
			isDict() {
				return this.loadType === 'dict'
			},
			isStatic() {
				return this.loadType === 'static'
			}
		},
		data() {
			return {
				select: this.value,
				options: [],
			};
		},
		watch: {
			value(n, o) {
				this.select = n
			},
			data(n, o) {
				this.init()
			}
		},
		mounted() {
			this.init()
		},
		methods: {
			init() {
				this.options = []
				if (this.isDict) {
					this.loadDict()
				} else if (this.isLocal) {
					this.options = this.data
				} else if (this.isStatic) {
					this.loadStatic()
				}
			},
			loadDict() {
				getDict2(this.data, true).then(list => {
					this.options = list.map(d => {
						const ret = {
							text: d.enumname,
							raw: d,
						}
					
						if (this.field === 'default') {
							ret.value = d.enumid
						} else if (this.field.startsWith('raw.')) {
							const [
								_,
								f
							] = this.field.split('\.')
							ret.value = d[f]
						}
					
						return ret
					})
				})
			},
			loadStatic() {
				const opts = staticAll[this.data]
				this.options = opts
			},
			change(n) {
				this.$emit('input', this.select)
				if(this.multiple) {
					const o = this.options.filter(d => this.select.includes(d.value))
					this.$emit('on-change', this.select, o)
				}else {
					console.log('change', this.options, n);
					this.$emit('on-change', this.select, n)
				}
				
			}
		}
	}
</script>

<style>

</style>