<template>
	<view>
		<!-- 顶部 -->
		<cu-custom bgColor="bg-blue" :isBack="true">
			<block slot="backText">{{titleName}}</block>
			<block slot="content"></block>
			<block slot="right">
				<view class=" flex justify-center">
				 	<button @click="navTo()" class="cu-btn round line-blue text-white" style="width: 160rpx;height: 66rpx;padding: 0;">确定({{num}})</button>
				</view>
			</block>
		</cu-custom>
		<!-- 内容 -->
		<checkboxTree v-if="type==0" :selectList="selectList"></checkboxTree>
		<radioTree v-else :selectList="selectList"></radioTree>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				 titleName:"选择施工员",
				 num:0,
				 selectList:[
					 {name: '施工方',checked:false,show:true,children:[]},
				     {name: '监理',checked:false,show:false,children:[]},
				     {name: '业主',checked:false,show:false,children:[]}],
			     type:0
			}
		},
		deep:true,
		methods: {
			 navTo(){
				 //刷新确定的选择的数据
				 let choose =[];
				 if(this.type!=0){//单选类型
					this.selectList.forEach(item => {
						item.children.forEach((items)=>{
							if(items.checked){
								choose.push(items)
							 } 											 
						})					 
					});
					if(choose.length>=2){
						choose =choose[choose.length-1];
						let index = [];
						index.push(choose)
						choose=index.concat();
					}
				 }else{//多选类型
					 this.selectList.forEach(item => {
						 item.children.forEach((items)=>{
							 if(items.checked){
							 	choose.push(items)
							  } 
						 })
					 });
				 }
				 if(choose.length==0){
					 this.$prompt.none("请选择人员");
					 return;
				 }
				 if(this.type==1 && this.titleName=="选择施工员"){//施工
					  uni.$emit("handleFun",choose);
				 }
				 else if(this.type==2 && this.titleName=="选择监理员"){//监理
				 	uni.$emit("handleFunBy",choose);			 
				 }
				 else if(this.titleName=="选择负责人" && this.type==0){
					//console.log(this.selectList);
					uni.$emit("principal",choose)
				 }
				 else if(this.titleName=="选择审批人" && this.type==1){
				 	  uni.$emit("approve",choose)
				 }
				uni.navigateBack({
				 	delta:1
				 })
			 },
			 /* 获取施工员和监理员 */
			 findUserType(type){
				 var that =this;
				 that.selectList=[];
				 var contractId = uni.getStorageSync("porject"+"_"+uni.getStorageSync("userInfo").id).contractId;
				  that.http.request('/app/diary/listByType', {type:type,contractId:contractId}).then((result)=>{
					  if(result.datas!=null){
					  	if(type==1){//施工员
					  		that.selectList=[{name: '施工方',checked:false,show:true,children:[]}];
					  	}else{//监理员
					  		that.selectList=[{name: '监理方',checked:false,show:true,children:[]}];
					  	}
					  result.datas.forEach((item,index,arr)=>{
					  		item.checked=false;
					  	})
					  	that.selectList[0].children=result.datas;
					  }
				  })
				
			 },
		     /* 获取负责人（获取1为施工方、 2为监理方 3、为业主方）*/
			 findUserTypeOne(){
				 var that=this;
				 var contractId = uni.getStorageSync("porject"+"_"+uni.getStorageSync("userInfo").id).contractId;
				 that.http.request("/app/task/listByTask",{contractId:contractId}).then((res)=>{
					 res.datas.forEach((item,index,arr)=>{
						 item.checked=false;
						 if(item.type==1){
							 that.selectList[0].children.push(item)
						 }else if(item.type==2){
							  that.selectList[1].children.push(item)
						 }else if(item.type==3){
							 that.selectList[2].children.push(item)
						 }
					 })
					 
					 //把已选的添加上
					 if(that.titleName=="选择负责人"){
						let selects = getApp().globalData.principal;
						this.num = selects.length;
						selects.forEach((sele)=>{
							let index = 0;
							if(sele.type==1){
								index = 0;
							}else if(sele.type==2){
								index = 1;
							}else if(sele.type==3){
								index = 2;
							}
							that.selectList[index].children.forEach((person)=>{
								if(person.id == sele.id){
									person.checked = true;
								}
							})
						})
					 }
				 })
			 }
		},
		onLoad(e) {
			if(e.type){
				if(e.type==1){
					this.titleName ="选择施工员";
					this.findUserType(e.type);
				}
				if(e.type==2){
					this.titleName ="选择监理员";
					this.findUserType(e.type);
				}
              this.type=e.type
			}else{
				this.titleName =e.name;
				if(this.titleName=="选择负责人"){
					this.findUserTypeOne();
				}else{//选择审批人
				    this.type=1;//单选
					this.findUserTypeOne();
				}
				
			}
			
			/* 全局事件订阅，只要订阅了事件就可以收到值 */
			uni.$on("num",(rel)=>{
			    this.num=rel
			}) 
		}
	}
</script>

<style>

</style>
