define(function(require){
	var $ = require('jquery');
	require('grid')($);
	var IFA=require('util');
	require('uriTemplate')($);
	require('dialog')($);
	require('globalization');
	require('dateTimePicker')($);
	require("../../../styles/cim/jquery-ui-1.8.14.custom.css");
	require('../../../styles/cim/cim-grid.css');
	require('../../../styles/cim/autoComplete.css');
	require('../../../js/seaJsExtends/autoComplete.js')($);
	$(function(){
     	searchResult();
		$("#search").bind("click",searchResult);
		$("#reset").bind("click",reset);
		$("#dispatcherSearchForm :input:not(#serviceHokKey,#districtHokKey)").bind("keydown",function(e){
			var e = e ? e : window.event;
			if(e.keyCode==13){
			   searchResult();
			}
		});
		hotKeyDistrict();
		hotKeyProduct();
		//初始化日期
		initializehDate();
	});
	function initializehDate(){
		$("#pickupDateStart").datetimepicker({
			showSecond:false,
			stepMinute:15,
		    onSelect:function(date){
			var date=date.split("-").join("/");
			$("#pickupDateEnd").datetimepicker("option","minDate",new Date(date));
			}});
		$("#pickupDateEnd").datetimepicker({
			stepMinute:15,
			showSecond:false,
		    onSelect:function(date){
			var date=date.split("-").join("/");
			$("#pickupDateStart").datetimepicker("option","maxDate",new Date(date));
			}});
		
		$("#returnDateStart").datetimepicker({
			stepMinute:15,
			showSecond:false,
		    onSelect:function(date){
			var date=date.split("-").join("/");
			$("#returnDateEnd").datetimepicker("option","minDate",new Date(date));
			}});
		
		$("#returnDateEnd").datetimepicker({
			stepMinute:15,
			showSecond:false,
		    onSelect:function(date){
			var date=date.split("-").join("/");
			$("#returnDateStart").datetimepicker("option","maxDate",new Date(date));
			}});
	}
	
	//重置查询条件
	function reset(){
		$("#dispatcherSearchForm :input").val("");
		$("#pickupDateStart").datetimepicker("destroy");
		$("#returnDateStart").datetimepicker("destroy");
		$("#pickupDateEnd").datetimepicker("destroy");
		$("#returnDateEnd").datetimepicker("destroy");
		initializehDate();
	}
	
	//根据条件查询
	function searchResult(){
		var url=$("#queryUrl").val().trim();
		$("#searchResult").grid({
			url:url,
			root:"reservationList",
			isREST :false,
			pageTheme:'Theme1',
			method:"POST",
			defaultParams:$("#dispatcherSearchForm").serialize(),
			fields:[
			        {name:i18n["Page.workOrder.rentalNo"],id:"rentalNo",style:{width:"14%"},render:function(data){
				       return $("<a href='#'/>").html(data.orderNum).click(function(){
				    	   showDetail(data.id);
				       });
				       }},
				    {name:i18n["JS.workOrder.contractAmount"],id:"totalCharges",style:{width:"10%"},render:function(data){
				    	return  Globalize.format(data.totalCharges,'n');
				    }},
				    {name:i18n["JS.workOrder.cash"],id:"charge",style:{width:"10%"},render:function(data){
				    	return  Globalize.format(data.charge,'n');
				    }},
			        {name:i18n["Page.workOrder.customName"],id:"customerName",style:{width:"7%"}},
			       // {name:i18n["JS.workOrder.paymentType"],id:"paymentType",style:{width:"7%"},options:{1:i18n["Page.workOrder.bankTransfer"],2:i18n["Page.workOrder.alipay"],3:i18n["Page.workOrder.creditCard"],4:i18n["Page.workOrder.cash"]}},
			        {name:i18n["Page.workOrder.serviceName"],style:{width:"8%"},render:function(data){
				        var name=data.productName;
		                if(name.length>10){
		             	    name=name.substring(0,10)+"...";
		                }
		 			    return $('<span title="'+data.productName+'"/>').html(name);
				        }},
			        {name:i18n["Page.workOrder.ownedStores"],style:{width:"8%"},render:function(data){
				        var name=data.districtName;
		                if(name.length>10){
		             	    name=name.substring(0,10)+"...";
		                }
		 			    return $('<span title="'+data.districtName+'"/>').html(name);
			        }},
			        {name:i18n["Page.workOrder.vehicleCategory"], id:"vehicleName",style:{width:"9%"}},
			        {name:i18n["Page.workOrder.pickupLocation"], id:"pickupLocation",style:{width:"10%"}},
			        {name:i18n["Page.workOrder.returnLocation"], id:"returnLocation",style:{width:"10%"}},
//			        {name:i18n["JS.workOrder.pickupTime"],style:{width:"12%"},render:function(data){
//			        	var pickDate=data.pickupDate;
//			        	if(pickDate!=null){
//			        		pickDate=new Date(pickDate).format("yyyy-MM-dd hh:mm");
//			        	}
//			        	return pickDate;
//			        }},
//			        {name:i18n["JS.workOrder.pickOffTime"],style:{width:"12%"},render:function(data){
//			        	var returnDate=data.returnDate;
//			        	if(returnDate!=null){
//			        		returnDate=new Date(returnDate).format("yyyy-MM-dd hh:mm");
//			        	}
//			        	return returnDate;
//			        }},
			        {name:i18n["Entity.Operation"],operation:[{text:i18n["JS.workOrder.detail"],display:true,action:function(data){showDetail(data.id);}}]}
			        ]
			});
	}

	//显示详情
	function showDetail(id){
		var urlTemplate=$("#detailUrl").val().trim();
		var urlString=new $.Template(urlTemplate);
		var journalId=$("#journalId").val();
		var urlParam = {"id":id,"journalId":journalId};
		var url=urlString.expand(urlParam);
		location.href=url;
	}
	//产品代码热键
	function hotKeyProduct(){
	  	$.ajax({
			type:"get",
			url:$("#hotkeyProduct").val(),
			success:function(response){
			var acdata=response.servicingProduct;
			$("#serviceHokKey").autocomplete(acdata,
				      {
						caption: "&nbsp;&nbsp;"+i18n["JS.productHotKey.caption"],
						findCaption: "&nbsp;,"+i18n["JS.districtHotKey.findCaption"],
						noFindCaption: "&nbsp;&nbsp;"+i18n["JS.productHotKey.noFindCaption"]+":&nbsp; ",
						width: 200,
						max: 5,
						formatItem: function(row, i, max) {
				             var title="";
				             if(row.isRestrictive == 'Y'){
				            	 var maxService=Math.floor(row.fullUsageHrs/row.avgTransactionActual);
				            	 title=i18n["JS.productHotKey.title0"]+".&#13;"+i18n["JS.productHotKey.title1"]+":"+maxService;
				             }else{
				            	 title=i18n["JS.productHotKey.title2"];
				             }
							return '<span title="{2}"><span class="ac_code" >{0}</span><span class="ac_Name" >{1}</span></span>'.patchContent(row.serviceId, row.serviceDescription,title);
						},
						formatMatch: function(row, i, max) {
							return "{0} {1}".patchContent(row.serviceId, row.serviceDescription);
						},
						formatResult: function(row) {
							return "{0}".patchContent(row.serviceDescription);
						}
					}).result(function(event, data, formatted) {
						$("#serviceId").val(data.serviceId);
					});
		}
		});
	}
	//地区热键
	function hotKeyDistrict(){
	  	$.ajax({
			type:"get",
			url:$("#hotkeyDistrict").val(),
			success:function(response){
			var acdata=response.district;
			$("#districtHokKey").autocomplete(acdata,
				      {
						caption: "&nbsp;&nbsp;"+i18n["JS.districtHotKey.caption"],
						findCaption: "&nbsp;,"+i18n["JS.districtHotKey.findCaption"],
						noFindCaption: "&nbsp;&nbsp;"+i18n["JS.districtHotKey.noFindCaption"]+":&nbsp; ",
						width: 200,
						max: 5,
						formatItem: function(row, i, max) {
							return '<span class="ac_code">{0}</span><span class="ac_Name">{1}</span>'.patchContent(row.districtHotkey, row.districtName);
						},
						formatMatch: function(row, i, max) {
							return "{0} {1}".patchContent(row.districtHotkey, row.districtName);
						},
						formatResult: function(row) {
							return "{0}".patchContent(row.districtName);
						}
					}).result(function(event, data, formatted) {
						$("#districtId").val(data.districtId);
					});
		}
		});
	}
});

