define(function(require){
	var $ = require('jquery');
	require('grid')($);
    require('uriTemplate')($);
    require('dialog')($);
    require('datepicker')($);
	var IFA = require('util');
	require("../../../styles/cim/jquery-ui-1.8.14.custom.css");
	require('../../../styles/cim/cim-grid.css');   
	$(function(){
		search();
		$("#search").bind("click",search);
		$("#reset").bind("click",reset);
		$("#new").bind("click",function(){
			location.href=$("#newUrl").val().trim();
		});
		$("#delete").click(function(){batchDelete();});
		$("#searchForm :input").bind("keydown",function(e){
			var e = e ? e : window.event;
			if(e.keyCode==13){
			   search();
			}
		});
	});
	//根据条件查询
	function search(){
		var url=$("#queryUrl").val().trim();
		$("#channelList").grid({
			url:url,
			root:"bookingChannelList",
			pageTheme:'Theme1',
			isREST :false,
			method:"POST",
			defaultParams:{name:$("#name").val()},
			fields:[{id:"id",type:"checkbox",style:{width:"5%"}},
			        {name:i18n["Entity.bookingChannel.name"],style:{width:"20%"},id:"name",render:function(data){
					    var channelName='';
		                var dataName=data.name;
		                if(dataName.length>20){
		             	    channelName=dataName.substring(0,20)+"...";
		                }else{
		                    channelName=dataName;
		                    }
		 			    return $('<a href="#" title='+data.name+' class="bb"/>').html(channelName).click(function(){
		  				    showDetail(data);
		     			    });
			        }},
			        {name:i18n["Entity.bookingChannel.rmarks"],style:{width:"25%"},id:"remarks",render:function(data){
					    var channelRemarks='';
		                var dataRemarks=data.remarks;
		                if(dataRemarks.length>20){
		                	channelRemarks=dataRemarks.substring(0,20)+"...";
		                }else{
		                	channelRemarks=dataRemarks;
		                    }
		 			    return channelRemarks
			        }},
			        {name:i18n["Entity.creationdate"],style:{width:"15%"},render:function(data){
	  				  	var timeCheck = /^\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}$/;
	  				  	if(!timeCheck.exec(data.creationDate)){
	  				  		 data.creationDate = new Date(data.creationDate).format("yyyy-MM-dd hh:mm");
	  				  	}
	  				  	return data.creationDate;
				        }},
			        {name:i18n["Entity.creator"],style:{width:"20%"},id:"creationName",render:function(data){
			        	var createName="";
			        	if(data.creatorName!=null){			        		
			        		var dataName=data.creatorName;
			        		if(dataName.lenth>5){
			        			createName=dataName.substring(0,5)+"...";
			        		}else{
			        			createName=dataName;
			        		}
			        	}
			        	return createName;
			        }},
			        {name:i18n["Entity.Operation"],style:{width:"15%"},operation:[
			                                                  {text:i18n["Entity.Operation.edit"],display:true,action:function(data){
			                                    					var id = data.id;
			                                    					var hiddenUrl = $('#editUrl').val();
			                                    					var urlStr = new $.Template(hiddenUrl);
			                                    					var urlParam = {"id":id};
			                                    					var url = urlStr.expand(urlParam);	
			                                    				    location.replace(url);	
			                                                  }},
			                                                  {text:i18n["Entity.Operation.delete"],display:true,action:function(data){
			                                                		var obj = this;
			                                                         //判断是否能删除   
			                                    					var id = data.id;
			                                    					var hiddenUrl = $('#deleteUrl').val();
			                                    					var urlParam = {"id":id,'_method':'DELETE'};
			                                    					$.ajax({
			                                    						type:"POST",
			                                    						url: hiddenUrl,
			                                    					    data:urlParam,
			                                    						dataType:"json",
			                                    						timeout:5000,
			                                    						success:function(response){
			                                                             if(response.flag)
			                                                             {
			                                                                $.msg.alert(i18n["Page.title.dialogshow.title"],i18n["page.tip.deleteSuccessfully"],function(){obj.refresh();});}
			                                    					    },
			                                    						error:function(response){
			                                    							return response;}
			                                    				    });	
			                                                  }}
			                                                  ]}
			        ]
			});
	}
	
	//重置查询条件
	function reset(){
		$("#name").val("");
	}

	  // 批量删除
	  function batchDelete(){
	  	var ids = getCheckedVals();

	  	// 用户未选择删除项
	  	if(ids.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.warnning"],i18n["Validation.bookingChannel.deletechoSure"],{
	  			buttons:IFA.generateJson(i18n["Entity.page.YES"],function(){
	             	//判断是否能删除   
   					var hiddenUrl = $('#deleteUrl').val();
   					//var urlStr = new $.Template(hiddenUrl);
   					var urlParam = {"id":ids,'_method':'DELETE'};
   					//var url = urlStr.expand(urlParam);
   					$.ajax({
   						type:"POST",
   						url: hiddenUrl,
   					    data:urlParam,
   						dataType:"json",
   						timeout:5000,
   						success:function(response){
                            if(response.flag)
                            {
                               $.msg.alert(i18n["Page.title.dialogshow.title"],i18n["page.tip.deleteSuccessfully"],function(){search();});}
   					    },
   						error:function(response){
   							return response;}
   				    });	
   					$(this).dialog("close");
			       },	
			       i18n["Entity.page.No"],function() {
		         	$(this).dialog("close");
			       }		       
	  		)});
	  	}
	  }
	
	function getCheckedVals(){
		var checkVal = "";
		$(":checkbox[name='id'][checked]").each(		
				function(i) {
					checkVal += $(this).val();
					checkVal +=",";
				}
			);
		return checkVal.substring(0,checkVal.length-1);
	}
	function showDetail(data){
			    var id = data.id;
			    var hiddenUrl = $('#detailUrl').val();
				var urlStr = new $.Template(hiddenUrl);
				var urlParam = {"id":id};
				var url = urlStr.expand(urlParam);	
			    location.replace(url);	
	}
});