
function showUriPage(uriId) {
	showUriTitle(uriId);
	showUriSearch();
	showUriBody(uriId);
	newRequest.mockUriId = uriId;
}

function showUriTitle(projectId) {
	$("#title")
			.html(
					"<h2>接口request策略 管理</h2> <a onclick=\"RequestPop(newRequest,'新增Request策略','insert')\" href='#'>新增</> ");
}

function showUriSearch() {
	$("#search").html("<hr>");
}

function showUriBody(UriId) {
	var bodyTalbe = "<table width='100%' border=1 class='Datatable'>" //
			+ "<thead>" //
			+ "<tr>"//
			+ "<th>序号</th>" //
			+ "<th>策略名称</th>" //
			+ "<th>请求等待</th>" //
			+ "<th>响应等待</th>"//
			+ "<th>顺序</th>"//
			+ "<th>类型</th>"//
			+ "<th>启用</th>"//
			+ "</tr>"//
			+ "</thead>" //
			+ "<tbody id='queryDataTable'>" //
			+ "</tbody>"//
			+ "<table>";
	$("#body").html(bodyTalbe);
	
	$.ajax({
				type : "get",
				url : "/mock/request/uri/" + UriId,
				error : function() {
					alert("Error")
				},
				success : function(data) {
					var entity = eval(data).entity;
					if (entity.status == 200) {
						$("#queryDataTable").html("");

						var tr = "<tr>";

						for (var i = 0; i < entity.data.length; i++) {
							requests[i] = entity.data[i];

							tr += "<td><p>" + (i + 1) + "</p></td>";
							tr += "<td><p>" + entity.data[i].mockRequestName
									+ "</p></td>";
							tr += "<td><p>" + entity.data[i].requestWait
									+ "</p></td>";
							tr += "<td><p>" + entity.data[i].responseWait
									+ "</p></td>";
							tr += "<td><p>" + entity.data[i].orderNum + "</p></td>";
							tr += "<td><p>" + entity.data[i].type + "</p></td>";
							tr += "<td><p>" + entity.data[i].isRun
									+ "</p></td>";
							tr += "<td >"
							tr += "<a onclick=\"RequestPop(requests["
									+ i
									+ "],'编辑Uri','update')\" href=\"#\">编辑</a> &nbsp;&nbsp;&nbsp;&nbsp;";
							tr += "<a onclick=\"deleteRequest('"+entity.data[i].id+"')\" href=\"#\">删除</a>";
							tr += "</td>";
							tr += "</tr>";
						}
						$("#queryDataTable").append(tr);
					}
				}
			});
}

