<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="eFrameWork.AppWebAPI.Test" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script src="../Scripts/jquery-1.12.1.js"></script>
</head>
<script>
 var token = "anonymous";
var eServer = "http://192.168.1.9/eframework/AppWebAPI/";

function gettoken(usr,pwd)
{
	$.ajax({
		type: "POST",
        url: eServer + "getToKen.aspx",
		data: { username: usr, password: pwd },
        dataType: "json",
        success: function (data, textStatus, request)
		{
			//alert(JSON.stringify(data));
			if (data.errcode == 0)
			{
				token = data.token;
				$("#txttoken").val(token);
			}
			else
			{
				alert(data.message);
			}
        },
		error: function (XMLHttpRequest, textStatus, errorThrown)
		{
			alert("error:" + XMLHttpRequest.responseText + "::" + XMLHttpRequest.status + "::" + XMLHttpRequest.readyState + "::" + XMLHttpRequest.statusText);
        }
    });
};
var eModel=function(appitem,modelid)
{
	this.appitem=appitem;	
	this.modelid=modelid;	
	this.list=function()
	{
		var result = {};
		$.ajax({
			type: "GET",
			cache: false,
			url: eServer + "Model.aspx?appitem=" + appitem + (modelid.length > 0 ? "&modelid=" + modelid : "") + "&page=1",//+ "&debug=1",
			headers: {"auth": token},
			dataType: "json",
			async:false,
			success: function (data,textStatus,request)
			{
				result = data;
			},
			error: function (XMLHttpRequest, textStatus, errorThrown)
			{
				result = {"errcode":"0","message": XMLHttpRequest.status　+ ":" + errorThrown};
			}
		});
		return result;
	};
	this.insert=function(data)
	{
		var result={};
		data["act"]="apisave";
		data["id"]="";
		$.ajax({
			type: "POST",
			url: eServer + "Model.aspx?appitem=" + appitem + (modelid.length > 0 ? "&modelid=" + modelid : ""),// + "&debug=1",
			headers: {"auth": token},
			contentType: "application/json",
			data:JSON.stringify(data),
			dataType: "json",
			async:false,
			success: function (data,textStatus,request)
			{
				//alert(JSON.stringify(data));
				result=data;
			},
			error: function (XMLHttpRequest, textStatus, errorThrown)
			{
				result = {"errcode":"0","message": XMLHttpRequest.status　+ ":" + errorThrown};
				//alert("error:" + XMLHttpRequest.responseText + "::" + XMLHttpRequest.status + "::" + XMLHttpRequest.readyState + "::" + XMLHttpRequest.statusText);
			}
		});
		return result;
	};
	this.update=function(id,data)
	{
		var result={};
		data["act"]="apisave";
		data["id"]=id;
		$.ajax({
			type: "POST",
			url: eServer + "Model.aspx?appitem=" + appitem + (modelid.length > 0 ? "&modelid=" + modelid : "") ,//+ "&debug=1",
			headers: {"auth": token},
			contentType: "application/json",
			data:JSON.stringify(data),
			dataType: "json",
			async:false,
			success: function (data,textStatus,request)
			{
				result = data;
			},
			error: function (XMLHttpRequest, textStatus, errorThrown)
			{
				result = {"errcode":"0","message": XMLHttpRequest.status　+ ":" + errorThrown};
			}
		});
		return result;
	};
	this.read=function(id)
	{
		var result = {};
		$.ajax({
			type: "GET",
			cache: false,
			url: eServer + "Model.aspx?appitem=" + appitem + (modelid.length > 0 ? "&modelid=" + modelid : "") + "&act=apiedit&id=" + id,//+ "&debug=1",
			headers: {"auth": token},
			dataType: "json",
			async:false,
			success: function (data,textStatus,request)
			{
				//alert(JSON.stringify(data));
				result = data;
			},
			error: function (XMLHttpRequest, textStatus, errorThrown)
			{
				result = {"errcode":"0","message": XMLHttpRequest.status　+ ":" + errorThrown};
			}
		});
		return result;
	};
	this.view=function(id)
	{
		var result = {};
		$.ajax({
			type: "GET",
			cache: false,
			url: eServer + "Model.aspx?appitem=" + appitem + (modelid.length > 0 ? "&modelid=" + modelid : "") + "&act=apiview&id=" + id,//+ "&debug=1",
			headers: {"auth": token},
			dataType: "json",
			async:false,
			success: function (data,textStatus,request)
			{
				//alert(JSON.stringify(data));
				result = data;
			},
			error: function (XMLHttpRequest, textStatus, errorThrown)
			{
				result = {"errcode":"0","message": XMLHttpRequest.status　+ ":" + errorThrown};
			}
		});
		return result;
	};
	this.delete=function(id)
	{
		var result={};
		var data={"act":"apidel","id":id};
		$.ajax({
			type: "POST",
			url: eServer + "Model.aspx?appitem=" + appitem + (modelid.length > 0 ? "&modelid=" + modelid : "") ,//+ "&debug=1",
			headers: {"auth": token},
			data:data,
			dataType: "json",
			async:false,
			success: function (data,textStatus,request)
			{
				result = data;				
			},
			error: function (XMLHttpRequest, textStatus, errorThrown)
			{
				result = {"errcode":"0","message": XMLHttpRequest.status　+ ":" + errorThrown};
			}
		});
		return result;
	};	
	return this;
};

