<%@page import="org.activiti.engine.repository.ProcessDefinition" %>
<%@page import="org.activiti.engine.task.Task" %>
<%@page import="org.activiti.engine.repository.Deployment" %>
<%@page import="com.blueland.bpm.engine.config.model.*" %>
<%@page import="com.blueland.bpm.common.page.*" %>
<%@page import="com.blueland.bpm.common.util.*"%>
<%@page import="java.util.*" %>
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib tagdir="/WEB-INF/tags/simpletable" prefix="simpletable"%>
<%@ include file="/commons/taglibs.jsp"%>
<jsp:include page="/commons/userOrgTree.jsp" flush="true" />
<%
	String path = request.getContextPath();
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!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>
	<%@ include file="/commons/meta.jsp"%>
	<base href="<%=basePath%>">
	<title>流程待办查询</title>
	<script language="javascript" type="text/javascript" src="${ctx}/resource/js/plugins/My97DatePicker/WdatePicker.js"></script>
	<link href="${ctx}/resource/js/plugins/loadmask/jquery.loadmask.css" rel="stylesheet" type="text/css" />
	<script type='text/javascript' src='${ctx}/resource/js/plugins/loadmask/jquery.loadmask.js'></script>
	<link href="${ctx_js_plugins}/simpletable/simpletable.css" type="text/css" rel="stylesheet">
	<script type="text/javascript" src="${ctx_js_plugins}/simpletable/simpletable.js"></script>
	<script type="text/javascript" >
		$(document).ready(function() {
			// 分页需要依赖的初始化动作
			window.simpleTable = new SimpleTable('queryForm','${page.thisPageNumber}','${page.pageSize}','${pageRequest.sortColumns}');
		});
		function assigneeNameOnDbClick(name, value) {
			$("#assigneeName").val(name);
			$("#assignee").val(value);
		}
		function getTaskId(){
			var chk_value =[];
			var taskId="";
			$("input[name='items']:radio:checked").each(function(){
				chk_value.push($(this).val());
			})
			if(chk_value.length==0){
				alert("请选择一条流程数据！");
				return taskId;
			}else{
				var items=chk_value[0].split("=");
				for(var i=0;i<items.length;i++){
					if(items[0]=="taskId"){
						taskId=items[1];
					}
				}

			}
			return taskId;
		}
		function updateTaskData(url){
			var taskId=getTaskId();
			if(taskId.length==0){
				return;
			}
			showDiaLog(url+"?taskId="+taskId,"修改业务属性",'1024px','550px');
		}
		function updateProcessComment(url){
			var taskId=getTaskId();
			if(taskId.length==0){
				return;
			}
			showDiaLog(url+"?taskId="+taskId,"修改流转意见",'1024px','550px');
		}
		function jumpAnyTask(url){
			var taskId=getTaskId();
			if(taskId.length==0){
				return;
			}
			showDiaLog(url+"?taskId="+taskId,"自由跳转",'800px','400px');
		}

		function againSetTaskReceiver(url){
			var taskId=getTaskId();
			if(taskId.length==0){
				return;
			}
			showDiaLog(url+"?taskId="+taskId,"重新分配人员",'600px','400px');
		}
		function addMultiInstanceView(url){
			var taskId=getTaskId();
			if(taskId.length==0){
				return;
			}
			if(isButOperatePower(taskId,"ADD_MULTIINST_ACT_BTN","当前待办无法进行补发多实例操作！")){
				return ;
			}
			showDiaLog(url+"?taskId="+taskId,"补发多实例",'600px','400px');
		}
		function isButOperatePower(taskId,butOperateId,tip){
			var data={ "curActInstId": taskId,"butOperateId":butOperateId};
			var needReturn = false;
			//判断是否有权限进行作废操作
			jQuery.ajax({
				url: "${ctx}/taskMgr/getButOperatePower.do",
				data: data,
				type: "POST",
				async: false,
				success: function(resp)
				{
					if(!resp){
						alert(tip);
						needReturn = true;
					}
				}
			});
			return needReturn;
		}
		function processAction(myUrl,title,type){
			var taskId=getTaskId();
			if(taskId.length==0){
				return;
			}

			var needReturn = false;
			if(type=='1'){//退回拟稿环节
				needReturn=isButOperatePower(taskId,"BACK_STARTER_BTN","当前待办无法进行退回拟稿环节操作！");
			}else if(type=='2'){//退回上一环节
				needReturn=isButOperatePower(taskId,"BACK_PREV_ACT_BTN","当前待办无法进行退回上一环节操作！");
			}else if(type=='3'){//删除多实例待办
				needReturn=isButOperatePower(taskId,"DELETE_MULTIINST_ACT_BTN","当前待办无法进行删除多实例操作！");
			}
			if(needReturn){
				return;
			}

			if (confirm(title)) {
				jQuery.ajax({
					url: myUrl,
					data: { "taskId": taskId,"isHistory": false},
					type: "POST",
					success: function(resp)
					{
						if(resp.executeStatus=='SUCCESS'){
							alert("操作成功!");
							window.location.reload();
						}else{
							alert("操作失败！");
						}
					}
				});
			}
		}
		function openSysDialog(ctx){
			var height1 = window.parent.document.documentElement.clientHeight;
			var height = (parseInt(height1)-45)+"px";
			showDiaLog(ctx,'流程配置','1400px',height);
		}
		function query(){
			setMask("main-container");
			document.getElementById("queryForm").submit();
		}
	</script>