function RequestPop(request, title, saveOrUpdate) {

	
	var proTable = ""
	proTable += "<table class='tableInput'>";
	proTable += "<tr>";
	proTable += "<td colspan='4'>";
	proTable += "策略名称：";	
	proTable += "<input type='hidden' id='ReqId' value=''  >";
	proTable += "<input type='hidden' id='MockUriId' value=''  >";
	proTable += "<input type='text' id='MockReqName' value=''  class='wt300'>";
	proTable += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;顺序：&nbsp;&nbsp;&nbsp;&nbsp;<input type='text' id='OrderNum' value=''  class='wt50'>"
	proTable += "</td >";
	proTable += "</tr>";
	proTable += "<tr>";
	proTable += "<td colspan='4'>";
	proTable += "";
	proTable += "</td>";
	proTable += "</tr>";
	proTable += "<tr>";
	proTable += "<td >";
	proTable += "类型：";
	proTable += "</td >";
	proTable += "<td >";
	proTable += "<select id='type'  class='wt100'>";	
	proTable += "<option id='typeFD' value='FD'>正向</option>";
	proTable += "<option id='typeBD' value='BD'>反向</option>";
	proTable += "</select>";
	proTable += "</td>";	
	proTable += "<td>状态</td>";
	proTable += "<td>";
	proTable += "<select id='isRun'  class='wt100'>";	
	proTable += "<option id='isRunRUN' value='RUN'>启用</option>";
	proTable += "<option id='isRunSTOP' value='STOP'>停用</option>";
	proTable += "</select>";
	proTable += "</td></tr>";
	proTable += "<tr>";
	proTable += "<tr>";
	proTable += "<td>";
	proTable += "请求等待时间：";
	proTable += "</td >";
	proTable += "<td>";
	proTable += "<input type='text' id='ReqWait' value='' class='wt50'>";
	proTable += "</td >";
	proTable += "<td >";
	proTable += "响应等待时间：";
	proTable += "</td >";
	proTable += "<td >";
	proTable += "<input type='text' id='ResWait' value=''  class='wt50' >";
	proTable += "</td >";
	proTable += "</tr>";
	proTable += "<tr>";
	proTable += "<td colspan='4'>";
	proTable += "";
	proTable += "预期验证JS：";
	proTable += "</td >";
	proTable += "</tr>";
	proTable += "<tr>";
	proTable += "<td colspan='4'>";
	proTable += "<textarea id='varifyExpect'></textarea>";
	proTable += "</td>";
	proTable += "</tr>";
	proTable += "<tr>";
	proTable += "<td colspan='4'>";
	proTable += "";
	proTable += "预期响应JS：";
	proTable += "</td >";
	proTable += "</tr>";
	proTable += "<tr>";
	proTable += "<td  colspan='4'>";
	proTable += "<textarea id='responseExpect'></textarea>";
	proTable += "</td>";
	proTable += "</tr>";
	proTable += "</table>";
	new pop.ui.alert({
		title : title,
		content : proTable,
		width : 700,
		beforeCallback : function() {
			$('#ReqId').val(request.id);
			$('#MockUriId').val(request.mockUriId) ;
			$('#MockReqName').val(request.mockRequestName);			
			$('#OrderNum').val(request.orderNum);
			$('#type' + request.type).attr('selected', 'selected');
			$('#isRun' + request.isRun).attr('selected', 'selected');
			$('#ReqWait').val(request.requestWait);
			$('#ResWait').val(request.responseWait);
			$('#varifyExpect').val(request.varifyExpect);
			$('#responseExpect').val( request.responseExpect);	
		
		
		
		},
		callback : function() {
			saveOrUpdateRequest(saveOrUpdate);
		}
	}).init();
}

function saveOrUpdateRequest(SaveOrUpdate) {
	reqType = SaveOrUpdate == "insert" ? "POST" : "PUT";
	var URL = "/mock/request";
	
	var reqSU = {};
	if (reqType == "PUT") {
		reqSU.id = $('#ReqId').val();
	}
	reqSU.mockUriId = $('#MockUriId').val();
	reqSU.mockRequestName = $('#MockReqName').val();
	reqSU.responseWait = $('#ReqWait').val();
	reqSU.varifyExpect = $('#varifyExpect').val();
	reqSU.responseExpect = $('#responseExpect').val();
	reqSU.type = $('#type').val();
	reqSU.orderNum = $('#OrderNum').val();
	reqSU.isRun = $('#isRun').val();
	reqSU.requestWait = $('#ReqWait').val();
	reqSU.responseWait = $('#ResWait').val();
	
	data = JSON.stringify(reqSU);
	$.ajax({
		type : reqType,
		url : URL,
		contentType:'application/json; charset=UTF-8',
		data : data,
		error : Error,
		success : function(data) {
			var obj = eval(data).entity;
			var status = obj.status;
			if (status == "201" || status == "200") {
				showUriBody(newRequest.mockUriId)
				alert("操作成功！");

			} else {
				alert("操作失败");
			}
		}
	});
}


function deleteRequest(id){
	var a=confirm("确认删除？");
	 if(a==false){
		 return;
	 }
	var URL = "/mock/request";
	var reqSU = {id:id};
	data = JSON.stringify(reqSU);
	$.ajax({
		type : "DELETE",
		url : URL,		
		contentType:'application/json; charset=UTF-8',
		data : data,
		error : Error,
		success : function(data) {
			var obj = eval(data).entity;
			var status = obj.status;
			if (status == "201" || status == "200") {
				showUriBody(newRequest.mockUriId)
				alert("删除成功！");

			} else {
				alert(obj.message)
			}
		}
	});	
	
}
var requests = new Array()

var newRequest = {
	id : "",
	mockUriId : "",
	mockRequestName : "",
	requestWait : "0",
	responseWait : "0",
	varifyExpect : "",
	responseExpect : "",
	type : "FD",
	isRun : "RUN",
	orderNum : "0"
}