var model=new eModel("cc89067d-3ec8-4f28-9fdd-507f0dc3df58","");
function model_list()
{
	var data=model.list();	
	$("#txttoken").val(JSON.stringify(data));
	//alert(JSON.stringify(data));	
};
function model_insert()
{
	var data={"M11_F2":"公司名称","M11_F17":"1","eformdata_593a5e31-4d7d-45df-ac7f-39c4689b3d04":{"ID":"","Delete":"false","M12_F16":"","M25_F1":"","M12_F3":"xm","M12_F4":"1","M12_F4_Text":"男","M12_F15":"1","M12_F15_Text":"小学","M25_F4":"1","M25_F4_Text":"类型一"}};	
	
	data={"comName":"公司名称"};	
	data={"comName":"公司名称","eformdata_e1e951c9-a076-45b8-b551-24a0ddbe6291":[{"XM":"东东","XB":"2"}]};	
	data={"comName":"公司名称","Persons":[{"XM":"东东","XB":"2"},{"XM":"西西","XB":"1"}]};	
	

	
	var result=model.insert(data);
	$("#txttoken").val(JSON.stringify(result));;
};

function model_update()
{
	var data={"comName":"公司名称1"};	
	data={"comName":"公司名称2","eformdata_e1e951c9-a076-45b8-b551-24a0ddbe6291":[{"ID":"16","XM":"东2东","XB":"2"}]};	
	data={"comName":"公司名称3","Persons":[{"ID":"16","XM":"东东4","XB":"2"},{"ID":"17","XM":"西3西","XB":"1"},{"ID":"18","Delete":"true","XM":"BDD","XB":"1"}]};	
		data={"comName":"公司名称2","eformdata_e1e951c9-a076-45b8-b551-24a0ddbe6291":[{"ID":"31","XM":"东2东","XB":"2"}]};	

	var result=model.update("2", data);
	$("#txttoken").val(JSON.stringify(result));;
};
function model_delete()
{
    var result=model.delete("2");
	$("#txttoken").val(JSON.stringify(result));;
};
function model_read()
{
    var data=model.read("2");
	$("#txttoken").val(JSON.stringify(data));
};
function model_view()
{
     var data = model.view("2");
	 $("#txttoken").val(JSON.stringify(data));
};

</script>
<body>
<textarea id="txttoken" style="width:800px;height:130px;padding:6px;"></textarea><br>
 <button onClick="gettoken('admin','123456');">获取令牌</button><br>
<button onClick="model_list();">Model列表</button><br>

<button onClick="model_insert();">Model添加</button>
<button onClick="model_update();">Model修改</button>
<button onClick="model_delete();">Model删除</button>
<button onClick="model_read();">Model读取编辑数据</button>
<button onClick="model_view();">Model查看数据</button>
</body>
</html>