define(function(require){
	var $ = require('jquery');
	require('validation')($);
	require('grid')($);
	require('dialog')($);
	require('uriTemplate')($);
	//require('demoAssets/../../style/jquery-ui-1.8.14.custom.css');
	require("../../../styles/cim/jquery-ui-1.8.14.custom.css");
	require('../../../styles/cim/cim-grid.css');  
	var IFA = require('util'); // 
	
	 $(function(){
		   query(); // 加载页面时,做一次查询
//		   $("#parentCode").one("click",function(){getDistrictForArea();});
		   $("#parentCode").change(function(){getDistrictForId();});
		   getDistrictForArea();
		   getDistrictForId();
	  	   // 绑定click事件,点击按钮(链接)'新建',触发此事件
	  	   $("#new").click(function(){newChauffeur();});
	  	   //绑定click事件,点击按钮(链接)'查询',触发此事件
	  	   $("#query").click(function(){query();});
	  	   $("#delete").click(function(){batchDelete();});
	  });
	  // 查询
	  function query(){
		    var queryUrl = $("#queryUrl").val();
		  	$("#peopleProfileList").grid({
		  		url:queryUrl,
		  		root:"peopleProfileList",  // 从ModleMap传递过来的数据
		  		method:"POST",  // 默认为get方法
		  		isREST:false,   // 默认为true,当method是POST时,设为false
		  		pageTheme:'Theme1',
		  		defaultParams:IFA.convertQueryStringToJson($("#chauffeurForm").serialize()),
		  		fields:[
		  				{id:"peopleId",name:"peopleId",style:'width:5%',type:"checkbox"},
		  				{id:"cnName",name:i18n["Entity.peopleProfile.cnName"],style:'width:10%',render:function(data){
		  					     return $("<a href='#' />").html(data.cnName).attr("title",data.cnName).click(function(){
		  					    	  showDetail(data.peopleId);
		  						  }); // return结束
		  					}}, // render对应的函数结束
		  				{id:"enName",name:i18n["Entity.peopleProfile.enName"],style:'width:10%'},
		  				{id:"jobNumber",name:i18n["Entity.peopleProfile.jobNumber"],style:'width:10%'},
		  				{id:"mobileTel",name:i18n["Entity.peopleProfile.mobileTel"],style:'width:10%'},
		  				{id:"rentalType",name:i18n["Entity.peopleProfile.rentalType"],
		  					options:{'L':i18n["Entity.peopleProfile.rentalType.long"],
		  					         'S':i18n["Entity.peopleProfile.rentalType.short"]},style:'width:5%'},
		  				{id:"districtName",name:i18n["Entity.peopleProfile.city"],style:'width:10%'},
		  				{id:"realDistrictName",name:i18n["Page.workOrder.ownedStores"],style:'width:10%'},
		  				{id:"email",name:i18n["Entity.peopleProfile.email"],style:'width:10%'},
		  				{id:"modifierName",name:i18n["Entity.modifier"],style:'width:10%'},
		  				{id:"modificationDate",name:i18n["Entity.modificationDate"],style:'width:10%',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.peopleId);}},                                 
		  				      {text:i18n["Entity.page.edit"],display:true,action:function(data){edit(data.peopleId);}},
		  				      {text:i18n["Entity.Operation.delete"],display:true,action:function(data){deleteInfo(data.peopleId);}},
                              {text:i18n["Entity.Operation.audits"], display:true, action:function(data) {
                           		var urlStrEdit = new $.Template($("#auditUrl").val());
                           		var urlParamEdit = {"id":data.peopleId};
                           		var auditUrl = urlStrEdit.expand(urlParamEdit);
                         		window.location.href=auditUrl;
                           }}
		  				    ]}			
		  				]
		  	});
	  }
	  
	   // 获取区域级别的门店
	  function getDistrictForArea(){
		   var params ={"districtLevel":0,"parentCode":0};
		   var getUrl = $("#getUrl").val()+".json";
		   $.ajax({
			   url:getUrl,
			   type:"POST",
			   data:params,
			   dataType:"json",
			   success:function(response,ioArgs){
			       //$obj.append("<option value='-1'>----请选择----</option>");
			       var list = response.districtList;
			       if(list!=null && list.length>0){
			    	   $("#parentCode").empty();
					   $("<option></option>").val(-1).text(i18n["Title.peopleProfile.choose"]).appendTo($("#parentCode"));
					   $.each(list,function(index,item){
						    // 在此处同时保存门店的id和名字,显示门店名称,但保存门店id
						    //$obj.append("<option value="+item.districtId+">"+item.districtName+"</option>");
						    $("<option></option>").val(item.districtId).text(item.districtName).appendTo($("#parentCode"));
					   });
			       }
		       }
		   });
		   getDistrictForId();
	   };
	   // 获取城市级别信息
	   function getDistrictForId(){
		   var parentCode = $("#parentCode").val(); // 获取父节点编号
		   if(parentCode>0){
			   var params ={"districtLevel":1,"parentCode":parentCode};
			   getDistrict($("#districtId"),params);
		   }else{
			   $("#districtId").empty();
			   $("<option></option>").val(-1).text(i18n["Title.peopleProfile.choose"]).appendTo($("#districtId"));
		   }
	   };
	   // 根据参数获取门店信息
	   function getDistrict($obj,params){
		   var getUrl = $("#getUrl").val()+".json";
		   $.ajax({
			   url:getUrl,
			   type:"POST",
			   data:params,
			   dataType:"json",
			   success:function(response,ioArgs){
			       //$obj.append("<option value='-1'>----请选择----</option>");
			       var list = response.districtList;
			       if(list!=null && list.length>0){
					   $obj.empty();
					   $.each(list,function(index,item){
						    // 在此处同时保存门店的id和名字,显示门店名称,但保存门店id
						    //$obj.append("<option value="+item.districtId+">"+item.districtName+"</option>");
						    $("<option></option>").val(item.districtId).text(item.districtName).appendTo($obj);
					   });
			       }
		       }
		   });
	   };
	   
	  // 新增
	  function newChauffeur(){
		var newUrl = $("#newUrl").val();
	  	window.location = newUrl;
	  }
	  // 显示详细信息
	  function showDetail(peopleId){
	      var param = {"peopleId":peopleId};
		  var showUrl = new $.Template($("#showUrl").val());
	      window.location= showUrl.expand(param);
	  }
	  // 编辑
	  function edit(peopleId){
		  var param = {"peopleId":peopleId};
		  var editUrl = new $.Template($("#editUrl").val());
	      window.location= editUrl.expand(param);
	  }
	  // 删除
	  function deleteInfo(peopleIds){
		  $.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={'peopleId':peopleIds,'_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();
									        }),
									   width:350,height:150
							 	 });// $.msg.alert()结束
			    		      }else{
			    		    	  $.msg.alert(i18n["Page.title.dialogshow.title"],i18n["Entity.delete.failed.tip"],{
			    		    		  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 peopleIds = getCheckedIds(this);
	  	// 用户未选择删除项
	  	if(peopleIds.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(peopleIds);
	  	}
	  }
	  // 获取用户选择的Id(数组形式),(通过选择checkbox)
	  function getCheckedIds(context){
	  	var ids = "";
	  	$("#peopleProfileList").find(":checkbox[name='peopleId'][checked]").each(function(i,data){
	  		ids = ids + data.value.toString()+",";
	  	});
	  	return ids;
	  }
});

