<!DOCTYPE HTML>
<html  lang="zh" xmlns:th="http://www.thymeleaf.org">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-taskScheduling-add">
           <div class="form-group">	
				<label class="col-sm-3 control-label">调度名称：</label>
				<div class="col-sm-8">
					<input id="schedulingName" name="schedulingName" class="form-control" type="text">
				</div>
		   </div>
           <div class="form-group">	
				<label class="col-sm-3 control-label">项目名称：</label>
				<div class="col-sm-8">
					<select id="projectId" name="projectId" class="form-control m-b" th:with="type=${projects}" onchange="initializeChildren()">
	                    <option th:each="project : ${type}" th:text="${project.projectName}" th:value="${project.projectId}" th:selected="${project.projectId == defaultProjectId}"></option>
	                </select>
				</div>
			</div>
			<div class="form-group">	
				<label class="col-sm-3 control-label">测试计划：</label>
				<div class="col-sm-8">
					<select id="planId" name="planId" class="form-control m-b" th:with="type=${planList}">
	                    <option th:each="plan : ${type}" th:text="${plan.planName}" th:value="${plan.planId}"></option>
	                </select>
				</div>
			</div>
			<div class="form-group">	
				<label class="col-sm-3 control-label">客户端：</label>
				<div class="col-sm-8">
					<select id="clientId" name="clientId" class="form-control m-b" th:with="type=${clientList}" onchange="setDriverPathList()">
	                    <option th:each="client : ${type}" th:text="${client.status==0} ? ${client.clientName}+'【'+${client.clientIp}+'】' : ${client.clientName}+'【'+${client.clientIp}+'】>>>状态异常'" th:value="${client.clientId}"></option>
	                </select>
				</div>
			</div>
			<div class="form-group">	
				<label class="col-sm-3 control-label">客户端测试驱动桩路径：</label>
				<div class="col-sm-8">
					<select id="clientDriverPath" name="clientDriverPath" class="form-control m-b" th:with="type=${driverPathList}">
	                    <option th:each="driverPath : ${type}" th:text="${driverPath}" th:value="${driverPath}"></option>
	                </select>
				</div>
			</div>
			<div class="form-group">	
				<label class="col-sm-3 control-label">客户端执行线程数：</label>
				<div class="col-sm-8">
					<input id="exThreadCount" name="exThreadCount" class="form-control" type="text" value="1">
				</div>
			</div>
			<div class="form-group">	
				<label class="col-sm-3 control-label">任务类型：</label>
				<div class="col-sm-8">
					<label class="radio-box"> <input type="radio" name="taskType" value="0" th:checked="true" /> 接口 </label> 
					<label class="radio-box"> <input type="radio" name="taskType" value="1" /> Web UI </label> 
					<label class="radio-box"> <input type="radio" name="taskType" value="2" /> 移动端 </label>
				</div>
			</div>
			<div class="form-group" id="divBrowserType" style="display:none;">	
				<label class="col-sm-3 control-label">浏览器类型：</label>
				<div class="col-sm-8">
					<label class="radio-box"> <input type="radio" name="browserType" value="0" th:checked="true"/> IE </label> 
					<label class="radio-box"> <input type="radio" name="browserType" value="1" /> 火狐 </label> 
					<label class="radio-box"> <input type="radio" name="browserType" value="2" /> 谷歌 </label>
					<label class="radio-box"> <input type="radio" name="browserType" value="3" /> Edge </label>
				</div>
			</div>			
			<div class="form-group">	
				<label class="col-sm-3 control-label">任务超时时间(分钟)：</label>
				<div class="col-sm-8">
					<input id="taskTimeout" name="taskTimeout" class="form-control" type="text" value="60">
				</div>
			</div>
			<div class="form-group">	
				<label class="col-sm-3 control-label">Cron执行表达式：</label>
				<div class="col-sm-8">
					<input id="cronExpression" name="cronExpression" class="form-control placeholder" type="text" placeholder="每天凌晨3点45分运行，示例：0 45 3 * * ? ">
				</div>
			</div>
			<div class="form-group">	
				<label class="col-sm-3 control-label">执行策略：</label>
				<div class="col-sm-8">
					<label class="radio-box"> <input type="radio" name="misfirePolicy" value="3" th:checked="true"/> 默认Cron执行 </label> 
					<label class="radio-box"> <input type="radio" name="misfirePolicy" value="2" /> 按Cron自动执行一次 </label> 
				</div>
			</div>
			<div class="form-group">	
				<label class="col-sm-3 control-label">任务状态：</label>
				<div class="col-sm-8">
					<div class="radio-box" th:each="dict : ${@dict.getType('sys_job_status')}">
						<input type="radio" th:id="${dict.dictCode}" name="status" th:value="${dict.dictValue}" th:checked="${dict.isDefault == 'Y' ? true : false}">
						<label th:for="${dict.dictCode}" th:text="${dict.dictLabel}"></label>
					</div>
				</div>
			</div>
			<div class="form-group">	
				<label class="col-sm-3 control-label">任务结果通知：</label>
				<div class="col-sm-8">
				    <label class="radio-box"> <input type="radio" name="emailSendCondition" value="-1" th:checked="true"/> 不通知 </label> 
					<label class="radio-box"> <input type="radio" name="emailSendCondition" value="0" /> 默认 </label> 
					<label class="radio-box"> <input type="radio" name="emailSendCondition" value="1" /> 任务成功 </label> 
					<label class="radio-box"> <input type="radio" name="emailSendCondition" value="2" /> 任务失败 </label>
				</div>
			</div>
			<div class="form-group" id="divEmailAddress" style="display:none;">
				<label class="col-sm-3 control-label">邮件通知地址：</label>
				<div class="col-sm-8">
					<textarea id="emailAddress" name="emailAddress" class="form-control" placeholder="多个邮件地址用;分隔"></textarea>
				</div>
			</div>
			<div class="form-group" id="divPushUrl" style="display:none;">
				<label class="col-sm-3 control-label">第三方推送地址：</label>
				<div class="col-sm-8">
					<textarea id="pushUrl" name="pushUrl" class="form-control" placeholder="例如：企业微信或是钉钉机器人WebHook推送地址"></textarea>
				</div>
			</div>
			<div class="form-group">	
				<label class="col-sm-3 control-label">jenkins名称：</label>
				<div class="col-sm-8">
					<textarea id="buildingLink" name="buildingLink" class="form-control" placeholder="Jenkins中的项目名称，多个以;分隔"></textarea>
				</div>
			</div>
			<div class="form-group">	
				<label class="col-sm-3 control-label">远程执行Shell：</label>
				<div class="col-sm-8">
					<textarea id="remoteShell" name="remoteShell" class="form-control" placeholder="格式：服务器IP;服务器用户名;服务器密码;ssh端口;Shell命令; 例：10.218.88.88;root;123456;22;cd /home/tomcat-7.0-7080/bin&&./restart.sh;"></textarea>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">备注：</label>
				<div class="col-sm-8">
					<textarea id="remark" name="remark" class="form-control"></textarea>
				</div>
			</div>
		</form>
	</div>
    <div th:include="include::footer"></div>
    <script type="text/javascript">
		var prefix = ctx + "testexecution/taskScheduling";
		var clientPrefix = ctx + "system/client";
		var planPrefix = ctx + "testmanagmt/projectPlan";
		
		$.validator.setDefaults({
			ignore:":hidden:not(select)",
			submitHandler : function() {
				save();
			}
		});
		
		$("#form-taskScheduling-add").validate({
			rules:{
				schedulingName:{
					required:true,
        			maxlength: 50,
                    remote: {
                        url: prefix + "/checkSchedulingNameUnique",
                        type: "post",
                        dataType: "json",
                        data: {
                            name: function () {
                                return $.common.trim($("#schedulingName").val());
                            }
                        },
                        dataFilter: function (data, type) {
                        	return $.validate.unique(data);
                        }
                    }
				},
				projectId:{
					required:true,
				},
				planId:{
					required:true,
				},
				clientId:{
					required:true,
				},
				clientDriverPath:{
					required:true,
				},
				exThreadCount:{
					required:true,
        			isIntGtZero:true,
        			isInteger:true,
        			max:500
				},
				taskTimeout:{
					required:true,
        			isIntGtZero:true,
        			isInteger:true,
        			min:5,
        			max:180
				},
				cronExpression:{
					required:true,
        			maxlength: 250,
                    remote: {
                        url: prefix + "/checkCronExpressionIsValid",
                        type: "post",
                        dataType: "json",
                        data: {
                            name: function () {
                                return $.common.trim($("#cronExpression").val());
                            }
                        },
                        dataFilter: function (data, type) {
                        	return $.validate.unique(data);
                        }
                    }
				},
				"emailAddress":{
					maxlength:300
				},
				"pushUrl":{
					maxlength:200
				},
				"buildingLink":{
					maxlength:200
				},
				"remoteShell":{
					maxlength:200
				},
				"remark":{
					maxlength:500
				},
			},
        	messages: {
                "schedulingName": {
                    remote: "调度名称在列表中已经存在"
                },
                "cronExpression": {
                    remote: "Cron表达式无效"
                }
            }
		});
				
		function submitHandler() {
	        if ($.validate.form()) {
	            $.operate.save(prefix + "/add", $('#form-taskScheduling-add').serialize());
	        }
	    }
		
		//获取radio当前选中的值；icheck-radio change事件；点击事件，不能使用onclick;
		$("input:radio[name='taskType']").on('ifChecked', function(event){
			var taskType=$(this).val();
		    if(taskType=='1'){
				$("#divBrowserType").css("display","block");	
			}else{
				$("#divBrowserType").css("display","none");
			}
		});
		
		//获取radio当前选中的值；icheck-radio change事件；点击事件，不能使用onclick;
		$("input:radio[name='emailSendCondition']").on('ifChecked', function(event){
			var emailSendCondition=$(this).val();
		    if(emailSendCondition!='-1'){
				$("#divEmailAddress").css("display","block");
				$("#divPushUrl").css("display","block");
			}else{
				$("#divEmailAddress").css("display","none");
				$("#divPushUrl").css("display","none");
			}
		});

		function initializeChildren() {
			var projectId = $("#projectId").val();
			if($.common.isEmpty(projectId)) return;
			setPlanList(projectId);
			setClientList(projectId);
	    }
		
		 /*设置客户端列表*/
		 function setClientList(projectId){
	       var url =clientPrefix + "/getClientListByProjectId/"+projectId;
	       var options = "";
	       $.getJSON(url, null, function call(result) {
	    	   /*清空原有客户端列表*/
	 		   while($("#clientId").length>1){
				  $("#clientId option[index='1']").remove();
			    }
	 		   /*设置新的客户端列表*/
	            $.each(result, function(i, node){
	     		   if(node.status==0){
	    		       options +=  "<option style='color:#228B22' value='"+node.clientId+"'>【"+node.clientName+"】"+node.clientIp+" 【客户端状态正常】</option>";
	    		   }else if(node.status==1){
	    			   options +=  "<option style='color:#FF6347' value='"+node.clientId+"'>【"+node.clientName+"】"+node.clientIp+" 【客户端状态异常】</option>";
	    		   }else{
	    			   options +=  "<option style='color:#DAA520' value='"+node.clientId+"'>【"+node.clientName+"】"+node.clientIp+" 【客户端状态未知】</option>";
	    		   }
	  		    }); 
	  		    $("#clientId").html(options);
	  		    /*初始化驱动目录列表*/
	  		    setDriverPathList();
	       });
		  }
		 
		 /*设置项目计划列表*/
		 function setPlanList(projectId){
	       var url =planPrefix + "/getProjectPlanListByProjectId/"+projectId;
	       var options = "";
	       $.getJSON(url, null, function call(result) {
	    	   /*清空原有客户端列表*/
	 		   while($("#planId").length>1){
				  $("#planId option[index='1']").remove();
			    }
	 		   /*设置新的客户端列表*/
	            $.each(result, function(i, node){
	  			  options +=  "<option value='"+node.planId+"'>"+node.planName+"</option>";
	  		    }); 
	  		    $("#planId").html(options);
	       });
		  }
		 
		 /*设置驱动目录列表*/
		 function setDriverPathList(){
		   var clientId = $("#clientId").val();
		   if($.common.isEmpty(clientId)) return;
	       var url =clientPrefix + "/getDriverPathList/"+clientId;
	       var options = "";
	       $.getJSON(url, null, function call(result) {
	    	   /*清空原有客户端列表*/
	 		   while($("#clientDriverPath").length>1){
				  $("#clientDriverPath option[index='1']").remove();
			    }
	 		   /*设置新的客户端列表*/
	            $.each(result, function(i, node){
	  			  options +=  "<option value='"+node+"'>"+node+"</option>";
	  		    }); 
	  		    $("#clientDriverPath").html(options);
	       });
		  }

	</script>
</body>
</html>
