<%@page import="org.activiti.engine.repository.ProcessDefinition" %>
<%@page import="org.activiti.engine.impl.persistence.entity.*" %>
<%@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" %>
<%
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>
	<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>

		<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>
	<script type="text/javascript" >
		$(document).ready(function() {
			// 分页需要依赖的初始化动作
			window.simpleTable = new SimpleTable('queryForm','${page.thisPageNumber}','${page.pageSize}','${pageRequest.sortColumns}');
			
		});
		
		
		function updateProcessComment(url){
			var procInstId=getOneProcInstId();
			if(!procInstId){
				return;
			}
			showDiaLog(url+"?procInstId="+procInstId,"修改流转意见",'1024px','550px');
		}
		
		function updateProcessTime(url){
			var procInstId=getOneProcInstId();
			if(!procInstId){
				return;
			}
			showDiaLog(url+"?procInstId="+procInstId,"修改流转时间",'1200px','550px');
		}
		function delProcessRoam(url){
			var procInstId=getOneProcInstId();
			if(!procInstId){
				return;
			}
			showDiaLog(url+"?procInstId="+procInstId,"修改流程环节",'1400px','550px');
		}
		
		
		function getOneProcInstId(){
			var chk_value =[]; 
			$("input[name='items']:checkbox:checked").each(function(){ 
				chk_value.push($(this).val());
			})
			if(chk_value.length==0){
				alert("请选择一条流程数据！");
				return;
			}else if(chk_value.length>1){
				alert("请选择一条流程数据！");
				return;
			}
			return chk_value[0];
		}
		
		function processAction(myUrl,title,id){
			
			var chk_value =[]; 
			var processInstanceId="";
			$("input[name='items']:checkbox:checked").each(function(){ 
				chk_value.push($(this).val());
			})
			if(chk_value.length==0){
				alert("请选择一条流程数据！");
				return;
			}else{
				for(var i=0;i<chk_value.length;i++){
					processInstanceId+=chk_value[i]+";";
				}
				
				/* if(id=='cancel'){//作废操作
					var needReturn = false;
					//判断是否有权限进行作废操作
					jQuery.ajax({
						url: "${ctx}/taskMgr/getButOperatePower.do",
						data: { "curProcInstId": processInstanceId,"butOperateId":"CANCEL_BTN"},
						type: "POST",
						async: false,
						success: function(resp)
						{
							if(!resp){
								alert("当前流程无法进行作废操作！");
								needReturn = true;
							}
						}
					});
					if(needReturn){
						return ;
					}
				
				} */
				
			}
			setMask("main-container");
			if (confirm(title)) {
				jQuery.ajax({
					url: myUrl,
					data: { "processInstanceId": processInstanceId},
					type: "POST",
					success: function(resp)
					{
						alert(resp);
						window.location.reload();
					}
				});
			}else{
				$("#main-container").unmask();
			}
		}
		
	</script>