</head>
<body>
<%@ include file="/commons/messages.jsp"%>
<form id="queryForm" name="queryForm" method="post" style="display: inline;" action="${ctx}/sysMgr/sysTaskList.do">
	<div class="main-container" id="main-container">
		<div class="panel panel-primary">
			<div class="panel-heading">
				<h8>流程待办查询</h8>
			</div>
			<div class="panel-body" id="panel-body" style="padding:3px;">
				<div class="well well-sm" style="margin-bottom: 2px;">

					<table border="0" cellspacing="0" cellpadding="0"
						   class="table-search">
						<tr>
							<td>应用名称：</td>
							<td>
								<select name="appId">
									<option value="">--请选择--</option>
									<c:forEach items="${applicationList}" var="item">
										<option ${params.appId==item.appId?"selected":""} value="${item.appId }">${item.appName}</option>
									</c:forEach>
								</select>
							</td>
							<td>流程标题：</td>
							<td><input value="${params.title}" id="title" name="title" /></td>
							<td width="100px">流程实例ID：</td>
							<td><input value="${params.processInstanceId}" id="processInstanceId" name="processInstanceId" /></td>
							<td width="100px">流程定义名称：</td>
							<td width="100px"><input value="${params.processDefName}" id="processDefName" name="processDefName" /></td>
						</tr>
						<tr>
							<td>执行人：</td>
							<td>
								<input value="${params.assigneeName}" id="assigneeName" name="assigneeName" maxlength="30" onclick="showUserOrg('assigneeName','','','USER'); return false;" readonly />
								<input value="${params.assignee}" type="hidden" id="assignee" name="assignee" maxlength="30"  />
							</td>
							<td  width="100px">环节实例ID：</td>
							<td><input value="${params.actInstId}" id="actInstId" name="actInstId" /></td>
							<td  width="70px">环节名称：</td>
							<td><input value="${params.actInstName}" id="actInstName" name="actInstName" /></td>
							<td>
								<input value="${params.taskDefName}" id="taskDefName" name="taskDefName" type="hidden"/>
								<button type="submit" onclick="query()"class="btn btn-xs btn-primary">
									查询<i class="ace-icon fa fa-search bigger-110"></i>
								</button>
							</td>
						</tr>
						<tr>
							<td colspan="7">
								<button type="button" onclick="againSetTaskReceiver('${ctx}/taskMgr/againSetTaskReceiverView.do');" class="btn btn-xs btn-success">
									重新分配人员<i class="ace-icon fa fa-share bigger-110"></i>
								</button>
								<c:if test="${ebpmAdmin}">
									<button type="button" onclick="jumpAnyTask('${ctx}/taskMgr/jumpAnyTaskView.do');" class="btn btn-xs btn-success">
										自由跳转<i class="ace-icon fa fa-paper-plane-o bigger-110"></i>
									</button>
								</c:if>
								<button type="button" onclick="processAction('${ctx}/taskMgr/backStarterTask.do','您确定要退回拟稿人环节吗？','1');" class="btn btn-xs btn-yellow">
									退回拟稿环节<i class="ace-icon fa fa-reply-all bigger-110"></i>
								</button>
								<button type="button" onclick="processAction('${ctx}/taskMgr/backPrevTask.do','您确定要退回上一环节吗？','2');" class="btn btn-xs btn-yellow">
									退回上一环节<i class="ace-icon fa fa-reply bigger-110"></i>
								</button>
								<button type="button" onclick="addMultiInstanceView('${ctx}/taskMgr/addMultiInstanceView.do');" class="btn btn-xs btn-success">
									补发多实例<i class="ace-icon fa fa-plus bigger-110"></i>
								</button>
								<button type="button" onclick="processAction('${ctx}/taskMgr/deleteMultiInstance.do','您确定要删除多实例吗？','3');"  class="btn btn-xs btn-grey">
									删除多实例<i class="ace-icon fa fa-trash-o bigger-110"></i>
								</button>
								<c:if test="${ebpmAdmin}">
									<button type="button" onclick="updateTaskData('${ctx}/taskMgr/updateTaskDataView.do');" class="btn btn-xs btn-primary">
										修改业务属性<i class="ace-icon fa fa-edit bigger-110"></i>
									</button>
								</c:if>
							</td>
						</tr>
					</table>
				</div>
				<div class="row">
					<div class="col-xs-12">
						<div class="table-responsive">
							<div style="width: 100%;overflow: auto;">
								<table style=" width: inherit;max-width: 600%;table-layout: fixed;" id="sample-table-1"
									   class="table table-striped table-bordered table-hover">
									<thead>
									<tr>
										<th style="width: 20px"></th>
										<th>应用名称</th>
										<th>流程标题</th>
										<th style="width: 150px;">流程定义</th>
										<th>流程实例ID</th>
										<th>执行人</th>
										<th>当前环节</th>
										<th>发送人</th>
										<th style="width: 150px">接收时间</th>
										<th style="width: 90px">持续时间</th>

									</tr>
									</thead>
									<tbody>
									<%
										Page mypage=(Page)request.getAttribute("page");
										List<Task> tasks=mypage.getResult();
										if(tasks!=null){
											for(int i=0;i<tasks.size();i++){
												Task task=(Task)tasks.get(i);
									%>
									<tr class="<%=((i+1)%2== 0 ? "odd" : "even") %>">
										<td><input type="radio" name="items" value="taskId=<%=task.getId() %>"></td>
										<td class="td" title="<%=task.getTenantId()%>"><%=task.getTenantId()%></td>
										<td class="td">
											<a title="流程实例ID:<%=task.getProcessInstanceId()%>,环节实例ID:<%=task.getId()%>" href="javascript:void(0);" onclick="openProcessTrackPage('${ctx}','<%=task.getProcessInstanceId() %>');">
												<%=task.getProcTitle()%>&nbsp;</a>
										</td>
										<td class="td">
											<a href="javascript:void(0);" onclick="openSysDialog('${ctx}/procDefMgr/config.do?procDefId=<%=task.getProcessDefinitionId() %>');">
												<%=task.getProcessDefinitionName()%>
											</a>
										</td>
										<td class="td" title="<%=task.getProcessInstanceId()%>">
											<%=task.getProcessInstanceId()%>
										</td>
										<td class="td" title="执行人组织：<%=task.getAssigneeOrgName()%>">
											<%if(null!=task.getAssigneeUserName()){ %>
											<%=task.getAssigneeUserName() %>(<%=task.getAssigneeUserId()!=null?task.getAssigneeUserId():""%>)
											<%}else{ %>
											<%=task.getAssigneeUserId()!=null?task.getAssigneeUserId():""%>
											<%} %>
										</td>
										<td class="td" title="<%=task.getName()%>&nbsp;">
											<%=task.getName()%>&nbsp;</td>
										<td class="td" title="发送人组织：<%=task.getSenderOrgName()%>">
											<%if(null!=task.getSendUserName()){ %>
											<%=task.getSendUserName()!=null?task.getSendUserName():""%>(<%=task.getSendUserId()!=null?task.getSendUserId():""%>)
											<%}else{ %>
											<%=task.getSendUserId()!=null?task.getSendUserId():""%>
											<%} %>
										</td>
										<td><%=DateUtil.convertDateToString("yyyy-MM-dd HH:mm:ss",task.getCreateTime())%>&nbsp;</td>
										<td>
											<%String day=com.blueland.bpm.manage.web.ProcessInstanceController.differByDay(task.getCreateTime(),new Date());%>
											<%if(Integer.parseInt(day)<=3){
											%><span class="label label-success">耗时<%=day%>天</span>
											<%
											}else if(Integer.parseInt(day)<=10){%>
											<span class="label label-warning">耗时<%=day%>天</span>
											<%}else if(Integer.parseInt(day)>10){%>
											<span class="label label-danger">耗时<%=day%>天</span>
											<%}%>
										</td>
									</tr>
									<%}} %>
									</tbody>
								</table>
							</div>
							<simpletable:pageToolbar page="${page}"></simpletable:pageToolbar>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</form>
</body>
</html>

