<!DOCTYPE HTML>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="gray-bg">
    
     <div class="container-div">
		<div class="row">
			<div class="col-sm-12 search-collapse">
				<form id="formId">
					<div class="select-list">
						<ul>
						   <li>
								项目：
								<select id="projectId" name="projectId" th:with="type=${projects}" onchange="setTaskExecuteList()">
								    <option value="0">所有项目</option>
	                                <option th:each="projects : ${type}" th:text="${projects.projectName}" th:value="${projects.projectId}" th:selected="${projects.projectId == defaultProjectId}"></option>
	                            </select>
							</li>
							
							<li>
								任务名称：
								<select id="taskId" name="taskId" th:with="type=${taskExecutes}">
								    <option value="">所有</option>
	                                <option th:each="taskExecutes : ${type}" th:text="${taskExecutes.taskName}" th:value="${taskExecutes.taskId}" th:selected="${taskExecutes.taskId == defaultTaskId}"></option>
	                            </select>
							</li>

							<li>
								用例标识：<input type="text" name="caseSign"/>
							</li>

							<li>
								用例名称：<input type="text" name="caseName"/>
							</li>
							
							<li>
								用例状态：<select name="caseStatus" id="caseStatus" th:with="type=${@dict.getType('case_execute_status')}">
									<option value="">所有</option>
									<option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:selected="${dict.dictValue == defaultCaseStatus}"></option>
								</select>
							</li>

							<li class="select-time">
								<label>时间： </label>
								<input type="text" class="time-input" id="startTime" placeholder="开始时间" name="params[beginTime]"/>
								<span>-</span>
								<input type="text" class="time-input" id="endTime" placeholder="结束时间" name="params[endTime]"/>
							</li>

							<li>
								<a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
								<a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
							</li>
						</ul>
					</div>
				</form>
			</div>
			
	        <div class="btn-group-sm hidden-xs" id="toolbar" role="group">
				<shiro:hasPermission name="testexecution:taskCaseExecute:execution">
				<a class="btn btn-success" onclick="run()">
					<i class="fa fa-forward"></i> 执行全部非成功用例
				</a>
				</shiro:hasPermission>
				<shiro:hasPermission name="testexecution:taskCaseExecute:execution">
				<a class="btn btn-info btn-del btn-del disabled" onclick="runSelect()">
					<i class="fa fa-play"></i> 执行所选用例
				</a>
				</shiro:hasPermission>
			</div>
			<div class="col-sm-12 select-table table-striped">
				<table id="bootstrap-table" data-mobile-responsive="true" style="table-layout:fixed"></table>
			</div>
		</div>
	</div>
    <div th:include="include :: footer"></div>
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('testexecution:taskCaseExecute:edit')}]];
        var removeFlag = [[${@permission.hasPermi('testexecution:taskCaseExecute:remove')}]];
        var prefix = ctx + "testexecution/taskCaseExecute";
        var logPrefix = ctx + "testexecution/taskCaseLog";
        var taskExecutePrefix = ctx + "testexecution/taskExecute";

        $(function() {


			//初始化子表格(无限循环)
			const InitSubTable = function (index, row, $detail) {
				const cur_table = $detail.html('<table style="word-break:break-all;"></table>').find('table');
				$(cur_table).bootstrapTable({
					url: logPrefix + "/list",
					method: 'get',
					queryParams: {
						taskCaseId: row.taskCaseId
					},
					clickToSelect: false,
					detailView: false,//父子表
					uniqueId: "logId",
					columns: [{
						field: 'logId',
						title: '序号',
						width: '5%',
						formatter: function (value, row, index) {
							return '<p align="center">' + (index + 1) + '</p>';
						}
					},
						{
							field: 'logStep',
							title: '步骤编号',
							width: '8%',
							formatter: function (value,
												 row, index) {
								if (value != "ending") {
									return "步骤_" + value;
								} else {
									return '<font style="color:#00bf5f">' + value + '</font>';
								}

							}
						}, {
							field: 'logDetail',
							title: '日志明细',
							width: '65%',
							cellStyle: function (value, row, index, field) {
								if (row.logGrade != "info") {
									return {
										css: {"color": "#ff0000"}
									};
								} else {
									return {
										css: {"color": "#00bf5f"}
									};
								}
							}
						}, {
							field: 'createTime',
							title: '日志时间',
							width: '12%',
							formatter: function (value,
												 row, index) {
								return $.common.formatTime(value);
							}
						}, {
							field: 'logGrade',
							title: '日志级别',
							width: '10%',
							formatter: function (value,
												 row, index) {
								if (row.imgname == null) {
									if (value != 'info') {
										if (row.logDetail.indexOf('测试结果：') > 0) {
											return '<font style="color:#ff0000">' + value + '</font>&nbsp;&nbsp;&nbsp;&nbsp;' +
													'<a href="#" onclick="updateStep(' + row.logId + ')">同步结果</a> ';
										} else {
											return '<font style="color:#ff0000">' + value + '</font>';
										}
									} else {
										return '<font style="color:#00bf5f">' + value + '</font>';
									}
								} else {
									return '<font style="color:#ff0000">' + value + '</font>&nbsp;&nbsp;&nbsp;&nbsp;' +
											'<a onclick="showPlanCase(' + row.logId + ')">错误截图</a> ';
								}
							}
						},],
					//无线循环取子表，直到子表里面没有记录
					onExpandRow: function (index, row, $Subdetail) {
						oInit.InitSubTable(index, row, $Subdetail);
					},

				});
			};
			var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
				exportUrl: prefix + "/export",
                modalName: "任务用例执行记录",
				search: false,
				queryParams: function(params) {
	            	return {
	        			// 传递参数查询参数
	                    pageSize:       params.limit,
	                    pageNum:        params.offset / params.limit + 1,
	                    searchValue:    params.search,
	                    orderByColumn:  params.sort,
	                    isAsc:          params.order,
	                    taskId:         $('#taskId').val(),
	                    caseStatus:     $('#caseStatus').val(),
	                    projectId:      $('#projectId').val()
	        		};
				},
		        showExport: false,
		        detailView : true, //是否显示父子表
                columns: [{
		            checkbox: true,
					formatter : function(value,
							row, index) {
						if(row.caseStatus==0){
			                return {  
			                    disabled : "disabled",//设置是否可用
			                }; 
						}

					}
		        },
				{
					field : 'taskCaseId', 
					title : '用例执行ID',
					sortable: true,
					visible: false
				},
				{
					field : 'caseId', 
					title : '用例ID',
					visible: false
				},
				{
					field : 'taskId', 
					title : '任务ID',
					visible: false
				},
				{
					field : 'projectId', 
					title : '项目ID',
					visible: false
				},
				{
					field : 'caseSign', 
					title : '用例标识',
					width : '10%',
					class : 'myTDLengthHidden'
				},
				{
					field : 'caseName', 
					title : '用例名称',
					width : '50%',
					class : 'myTDLengthHidden'
				},
				{
					field : 'createTime', 
					title : '开始时间',
					width : '15%',
					class : 'myTDLengthHidden'
				},
				{
					field : 'finishTime', 
					title : '结束时间',
					width : '15%',
					formatter : function(value,
							row, index) {
						if(value==null){
							return "";
						}else{
							return value;
						}
					}
				},
				{
					field : 'caseStatus', 
					title : '用例状态',
					width : '10%',
					sortable: true,
					class : 'myTDLengthHidden',
					formatter : function(value,
							row, index) {
						if(value==0){
							return '<span style="color:#00bf5f">成功</span>';
						}else if(value==1){
							return '<span style="color:#ff0000">失败</span>';
						}else if(value==2){
							return '<span style="color:#FF7F00">锁定</span>';
						}else if(value==3){
							return '<span style="color:#0066FF">执行中</span>';
						}else{
							return '<span style="color:#9966FF">未执行</span>';
						}
					}
				}],
				//注册加载子表的事件。注意下这里的三个参数！
				onExpandRow : function(index, row, $detail) {
					InitSubTable(index, row, $detail);
				}
            };
                
            $.table.init(options);
        });
        
		/* 同步测试结果到用例步骤 */
		function updateStep(logId){
    		$.modal.confirm("确认要同步测试结果到步骤中吗？", function() {
    			var url = prefix + "/synchronousTestResults";
    			var data = {"logId": logId};
    			$.operate.submit(url, "post", "json", data);
    		});
		}
		
		/* 运行所有非成功用例 */
		function run(){
    		$.modal.confirm("确认要运行所有非成功用例吗？", function() {
    			var url = prefix + "/runAllFailCase";
    			var data = {"taskId": $('#taskId').val()};
    			$.operate.submit(url, "post", "json", data);
    		});
		}
		
        function runSelect(){
            var rows = $.map($('#bootstrap-table').bootstrapTable('getSelections'), function (row) {
    	        return row[$.table._option.columns[2].field];
    	    });
     		if (rows.length == 0) {
    			$.modal.alertWarning("请至少选择一条记录");
    			return;
    		}
    		$.modal.confirm("确认要运行选中的" + rows.length + "条用例吗?", function() {
    			var url = prefix + "/runSelectCase";
    			var data = { "taskId": $('#taskId').val(),"ids": rows.join()};
    			$.operate.submit(url, "post", "json", data);
    		});
        }
        
        /*设置任务列表*/
        function setTaskExecuteList(){
          var projectId = $("#projectId").val();
          if($.common.isEmpty(projectId)) return;
          var url =taskExecutePrefix + "/gettaskExecuteListByProjectId/"+projectId;
          var options = "";
          $.getJSON(url, null, function call(result) {
        	   /*清空原有调度列表*/
        	   while($("#taskId").length>1){
        		  $("#taskId option[index='1']").remove();
        	    }
        	   /*设置所有任务*/
        	   options +=  "<option value=''>所有</option>";
        	   /*设置新的调度列表*/
               $.each(result, function(i, node){
        		  options +=  "<option value='"+node.taskId+"'>"+node.taskName+"</option>";
        	    }); 
        	    $("#taskId").html(options);
          });
         }
        
        /*弹出层展示图片*/
        function showPlanCase(logId) {
        	var url = prefix+"/showImage.do?logId="+logId;
        	$.modal.openViewFull($.table._option.childrenModalName, url, null, null);
        }
    </script>
</body>
</html>