</head>
<body>
	<%@ include file="/commons/messages.jsp" %>
	<form id="queryForm" name="queryForm" action="${ctx}/procInstMgr/list.do" method="post" style="display: inline;">
			<div class="main-container" id="main-container">
		<div class="panel panel-primary">
			<div class="panel-heading">
				<h8>流程实例管理</h8>
			</div>
			<div class="panel-body">
				<div class="form-group well well-sm" style="margin-bottom: 2px;">

				<table border="0" cellspacing="0" cellpadding="0"
					class="table-search">
					<tr>	
						<!-- <td>流程定义Key：</td>		
						<td>
							<input value="${query.processDefKey}" id="processDefKey" name="processDefKey" />
						</td>-->
						<td  width="100px">流程定义名称：</td>
						<td>
							<input value="${query.processDefName}" id="processDefName" name="processDefName" />
						</td>
						<td>流程标题：</td>		
						<td>
							<input value="${query.title}" id="title" name="title" />
						</td>
						<td  width="80px">流程实例ID：</td>		
						<td>
							<input value="${query.processInstanceId}" id="processInstanceId" name="processInstanceId" />
							<button type="submit" class="btn btn-xs btn-primary">
									查&nbsp;&nbsp;询
									<i class="ace-icon fa fa-search bigger-110"></i>
							</button>
							
						</td>
						
					</tr>
					<tr>
						<td>创建人：</td>		
						<td>
							<input value="${query.userId}" id=userId name="userId" />
						</td>	
						
					</tr>
					<tr>
						<td colspan="6">
						<!-- 
						<c:if test="${isBpmAdmin}">
							<button type="button" id="pause" onclick="processAction('${ctx}/procInstMgr/suspend.do','您确认要暂停该流程吗？',this.id);" class="btn btn-xs btn-warning">
									暂&nbsp;&nbsp;停
									<i class="ace-icon fa fa-pause bigger-110"></i>
							</button>
							<button type="button" id="recover" onclick="processAction('${ctx}/procInstMgr/restore.do','您确认要恢复该流程吗？',this.id);" class="btn btn-xs btn-success">
									恢&nbsp;&nbsp;复
									<i class="ace-icon fa fa-play bigger-110"></i>
							</button>
						</c:if>
						 -->
						<button type="button" id="cancel" onclick="processAction('${ctx}/procInstMgr/cancel.do','您确认要作废该流程吗？作废后的流程可在流程历史管理中查询到！',this.id);" class="btn btn-xs btn-danger">
									作&nbsp;&nbsp;废
						<i class="ace-icon fa fa-trash-o bigger-110"></i>
						</button>
						<c:if test="${isBpmAdmin}">
							<button type="button" id="delete" onclick="processAction('${ctx}/procInstMgr/delete.do','您确认要删除该流程吗？删除流程后数据将无法恢复！',this.id);" class="btn btn-xs btn-inverse">
										删&nbsp;&nbsp;除
							<i class="ace-icon fa fa-trash-o bigger-110"></i>
							</button>
							
							<button type="button" onclick="updateProcessComment('${ctx}/taskMgr/updateProcessCommentView.do');" class="btn btn-xs btn-primary">
									修改流转意见
								<i class="ace-icon fa fa-pencil-square-o bigger-110"></i>
							</button>
							
							<button type="button" onclick="updateProcessTime('${ctx}/procInstMgr/updateProcessTimeView.do');" class="btn btn-xs btn-primary">
									修改流转时间
								<i class="ace-icon fa fa-pencil-square-o bigger-110"></i>
							</button>
							<button type="button" onclick="delProcessRoam('${ctx}/procInstMgr/delProcessRoamView.do');" class="btn btn-xs btn-primary">
									修改流程环节
								<i class="ace-icon fa fa-pencil-square-o bigger-110"></i>
							</button>
						</c:if>
						
						</td>
					</tr>	
					
				</table>
			</div>
			<div class="row">
					<div class="col-xs-12">
						<div class="table-responsive">
							<div>

								<table id="sample-table-1"
									   style="max-width: 600%;table-layout: fixed;"
									   class="table table-striped table-bordered table-hover">
									<thead>
									<tr>
										<th style="width: 20px"><input type="checkbox" onclick="setAllCheckboxState('items',this.checked)"></th>
										<th style="width: 150px;">流程定义名称</th>
										<th>流程标题</th>
										<th >流程实例ID</th>
										<th>创建人</th>
										<th >创建部门</th>
										<th style="width: 150px">发起时间</th>
										<th style="width: 90px">持续时间</th>
										<th style="width: 90px">状态</th>
										<!-- 排序时为th增加sortColumn即可,new SimpleTable('sortColumns')会为tableHeader自动增加排序功能; -->


									</tr>
									</thead>
									<tbody>

									<%
										Page mypage=(Page)request.getAttribute("page");
										List<HistoricProcessInstanceEntity> histProcInsts=mypage.getResult();


										if(histProcInsts!=null){
											for(int i=0;i<histProcInsts.size();i++){
												HistoricProcessInstanceEntity histProcInst=(HistoricProcessInstanceEntity)histProcInsts.get(i);

									%>
									<tr class="<%=((i+1)%2== 0 ? "odd" : "even") %>">
										<td><input type="checkbox" name="items" value="<%=histProcInst.getId()%>"></td>
										<td title="<%=histProcInst.getProcessDefinitionName()%>"  class="td"><%=histProcInst.getProcessDefinitionName()%>&nbsp;					</td>
										<td title="<%=histProcInst.getProcTitle()%>&nbsp;"  class="td">
											<a href="javascript:void(0);" onclick="openProcessTrackPage('${ctx}','<%=histProcInst.getId() %>');">
												<%=histProcInst.getProcTitle()%>&nbsp;</a>
										</td>
										<td title="<%=histProcInst.getId()%>"
											class="td"><%=histProcInst.getId()%></td>
										<td title="<%if(null!=histProcInst.getStartUserName()){%>
											<%=histProcInst.getStartUserName()%>&nbsp;(<%=histProcInst.getStartUserId()%>)
											<%}else{ %>
											<%=histProcInst.getStartUserId()%>
											<%} %>" class="td">
											<%if(null!=histProcInst.getStartUserName()){%>
											<%=histProcInst.getStartUserName()%>&nbsp;(<%=histProcInst.getStartUserId()%>)
											<%}else{ %>
											<%=histProcInst.getStartUserId()%>
											<%} %>
										</td>
										<td title="<%=histProcInst.getStarterOrgName()%>" class="td">
											<%=histProcInst.getStarterOrgName()%>
										</td>
										<td title="<%=DateUtil.convertDateToString("yyyy-MM-dd HH:mm:ss", histProcInst.getStartTime()) %>"  class="td">
											<%=DateUtil.convertDateToString("yyyy-MM-dd HH:mm:ss", histProcInst.getStartTime()) %>
										</td>

										<td>
											<%String day=com.blueland.bpm.manage.web.ProcessInstanceController.differByDay(histProcInst.getStartTime(),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>
										<td>
											<%if(histProcInst.getProcState()==1){
											%><span class="label label-success">正常</span>
											<%
											}else if(histProcInst.getProcState()==2){%>
											<span class="label label-warning">暂停</span>
											<%}else if(histProcInst.getProcState()==3){%>
											<span class="label label-danger">作废</span>
											<%}%>
										</td>
									</tr>
									<%}} %>
									</tbody>
								</table>
							</div>
				<simpletable:pageToolbar page="${page}">
				</simpletable:pageToolbar>
			</div>
						<!-- /.table-responsive -->
					</div>
					<!-- /span -->
				</div>
			</div>
		</div>

	</div>
		</form>
</body>
</html>	

