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 = {"peopleId":$("#chauffeurId").val()};
     		var editUrl = urlStrEdit.expand(urlParamEdit);
     		window.showModalDialog(editUrl,window,'dialogWidth=1050px;dialogHeight=700px');
     		window.location.reload();
		});
		$("#nonRev").bind("click",addNoneBillable);
	});
	function loadGrid() {
		var queryUrl = $("#queryUrl").val();
		$("#chauffeurList")
				.grid(
						{
							url : queryUrl,
							root : "carDispatchHistories", // 从ModleMap传递过来的数据
							method : "POST", // 默认为get方法
							isREST : false, // 默认为true,当method是POST时,设为false
							pageTheme : 'Theme1',
							defaultParams : {'chauffeurId':$("#chauffeurId").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 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 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() {
													var url = $(
															"#checkNoneBillableUrl")
															.val();
													if ($("#addForm").valid()) {
														$
																.ajax({
																	url : url,
																	type : "POST",
																	dataType : "json",
																	data : $(
																			"#addForm")
																			.serialize(),
																	timeout : 5000,
																	success : function(
																			response,
																			ioArgs) {

																		if (response.result == -1) {
																			$.msg
																					.alert(
																							i18n['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].rentalNo
																						+ "</font><br/>";
																			}
																			var warnMessage = i18n['ConditionMap.addNoneBill.WarningMessage']
																					+ "<br/>"
																					+ strResNos;

																			$.msg
																					.alert(
																							i18n['Page.title.dialogshow.warnning'],
																							warnMessage,
																							{
																								buttons : IFA
																										.generateJson(
																												i18n["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 createNoneBillable() {
			var url = $("#addNoneBillableUrl").val();
			$.post(url, $("#addForm").serialize(), function(data) {
				if (data.result == "-1") {
					$.msg.alert(i18n['Page.Dialog.title'],
							i18ns("VehicleConditionMap.exist"));
					return;
				} else {
					$.msg.alert(i18n['DriverGanttMap.Dispatch.Success'],
							function() {
							}, {
								buttons : IFA.generateJson(i18n['ConditionMap.ok'],
										function() {
											loadGrid();
											$(this).dialog("close");
										})
							});
				}

			});
		}
});