define(function(require){
	var $ = require('jquery');
	var IFA = require('util');
	require('uriTemplate')($);
	require('validation')($);
	require('dialog')($);
	require('jqueryCSS');
	require('dateTimePicker')($);
	$(function(){
		window.name = "vehicleConditionDetail";
		bindEvent();
		$("#addForm").validate({
			rules:{
			lockDate:{
			required:true	
			},
			unlockDate:{
			required:true	
			}
			},
			messages:{
			lockDate:{
			required:i18ns("VehicleConditionMap.Validate.lockTime")	
			},
			unlockDate:{
			required:i18ns("VehicleConditionMap.Validate.unlockTime")	
			}	
			}
			});
	});
	function bindEvent(){
		$("a[name='j_unlock']").click(function(){
			var value = $(this).attr("data");
			unlocked(value,this);
			return false;
		});
		$('#a_dispatch').bind('click',todispatch);
		$("#add").bind("click",addNoneBillable);
		$("#back").bind("click",function(){
			window.open(document.referrer,"vehicleConditionDetail");
			
		});
		$("#lockDate").datetimepicker({
			stepMinute:15,
			showSecond:false,
			onSelect:function(){
			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);
			}
			}
			$("#addForm").validate().element("#lockDate");
		}
		});
		$("#unlockDate").datetimepicker({
			stepMinute:15,
			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);
			}
			}
			$("#addForm").validate().element("#unlockDate");
			}
		});
		
		$("a[id^=edit]").bind("click",function(){
			var urlStr = new $.Template($("#queryNoneBillableUrl").val());
			var param = {"id":$(this).attr("noneId")};
			var url = urlStr.expand(param);
			$.get(url,function(data){
				$("#noneBillableId").val(data.noneBillableRes.id);
				$("#codeId").find("option[value="+data.noneBillableRes.codeId+"]").attr("selected","selected");
				$("#lockDate").val(new Date(data.noneBillableRes.lockDate).format("yyyy-MM-dd hh:mm:ss"));
				$("#unlockDate").val(new Date(data.noneBillableRes.unlockDate).format("yyyy-MM-dd hh:mm:ss"));
				$("#remark").val(data.noneBillableRes.remark);
				$.msg.alert(i18ns("VehicleConditionMap.updateNonbusiness"),$("#addTemplate"),function(){
				},{width:550,height:300,buttons:
					IFA.generateJson(i18ns("ConditionMap.ok"),function(){
					if($("#addForm").valid()){
					$(this).dialog("close");
					updateNoneBillable();
					}
				},
				i18ns("ConditionMap.cancel"),function(){
					$(this).dialog("close");
				}
				)});
			});
		});
		$("a[id^=delete]").bind("click",function(){
			var id =$(this).attr("noneId");
			$.msg.alert(i18ns("Page.Dialog.title"),i18ns("Page.title.needDelete"),function(){
			},{buttons:
				IFA.generateJson(i18ns("ConditionMap.ok"),function(){
				$(this).dialog("close");
				var urlStr = new $.Template($("#deleteNoneBillableUrl").val());
				var params = {"id":id};
				var url = urlStr.expand(params);
				var data = {"_method":"DELETE"};
				$.post(url,data,function(data){
					location.reload();
				});
			},
			i18ns("ConditionMap.cancel"),function(){
				$(this).dialog("close");
			}
			)});
		});
	}
	function addNoneBillable(){
		$("#noneBillableId").val("");
		$("#lockDate").val("");
		$("#unlockDate").val("");
		$("#remark").val("");
		$.msg.alert(i18ns("VehicleConditionMap.addNonbusiness"),$("#addTemplate"),function(){
		},{width:550,height:300,buttons:
			IFA.generateJson(i18ns("ConditionMap.ok"),function(){
			if($("#addForm").valid()){
			$(this).dialog("close");
			$("#add").unbind();
			createNoneBillable();
			}
		},
		i18ns("ConditionMap.cancel"),function(){
			$(this).dialog("close");
		}
		)});
}

function createNoneBillable(){
	var url = $("#addNoneBillableUrl").val();
	$.post(url,$("#addForm").serialize(),function(data){
		if(data.result=="-1"){
			$.msg.alert(i18n['Page.Dialog.title'],i18ns("VehicleConditionMap.exist"));
			$("#add").bind("click",addNoneBillable);
			 return;
		}
		location.reload();
	});
}

function updateNoneBillable(){
	var url = $("#updateNoneBillableUrl").val();
	$.post(url,$("#addForm").serialize()+"&_method=PUT",function(data){
		if(data.result=="-1"){
			$.msg.alert(i18n['Page.Dialog.title'],i18ns("VehicleConditionMap.exist"));
			 return;
		}
		location.reload();
	});
}


    function todispatch(){
		if($("#dispatchTips").val()){			
			$.msg.confirm(i18ns("Page.Dialog.title"),i18ns("VehicleConditionMap.goOnLock"),function(result){
				if(result){
					dispatch();
				}
			});
		}else{
			dispatch();
		}
    }
	function dispatch(){
		var url = $("#dispatchForm").attr('action');
		$('#a_dispatch').unbind();
		$.ajax({
			  type: 'POST',
			  url: url,
			  data: $("#dispatchForm").serialize(),
			  success:function(response,ioArgs){
				if(response.result==0){
				$.msg.confirm(i18n['Page.Dialog.title'],i18n['VehicleMap.Dispatch.Success'],function(){
				},{buttons:IFA.generateJson(
					i18n['VehicleMap.GotoWaitForDispatchingList'],function(){
					var urlStr = new $.Template($("#gotoWaitDispatchingUrl").val());
					var params = {'reservationId':$("#reservationId").val()};
					var url = urlStr.expand(params);
					location.href = url;
					$(this).dialog("close");
				},
					i18n['VehicleMap.ContinueDispatchingDriver'],function(){
					 var urlStr = new $.Template($("#dispatchDriver").val());
					 var params = {'reservationId':$("#reservationId").val()};
					 var url = urlStr.expand(params);
					 location.href=url;
					 $(this).dialog("close");
				}
				)});
				}else{
					$.msg.alert(i18n['Page.Dialog.title'],i18ns("VehicleConditionMap.nonbusinessExist"));
					$('#a_dispatch').bind('click',dispatch);
				}
			  },
			  error:function(){
					$('#a_dispatch').bind('click',dispatch);
				  /*$.msg.alert('error',function(){},{width:'400px',buttons:{
						'confirm':function(){$(this).dialog("close");},
						'xxx':function(){$(this).dialog("close");}
					}});
					*/
			  }
		});
	}
	function unlocked(reservationId,obj){
		$.msg.alert(i18n['ConditionMap.reDispatch.confirm'],function(){},{buttons:
			IFA.generateJson(i18n['ConditionMap.ok'],function(){
			var urlTemplate=$("#unlockedUrl").val();
			var urlString=new $.Template(urlTemplate);
			var urlParam = {"reservationId":reservationId};
			var url=urlString.expand(urlParam);
			$.ajax({
				type:"GET",
				url:url,
				timeout:3000,
				success:function(response,ioArgs){
					$(obj).parent().parent().hide();
					$.msg.alert(i18n['ConditionMap.reDispatch.success']);
				},
				error:function(){
					$.msg.alert('error');
				}
			});
			$(this).dialog('close');
		},
		i18n['ConditionMap.cancel'],function(){
			$(this).dialog('close');
		}
		)});

	}
});