define(function(require){
	var $ = require('jquery');
	var IFA = require('util');
	require('uriTemplate')($);
	require('validation')($);
	require('dialog')($);
	require('jqueryCSS');
	require('dateTimePicker')($);
	require("../../../../styles/cim/jquery-ui-1.8.14.custom.css");
	require('../../../../styles/cim/validation.css');
	$(function(){
		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(){
		bindDispatch();
		$("a[name='j_unlock']").bind("click",function(){
			var data = $(this).attr("data");
			redispatchChauffeur(data);
		});
		$("#add").bind("click",addNoneBillable);
		$("#back").bind("click",function(){
			location.href=document.referrer;
			
		});
		$("#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");
			}
		});
		
		$("tr[id^=item]").bind("click",function(){
			var status =  $(this).attr("itemStatus");
			var id = $(this).attr("itemId");
			var urlStr = "";
			if(status=='2'){
				urlStr = new $.Template($("#gotoOtherStatusDispatchingDetailNoMenuUrl").val());
			}else if(status=='3'){
				urlStr = new $.Template($("#gotoWaitSendConfirmationDetailNoMenuUrl").val());
			}else if(status=='4'){
				urlStr =new $.Template($("#gotoWaitTravelRouteDetailNoMenuUrl").val());
			}else if(status=="5"){ //status=='5'
				urlStr = new $.Template($("#gotoWaitAuditDetailNoMenuUrl").val());
			} 
			if(status!=""){
			var param = {"id":id};
			var url = urlStr.expand(param);
			window.showModalDialog(url,window,'dialogWidth=1050px;dialogHeight=700px');
			location.reload(false);
			}else{
				$.msg.alert("此单子已经关闭!");
			}
		});
		$("tr[id^=item]").bind("mouseover",function(){
			$(this).attr("style","cursor: pointer;background-color:#DFE8FF");
		});
		$("tr[id^=item]").bind("mouseout",function(){
			$(this).attr("style","cursor: pointer;");
		});
		$("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 bindDispatch(){
		$("a[id^=a_dispatch]").bind("click",function(){
			var peopleIdStr = $(this).attr("id");
			var chauffeurId = peopleIdStr.substring(11,peopleIdStr.length);
			var reservationId = $("#reservationId").val();
			dispatchChauffeur(reservationId,chauffeurId);
		});
	}
	
	
	function addNoneBillable(){
		$.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 dispatchChauffeur(reservationId,chauffeurId){
		var url = $("#dispatchChauffeurUrl").val();
		var data = {'reservationId':reservationId,'chauffeurId':chauffeurId};
		$("a[id^=a_dispatch]").unbind();
		$.post(url,data,function(res){
			if(res.result==0){
			var url = $("#gotoWaitDispatchingUrl").val();
			location.href = url;
			}else{
				$.msg.alert(i18n['Page.Dialog.title'],i18ns("VehicleConditionMap.nonbusinessExist"));
				bindDispatch();
			}
		});
	}
	/**
	 * 重新调配司机
	 */
	function redispatchChauffeur(reservationId){
		$.msg.alert(i18n['ConditionMap.reDispatch.confirm'],function(){},{buttons:
			IFA.generateJson(i18n['ConditionMap.ok'],function(){
			var url = $("#redispatchChauffeurUrl").val();
			var data = {'reservationId':reservationId};
			$.post(url,data,function(res){
				$("a[name='j_unlock']").each(function(){
					var data2 = $(this).attr("data");
					if(reservationId == data2){
						$(this).parent().parent().hide();
						
					}
				});
				$.msg.alert(i18n['ConditionMap.reDispatch.success']);
			});
			$(this).dialog("close");
		},
		i18n['ConditionMap.cancel'],function(){
			$(this).dialog("close");
		}
		)});
	}
	
});