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');   
	var deleteAuthority = $("#deleteAuthority").val();
	var editAuthority = $("#editAuthority").val();
	var detailAuthority = $("#detailAuthority").val();
	require('globalization');
	$(function(){
		search();
		$("#searchBtn").bind("click",search);
		$("#newBtn").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 urlVal=$("#searchUrl").val().trim();
	    var urlStr = new $.Template(urlVal);
		var id = $('#serviceKey').val();
		var urlParam = {"id":id};
		var url = urlStr.expand(urlParam);
		$("#productsList").grid({
			url:url,
			root:"serviceingProductsList",
			pageTheme:'Theme1',
			isREST :false,
			//method:"POST",
			defaultParams:IFA.convertQueryStringToJson($("#searchForm").serialize()),
			fields:[{id:"serviceId",type:"checkbox",style:{width:"5%"}},
			        {name:i18n["Entity.product.serviceId"],style:{width:"10%"},render:function(data){
					    var serId='';
		                var dataId=data.serviceId;
		                if(dataId.length>20){
		                	serId=dataId.substring(0,20)+"...";
		                }else{
		                	serId=dataId;
		                    }
		 			    if(detailAuthority=="true"){
		 			    return $('<a href="#" title='+data.serviceId+' class="bb"/>').html(serId).click(function(){
		  				    showDetail(data);
		     			    });
		 			    }else{
		 			    return 	serId;
		 			    }
			        }},
			        {name:i18n["Entity.product.serviceDescription"],style:{width:"15%"},id:"serviceDescription"},
			        {name:i18n["Entity.product.avgtransactionactual"],style:{width:"10%"},id:"avgTransactionActual"},
			        {name:i18n["Entity.product.fullusagehrs"],style:{width:"10%"},id:"fullUsageHrs"},
			        {name:i18n["Entity.product.avgtransactionprojected"],style:{width:"10%"},id:"avgTransactionProjected",render:function(data){
			        		return Globalize.format(data.avgTransactionProjected,'n2');			        	
			        }},
			        {name:i18n["Entity.product.utilizationratesingle"],style:{width:"10%"},id:"utilizationRateSingle",render:function(data){
			        	var rate =Globalize.format(data.utilizationRateSingle,'n2')+"%";
			        	return rate;
			        }},
			        {name:i18n["Entity.product.creationdate"],style:{width:"10%"},id:"creationDate",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.product.creator"],style:{width:"10%"},id:"creatorName",render:function(data){
			        	var creatorName="";
			        	var dataName=data.creatorName;
			        	if(dataName!=null){			        		
			        		if(dataName.lenth>10){
			        			creatorName=dataName.substring(0,10)+"...";
			        		}else{
			        			creatorName=dataName;
			        		}
			        	}
			        	return creatorName;
			        }},
			        {name:i18n["Entity.Operation"],style:{width:"20%"},operation:[
			                                                  {text:i18n["Entity.Operation.edit"],display:function(){
			                                                	  if(editAuthority=="true"){
			                                                		  return true;
			                                                	  }else{
			                                                		  return false;
			                                                	  }
			                                                  },action:function(data){
			                                                	  var id = data.serviceId;
			                                      			      var urlVal = $('#editUrl').val();
			                                      			      var urlStr = new $.Template(urlVal);
			                                      				  var urlParam = {"id":id};
			                                      				  var url = urlStr.expand(urlParam);	
			                                      			      location.replace(url);	
			                                                  }},
			                                                  {text:i18n["Entity.Operation.delete"],confirm: i18n["Page.title.deleteSure"],display:function(){
			                                                	  if(deleteAuthority=="true"){
			                                                		  return true;
			                                                	  }else{
			                                                		  return false;
			                                                	  }
			                                                  },action:function(data){
			                                                		var obj = this;
			                                    					var id = data.serviceId;
			                                    					var urlVal = $('#deleteUrl').val();
			                                    					var urlParam = {"id":id,'_method':'DELETE'};
			                                    					$.ajax({
			                                    						type:"POST",
			                                    						url: urlVal,
			                                    					    data:urlParam,
			                                    						dataType:"json",
			                                    						timeout:5000,
			                                    						success:function(response){
			                                                             if(response.flag)
			                                                             {
			                                                                $.msg.alert(i18n["Page.title.dialogshow.title"],i18n["Page.title.operation.successfully"],function(){obj.refresh();});}
			                                    					    },
			                                    						error:function(response){
			                                    							return response;}
			                                    				    });	
			                                                  }}
			                                                  ]}
			        ]
			});
	}
	
	  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["Page.product.title.deletetips"],{
		  			buttons:IFA.generateJson(i18n["Entity.page.YES"],function(){
		             	var urlVal = $('#deleteUrl').val();
	   					var urlParam = {"id":ids,'_method':'DELETE'};
	   					$.ajax({
	   						type:"POST",
	   						url: urlVal,
	   					    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='serviceId'][checked]").each(		
					function(i) {
						checkVal += $(this).val();
						checkVal +=",";
					}
				);
			return checkVal.substring(0,checkVal.length-1);
		}
		function showDetail(data){
		    var id = data.serviceId;
		    var hiddenUrl = $('#detailUrl').val();
			var urlStr = new $.Template(hiddenUrl);
			var urlParam = {"id":id};
			var url = urlStr.expand(urlParam);	
		    location.replace(url);	
}
});