define(function(require){
	var $ = require('jquery');
	require('validation')($);
	require('grid')($);
	require('dialog')($);
	require('uriTemplate')($);
	require("../../../styles/cim/jquery-ui-1.8.14.custom.css");
	require('../../../styles/cim/cim-grid.css');   
	var IFA = require('util'); 
	
	 $(function(){
		   query(); // 加载页面时,做一次查询
	  	   // 绑定click事件,点击按钮(链接)'新建',触发此事件
	  	   $("#new").click(function(){newCity();});
	  	   //绑定click事件,点击按钮(链接)'查询',触发此事件
	  	   $("#query").click(function(){query();});
	  	   $("#delete").click(function(){batchDelete();});
	  });
	  // 查询
	  function query(){
		    var queryUrl = $("#getUrl").val();
		  	$("#districtList").grid({
		  		url:queryUrl,
		  		root:"cityList",  // 从ModleMap传递过来的数据
		  		method:"POST",  // 默认为get方法
		  		isREST:false,   // 默认为true,当method是POST时,设为false
		  		pageTheme:'Theme1',
		  		defaultParams:$("#districtForm").serialize(),
		  		fields:[
		  				{id:"id",name:"id",style:'width:15%',type:"checkbox"},
		  				{id:"name",name:i18n["Entity.district.districtLevel.city"],style:'width:25%'}, 
		  				{id:"rankLevel",name:i18n["Entity.city.level"],style:'width:25%',render:function(data){
		  						if(data.rankLevel==0){
		  							return i18n["Entity.city.province"];
		  						}
		  						else if(data.rankLevel==1){
		  							return i18n["Entity.city.district"];
		  						}
		  						else{
		  							return i18n["Entity.city.town"];
		  						}
		  				}},
		  				{id:"hotKey",name:i18n["Entity.district.hotkey"],style:'width:25%'},
		  				{name:i18n["Entity.Operation"],style:'width:35%',operation:[                               
		  				{text:i18n["Entity.page.edit"],display:true,action:function(data){edit(data.id);}},
		  				{text:i18n["Entity.Operation.delete"],display:true,action:function(data){deleteInfo(data.id);}}
		  				]}		
		  				]// Fields结束
		  	});
	  }
	  // 新增
	  function newCity(){
		var newUrl = $("#addUrl").val();
	  	window.location = newUrl;
	  }
	  
	  // 编辑
	  function edit(id){
			var url = $("#editUrl").val();
			var temple = new $.Template(url);
			var params = {"id":id};
			url = temple.expand(params);
			location.href=url;	
	  }
	  // 删除
	  function deleteInfo(id){
		  $.msg.alert(i18n["Page.title.dialogshow.title"], i18n["Entity.Sure.tip"],{
			  buttons:IFA.generateJson(i18n["Entity.page.YES"], function() {
		    		   var deleteUrl = $("#delUrl").val();
		    		   var param={'id':id};
			    	   $.ajax({
			    		   url:deleteUrl,
			    		   type:"POST",
			    		   data:param,
			    		   dataType:'json',
			    		   success:function(response,ioArgs){
			    		     $(this).dialog("close");
			    		    	 $.msg.alert(i18n["Page.title.dialogshow.title"], i18n["Entity.delete.succeed"],{
			    		    		  buttons: IFA.generateJson(i18n["Entity.Operation.ok"], function() {
									        $(this).dialog("close");
									        query();
									   }), //buttons结束
									   width:350,height:150
							 	 });// $.msg.alert()结束
			    		      
			    	        },
			    	        error:function(response,ioArgs){
			    	        	$(this).dialog("close");
								 $.msg.alert(i18n["Page.title.dialogshow.title"], i18n["Entity.delete.failed"],{
									   buttons: IFA.generateJson(i18n["Entity.Operation.ok"], function() {
									        $(this).dialog("close");
									        query();
									   }), //buttons结束
									   width:350,height:150
							 	 });// $.msg.alert()结束
			    	        }
			    	   });
		    	       $(this).dialog("close");
				   },i18n["Entity.page.No"],function() {$(this).dialog("close");}
	 		  ),
			width:350,height:150
	 		}
	 	);
	  }
	  
	
	  // 批量删除
	  function batchDelete(){
	  	var districtIds = getCheckedIds(this);
	  	// 用户未选择删除项
	  	if(districtIds.length==0){
	  		$.msg.alert(i18n["Page.title.dialogshow.title"], i18n["Page.warnning.tips"],{
		  			 buttons: IFA.generateJson(i18n["Entity.Operation.ok"], function() {
			               $(this).dialog("close");
			           }),
	  		        width:350,height:150
	  			});
	  	}else{
	  		$.msg.alert(i18n["Page.title.dialogshow.title"], i18n["Entity.Sure.tip"],{
				  buttons:IFA.generateJson(i18n["Entity.page.YES"], function() {
			    		   var deleteUrl = $("#delBatchUrl").val();
			    		   var param={'id':districtIds};
				    	   $.ajax({
				    		   url:deleteUrl,
				    		   type:"POST",
				    		   data:param,
				    		   dataType:'json',
				    		   success:function(response,ioArgs){
				    		     $(this).dialog("close");
				    		    	 $.msg.alert(i18n["Page.title.dialogshow.title"], i18n["Entity.delete.succeed"],{
				    		    		  buttons: IFA.generateJson(i18n["Entity.Operation.ok"], function() {
										        $(this).dialog("close");
										        query();
										   }), //buttons结束
										   width:350,height:150
								 	 });// $.msg.alert()结束
				    		      
				    	        },
				    	        error:function(response,ioArgs){
				    	        	$(this).dialog("close");
									 $.msg.alert(i18n["Page.title.dialogshow.title"], i18n["Entity.delete.failed"],{
										   buttons: IFA.generateJson(i18n["Entity.Operation.ok"], function() {
										        $(this).dialog("close");
										        query();
										   }), //buttons结束
										   width:350,height:150
								 	 });// $.msg.alert()结束
				    	        }
				    	   });
			    	       $(this).dialog("close");
					   },i18n["Entity.page.No"],function() {$(this).dialog("close");}
		 		  ),
				width:350,height:150
		 		}
		 	);
	  	}
	  }
	  // 获取用户选择的Id(字符串形式),(通过选择checkbox)
	  function getCheckedIds(context){
	  	var ids = "";
	  	$("#districtList").find(":checkbox[name='id'][checked]").each(function(i,data){
	  		ids = ids + data.value.toString()+",";
	  	});
	  	return ids;
	  }
});

