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"));//初始化热键
		
		
		$("#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) {
					});
		}
		});
	}

	$("#productType").change(function (){
		var status = $("#productType").val();
		//cd-sd
		//Chauffeur Drive
		 if(status == 1 ){
			 $("#freshAptFrom").hide();
			 $("#freshIntercityFrom").hide();
			 $("#freshCdsdFrom").show();
			 
			 $("#cdsd_category").val("Chauffeur Drive");
			 $("#cdsd_P25km2h").attr("disabled",false);
			 $("#cdsd_P50km4h").attr("disabled",false);
			 
		  }
		 else if(status == 2 ){
			 $("#freshAptFrom").hide();
			 $("#freshIntercityFrom").hide();
			 $("#freshCdsdFrom").show();
			 $("#cdsd_category").val("self-drive");
			 $("#cdsd_P25km2h").attr("disabled",true);
			 $("#cdsd_P50km4h").attr("disabled",true);
		  }
		  //intercity
		  else if (status == 3){ 
			  $("#freshAptFrom").hide();
			  $("#freshCdsdFrom").hide();
			  $("#intercity_category").val("Inter city");
			  $("#freshIntercityFrom").show();
		  }
		//Cross Border
		  else if (status == 4){ 
			  $("#freshAptFrom").hide();
			  $("#freshCdsdFrom").hide();
			  $("#intercity_category").val("Cross Border");
			  $("#freshIntercityFrom").show();
		  }
		 //apt
		  else{
			  $("#freshCdsdFrom").hide();
			  $("#freshIntercityFrom").hide();
			  $("#freshAptFrom").show();
		  }
	});
	function saveInfo(){
		var status = $("#productType").val();
		var obj;
		if(status==1 || status==2){
			obj = $("#freshCdsdFrom");
			$("#cdsd_productType").val(status);
			
			if(checkSameData(obj) == 0){
				obj.submit();
			}
		}
		else if(status==3 || status==4){
			obj = $("#freshIntercityFrom");
			$("#intercity_productType").val(status);
			 
			if(checkSameData(obj) == 0){
				obj.submit();
			}
		}
		else{
			obj = $("#freshAptFrom");
			$("#apt_productType").val(status);
			 
			var length = checkSameData(obj);
			if(length == 0){
				obj.submit();
			}
		}
	}
	$("#intercity_productCode").blur(function(){

		  if($("#intercity_productCode").val()!=""){
			  $("#intercity_iscost").attr("checked",false);
			  $("#intercity_iscost").attr("disabled",true);
		  }
		  else{
			  $("#intercity_iscost").attr("disabled",false);
		  }
		});
	$("#apt_product_code").blur(function(){

		  if($("#apt_product_code").val()!=""){
			  $("#apt_iscost").attr("checked",false);
			  $("#apt_iscost").attr("disabled",true);
		  }
		  else{
			  $("#apt_iscost").attr("disabled",false);
		  }
		});
	$("#cdsd_productCode").blur(function(){

		 if($("#cdsd_productCode").val()!=""){
			 $("#cdsd_iscost").attr("checked",false);
			  $("#cdsd_iscost").attr("disabled",true);
		  }
		  else{
			  $("#cdsd_iscost").attr("disabled",false);
		  }
		});
	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["Entity.Date.repeat"]);
			 }
			 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
			},
			p25km2h:{
				required:true,
				number:true,
				min:0
			},p50km4h:{
				required:true,
				number:true,
				min:0
			},p1to6days:{
				required:true,
				number:true,
				min:0
			},p7to13days:{
				required:true,
				number:true,
				min:0
			},p14to27days:{
				required:true,
				number:true,
				min:0
			},p28days:{
				required:true,
				number:true,
				min:0
			},p3months:{
				required:true,
				number:true,
				min:0
			},
			p6months:{
				required:true,
				number:true,
				min:0
			},
			p9months:{
				required:true,
				number:true,
				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"]
			},
			p25km2h:{
				required:i18n["RateSheet.ProductPrce.Validate.privce"]
			},p50km4h:{
				required:i18n["RateSheet.ProductPrce.Validate.privce"]
			},p1to6days:{
				required:i18n["RateSheet.ProductPrce.Validate.privce"]
			},p7to13days:{
				required:i18n["RateSheet.ProductPrce.Validate.privce"]
			},p14to27days:{
				required:i18n["RateSheet.ProductPrce.Validate.privce"]
			},p28days:{
				required:i18n["RateSheet.ProductPrce.Validate.privce"]
			},p3months:{
				required:i18n["RateSheet.ProductPrce.Validate.privce"]
			},p6months:{
				required:i18n["RateSheet.ProductPrce.Validate.privce"]
			},p9months:{
				required:i18n["RateSheet.ProductPrce.Validate.privce"]
			}
		}
	});
	$("#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"]
			}
		}
	});
});