<script type="text/javascript">
	var todoid = $.getURLParam('_todoid');//待办任务的ID
	var piid = $.getURLParam('_piid');//piid
	$(function(){
		var _data ={
			_SRVNAME : 'service.wdk.wftodo',
        	_SRVMETHOD: 'getTodoDealInfo',
        	_DATA: $.json2str({
        		piid: ''
        		,todoid: todoid
            }),
		};
		var url = "wdk?action=wdk.pub&method=call_service";
		$.cuajax({
		  	url:url+"&ajaxparam="+new Date().getTime(),
			method:"post", 
			timeout:WDK_Timeout,
			data:_data,   
			success:function(result) {
				var jres = $.str2json(result);
				if(jres && jres.code=='1'){
					var info = jres.result;
					$('#wf_nodetitle').text(info.nodetitle);
					$('#wf_nodename').val(info.nodename);
					$('#wf_title').text(info.title);
					piid = info.piid;
					var nextnodes = info.nextnodes;
					var htmlstr = [];
					var nextnodemap = {};
					if(nextnodes && nextnodes.length>0){
						for(var i=0;i<nextnodes.length;i++){
							nextnodemap[nextnodes[i].nodename] = nextnodes[i];
							htmlstr.push("<input type='radio' name='wf_nextnode' id='"+nextnodes[i].nodename+"' value='"+nextnodes[i].nodename+"' nodetype='"+nextnodes[i].nodetype+"'><label for='"+nextnodes[i].nodename+"'>"+nextnodes[i].nodetitle+"</label>");
						}
						$('#wf_actors').parent().parent().parent().css("display","");
						$('#wf_nextnodes').parent().parent().parent().css("display","");
					}else{
						//如果无下一环节的话,隐藏
						$('#wf_actors').parent().parent().parent().css("display","none");
						$('#wf_nextnodes').parent().parent().parent().css("display","none");
					}
					$('#wf_nextnodes').empty();
					$('#wf_nextnodes').append(htmlstr.join(''));
					$('#wf_nextnodes').data('nextnodes',nextnodemap);//信息添加到节点上
					if(info.defaultnode){
						$('#'+info.defaultnode).click();
					}
				}
			},
			error:function(result){
				alert('网络错误!result='+result);
				$.wait_close();
			}
		});

		//绑定单击事件
		$("#wf_nextnodes").on("click","input[type='radio']", function() {
		     //do something here
		     var nodename = $(this).val();
		     var nodetype = $(this).attr('nodetype');
		     var checkbox = false;
		     $('#wf_actors').parent().parent().parent().css("display","");
		     if(nodetype == 'frok'){
		     	//暂时可这样判断
		     	checkbox = true;
		     }else if(nodetype == 'end' ){
		     	$('#wf_actors').parent().parent().parent().css("display","none");
		     	return;
		     }else if(nodetype == 'sign'){
		     	//多人会签时,需要可以多选
		     	checkbox = true;
		     }

		     var _data ={
				_SRVNAME : 'service.wf.frame',
	        	_SRVMETHOD: 'getParticipantByNodename',
	        	_RSTYPE:'tree',
	        	_RSFIELD:'result',
	        	_RSPARAM:$.json2str({
	        		asyn:0
	        		,idField:'id'
	        		,textField:'text'
	        		,parentField:'parentid'
	        		,iconField:''
	        		,attrs:'type,node'
	        	}),
	        	_DATA: $.json2str({
	        		su:$.getSessionUser()
	        		,piid: piid
	        		,nodename: nodename
	        		,todoid:todoid
	            }),
			};
		     $.tree({
		     	id:'wf_actors'
		     	,url:'wdk?action=wdk.pub&method=call_service'
		     	,queryParams:_data
		     	,checkbox:checkbox
		     });
		 });
	});

