define(function(require) {
	//加载JS库
	var $ = require('jquery');
	require('grid')($);
	require('validation')($);
	require('uriTemplate')($);
	require('datepicker')($);
	var uploadFile = require('./uploadFiles');
	require('../../../styles/cim/jquery-ui-1.8.14.custom.css');
	require('../../../styles/cim/validation.css');
	require('../../../styles/cim/autoComplete.css');
	require('../../../js/seaJsExtends/autoComplete.js')($);
	var IFA = require('util');
	require('dialog')($);
	var validateList;
	var flag = false;
	var vehicleCodeBefore;
	$(function() {
		//查看图片
		$("a[id^='showImage']").bind("click",function(){
			var url = new $.Template($("#searchImageUrl").val());
			var para={"imageName":$(this).attr("imageName"),"vehicleId":$(this).attr("vehicleId")};
			url = url.expand(para);
			window.showModalDialog(url);
			//window.location.href=url;
		});
		
		uploadFile.init();
		hotKeyDistrict();
		vehicleCodeBefore = $("#vehicleCode").val();
		if(flag){
	    	window.location.href=$("#cancelUrl").val();
	    }
		flag == false?true:false;
		$("#save").bind("click", saveVehicle);
		$("#cancel").bind("click", close);
		validateList = $("#editVehilce").validate({
			rules: {
			vehicleCode: {
				required:true,
				rangelength: [1, 20],
				regex:/^([a-zA-Z0-9]|[_]|[-])*$/,
				checkExists:true
			},
			plateNumber: {
				required:true,
				rangelength: [1, 20],
				//regex:/^([\u4e00-\u9fa5])?([a-zA-Z0-9])*$/,
				checkPlateNumberExist:true
			},
			color: {
				required:true,
				rangelength: [1, 10],
				regex:/^([a-zA-Z0-9]|[\u4e00-\u9fa5])*$/
			},
			carTypeId: {
				required:true,
				rangelength: [1, 10],
				regex:/^([0-9])*$/
			},
			putIntoOperation: {
				rangelength: [1, 20],
				regex:/^([0-9]|[ ]|[:]|[-])*$/
			},
			putEndOperation: {
				rangelength: [1, 20],
				regex:/^([0-9]|[ ]|[:]|[-])*$/
			},
			leasingType: {
				required:true,
				rangelength: [0, 1],
				regex:/^([a-zA-Z])*$/
			},
			gasTank: {
				rangelength: [0, 20],
				//regex:/^([a-zA-Z0-9]|[\u4e00-\u9fa5]|[_]|[-])*$/
			},
			fuleConsumption: {
				//required:true,
				rangelength: [0, 15],
				//regex:/^([a-zA-Z0-9]|[\u4e00-\u9fa5]|[_]|[-]|[.])*$/
			},
			procurementCosts: {
				rangelength: [0, 16],
				//regex: /^((([0-9]|\,)*(\.[0-9]{1,2})?)|(([0-9]|\.)*(\,[0-9]{1,2})?)|(([0-9]|\')*((\.|\,)[0-9]{1,2})?))$/
			},
			vehicleRemarks: {
				rangelength: [1, 100],
				regex:/^([a-zA-Z0-9]|[\u4e00-\u9fa5]|[_]|[-]|[ ])*$/
			},
			districtHokKey:{
				required:true
			}
		},
		messages: {
			vehicleCode: {
				required:i18n["Entity.vehicle.inputVehicleCode"],
				rangelength: i18n["Entity.vehicle.requireVehicleCode"],
				regex: i18n["Entity.vehicle.requireVehicleCodeDescription"]
			},
			plateNumber: {
				rangelength: i18n["Entity.vehicle.requirePlateNumber"],
				//regex: i18n["Entity.vehicle.requirePlateNumberDescription"]
			},
			color: {
				required:i18n["Entity.vehicle.inputColor"],
				rangelength: i18n["Entity.vehicle.requireColor"],
				regex: i18n["Entity.vehicle.requireColorDescription"]
			},
			carTypeId: {
				required:i18n["Entity.vehicle.pleaseSelectCarType"]
			},
			putIntoOperation: {
				required:i18n["Entity.vehicle.pleaseSelectPutIntoOperation"]
			},
			putEndOperation: {
				required:i18n["Entity.vehicle.pleaseSelectPutEndOperation"]
			},
			leasingType: {
				required:i18n["Entity.vehicle.pleaseSelectLeasingType"]
			},
			gasTank: {
				required:i18n["Entity.vehicle.pleaseInputGasTank"],
				rangelength: i18n["Entity.vehicle.requireGasTank"],
				regex: i18n["Entity.vehicle.requireGasTankDescription"]
			},
			fuleConsumption: {
				required:i18n["Entity.vehicle.inputFuleConsumption"],
				rangelength: i18n["Entity.vehicle.requireFuleConsumption"],
				regex: i18n["Entity.vehicle.requireGasTankDescription"]
			},
			procurementCosts: {
				required:i18n["Entity.vehicle.inputProcurementCosts"],
				rangelength: i18n["Entity.vehicle.requireProcurementCosts"],
				regex: i18n["Entity.vehicle.requireProcurementCosts"]
			},
			vehicleRemarks: {
				required:i18n["Entity.vehicle.inputReamrks"],
				rangelength: i18n["Entity.vehicle.requireReamrks"],
				regex: i18n["Entity.vehicle.requireGasTankDescription"]
			},
			districtHokKey:{
				required:i18n['Entity.vehicle.districtRequired']
			}
		}
		});
		$.validator.addMethod("checkExists",function(value,element,param) {
			var flag = true;
			var urlStr = new $.Template($("#checkUrl").val());
			var urlParam = {"vehicleCode":$("#vehicleCode").val()};
			var urlStr = urlStr.expand(urlParam);
			if(vehicleCodeBefore == $("#vehicleCode").val()) {
				//更新的时候可以不更新原有车辆代码
				return true;
			} else {
				$.ajax({
					url:urlStr,
					async: false,
					success:function(data) {
					if(data.flag == "vehicleCodeRepeat") {
						flag = false;
					} else {
						flag = true;
					}
				}
				});
				return flag;
			}
			
		}, i18n["Entity.vehicle.vehicleCodeExists"]);
		$.validator.addMethod("checkPlateNumberExist",function(value,element,param) {
			var flag = true;
			var urlStr = new $.Template($("#checkPlateNumberUrl").val());
			var urlParam = {"plateNumber":value,"vehicleId":$("#vehicleId").val()};
			var urlStr = urlStr.expand(urlParam);
			$.ajax({
				url:urlStr,
				async: false,
				success:function(data) {
				flag = data.flag;
			}
			});
			return flag;
		}, i18n["Entity.vehicle.plateNumberExists"]);
	});
	$(function() {
		//添加时间框
		$("#putIntoOperation").datepicker({
			
		});
		
		$("#putEndOperation").datepicker({
			
		});
		
	});
	//点击触发，保存新增车型信息
	function saveVehicle() {
		if($("#editVehilce").valid()){
		$.ajax({
			type:"post",
			url:$("#updateUrl").val(),
			data:$("#editVehilce").serialize(),
			success:function(data) {
				if(data.flag==1){
					$.msg.alert(i18ns("Page.Dialog.title"),i18ns("Page.Dialog.content.success"),{
			    		buttons: IFA.generateJson(i18ns("Entity.Operation.ok"), function() {
			    			self.close();
			    		})});
				}else{
					$.msg.alert(i18ns("Page.Dialog.title"),i18ns("Page.Dialog.update.error"),{
			    		buttons: IFA.generateJson(i18ns("Entity.Operation.ok"), function() {
			    			self.close();
			    		})});
				}
		}});
		}
	}
	//点击取消按钮回到车型管理主页面
	function close() {
		window.close();
	}
	$(function init() {
		if($("#messageUpdate").val() == $("#I18nMsgKey").val()){
	    	window.location.href = $("#cancelUrl").val();
	    }
	});
	
	function hotKeyDistrict(){
	  	$.ajax({
			type:"get",
			url:$("#hotkeyDistrict").val(),
			success:function(response){
			var acdata=response.district;
			$("#districtHokKey").autocomplete(acdata,
				      {
						caption: i18n["JS.districtHotKey.caption"],
						findCaption: i18n["JS.districtHotKey.findCaption"],
						noFindCaption: i18n["JS.districtHotKey.noFindCaption"],
						width: 200,
						max: 5,
						formatItem: function(row, i, max) {
							return '<span class="ac_code">{0}</span><span class="ac_Name">{1}</span>'.patchContent(row.districtHotkey, row.districtName);
						},
						formatMatch: function(row, i, max) {
							return "{0} {1}".patchContent(row.districtHotkey, row.districtName);
						},
						formatResult: function(row) {
							return "{0}".patchContent(row.districtName);
						}
					}).result(function(event, data, formatted) {
						$("#districtId").val(data.districtId);
					});
		}
		});
	}
});