define(function(require){
	var $ = require('jquery');
	require('grid')($);
	require('util');
	require('uriTemplate')($);
	require('dialog')($);
	require("../../../styles/cim/jquery-ui-1.8.14.custom.css");
	require('../../../styles/cim/cim-grid.css');
	$(function(){
     	searchResult();
	});

	function searchResult(){
//      0       1          2       3       4         5            6      7         8  9    10          11
		var status=['QUOTE','REQUEST','REQUEST','SOLD','OPEN','VERIFICATION','CLOSED','CANCELLED','','','DENIED','TRANSFERRED'];	
		var url=$("#searchOrderBySegment").val();
		$("#searchResult").grid({
			url:url,
			root:"order",
			isREST :false,
			pageTheme:'Theme1',
			method:"POST",
			defaultParams:{'segmentNo':$("#segmentNo").val()},
			fields:[{name:i18n["Page.workOrder.bookingNumber"],id:"rentalNo",style:{width:"12%"},render:function(data){
				       return $("<a href='#' id='resId"+data.id+"' />").html(data.rentalNo).click(function(){
				    	   showDetail(data.id);
				       });
				       }},
				       {name:i18n["Entity.reservation.status"],id:"resStatus",style:{width:"12%"},render:function(data){
					    	if(data.resStatus==-1){
					    		return '';
					    	}else{
					    		return status[data.resStatus];
					    	}}},
				       {name:i18n["Page.workOrder.serviceName"],id:"productTypeName",style:{width:"12%"}},
				       {name:i18n["Page.workOrder.vehicleCategory"],id:"carcategoryName",style:{width:"12%"}},
				       {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"],id:"pickupDate",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"],id:"returnDate",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;
				        }}
			        ],
			        callback:function(data){
						addScrollBar();
						if(data.order){
							if($("#segmentNo").val().indexOf(".")==-1){			
								$("#resId"+data.order[0].id).parent().parent().attr("style","background-color:yellow"); 
							}else{
								var list = data.order;	
								var l = list.length;
								for(var i=0;i<l;i++){	
									if($("#segmentNo").val()==data.order[i].rentalNo){
										$("#resId"+data.order[i].id).parent().parent().attr("style","background-color:yellow"); 
										break;
									}
								}
							}
						}
			        }
			});
	}

	function addScrollBar(){
		   $($("#searchResult").find("table")[0]).css("width","1300px").wrap("<div style='overflow-x:scroll;overflow-y:auto;' ></div>");
	   }
	
	//显示详情
	function showDetail(id){
		var urlTemplate=$("#resHisUrl").val().trim();
		var urlString=new $.Template(urlTemplate);
		var urlParam = {"resId":id};
		var url=urlString.expand(urlParam);
		window.open(url,'newWin');
	}	
});

