define(['jquery','httprequest','bootstrap-dialog'],
function($,httprequest,BootstrapDialog){
	var MockCallback = {
			data : [],
			
			
			
	};
	
	MockCallback.getCallbackByRequest = function(requestId,ulcb){
		MockCallback.requestId =requestId;
		MockCallback.ulcb =ulcb;
		
		$("#"+ulcb).html("");
		httprequest.send({
			type : "GET",
			url : "/mock/callback/request/" + requestId,
			dataType:"json",
			success : function(obj,status) {
				var entity = obj.entity;
				if (entity.status == 200) {
					$("#"+ulcb).html("")
					MockCallback.data = entity.data;
					for (var i = 0; i < entity.data.length; i++) {
						var callback = entity.data[i];
						var li = "<li class='list-group-item'>";
						li += "	<div class='row'>";
						li += "		<div class='col-md-6 '>";
						li += "			<h4><span class='label label-info'><span class='glyphicon glyphicon-repeat' > "+callback.callbackName+"</span></span></h4>";
						li += "		</div>";
						
						li += "		<div class='col-md-1'>";
						var type = callback.type=== "FD" ? "label-success" :"label-warning";
						li += "			<h4><span class='label "+type+"'><span class='glyphicon glyphicon-asterisk'> "+callback.type+" </span></span></h4>";
						li += "		</div>";
						
						li += "		<div class='col-md-1'>";
						var isRun = callback.isRun=== "RUN" ? "label-success" :"label-danger";
						li += "			<h4><span class='label "+isRun+"'><span class='glyphicon glyphicon-retweet'> "+callback.isRun+" </span></span></h4>";
						li += "		</div>";
						
						li += "		<div class='col-md-1'>";
						li += "			<h4><span class='label label-primary'><span class='glyphicon glyphicon-time'> "+callback.wait+"s</span></span></h4>";
						li += "		</div>";
						
						li += "		<div class='col-md-1'>";
						li += "			<h4><span class='label label-primary'><span class='glyphicon glyphicon-sort-by-attributes'> "+callback.orderNum+"</span></span></h4>";
						li += "		</div>";
						
						
						li += "		<div class='col-md-2'>";
						li += "				<a id='a-edit"+callback.id+"' reid ='"+callback.id+"'class='btn btn-primary' href='#'><span class='glyphicon glyphicon-cog'></span></a>";
						li += "				<a id='a-delete"+callback.id+"' reid ='"+callback.id+"' class='btn btn-primary' href='#'><span class='glyphicon glyphicon-minus'></span></a>";
						li += "		</div>";
						li += "	</div>";
						li += "</li>";
						$("#"+ulcb).append(li)
						
						
						$("#a-edit"+callback.id).on('click',function(){
							var id = $(this).attr("reid");
							var data = getCallback(id);
							if(data.id != undefined){
								MockCallback.editCallback(data);
							}	
							
						});
						$("#a-delete"+callback.id).on('click',function(){
							var id = $(this).attr("reid");
							MockCallback.delCallback(id);
							
						});
					
					}
				}
			}
		});
		
		
	};
	
	
	MockCallback.createCallback = function(requestId){
		 BootstrapDialog.show({
	            size: BootstrapDialog.SIZE_WIDE,
	            title:"新增回调",
	            //CallbackReqId,CallbackId,CallBackName,OrderNum,,WaitTime,Type,IsRun,CallBackExpect
	            message: callbackDemo(requestId ,'','' ,'0' ,'0','','',''),
	            closable: true,
	            closeByBackdrop: false,
	            closeByKeyboard: false,
	            buttons: [{
	                label: '确定',
	                cssClass: 'btn-primary',
	                action: function(dialog){
	            		saveCallback("insert",dialog)
	                }
	            }, {
	                label: '取消',
	                action: function(dialog){
	            		dialog.close();
	                }
	            }]
	        });
	}
	
	MockCallback.editCallback = function(data){
		 BootstrapDialog.show({
	            size: BootstrapDialog.SIZE_WIDE,
	            title:"修改回调",
	            //CallbackReqId,CallbackId,CallBackName,OrderNum,,WaitTime,Type,IsRun,CallBackExpect
	            message: callbackDemo(data.requestStrategyId ,data.id ,data.callbackName ,data.orderNum ,data.wait,data.type,data.isRun,data.callbackExpect),
	            closable: true,
	            closeByBackdrop: false,
	            closeByKeyboard: false,
	            buttons: [{
	                label: '确定',
	                cssClass: 'btn-primary',
	                action: function(dialog){
	            		saveCallback("update",dialog)
	                }
	            }, {
	                label: '取消',
	                action: function(dialog){
	            		dialog.close();
	                }
	            }],
	            onshown : function(){
		 		$("#CallBackExpect").val(data.callbackExpect);
		 		 
	 		}
	            		
	        });
	}
	
	
	MockCallback.delCallback = function(id){
		var a=confirm("确认删除？");
		 if(a==false){
			 return;
		 }
		$.ajax({
			type : "DELETE",
			url : "/mock/callback",		
			contentType:'json',
			dataType:'json',
			data : {id:id},
			success : function(obj,status) {
				var entity = obj.entity;
				var status = entity.status;
				if (status == "201" || status == "200") {
					dialog.close();
					MockCallback.getCallbackByRequest(MockCallback.requestId,MockCallback.ulcb);
					BootstrapDialog.alert("操作成功！");

				} else {
					BootstrapDialog.alert("操作失败");
				}
			}
		});	
	}
	
	function getCallback(id){
		for(var i=0; i<MockCallback.data.length ;i++){
			var data = MockCallback.data[i];
			if(data.id === id){
				return data;
			}
		}
		return {};
	}
	
	
	function saveCallback(SaveOrUpdate,dialog) {
		type = SaveOrUpdate == "insert" ? "POST" : "PUT";

		var callbackSU = {};
		if (type == "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();
		
		httprequest.send({
			type : type,
			url : "/mock/callback",
			contentType:'json',
			dataType:'json',
			data : callbackSU,
			success : function(obj,status) {
				var entity = obj.entity;
				var status = entity.status;
				if (status == "201" || status == "200") {
					dialog.close();
					MockCallback.getCallbackByRequest(MockCallback.requestId,MockCallback.ulcb);
					BootstrapDialog.alert("操作成功！");

				} else {
					BootstrapDialog.alert("操作失败");
				}
			}
		});
	}
	
	function callbackDemo(CallbackReqId,CallbackId,CallBackName,OrderNum,WaitTime,Type,IsRun,CallBackExpect){
		
		var typeFD = Type ==="FD" ? "selected" :"";
		var typeBD = Type ==="BD" ? "selected" :"";
		
		var runRun = IsRun ==="RUN" ? "selected" :"";
		var runStop = IsRun ==="STOP" ? "selected" :"";
		
		var table = "";
			table += "<table class='table'>";
			table += "<tr>";
			table += "<td colspan='4'>";
			table += "callback名称：";
			table += "<input type='hidden' id='CallbackReqId' value='"+CallbackReqId+"'  >";
			table += "<input type='hidden' id='CallbackId' value='"+CallbackId+"'  >";
			table += "<input type='text' id='CallBackName' value='"+CallBackName+"' class='wt400' >";
			table += "</td>";
			table += "</tr>";
			table += "<tr>";
			table += "<td >";
			table += "执行顺序：";
			table += "</td >";
			table += "<td >";
			table += "<input type='text' id='OrderNum' value='"+OrderNum+"'  class='wt50'>";
			table += "</td>";
			table += "<td >";
			table += "回调等待：";
			table += "</td >";
			table += "<td >";
			table += "<input type='text' id='WaitTime' value='"+WaitTime+"'  class='wt50'>S";
			table += "</td>";
			table += "</tr>";
			table += "<tr><td>类型</td>";
			table += "<td>";
			table += "<select id='Type' class='wt100'>";	
			table += "<option id='TypeFD' value='FD' "+typeFD+">正向</option>";
			table += "<option id='TypeBD' value='BD' "+typeBD+">反向</option>";
			table += "</select>";
			table += "</td>";	
			table += "<td>状态</td>";
			table += "<td>";
			table += "<select id='IsRun' class='wt100'>";	
			table += "<option id='IsRunRUN' value='RUN' "+runRun+">启用</option>";
			table += "<option id='IsRunSTOP' value='STOP' "+runStop+">停用</option>";
			table += "</select>";
			table += "</td></tr>";
			table += "<tr>";
			table += "<td colspan='4'>";
			table += "预期回调JS：";
			table += "</td >";
			table += "</tr>";
			table += "<tr>";
			table += "<td colspan='4'>";
			table += "<textarea id='CallBackExpect' style='margin: 0px; width: 850px; height: 300px;' ></textarea>";
			table += "</td>";
			table += "</tr>";
			table += "</table>";
			return table;
	}
	
	
	return MockCallback;
	
});