define(function(require){
	var $ = require('jquery');
	require('uriTemplate')($);
	require('dialog')($);
	require('datepicker')($);
	require('validation')($);
	require('grid')($);
	var IFA = require('util');
	require('../../../styles/cim/jquery-ui-1.8.14.custom.css');
	require('../../../styles/cim/cim-grid.css');
	require('globalization');
	$(function(){
		searchResult();
		$("#reset").bind("click",function(){$("#rateForm :input").val("");	});
		$("#searchBtn").bind("click",searchResult);
		$("#searchForm :input").bind("keydown",function(e){
			var e = e  ? e : window.event;
			if(e.keyCode==13){
				e.preventDefault();
				searchResult();
			}
		});
		$("#newRate").bind("click",newRate);
		$("#reset").bind("click",function(){
			$("#rateName").val("");
		});
	});
	
	function newRate(){
		var newRate =$("#newUrl").val();
		window.location.href=newRate;
	}
	
	
	
	//根据条件查询
	function searchResult(){
		var url=$("#queryUrl").val().trim();
		$("#searchResult").grid({
			url:url,
			root:"rateResult",
			pageTheme:'Theme1',
			isREST :false,
			method:"post",
			defaultParams:IFA.convertQueryStringToJson($("#searchForm").serialize()),
			fields:[
			        
			        {name:i18n["Rate.rateName"],id:"rateName",style:{width:"13%"},render:function(data){
			        	  return $("<a href='#' />").html(data.rateName).attr("title",data.rateName).click(function(){
  					    	  showDetail(data.rateId);
  						  }); 
			        }},
			        {name:i18n["Rate.price"],id:"ratePrice",style:{width:"13%"},render:function(data){
			        	return Globalize.format(data.ratePrice,'n2');
			        }},
			        {name:i18n["Rate.discount"],id:"discount",style:{width:"13%"}},
			        {name:i18n["Rate.required"],id:"required",style:{width:"13%"}},
			       // {name:i18n["Rate.applicable.products"],id:"applicable",style:{width:"13%"}},
			        {name:i18n['Entity.Operation'],style:{width:"12%"},operation:[
			        			                                              
											  {text:i18n['Entity.page.edit'],display:function(){
											
													  return true;
												
											  },action:function(data){editRate(data.rateId);}},
											  {text:i18n['Entity.page.Delete'],display:function(){
												
													  return true;
												
											  },action:function(data){deleteRate(data.rateId);}}]}
				    
			        ] 
			});
		
	}
	
	function showDetail(rateId){
		var url=$("#detail").val().trim();
		var temp = new $.Template(url);
		var para = {"rateId":rateId};
			url  = temp.expand(para);
			//alert(url);
			location.href=url;
	}
	
/**
 * 编辑
 */
function editRate(rateId){
	var url = $("#editUrl").val();
	var temple = new $.Template(url);
	var para = {"rateId":rateId};
	url = temple.expand(para);
	location.href=url;
	
	
}


function deleteRate(rateId){
	$.ajax({
		type:"post",
		url:$("#deleteCheck").val().trim(),
		async:false,
		data:{"rateId":rateId},
		dataType:"json",
		success:function(data){
			var check=data.check;
			if(check=='no'){
			$.msg.alert(i18ns("Rate.extraCostUsing"));
				return false;
			}
			if(check=='yes'){
				$.msg.confirm(i18n['Page.title.deleteSure'],function(result){
					if(result){
						$.ajax({
							type:"post",
							url:$("#deleteUrl").val().trim(),
							async:false,
							data:{"rateId":rateId,'_method':'DELETE'},
							dataType:"json",
							timeout:3000,
							success:function(response,ioArgs){
								$.msg.alert(i18n['Entity.page.Delete.Success']);
								searchResult();
							}
						});
					}
				});
			}
		}
	});
	

}

});