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(){newChauffeur();});
	  	   //绑定click事件,点击按钮(链接)'查询',触发此事件
	  	   $("#query").click(function(){query();});
	  	   $("#delete").click(function(){batchDelete();});
	  });
	  // 查询
	  function query(){
		    var queryUrl = $("#queryUrl").val();
		  	$("#districtList").grid({
		  		url:queryUrl,
		  		root:"districtList",  // 从ModleMap传递过来的数据
		  		method:"POST",  // 默认为get方法
		  		isREST:false,   // 默认为true,当method是POST时,设为false
		  		pageTheme:'Theme1',
		  		defaultParams:IFA.convertQueryStringToJson($("#districtForm").serialize()),
		  		fields:[
		  				{id:"districtId",name:"districtId",style:'width:10%',type:"checkbox"},
		  				{id:"districtName",name:i18n["Entity.district.name"],style:'width:15%',render:function(data){
		  					     return $("<a href='#' />").html(data.districtName).attr("title",data.districtName).click(function(){
		  					    	  showDetail(data.districtId);
		  						  }); // return结束
		  					}}, // render对应的函数结束
		  				{id:"districtLevel",name:i18n["Entity.district.districtLevel"],style:'width:10%',render:function(data){
		  					if(data.districtLevel==0){
		  						return i18n["Entity.district.districtLevel.area"];
		  					}
		  					else if(data.districtLevel==1){
		  						return i18n["Entity.district.districtLevel.city"];
		  					}
		  					else{
		  						return i18n["Entity.district.districtLevel.district"];
		  					}
		  				}},
		  				{id:"districtHotkey",name:i18n["Entity.district.hotkey"],style:'width:10%'},
		  				{id:"districtAddress",name:i18n["Entity.district.address"],style:'width:15%'},
		  				{id:"contactPhone",name:i18n["Entity.district.phone"],style:'width:15%'},
		  				{id:"modifierName",name:i18n["Entity.modifier"],style:'width:10%'},
		  				{id:"modificationDate",name:i18n["Entity.modificationDate"],style:'width:15%',render:function(data){
		  				    // 此正则表达式的验证格式:yyyy-MM-dd hh:mm '\d'表示0-9之间的数字
		  				  	var timeCheck = /^\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}$/;
		  				  	if(!timeCheck.exec(data.modificationDate)){
		  				  		 data.modificationDate = new Date(data.modificationDate).format("yyyy-MM-dd hh:mm");
		  				  	}
		  				  	return data.modificationDate;
		  				}},
		  				{name:i18n["Entity.Operation"],style:'width:10%',operation:[
		  				      {text:i18n["Entity.Operation.detail"],display:true,action:function(data){showDetail(data.districtId);}},                                 
		  				      {text:i18n["Entity.page.edit"],display:true,action:function(data){edit(data.districtId);}},
		  				      {text:i18n["Entity.Operation.delete"],display:true,action:function(data){deleteForTipInfo(data);}}
		  				    ]}			
		  				]// Fields结束
		  	});
	  }
	  // 新增
	  function newChauffeur(){
		var newUrl = $("#newUrl").val();
	  	window.location = newUrl;
	  }
	  // 显示详细信息
	  function showDetail(districtId){
		  var showUrl = new $.Template($("#showUrl").val());
		  var param = {"districtId":districtId};
	      window.location= showUrl.expand(param);
	  }
	  // 编辑
	  function edit(districtId){
	      var editUrl = new $.Template($("#editUrl").val());
		  var param = {"districtId":districtId};
	      window.location= editUrl.expand(param);
	  }
	  // 删除
	  function deleteInfo(districtIds){
		  $.msg.alert(i18n["Page.title.dialogshow.title"], i18n["Entity.Sure.tip"],{
			  buttons:IFA.generateJson(i18n["Entity.page.YES"], function() {
		    		   var deleteUrl = $("#deleteUrl").val();
		    		   var param={'districtId':districtIds,'_method':'DELETE'};
			    	   $.ajax({
			    		   url:deleteUrl,
			    		   type:"POST",
			    		   data:param,
			    		   dataType:'json',
			    		   success:function(response,ioArgs){
			    		     $(this).dialog("close");
			    		     if(response.flag){
			    		    	 $.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()结束
			    		      }else{
			    		    	  var ids = response.districtIds;
			    		    	  var info = i18n["Entity.district.delete.failed.tip"];
			    		    	  $.msg.alert(i18n["Page.title.dialogshow.title"],info,{
			    		    		  buttons: IFA.generateJson(i18n["Entity.Operation.ok"], function() {
									        $(this).dialog("close");
									   }), //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 deleteForTipInfo(data){
		  var districtIds = data.districtId;
		  var div = $('#InfoTemplate');
		  $('#districtIdInfo').text(data.districtId);
		  $('#districtNameInfo').text(data.districtName);
		  $('#tipMessage').text(i18n['Page.title.deleteSure']);
		  $.msg.alert(i18n["Page.title.dialogshow.title"], div,{
			  buttons:IFA.generateJson(i18n["Entity.page.YES"], function() {
		    		   var deleteUrl = $("#deleteUrl").val();
		    		   var param={'districtId':districtIds,'_method':'DELETE'};
			    	   $.ajax({
			    		   url:deleteUrl,
			    		   type:"POST",
			    		   data:param,
			    		   dataType:'json',
			    		   success:function(response,ioArgs){
			    		     $(this).dialog("close");
			    		     if(response.flag){
			    		    	 $.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()结束
			    		      }else{
			    		    	  var ids = response.districtIds;
			    		    	  var info = i18n["Entity.district.delete.failed.tip"];
			    		    	  $.msg.alert(i18n["Page.title.dialogshow.title"],info,{
			    		    		  buttons: IFA.generateJson(i18n["Entity.Operation.ok"], function() {
									        $(this).dialog("close");
									   }), //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{
	  		deleteInfo(districtIds);
	  	}
	  }
	  // 获取用户选择的Id(字符串形式),(通过选择checkbox)
	  function getCheckedIds(context){
	  	var ids = "";
	  	$("#districtList").find(":checkbox[name='districtId'][checked]").each(function(i,data){
	  		ids = ids + data.value.toString()+",";
	  	});
	  	return ids;
	  }
});

