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');
	require('../../../js/seaJsExtends/autoComplete.js')($);
	require('../../../styles/cim/autoComplete.css');
	$(function(){
		hotKeyCompany();
		search();
		$("#searchBtn").bind("click",search);
		$("#searchForm :input").bind("keydown",function(e){
			var e = e ? e : window.event;
			if(e.keyCode==13){
			   search();
			}
		});
	});

	function search(){
		var billId = $('#billId').val();
		var url=$("#searchUrl").val().trim();
		$("#raList").grid({
			url:url,
			root:"reservationList",
			pageTheme:'Theme1',
			isREST :false,
			method:"POST",
			defaultParams:IFA.convertQueryStringToJson($("#searchForm").serialize()),
			fields:[{id:"id",type:"checkbox",style:{width:"2%"}},
			        {name:i18ns("Page.workOrder.rentalNo"),style:{width:"12%"},id:"orderNum"},
			        {name:i18ns("Page.workOrder.customName"),style:{width:"10%"},id:"personName",render:function(data){
			        	var cnName = "";
			        	var enfstName ="" ;
			        	var enlstName = "";
			        	var customerName = "";
			        	if(data.customerName!=null){
			        		cnName = data.customerName;
			        	}
			        	if(data.customerUS1stName!=null){
			        		enfstName = data.customerUS1stName;
			        	}
			        	if(data.customerUSLstName){
			        		enlstName = data.customerUSLstName;
			        	}
			        	customerName=enfstName+enlstName+cnName;
			        	return customerName;
			        }},
			        {name:i18n["Entity.billing.companyName"],style:{width:"10%"},id:"companyName",render:function(data){
					    var comName='';
		                var dataName=data.companyName;
		                if(dataName.length>10){
		                	comName=dataName.substring(0,10)+"...";
		                }else{
		                	comName=dataName;
		                    }
		 			    return comName;
			        }},
			        {name:"CDP#",style:{width:"10%"},id:"cdpNo"},
			        {name:i18n["Entity.billing.raActualCost"],style:{width:"10%"},id:"revenue",type:'currency'},
			        {name:i18ns("Entity.billing.partAmount"),style:{width:"10%"},id:"charge",type:'currency'},
			        {name:i18n["Entity.billing.raCreationDate"],style:{width:"5%"},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:i18ns("JS.workOrder.pickupTime"),style:{width:"10%"},id:"pickupDate",render:function(data){
			        	var timeCheck = /^\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}$/;
	  				  	if(!timeCheck.exec(data.pickupDate)){
	  				  		 data.pickupDate = new Date(data.pickupDate).format("yyyy-MM-dd hh:mm");
	  				  	}
	  				  	return data.pickupDate;
			        }},
			        {name:i18ns("JS.workOrder.pickOffTime"),style:{width:"10%"},id:"returnDate",render:function(data){
			        	var timeCheck = /^\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}$/;
	  				  	if(!timeCheck.exec(data.returnDate)){
	  				  		 data.returnDate = new Date(data.returnDate).format("yyyy-MM-dd hh:mm");
	  				  	}
	  				  	return data.returnDate;
			        }}
			        ],
	  		buttons:[{text:i18n["Entity.billing.addtional"],action:function(){
	  			        addRa(billId);
	  			     }},
  		             {text:i18n["Entity.billing.cancel"],action:function(){
  		            	var urlVal = $('#cancelUrl').val();
  				        var urlStr = new $.Template(urlVal);
  					    var urlParam = {"id":billId};
  					    var url = urlStr.expand(urlParam);	
  				        location.replace(url);
  		             }}] 
			});
	}
 
	function addRa(id){
		var ids = getCheckedVals();
	  	if(ids.length==0){
	  		$.msg.alert(i18n["Page.title.dialogshow.title"],i18n["Page.billing.dialogshow.chooseAddtionalRa"],{
	  			    buttons:
	  			    	IFA.generateJson(i18n["Entity.Operation.ok"],function(){
	                           $(this).dialog("close");
	                      }),
	  		        width:350,height:150
	  			});
	  	}else{
	  		$.msg.alert(i18n["Page.title.dialogshow.title"],i18n["Page.billing.dialogshow.continueAddtionalRa"],{
	  			buttons:IFA.generateJson(i18n["Entity.page.YES"],function(){
   					   $(this).dialog("close");
			       },
			       i18n["Entity.page.No"],function(){
			    	   var urlVal = $('#addedUrl').val();
				       var urlStr = new $.Template(urlVal);
					   var urlParam = {"id":id,"ids":ids};
					   var url = urlStr.expand(urlParam);	
				       location.replace(url);
			           $(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);
	}
	
    $("#startDate").datepicker({
	});
    $("#endDate").datepicker({
	});
    $("#startCloseDate").datepicker({
    	
    });
    $("#endCloseDate").datepicker({
    	
    });
	//公司热键
	function hotKeyCompany(){
	  	$.ajax({
			type:"get",
			url:$("#companyUrl").val(),
			success:function(response){
			$("#cdpNo").autocomplete(response.company,
				      {
						caption: "&nbsp;&nbsp;"+i18n["JS.districtHotKey.caption"],
						findCaption: "&nbsp;,"+i18n["JS.districtHotKey.findCaption"],
						noFindCaption: "&nbsp;&nbsp;"+i18n["JS.companyHotKey.noFindCaption"]+":&nbsp; ",
						max: 5,
						width: 200,
						formatItem: function(row, i, max) {
				             var cpdNo="cdpNo:"+row.cdpNo+"&#13;";
				             var companyName=i18n["JS.companyHotKey.coName"]+":"+row.companyName+"&#13;";
				             var companyHotkey=i18n["JS.companyHotKey"]+":"+row.companyHotkey+"&#13;";
				             var companyAddress=i18n["JS.companyHotKey.coAddr"]+":"+row.companyAddress+"&#13;";
				             var companyContact1st=i18n["JS.companyHotKey.coContact"]+":"+row.companyContact1st+"&#13;";
				             var title=cpdNo+companyName+companyHotkey+companyAddress+companyContact1st;
				             var companyName=row.companyName+"("+row.companyHotkey+")";
							return '<span title="{2}"><span class="ac_code" >{0}</span><span class="ac_Name">{1}</span></span>'.patchContent(row.cdpNo,companyName,title);
						},
						formatMatch: function(row, i, max) {
							return "{0} {1} {2}".patchContent(row.cdpNo,row.companyHotkey,row.companyName);
						},
						formatResult: function(row) {
							return "{0}".patchContent(row.cdpNo);
						}
					}).result(function(event, data, formatted) {
						//$("#companyId").val(data.companyId);
					});
		}
		});
	}
});