define(function(require) {
	var $ = require('jquery');
	require('uriTemplate')($);
	require('dialog')($);
	require('datepicker')($);
	require('dateTimePicker')($);
	require('validation')($);
	require('grid')($);
	var IFA = require('util');
	require('../../../styles/cim/jquery-ui-1.8.14.custom.css');
	require('../../../styles/cim/cim-grid.css');
	require('../../../styles/cim/validation.css');
	require('../../../styles/cim/autoComplete.css');
	require('../../../js/seaJsExtends/autoComplete.js')($);
	$(function() {

		initHotkey($("#apt_product_code"), $("#apt_CDP"));// 初始化热键
		initHotkey($("#cdsd_productCode"), $("#cdsd_CDP"));// 初始化热键
		initHotkey($("#intercity_productCode"), $("#intercity_CDP"));// 初始化热键
		keepType();
		$("#save").bind("click", saveInfo);
		$("#cancel").bind("click", cancel);
	});
	function initHotkey(productCode, CDP) {
		
		$.ajax({
			type:"post",
			url:$("#HotkeyPCcode").val().trim(),
			data:{hotKey:""},
			success:function(response){
			var pc= response.pc;
			productCode.autocomplete(pc,
				      {
						caption: "&nbsp;&nbsp;"+i18n["JS.pcHotKey.caption"],
						findCaption: "&nbsp;,"+i18n["JS.districtHotKey.findCaption"],
						noFindCaption: "&nbsp;&nbsp;"+i18n["JS.pcHotKey.noFindcaption"]+":&nbsp; ",
						max: 5,
						selectedFirst:true,
						width: 200,
						formatItem: function(row, i, max) {
				             var title=row.pcRemarks;
				             var content=row.pcRemarks;
				             if(row.pcRemarks.length>10){
				            	 content= row.pcRemarks.substring(0,15)+"...";
				             }
							return '<span title="{2}"><span class="ac_code" >{0}</span><span class="ac_Name">{1}</span></span>'.patchContent(row.pcId,content,title);
						},
						formatMatch: function(row, i, max) {
							return "{0} {1}".patchContent(row.pcId, row.pcRemarks);
						},
						formatResult: function(row) {
							return "{0}".patchContent(row.pcId);
						}
					}).result(function(event, data, formatted) {
						
					});
			}
		});

		// CDP#
		$
				.ajax({
					type : "get",
					url : $("#CDPurl").val(),
					success : function(response) {
						CDP
								.autocomplete(
										response.company,
										{
											caption : "&nbsp;&nbsp;"
													+ i18n["JS.districtHotKey.caption"],
											findCaption : "&nbsp;,"
													+ i18n["JS.districtHotKey.findCaption"],
											noFindCaption : "&nbsp;&nbsp;"
													+ i18n["JS.companyHotKey.noFindCaption"]
													+ ":&nbsp; ",
											max : 5,
											width : 200,
											formatItem : function(row, i, max) {
												var cpdNo = "cdpNo:"
														+ row.cdpNo + "&#13;";
												var companyName = i18n["JS.companyHotKey.coName"]
														+ ":"
														+ row.companyName
														+ "&#13;";
												var companyHotkey = i18n["JS.companyHotKey"]
														+ ":"
														+ row.companyHotkey
														+ "&#13;";
												var companyAddress = i18n["JS.companyHotKey.coAddr"]
														+ ":"
														+ row.companyAddress
														+ "&#13;";
												var companyContact1st = i18n["JS.companyHotKey.coContact"]
														+ ":"
														+ row.companyContact1st
														+ "&#13;";
												var title = cpdNo + companyName
														+ companyHotkey
														+ companyAddress
														+ companyContact1st;
												var companyName = row.companyName
														+ "("
														+ row.companyHotkey
														+ ")";
												return '<span title="{2}"><span class="ac_code" >{0}</span><span class="ac_Name">{1}</span></span>'
														.patchContent(
																row.cdpNo,
																companyName,
																title);
											},
											formatMatch : function(row, i, max) {
												return "{0} {1} {2}"
														.patchContent(
																row.cdpNo,
																row.companyHotkey,
																row.companyName);
											},
											formatResult : function(row) {
												return "{0}"
														.patchContent(row.cdpNo);
											}
										}).result(
										function(event, data, formatted) {
											// $("#CDP").val(data.companyId);
											// $("#CDP").val(data.companyName);
										});
					}
				});
	}
	function keepType() {

		var status = $("#productType").val();
		// cd-sd
		// Chauffeur Drive
		if (status == 1) {
			$("#freshAptFrom").hide();
			$("#freshIntercityFrom").hide();
			$("#freshCdsdFrom").show();
			$("#cdsd_category").val("Chauffeur Drive");
			if($("#cdsd_productCode").val()!="-1"){
				$("#cdsd_iscost").attr("disabled",true);
			}
			$("#cdsd_productCode").attr("disabled",true);
			$("#cdsd_location").attr("disabled",true);
			$("#cdsd_CDP").attr("disabled",true);
			$("#cdsd_vehicleCategory").attr("disabled",true);
		} else if (status == 2) {
			$("#freshAptFrom").hide();
			$("#freshIntercityFrom").hide();
			$("#freshCdsdFrom").show();
			$("#cdsd_category").val("self-drive");
			if($("#cdsd_productCode").val()!="-1"){
				$("#cdsd_iscost").attr("disabled",true);
			}
			$("#cdsd_productCode").attr("disabled",true);
			$("#cdsd_location").attr("disabled",true);
			$("#cdsd_CDP").attr("disabled",true);
			$("#cdsd_vehicleCategory").attr("disabled",true);
		}
		// intercity
		else if (status == 3) {
			$("#freshAptFrom").hide();
			$("#freshCdsdFrom").hide();
			$("#freshIntercityFrom").show();
			if($("#intercity_productCode").val()!="-1"){
				$("#intercity_iscost").attr("disabled",true);
			}
			$("#intercity_productCode").attr("disabled",true);
			$("#intercity_pick_up_location").attr("disabled",true);
			$("#intercity_drop_off_location").attr("disabled",true);
			if($("#intercity_trip_single").is(":checked")){
				$("#intercity_trip_round").attr("disabled",true);
			}else{
			$("#intercity_trip_single").attr("disabled",true);
			}
			$("#intercity_CDP").attr("disabled",true);
			$("#intercity_vehicleCategory").attr("disabled",true);
		}
		// apt
		else {//check($("#apt_hideiscost"));
			$("#freshCdsdFrom").hide();
			$("#freshIntercityFrom").hide();
			$("#freshAptFrom").show();
			$("#apt_product_code").attr("disabled",true);
			if($("#apt_product_code").val()!="-1"){
				$("#apt_iscost").attr("disabled",true);
			}
			$("#apt_CDP").attr("disabled",true);
			$("#apt_location").attr("disabled",true);
			$("#vehicleCategory").attr("disabled",true);
			$("#apt_airport").attr("disabled",true);
		}
	}
	function saveInfo() {

		// var status=$("#productType").find("option:selected").val();
		var status = $("#productType").val();
		if (status == 1 || status == 2) {
			$("#cdsd_productType").val(status);
			$("#freshCdsdFrom").submit();
		} else if (status == 3) {
			$("#intercity_productType").val(status);
			$("#freshIntercityFrom").submit();

		} else {
			$("#apt_productType").val(status);
			$("#freshAptFrom").submit();
		}
	}
	
	function checkSameData(obj){
		var length = 1;
		$.ajax({
			type:"post",
			url:$("#CheckPriceUrl").val(),
			timeout:5000,
			async:false,
			data:obj.serialize(),
			success:function(response){
			 if(response.count > 0){
				 $.msg.alert(i18n["JS.workOrder.system.tips"],i18n["JS.workOrder.nologIn"]);
			 }
			 length = response.count;
			}
		});
		
		return length;
	}
	
	function cancel() {
		var url = $("#cancelUrl").val();
		location.href = url;
	}

	$("#freshAptFrom")
			.validate(
					{
						rules : {
							pickupLocation : {
								required : true
							},
							price : {
								required : true,
								number : true,
								min : 0
							},
							airport : {
								required : true
							},
							kilometerLimit : {
								required : true,
								number : true,
								maxlength : 8,
								min : 0
							},
							serviceHoursLimit : {
								required : true,
								number : true,
								maxlength : 8,
								min : 0
							},
							extraKmCharge : {
								required : true,
								number : true,
								maxlength : 8,
								min : 0
							},
							extraHourCharge : {
								required : true,
								number : true,
								maxlength : 8,
								min : 0
							}
						},
						messages : {
							pickupLocation : {
								required : i18n["RateSheet.ProductPrce.Validate.location"]
							},
							price : {
								required : i18n["RateSheet.ProductPrce.Validate.privce"]
							},
							airport : {
								required : i18n["RateSheet.productPrice.Validate.airport"]
							},
							kilometerLimit : {
								required : i18n["RateSheet.productPrice.Validate.maxKmPerDay"]
							},
							serviceHoursLimit : {
								required : i18n["RateSheet.productPrice.Validate.maxHoursPerDay"]
							},
							extraKmCharge : {
								required : i18n["RateSheet.ProductPrce.Validate.outNumberkilometerFee"]
							},
							extraHourCharge : {
								required : i18n["RateSheet.ProductPrce.Validate.overTimeFee"]
							}
						}
					});
	$("#freshCdsdFrom")
			.validate(
					{
						rules : {
							pickupLocation : {
								required : true
							},
							price : {
								required : true,
								number : true,
								min : 0
							},
							monthlyRate : {
								required:false,
								number:true
							},
							kilometerLimit : {
								required : true,
								number : true,
								maxlength : 8,
								min : 0
							},
							serviceHoursLimit : {
								required : true,
								number : true,
								maxlength : 8,
								min : 0
							},
							extraKmCharge : {
								required : true,
								number : true,
								maxlength : 8,
								min : 0
							},
							extraHourCharge : {
								required : true,
								number : true,
								maxlength : 8,
								min : 0
							}
						},
						messages : {
							pickupLocation : {
								required : i18n["RateSheet.ProductPrce.Validate.location"]
							},
							price : {
								required : i18n["RateSheet.ProductPrce.Validate.privce"]
							},
							kilometerLimit : {
								required : i18n["RateSheet.productPrice.Validate.maxKmPerDay"]
							},
							serviceHoursLimit : {
								required : i18n["RateSheet.productPrice.Validate.maxHoursPerDay"]
							},
							extraKmCharge : {
								required : i18n["RateSheet.ProductPrce.Validate.outNumberkilometerFee"]
							},
							extraHourCharge : {
								required : i18n["RateSheet.ProductPrce.Validate.overTimeFee"]
							}
						}
					});
	$("#freshIntercityFrom")
			.validate(
					{
						rules : {
							pickupLocation : {
								required : true
							},
							dropoffLocation : {
								required : true
							},
							price : {
								required : true,
								number : true,
								min : 0
							},
							kilometerLimit : {
								required : true,
								number : true,
								maxlength : 8,
								min : 0
							},
							serviceHoursLimit : {
								required : true,
								number : true,
								maxlength : 8,
								min : 0
							},
							extraKmCharge : {
								required : true,
								number : true,
								maxlength : 8,
								min : 0
							},
							extraHourCharge : {
								required : true,
								number : true,
								maxlength : 8,
								min : 0
							}
						},
						messages : {
							pickupLocation : {
								required : i18n["RateSheet.productPrice.Validate.pickupLocation"]
							},
							dropoffLocation : {
								required : i18n["RateSheet.productPrice.Validate.dropoffLocation"]
							},
							price : {
								required : i18n["RateSheet.ProductPrce.Validate.privce"]
							},
							kilometerLimit : {
								required : i18n["RateSheet.productPrice.Validate.maxKmPerDay"]
							},
							serviceHoursLimit : {
								required : i18n["RateSheet.productPrice.Validate.maxHoursPerDay"]
							},
							extraKmCharge : {
								required : i18n["RateSheet.ProductPrce.Validate.outNumberkilometerFee"]
							},
							extraHourCharge : {
								required : i18n["RateSheet.ProductPrce.Validate.overTimeFee"]
							}
						}
					});
});