<template>
	<view class="jnpf-tree-select">
		<u-input input-align='left' type="select" :select-open="selectShow" v-model="innerValue"
			:placeholder="placeholder" @click="openSelect"></u-input>
		<user-tree v-model="selectShow" @confirm="selectConfirm" :multiple="multiple" :props="props" :list="list"
			:selectedData="selectedData" :selectType="selectType" :query='query' :clearable="clearable" ref="userTree">
		</user-tree>
	</view>
</template>

<script>
	import userTree from './user-tree.vue';
	import {
		getUserInfoList,
		getSelectedUserList
	} from '@/api/common.js'
	export default {
		model: {
			prop: 'value',
			event: 'input'
		},
		components: {
			userTree
		},
		props: {
			bh: {
				default: 450
			},
			value: {
				default: ''
			},
			ableIds: {
				type: Array,
				default: () => []
			},
			ableRelationIds: {
				type: [Array, String],
				default: () => []
			},
			selectType: {
				type: String,
				default: 'all'
			},
			placeholder: {
				type: String,
				default: '请选择'
			},
			props: {
				type: Object,
				default: () => ({
					label: 'fullName',
					value: 'id',
					children: 'children',
					isLeaf: 'isLeaf'
				})
			},
			disabled: {
				type: Boolean,
				default: false
			},
			clearable: {
				type: Boolean,
				default: false
			},
			multiple: {
				type: Boolean,
				default: false
			},
		},
		data() {
			return {
				selectShow: false,
				innerValue: '',
				selectedData: [],
				list: [],
				query: {
					ids: this.ableIds
				},
				pagination: {
					currentPage: 1,
					pageSize: 20,
					keyword: ''
				},
			}
		},
		watch: {
			value: {
				handler(val) {
					if (!val) return this.innerValue = ''
					this.setDefault(val)
				},
				immediate: true
			}
		},
		created() {
			if (this.selectType !== 'all') this.getInfoList()
		},
		methods: {
			setDefault(id) {
				if (!id) return this.innerValue = ''
				this.selectedData = []
				const arr = typeof(id) === 'string' ? id.split(',') : id
				getUserInfoList(arr).then(res => {
					const list = res.data.list
					this.selectedData = list
					let txt = ''
					for (let i = 0; i < list.length; i++) {
						txt += (i ? ',' : '') + list[i].fullName
					}
					this.innerValue = txt
				})
			},
			getInfoList() {
				let query = this.query;
				query.pagination = this.pagination
				getSelectedUserList(query).then(res => {
					const list = res.data.list || [];
					this.list = list;
				})
			},
			openSelect() {
				if (this.disabled) return
				this.selectShow = true
			},
			selectConfirm(e) {
				this.selectedData = e;
				let label = ''
				let value = []
				for (let i = 0; i < e.length; i++) {
					label += (!label ? '' : ',') + e[i][this.props.label]
					value.push(e[i][this.props.value])
				}
				this.defaultValue = value
				this.innerValue = label
				if (!this.multiple) {
					this.$emit('input', value.join())
					this.$emit('change', value.join(), e[0])
					return
				}
				this.$emit('input', value)
				this.$emit('change', value, e)
			}
		}
	}
</script>
<style lang="scss" scoped>
	.jnpf-tree-select {
		width: 100%;
	}
</style>