
function showRequestPage(requestId) {
	showRequestTitle(requestId);
	showRequestSearch();
	showRequestBody(requestId);
	newCallback.requestStrategyId = requestId;
}

function showRequestTitle(projectId) {
	$("#title")
			.html(
					"<h2>接口callback策略 管理</h2> <a onclick=\"CallbackPop(newCallback,'新增Callback策略','insert')\" href='#'>新增</> ");
}

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

function showRequestBody(requestId) {
	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/callback/request/" + requestId,
				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++) {
							callbacks[i] = entity.data[i];

							tr += "<td><p>" + (i + 1) + "</p></td>";
							tr += "<td><p>" + entity.data[i].callbackName
									+ "</p></td>";
							tr += "<td><p>" + entity.data[i].orderNum
									+ "</p></td>";
							tr += "<td><p>" + entity.data[i].wait
							+ "S</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=\"CallbackPop(callbacks["
									+ i
									+ "],'编辑Callback','update')\" href=\"#\">编辑</a> &nbsp;&nbsp;&nbsp;&nbsp;";
							tr += "<a onclick=\"deleteCallback('"+entity.data[i].id+"')\" href=\"#\">删除</a>";
							tr += "</td>";
							tr += "</tr>";
						}
						$("#queryDataTable").append(tr);
					}
				}
			});
}

function CallbackPop(callback, title, saveOrUpdate) {

	
	var proTable = ""
	proTable += "<table class='tableInput'>";
	proTable += "<tr>";
	proTable += "<td colspan='4'>";
	proTable += "callback名称：";
	proTable += "<input type='hidden' id='CallbackReqId' value=''  >";
	proTable += "<input type='hidden' id='CallbackId' value=''  >";
	proTable += "<input type='text' id='CallBackName' value='' class='wt400' >";
	proTable += "</td>";
	proTable += "</tr>";
	proTable += "<tr>";
	proTable += "<td >";
	proTable += "执行顺序：";
	proTable += "</td >";
	proTable += "<td >";
	proTable += "<input type='text' id='OrderNum' value=''  class='wt50'>";
	proTable += "</td>";
	proTable += "<td >";
	proTable += "回调等待：";
	proTable += "</td >";
	proTable += "<td >";
	proTable += "<input type='text' id='WaitTime' value=''  class='wt50'>S";
	proTable += "</td>";
	proTable += "</tr>";
	proTable += "<tr><td>类型</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 += "<td colspan='4'>";
	proTable += "预期回调JS：";
	proTable += "</td >";
	proTable += "</tr>";
	proTable += "<tr>";
	proTable += "<td colspan='4'>";
	proTable += "<textarea id='CallBackExpect'></textarea>";
	proTable += "</td>";
	proTable += "</tr>";
	
	proTable += "</table>";
	new pop.ui.alert({
		title : title,
		content : proTable,
		width : 700,
		beforeCallback : function() {
			$('#CallbackId').val( callback.id );			
			$('#CallbackReqId').val( callback.requestStrategyId );		
			$('#CallBackName').val( callback.callbackName );
			$('#WaitTime').val( callback.wait );	
			$('#OrderNum').val( callback.orderNum );	
			$('#Type' + callback.type).attr('selected', 'selected');
			$('#IsRun' + callback.isRun).attr('selected', 'selected');
			$('#CallBackExpect').val( callback.callbackExpect );	
			
		},
		callback : function() {
			saveOrUpdateCallback(saveOrUpdate);
		}
	}).init();
}

function saveOrUpdateCallback(SaveOrUpdate) {
	reqType = SaveOrUpdate == "insert" ? "POST" : "PUT";
	var URL = "/mock/callback";	

	var callbackSU = {};
	if (reqType == "PUT") {
		callbackSU.id = $('#CallbackId').val();
	}
	callbackSU.requestStrategyId =$('#CallbackReqId').val();
	callbackSU.callbackName = $('#CallBackName').val();
	callbackSU.wait = $('#WaitTime').val();
	callbackSU.orderNum = $('#OrderNum').val();
	callbackSU.type = $('#Type').val();
	callbackSU.isRun = $('#IsRun').val();
	callbackSU.callbackExpect = $('#CallBackExpect').val();

	
	data = JSON.stringify(callbackSU);
	$.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") {
				showRequestBody(callbackSU.requestStrategyId);
				alert("操作成功！");

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


function deleteCallback(id){
	var a=confirm("确认删除？");
	 if(a==false){
		 return;
	 }
	var URL = "/mock/callback";
	var cbSU = {id:id};
	data = JSON.stringify(cbSU);
	$.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") {
				showRequestBody(newCallback.requestStrategyId);
				alert("删除成功！");

			} else {
				alert(obj.message)
			}
		}
	});	
	
}

var callbacks = new Array()

var newCallback = {
	id : "",
	callbackName : "",
	requestStrategyId : "",	
	callbackExpect : "",
	wait:"0",
	type : "FD",
	isRun : "RUN",
	orderNum : "0",

	
}


