<template>
	<view class="ass-content">
		<view class="flex-align-start">
			<view class="ass-label">
				<text>合规调查专员</text>
			</view>
			<view class="assign-view">
				<view class="cu-bar search">
					<view class="search-form round">
						<input type="text" v-model="search" placeholder="请输入" confirm-type="search"></input>
						<text class="cuIcon-search"></text>
					</view>
				</view>
				<mix-tree class="wd60" @checkedRund = "getChecked" :fliter="search" :list="data" @treeItemClick="handleCheck"></mix-tree>
				<!-- <ly-tree :tree-data="data"
					:props="defaultProps" 
					ref="tree" 
					node-key="id" 
					show-radio 		
					checkOnlyLeaf		
					@check="handleCheck"
					@radio-change="handleRadioChange"
					@node-click="handleNodeClick" 
					:filter-node-method="filterNode" 
					child-visible-for-filter-node
				/> -->
			</view>
		</view>		
		
		<view class="flex-align-start mat15">
			<view class="ass-label">
				<text>已选调查专员</text>
			</view>
			<view class="AB-input">
				<view class="flex">
					<input disabled type="text" :value="name" placeholder="请选择调查专员"/>
					<!-- <text class="cuIcon-roundclosefill" @click="A = ''"></text> -->
				</view>
			</view>
		</view>
		
		<view class="mat40 flex-justify-center ass-foot-btn">
			<button class="cu-btn block def-btn margin-tb-sm lg round mar15" @click="assetAssign">确定</button>
			<button class="cu-btn block cancel-btn margin-tb-sm lg round mal15" @click="back()">取消</button>
		</view>
	</view>
</template>

<script>
	import LyTree from '@/components/ly-tree/ly-tree.vue'
	export default {
		components: {
			LyTree
		},
		data() {
			return {
				filterNodeData: null,
				search: '',// 搜索文字
				name:'',// 名称
				data: [],
				defaultProps: {
					children: 'children',
					label: 'label'
				}
			};
		},
		onShow() {
			this.trees()// 每次展示调用树数据
		},
		onLoad() {
			// 如果初始化树时设置节点选择需要做适当延时，可以在下一个事件轮询中执行
			// this.$nextTick(() => {
			// 	// this.$refs.tree.setCheckedKeys([6])
			// })
		},
		created() {
			if(uni.getStorageSync('decideIndex')){
				let data = JSON.parse(uni.getStorageSync('decideIndex'))
				this.getItem.businessId = data.businessId
				this.getItem.companyId = data.companyId
				this.getItem.iid = data.detailId
				uni.removeStorageSync('decideIndex')
			}else{
				this.getItem = JSON.parse(uni.getStorageSync('applyMsg'))
			}
		},
		watch: {
			filterText(val) {
				this.$refs.tree.filter(val);
			}
		},
		// 如果不需要不用到这些方法，需要删除相应代码，打印大量日志会造成性能损耗
		methods: {
			// 只有在"点击"修改的指定节点会触发(也就是说这个方法只会触发一次)，obj中包含当前选中
			handleCheck(obj) {
			},
			
			// 只要节点的选中状态改变就触发（包括设置默认选中，点击选中/取消选中），会被触发多次
			handleRadioChange(obj) {
			},
			
			handleNodeClick(obj) {
			},
			// 查询过滤
			filterNode(value, data) {
				if (!value) return true;
				return data.label.indexOf(value) !== -1;
			},
			// 获取树结构数据
			trees(){	
				this.$http.get('/api-hrms//hrms/dept/empTrees').then(res=>{
					this.data = res.data 
					this.disposeData(this.data)// 处理数据
				})
			},
			// 处理树结构数据
			disposeData(data){// children和员工分开，整合到一个children里
				let len = data.length
				for(let i = 0;i<len;i++){
					if(data[i].children){ // children有值则---进入整合，实际上可能没有children，也需要整合
						if(data[i].userDept){//有员工就整合
							if(data[i].userDept.length!==0){//多个员工处理
								let len = data[i].userDept.length
								for(let a=0;a<len;a++){
									data[i].userDept[a].name=data[i].userDept[a].account
								}
							}
							data[i].children = [...data[i].children,...data[i].userDept] 
							data[i].children = [...new Set(data[i].children)]
						}
						if(data[i].children.length!==0){//有子级则进入子级，继续循环
							this.disposeData(data[i].children)
						}
					}else if(!data[i].children&&data[i].userDept&&data[i].userDept.length!==0){//没有子级，而有员工时，创建新的children
						data[i].children = []
						let len = data[i].userDept.length
						for(let a=0;a<len;a++){
							data[i].userDept[a].name=data[i].userDept[a].account
						}
						data[i].children = [...data[i].children,...data[i].userDept] 
						data[i].children =[...new Set(data[i].children)]
					}
				}
			},
			// 获取点击事件，可设置单选多选等，分别是名称数组和id数组
			getChecked(nameList,idsList){
				if(idsList.length!==0){
					this.ID = idsList[0]
				}else{
					this.ID = ''
				}
				if(nameList&&nameList.length!==0){
					this.name = nameList[0]
				}else{
					this.name = ''
				}
			},
			// 点击确定指派
			assetAssign(){
				let obj = {
					iid:this.getItem.iid,
				}
				if(!this.ID){
					return uni.showToast({title: '请选择后再确定！',icon:'none'})
				}
				// 选中的角色
				obj.userId = this.ID
				this.$http.get('/api-guarantee/compliance/investigation/assign',obj).then(res=>{
					uni.showToast({
						title:'操作成功！'
					})
					this.back()
				})
			},
			// handleCheck(obj) {
			// 	if (obj.node.checked) {
			// 		this.placeholder = `过滤"${obj.data.label}"的子节点`;
			// 		this.filterNodeData = obj.data;
			// 	} else {
			// 		this.placeholder = `过滤所有节点`;
			// 		this.filterNodeData = null;
			// 	}
				
			// 	// filter(val, nodeData),假如要搜索A节点下面的数据，那么nodeData代表treeData中A节点的数据
			// 	this.$refs.tree.filter(this.filterText, this.filterNodeData);
			// }
		}
	};
</script>

<style lang="scss" scoped>
	.ass-content{
		padding: 40rpx 40rpx;
		.ass-foot-btn .cu-btn.lg{
			padding: 0 100rpx;
		}
	}
	.ass-label{
		color: #00B9FF;
		font-size: 28rpx;
		margin-right: 40rpx;
	}
	.assign-view{
		border-radius: 20rpx;
		background-color: #fff;
		box-shadow: 0 0 16rpx #ccc;
		height: 800rpx;
		overflow-x: hidden;
		overflow-y: scroll;
	}
	.cu-bar .search-form{
		border-radius: 20rpx;
		background-color: #f5f5f5;
		height: 72rpx;
		line-height: 72rpx;
		input{
			height: 72rpx;
			line-height: 72rpx;
			padding: 0 40rpx;
		}
		.cuIcon-search{
			margin: 0 20rpx;
		}
	}
	.AB-input{
		view{
			background-color: #f5f5f5;
			color: #666;
			padding: 20rpx 40rpx;
			border-radius: 20rpx;
			text{
				color: #B2B2B2;
			}
		}
	}
</style>