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

<script>
	import userTree from './user-tree.vue';
	import {
		getFlowList
	} from '@/api/common.js'
	export default {
		model: {
			prop: 'value',
			event: 'input'
		},
		components: {
			userTree
		},
		props: {
			bh: {
				default: 450
			},
			value: {
				default: ''
			},
			options: {
				type: Array,
				default: () => []
			},
			ableDepIds: {
				type: Array,
				default: () => []
			},
			ableRoleIds: {
				type: Array,
				default: () => []
			},
			ablePosIds: {
				type: Array,
				default: () => []
			},
			ableGroupIds: {
				type: Array,
				default: () => []
			},
			ableUserIds: {
				type: Array,
				default: () => []
			},
			ableRelationIds: {
				type: Array,
				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: [],
				query: {}
			}
		},
		watch: {
			value: {
				handler(val) {
					if (!val) return this.innerValue = ''
					this.setDefault(val)
				},
				immediate: true
			},
			selectShow(val) {
				this.$emit('open', val)
			}
		},
		methods: {
			setDefault(id) {
				if (!id) return this.innerValue = ''
				this.selectedData = []
				const arr = typeof(id) === 'string' ? id.split(',') : id
				getFlowList(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
				})
			},
			openSelect() {
				if (this.disabled) return
				this.selectShow = true
				if (this.selectType === 'custom') {
					this.query = {
						departIds: this.ableDepIds,
						positionIds: this.ablePosIds,
						userIds: this.ableUserIds,
						roleIds: this.ableRoleIds,
						groupIds: this.ableGroupIds,
					}
				} else if (this.selectType != 'all') {
					const id = this.getAbleKey(this.selectType)
					this.query = {
						departIds: [],
						positionIds: [],
						userIds: [],
						roleIds: [],
						groupIds: []
					}
					this.query[id] = Array.isArray(this.ableRelationIds) ? this.ableRelationIds : [this.ableRelationIds]
				}
				this.$refs.userTree.resetData()
				this.setDefault()
			},
			getAbleKey(selectType) {
				if (selectType === 'dep') return 'departIds'
				if (selectType === 'pos') return 'positionIds'
				if (selectType === 'role') return 'roleIds'
				if (selectType === 'group') return 'groupIds'
			},
			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', this.innerValue)
					this.$emit('change', value.join(), e[0])
					return
				}
				this.$emit('input', this.innerValue)
				this.$emit('change', value, e)
			}
		}
	}
</script>
<style lang="scss" scoped>
	.jnpf-tree-select {
		width: 100%;
	}
</style>