define(function(require) {
	var $ = require('jquery');
	require('dialog')($);
	require('grid')($);
	require('uriTemplate')($);
	require('dateTimePicker')($);
	var IFA = require('util');
	require("../../../styles/cim/jquery-ui-1.8.14.custom.css");
	require('../../../styles/cim/cim-grid.css');
	$(function() {
		loadGrid();
		initDate();
		$("#edit").click(function() {
			var urlStrEdit = new $.Template($("#editUrl").val());
     		var urlParamEdit = {"vehicleId":$("#vehicleId").val()};
     		var editUrl = urlStrEdit.expand(urlParamEdit);
     		window.showModalDialog(editUrl,window,'dialogWidth=1050px;dialogHeight=700px');
     		window.location.reload();
		});
		$("#nonRev").bind("click",addNoneBillable);
		$("#weeklyCycle").bind('click',weeklyCycleFunc);
	});
	function loadGrid() {
		var queryUrl = $("#queryUrl").val();
		$("#vehicleList")
				.grid(
						{
							url : queryUrl,
							root : "carDispatchHistories", // 从ModleMap传递过来的数据
							method : "POST", // 默认为get方法
							isREST : false, // 默认为true,当method是POST时,设为false
							pageTheme : 'Theme1',
							defaultParams : {'vehicleId':$("#vehicleId").val()},
							fields : [									
									{
										id : "dateTime",
										name : i18n["Entity.vehicle.dateTime"],
										render : function(data) {
											return new Date(data.dateTime)
													.format("yyyy-MM-dd hh:mm");
										}
									},		
									{
										id : "action",
										name : i18n["Entity.vehicle.action"],
										render:function(data){
											if(data.actionMiddle){							
											    return $("<p>"+data.actionStart+"<a href='#'>"+data.actionMiddle+"</a>"+data.actionEnd+"</p>").click(
											    		function(){
											    			var urlTemplate=$("#detailUrl").val().trim();
															var urlString=new $.Template(urlTemplate);
															var urlParam = {"id":data.extra};
															var url=urlString.expand(urlParam);
															window.showModalDialog(url,window,'dialogWidth=1050px;dialogHeight=700px');
											    		});
											}else{
											    return data.actionStart;
										}}},
									{
										id : "modifier",
										name : i18n["Entity.modifier"]
									} ]
						});
	}
	 function createNoneBillable(){
		   
			var url = $("#addNoneBillableUrl").val();
			
			$.post(url,$("#addForm").serialize(),function(data){
				if($("#addNoneBillable").val()=="true"){
				$.msg.alert(i18ns('VehicleMap.Dispatch.Success'),function(){},{buttons:
					IFA.generateJson(i18ns('ConditionMap.ok'),function(){
						$("#weeklyCycle").attr('checked',false);		
						$("#divRight input[type='checkbox']").attr('checked','checked');
						$("#divRight").hide();
							$(this).dialog("close");
							loadGrid();
						})
					});
				
			}else{ $.msg.alert(i18ns("NoneBillableAauthority"));}});
		}  
	 function addNoneBillable(){
		   
			$("#noneBillableId").val("");
			$("#lockDate").val("");
			$("#unlockDate").val("");
			$("#remark").val("");
			
			$.msg.alert(i18ns("VehicleConditionMap.addNonbusiness"),$("#addTemplate"),function(){
			},{width:580,height:330,buttons:
				IFA.generateJson(i18ns("ConditionMap.ok"),function(){
				var	url = $("#checkNoneBillableUrl").val();
				if($("#addForm").valid()&& atLeastOne()==true){				
					$.ajax({
						 url:url,
						 type:"POST",
						 dataType:"json",
						 data:$("#addForm").serialize(),
						 timeout:5000,
						 success:function(response,ioArgs){
							 
							 if(response.result == -1){
								 $.msg.alert(i18ns('Page.Dialog.title'),i18ns("VehicleConditionMap.nonbusinessExist"));
							 } else if(response.result == 0) {
								 var conflictRes = response.listRes;
								 var strResNos = "";
								 for (var i = 0; i < conflictRes.length; i++){	
										strResNos += "<font color='red'>" + conflictRes[i].segmentId + "</font><br/>";
								}
								 var warnMessage = i18ns('ConditionMap.addNoneBill.WarningMessage') + "<br/>" + strResNos; 
										
								 $.msg.alert(i18ns('Page.title.dialogshow.warnning'),warnMessage, {buttons:
										IFA.generateJson(i18ns("Entity.Operation.ok"),function(){
											createNoneBillable();
											$(this).dialog("close");
										},
										i18ns("ConditionMap.cancel"),function(){		
											$(this).dialog("close");
										})
								});
							 } else {
								 createNoneBillable();
							 }
						 }
					});
					$(this).dialog("close");
				}
				},
				i18ns("ConditionMap.cancel"),function(){
					$(this).dialog("close");
				}
			)});
	   }
	 function weeklyCycleFunc(){
		   if($("#weeklyCycle").attr('checked')=='checked'){
			   $("#weeklyCycle").val(1);
				$("#divRight").show();			
			}else{
				$("#divRight").hide();
				$("#weeklyCycle").val(0);
			} 
	   }
	 function initDate(){
		 $("#lockDate").datetimepicker({
				stepHour:1,
				showMinute:false,
				showSecond:false,
				beforeShow : function(date) {
				},
				onSelect : function(date) {
					var date = $(this).datetimepicker("getDate");
					var returnDate = $("#unlockDate")
							.datetimepicker("getDate");
					var returnDateShow;
					if (returnDate) {
						returnDateShow = $("#unlockDate").val();
					}
					$("#unlockDate").datetimepicker("option", "minDate",
							new Date(date.getTime()));
					$("#unlockDate").datetimepicker("option", "minDateTime",
							new Date(date.getTime()));
					if (returnDate) {
						if (date.getTime() > returnDate.getTime()) {
							$("#unlockDate").val($(this).val());
						} else {
							$("#unlockDate").val(returnDateShow);
						}
					}
				}
				});
			$("#unlockDate").datetimepicker({
				stepHour:1,
				showMinute:false,
				showSecond:false,
				onSelect : function(date) {
					var date = $(this).datetimepicker("getDate");
					var pickupDate = $("#lockDate").datetimepicker(
							"getDate");
					var pickupDateShow;
					if (pickupDate) {
						pickupDateShow = $("#lockDate").val();
					}
					$("#lockDate").datetimepicker("option", "maxDate",
							new Date(date.getTime()));
					$("#lockDate").datetimepicker("option", "maxDateTime",
							new Date(date.getTime()));
					if (pickupDate) {
						if (date.getTime() < pickupDate.getTime()) {
							$("#lockDate").val($(this).val());
						} else {
							$("#lockDate").val(pickupDateShow);
						}
					}
				}
				});
	 }
	 function atLeastOne(){
		   if($("#weeklyCycle").attr('checked')=='checked'){
			   if($("#sun").attr('checked')=='checked'){
				   return true;
			   }
			   if($("#mon").attr('checked')=='checked'){
				   return true;
			   }
			   if($("#tue").attr('checked')=='checked'){
				   return true;
			   }
			   if($("#wed").attr('checked')=='checked'){
				   return true;
			   }
			   if($("#thu").attr('checked')=='checked'){
				   return true;
			   }
			   if($("#fri").attr('checked')=='checked'){
				   return true;
			   }
			   if($("#sat").attr('checked')=='checked'){
				   return true;
			   }
			   $.msg.alert(i18ns('Page.Dialog.title'),i18ns("VehicleGanttMap.selectDay"));
			   return false;
		   }else{
			   return true;
		   }
	   }
});