<template>
	<view v-if="returnCodeOrID === 'orgCode'">
		<uni-data-picker v-model="selectDepartID" ref="depart" :openSearch="true" :ellipsis="true"
			placeholder="请选择单位" popup-title="请选择单位" :localdata="departList" @nodeclick="onnodeclick"
			@popupclosed="onpopupclosed"  :map="{'text':'title','value':'orgCode'}"
			class="no-wrap-picker"></uni-data-picker>
	</view>
	<view v-else>
		<uni-data-picker v-model="selectDepartID" ref="depart" :openSearch="true" :ellipsis="true" placeholder="请选择单位id"
			popup-title="请选择单位" :localdata="departList" @nodeclick="onnodeclick" @popupclosed="onpopupclosed"
			 :map="{'text':'title','value':'id'}" class="no-wrap-picker"></uni-data-picker>
	</view>

</template>
<script setup>
	import {
		useStore
	} from '@/store';
	import {
		queryDepByCode,
		queryZbDepByLdhth
	} from '@/api/depart.js'
	import {
		queryMyDeptTreeListApi
	} from '@/api/api.js'
	import {
		onReady,
		onLoad
	} from '@dcloudio/uni-app';
	import {
		reactive,
		ref,
		watch,
		defineEmits,
		defineProps
	} from 'vue';
	const store = useStore()

	const props = defineProps({
		returnCodeOrID: {
			type: String,
			default: "orgCode"
		}
	})

	let departList = ref([])
	let selectDepartID = ref("") //当前选中的单位ID
	let selectDepartIDS = ref([]) //选中的级联单位ID数组
	let tempSelectDepartID = ref("") //临时选择的单位ID
	let depart = ref(null);

	let departInfo = ref({}) //"单位的全部信息"

	let $emit = defineEmits(['change']);
	
	
	watch(
		tempSelectDepartID,
		(newVal,oldVal)=>{ //通过监听触发change by 闵
			$emit('change', newVal, departInfo)
		},{
			immediate:true,
			deep:true
		}
	)
	

	const getDepartList = () => {
		queryMyDeptTreeListApi().then((res) => {
			if (res.success) {
				departList.value = res.result
			}
		}).catch((err) => {
			console.log(err);
		})
	}
	const onnodeclick = ((e) => {
		departInfo.value = e;
		if (props.returnCodeOrID == "orgCode") {
			tempSelectDepartID.value = e.orgCode
		} else {
			tempSelectDepartID.value = e.value
		}
		// if(!depart.value.isOpened){//如果页面已经关闭 就触发change方法 by 闵
		// 	$emit('change', selectDepartID.value, departInfo)
		// }
	})

	const onchange = ((e) => {
		selectDepartID.value = e.detail.value[e.detail.value.length - 1].value
		// $emit('change', e.detail.value[e.detail.value.length - 1].value, {})
	})

	const onpopupclosed = ((e) => {
		selectDepartID.value = tempSelectDepartID.value
		// $emit('change', selectDepartID.value, departInfo)
	})
	onLoad((e) => {
		getDepartList();
	})
</script>
<style scoped>
	/* 选择器根据实际情况调整 */
	.no-wrap-picker::v-deep .uni-data-picker-item {
		white-space: nowrap;
		/* 强制不换行 */
		overflow: hidden;
		/* 超出部分隐藏 */
		text-overflow: ellipsis;
		/* 超出部分显示省略号 */
	}
</style>