<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/commons/taglibs.jsp"%>
<%@page import="com.blueland.bpm.engine.util.WorkFlowContants"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
	String userId = request.getParameter("userId");
	String userName = request.getParameter("userName");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Cache-Control" content="no-store"/>
	<meta http-equiv="Pragma" content="no-cache"/>
	<meta http-equiv="Expires" content="0"/>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<%@ include file="/commons/meta.jsp"%>

	<script>
		$(document).ready(function() {
			var procInstId=$('#wf_procInstId', parent.document).attr("value");
			var procDefId=$('#wf_procDefId', parent.document).attr("value");
			var curActDefId=$('#wf_curActDefId', parent.document).attr("value");
			var curActInstId = $('#wf_curActInstId', parent.document).attr("value");
			getNextActivity(procDefId,procInstId,curActInstId,curActDefId);

		});
		function getNextActivity(procDefId,procInstId,curActInstId,curActDefId){
			var url="${ctx}/taskMgr/getNextActivity.do";
			var sendUserId=$('#wf_sendUserId', parent.document).attr("value");
			var senderOrgId=$('#wf_sendUserOrgId', parent.document).attr("value");
			var pageInputVar=$("form",parent.document).serialize();
			$.post(url, {"processDefinitionId":procDefId,"procInstId":procInstId,"activityId":curActDefId,"pageInputVar":pageInputVar},
					function (data, textStatus){
						//alert(data);
						var jsonObject = eval(data);

						var selectObj="<select id='actListSel' onchange='changeActUserTree();' style='border:none;WIDTH:100%;HEIGHT:100%' size='4' tabindex='3'>";
						$.each(jsonObject, function(index, value) {
							if(index==0){
								loadNextActivityUserTree(procDefId,procInstId,curActInstId,curActDefId,value.id,sendUserId,senderOrgId);
								selectObj+="<option isMulti="+value.isMulti+" isNotSelectReceiver="+value.isNotSelectReceiver+"  type="+value.type+" selected value="+value.id+">"+value.name+"</option>";
							}else{
								selectObj+="<option isMulti="+value.isMulti+" isNotSelectReceiver="+value.isNotSelectReceiver+" type="+value.type+" value="+value.id+">"+value.name+"</option>";
							}
						});
						selectObj+="</select>";
						//alert(selectObj);
						$("#actListDiv").append(selectObj);

					});

		}
		function loadNextActivityUserTree(procDefId,procInstId,curActInstId,curActDefId,nextActDefId,sendUserId,senderOrgId){
			var nextActivityUserUrl="${ctx}/pages/taskMgr/nextActivityUserTree.jsp";
			nextActivityUserUrl += "?userId="+sendUserId+"&userOrgId="+senderOrgId+"&procDefId=" + procDefId + "&procInstId="+procInstId+"&curActInstId="+curActInstId+"&destActivityId="+ nextActDefId+ "&curActivityId="+ curActDefId;
			$("#actUserTreeFrame").attr("src", nextActivityUserUrl);
		}
		function changeActUserTree() {
			var procInstId=$('#wf_procInstId', parent.document).attr("value");
			var procDefId = $('#wf_procDefId', parent.document).attr("value");
			var curActDefId = $('#wf_curActDefId', parent.document).attr("value");
			var curActInstId = $('#wf_curActInstId', parent.document).attr("value");
			var nextActDefId = $('#actListSel option:selected').val();
			var sendUserId=$('#wf_sendUserId', parent.document).attr("value");
			var senderOrgId=$('#wf_sendUserOrgId', parent.document).attr("value");
			loadNextActivityUserTree(procDefId,procInstId,curActInstId,curActDefId, nextActDefId,sendUserId,senderOrgId);
		}
		function addNextActivityUser(){
			var treeNodeId=document.getElementById('actUserTreeFrame').contentWindow.document.getElementById('treeNodeId').value;
			var treeNodeName=document.getElementById('actUserTreeFrame').contentWindow.document.getElementById('treeNodeName').value;
			var treeNodeType=document.getElementById('actUserTreeFrame').contentWindow.document.getElementById('treeNodeType').value;
			var count=$("#nextActUserSel option").length;
			var index=0;
			//过滤空格
			if(treeNodeId==""&&treeNodeId.length==0||treeNodeType=='ORG'||treeNodeType=='DEPT'){
				return;
			}

			var nextActSelOpt=$('#actListSel option:selected');
			var nextActDefTypeStr=nextActSelOpt.attr("type");
			var isMulti=nextActSelOpt.attr("isMulti");
			//alert(isMulti);
			for(var i=0;i<count;i++)
			{
				//不重复添加
				if($("#nextActUserSel").get(0).options[i].value ==treeNodeId)
				{
					index++;
					break;
				}
			}
			if(index>0){
				return ;
			}
			//alert(treeNodeId+":"+treeNodeName);
			if(isMulti){
				$("#nextActUserSel").append("<option  value="+treeNodeId+">"+treeNodeName+"</option>");
			}else{
				$("#nextActUserSel").empty();
				$("#nextActUserSel").append("<option  value="+treeNodeId+">"+treeNodeName+"</option>");
			}
		}
		function removeNextActivityUser(){
			$('#nextActUserSel option:selected').remove();
		}

		function executeNext(){
			var count=$("#nextActUserSel option").length;
			var nextActUserIdsStr="";
			for(var i=0;i<count;i++)
			{
				var tempNextActUserIdsStr=$("#nextActUserSel").get(0).options[i].value;
				if(tempNextActUserIdsStr=="")
					continue;
				nextActUserIdsStr+=tempNextActUserIdsStr;
				if(i!=count-1)
					nextActUserIdsStr+=",";
			}
			var nextActSelOpt=$('#actListSel option:selected');
			var nextActIdStr=nextActSelOpt.val();
			if(nextActIdStr==""){
				alert("下一环节定义ID[wf_nextActDefId]不能为空!");
				return false;
			}else{
				$('#wf_nextActDefId', parent.document).val(nextActIdStr);
				$('#wf_nextActDefName', parent.document).val(nextActSelOpt.text());
			}

			var nextActDefTypeStr=nextActSelOpt.attr("type");
			var isNotSelectReceiver=nextActSelOpt.attr("isNotSelectReceiver");
			var isMulti=nextActSelOpt.attr("isMulti");
			$('#wf_nextActDefType').val(nextActDefTypeStr);

			if (!isNotSelectReceiver && nextActUserIdsStr == "") {
				alert("下一环节办理人员不能为空!");
				return false;
			} else if (!isMulti && count > 1) {
				alert("下一环节只允许选择一位办理人员！");
				return false;
			}
			var nextActUserIdsStrs=nextActUserIdsStr.split(",");
			$("input[group='receive']",parent.document).remove();
			for(var i=0;i<nextActUserIdsStrs.length;i++){
				var tempUserIdAndOrgId=nextActUserIdsStrs[i].split("^");
				var nextActUserId="<input type='hidden' group='receive' id='wf_receivers["+i+"].receiveUserId' name='wf_receivers["+i+"].receiveUserId' value='"+tempUserIdAndOrgId[0]+"'/>";
				var nextActOrgId="<input type='hidden' group='receive' id='wf_receivers["+i+"].receiveUserOrgId' name='wf_receivers["+i+"].receiveUserOrgId' value='"+tempUserIdAndOrgId[1]+"'/>";
				$("#nextExecuteForm",parent.document).append(nextActUserId);
				$("#nextExecuteForm",parent.document).append(nextActOrgId);
			}
			parent.baseOnSubmit();
		}
	</script>