//获取树上选中的节点
function getSelectedPersons(){
	var ZTreeObj = $.fn.zTree.getZTreeObj('wf_actors');
	if(!ZTreeObj){
		return;
	}
	var isCheckBox = ZTreeObj.setting.check.enable;
	var targetactorsmap = {};
	//var targetactors = [];
	var selectedNodes = isCheckBox==true?$.tree_getCheckedNode({id:'wf_actors'}):$.tree_getSelectedNode({id:'wf_actors'});
	if(!selectedNodes || selectedNodes.length==0){
		return;
	}
	
	$.each(selectedNodes, function(key, val) {
		var attributes = selectedNodes[key].attributes;
		var node = attributes.node;
		var type = attributes.type;
		if('user'== type){
			//节点必须是人
			var personid = '';
			var deptuuid = '';
			var nodename = '';
			//正常任务的场景
			personid = selectedNodes[key].id;
			deptuuid = selectedNodes[key].getParentNode().id;
			if('-1' != node){
				nodename = node;
				//分支\聚合的场景
				personid = personid.replace(node+'_','');
				deptuuid = deptuuid.replace(node+'_','');
			}else{
				nodename = $("input[name='wf_nextnode']:checked").val();
			}
			var oldtargetactors = targetactorsmap[nodename];
			if(!oldtargetactors){
				oldtargetactors = [];
			}
			oldtargetactors.push(personid+',user,'+deptuuid);
			targetactorsmap[nodename] = oldtargetactors;
		}
		
	});
	return targetactorsmap;
}
function _WF_GET_SUBMIT_BEAN(unitId,personId,deptId){
	var result = {};
	var nodetype = $("input[name='wf_nextnode']:checked").attr('nodetype');
	var targetactormap = {}
	var ZTreeObj = $.fn.zTree.getZTreeObj('wf_actors');
	if(ZTreeObj){
		targetactormap = getSelectedPersons();
		if(!targetactormap || targetactormap.length==0){
			result.code = '0';
			result.desc = '请选择下一环节的办理人!';
			return result;
		}
	}
	
	var targetactors = [];
	var nodename = $("input[name='wf_nextnode']:checked").val();
	var extra = [];
	for(var key in targetactormap){
		if(key == nodename){
			targetactors = targetactormap[key];
		}else{
			var forkPath = {};
			forkPath.target = key;
			//var treeObj =$.fn.zTree.getZTreeObj('wf_actors');
			var rootPath = $.tree_getNodeById('wf_actors',key);
			var attributes = rootPath.attributes;
			forkPath.signtype = '';
			forkPath.targettype = attributes.type;
			forkPath.targetactors = targetactormap[key].join(';')
			extra.push(forkPath);
		}
	}
	if(1==1){
		console.log(targetactors);
		console.log(extra);
	}
	var nextnodemap = $('#wf_nextnodes').data('nextnodes');
	var selectedNode = nextnodemap[nodename];
	var bean_submit = {
		target:nodename ? nodename:''
		,targettype:nodetype ? nodetype:''
		,title:$('#wf_title').text()
		,unitid:unitId
		,deptuuid:deptId
		,actortype:'user'
		,actorid:personId
		,opertype:'submit'//送达
		,opercomment:$('#wf_opinion').val()
// 		,operdt:'2017-06-26'
		,targetactors:targetactors.join(';')
		,todoid:todoid
		,extratargets:extra
	};
	//有祖先环节的情况下,代表当前环节为跳跃环节,需要修改参数
	if(selectedNode && selectedNode.ancestor && selectedNode.ancestor.nodename){
		bean_submit.target = selectedNode.ancestor.nodename;
		bean_submit.targettype = selectedNode.ancestor.nodetype;
		bean_submit.targetactors = '';
		var extraNew = [];
		var targetNew = {target:nodename,signtype:'',targettype:nodetype,targetactors:targetactors.join(';')};
		extraNew.push(targetNew);
		bean_submit.extratargets = extraNew;
	}
	result.code = '1';
	result.submit_bean = bean_submit;
	return result;
}

	
</script>
</head>
<style type="text/css">
	#wf_actors{
		height: 150px;
		overflow-y: auto;
		overflow-x: hidden;
	}
	.row .value-display{
		padding-left: 10px;
	}
	#wf_nextnodes input{
		vertical-align: middle;
		margin-top: -2px;
		margin-bottom: 1px;
	}
	.edit-container{
		padding: 2px 30px 20px 30px;
	}
</style>
<br/>
<div class="edit-container"> 
	<input type="hidden" name="wf_nodename"/>

	<div class="row">
		<div class="formgroup col-12">
			<label>待办标题</label>
			<div class="value-display">    
	 			<span id="wf_title">未知</span>
			</div>
		</div>
	</div>

	<div class="row">
		<div class="formgroup col-12">
			<label>当前环节</label>
			<div class="value-display">    
	 			<span id="wf_nodetitle">未知</span>
			</div>
		</div>
	</div>

	<div class="row">
		<div class="formgroup col-12">
			<label>下一办理环节</label>
			<div class="value-display">    
	 			<div id="wf_nextnodes" style="width:100%;word-wrap: break-word; word-break: break-all;white-space: pre-wrap;">未知</div>
			</div>
		</div>
	</div>

	<div class="row">
		<div class="formgroup col-12">
			<label>下一环节办理人</label>
			<div class="value-display">    
	 			<div id="wf_actors"></div>
			</div>
		</div>
	</div>
	 
	<div class="row" style="height:120px;">
		<div class="formgroup col-12">
			<label>办理意见</label>
			<div>
				<textarea id="wf_opinion" rows="7" style="width:100%;"></textarea>
			</div>
		</div>
	</div>
</div>