<template>
	<uni-data-picker v-model="select" :placeholder="placeholder" popup-title="请选择"
		:localdata="options"
	@change="onchange" @nodeclick="onnodeclick">
	</uni-data-picker>
</template>

<script>
	import {
		getAllChildren
	} from '@/api/system'
	export default {
		name: "simpleCascader",
		props: {
			value: {
				type: [String, Number],
				required: false,
				default: null,
			},
			data: {
				type: [String, Array],
				required: true
			},
			loadType: {
				type: String,
				required: false,
				default: "local"
			},
			field: {
				type: String,
				required: false,
				default: "default"
			},
			ext: {
				type: Object,
				required: false,
				default: () => ({}),
			},
			readonly: {
				type: Boolean,
				required: false,
				default: false,
			},
			placeholder: {
				type: String,
				required: false,
				default: '请选择',
			}
		},
		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
			}
		},
		mounted() {
			this.init()
		},
		methods: {
			init() {
				this.options = []
				if(this.isDict) {
					this.loadDict()
				}else if(this.isLocal) {
					this.options = this.data
				}else if(this.isStatic) {
					
				}
			},
			parseList(list) {
				return list.map(d => {
						let children = null
						if(d.children && d.children.length > 0) {
							children = this.parseList(d.children)
						}
						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]
						}
						
						if(children) {
							ret.children = children
						}
						return ret
					})
			},
			loadDict() {
				getAllChildren({
					enumid: this.data,
					isQueryFristNode: false,
					... this.ext
				}).then(resp => {
					const list = resp.data || []
					this.options = this.parseList(list)
				})
			},
			onchange() {
				this.$emit('input', this.select)
			},
			onnodeclick() {

			}
		}
	}
</script>

<style>

</style>