</head>
<body class="white-page">
<table class='table table-bordered form-table' border="0" cellspacing="0" cellpadding="0" id="myTable">

	<tr>
		<td colspan="4" style="border: 0 !important;">
			<%--				<div style="margin: 10px; height: 2px;"></div>--%>
			<table class="user-table form-table">
				<tr>
					<td align="left" class="title">办理步骤1</td>
					<td align="left" class="title">待选用户</td>
					<td></td>
					<td  class="title">已选用户</td>
					<td></td>
				</tr>
				<tr>
					<td style="width:210px;height:260px;vertical-align: top;padding: 5px 0;" >
						<div id="actListDiv" style='height:260px;'></div>
					</td>
					<td style="height:260px;background-color: #FFFFFF;width:180px;padding: 0px; ">
						<IFRAME align="top" style="width: 260px; height: 100%;margin-top: -7px;"
								scrolling="auto" src="" frameBorder="0" id="actUserTreeFrame" allowTransparency="true"></IFRAME>
					</td>
					<td valign="middle"
						style="width: 50px; height: 50px; vertical-align: top;text-align: center;">
					<button type="button" id="addNextActUserBtn"
							onclick="addNextActivityUser()"
							class="btn btn-xs btn-primary" 	style="margin-top: 60px">>></button>

					<button type="button" id="removeNextActUserBtn"
							onclick="removeNextActivityUser()"
							class="btn btn-xs btn-primary" style="margin-top: 10px">>></button>
					</td>
					<td style="width:150px;height:250px;vertical-align: top;">
						<select  id='nextActUserSel' multiple="multiple"
								 style='border:none;WIDTH: 100%; HEIGHT: 100%' size='4' tabindex='3' onclick="removeNextActivityUser();"/>
					</td>
					<td align="left" valign="top" style="width:50px;height:50px;vertical-align: top;">
						&nbsp;
						<input  type="button" id="nextExecuteBtn" onclick="executeNext();" class="btn btn-xs btn-primary" value="流程处理"  style="border-radius: 3px !important;background: #0070d2 !important; border: none !important; color: #fff !important; font-size: 14px !important; display: inline-block; margin: 0 0 0 5px; height: 32px; line-height: 32px; padding: 0 10px; outline: none; margin-top: 60px;
						margin-top:66px;margin-left: 20px;"/>&nbsp;
					</td>
				</tr>
			</table>

		</td>
	</tr>
</table>
</div>

</body>
</html>