define(function(require) {
	//加载JS库
	var $ = require('jquery');
	require('grid')($);
	require('dialog')($);
	require('uriTemplate')($);
	require('../../../styles/cim/jquery-ui-1.8.14.custom.css');
	require('../../../styles/cim/cim-grid.css');   
	var IFA = require('util');
	var carTypeList;
	//文档模型加载完成
	$(document).ready(function() {
		searchResult();
		$("#search").bind("click", searchResult);
		$("#delete").click(function() {
			deleteMultiply();
		});
		$("#carTypeForm :input").bind("keydown",function(e){
			var e = e ? e : window.event;
			if(e.keyCode==13){
				searchResult();
			}
		});
	});
	
	function deleteCarTypeForTip(data){
		div=$('#tipInfo');
		$('#carTypeNameInfo').text(data.carTypeName);
		$('#categoryNameInfo').text(data.categoryName);
		var carTypeIds = data.carTypeId;
		$.msg.alert(i18n["Page.title.dialogshow.title"], div,{
			buttons: IFA.generateJson(i18n["Entity.Operation.batchDelete"],function() {
				$(this).dialog("close");
				var urlStr = $("#deleteUrl").val();
	     		var urlParam = {"carTypeIds":carTypeIds, "_method":"DELETE"};
	     		$.ajax({
	     			url: urlStr,			//异步请求的地址
	     			type: "POST",			//请求方法
	     			data: urlParam,			//请求中包含的参数
	     			success: function(response, ioArgs) {
	     				$(this).dialog("close");					//请求完成后关闭对话框
	     				$.msg.alert(i18n["Page.title.dialogshow.title"],i18n["Entity.delete.succeed"], function() {
	     							$(this).dialog("close");
	     							window.location.href = urlStr;
	     						}
	     					,{width:350,height:150}
	     				);
	     			}
	     		});
			},i18n["Entity.Operation.cancel"],function() {
				$(this).dialog("close");
				carTypeList.render();
			}),width:350, height:150
		});
	}
	//删除车型
	function deleteCarType(carTypeIds) {
		$.msg.alert(i18n["Page.title.dialogshow.title"],i18n["Entity.Sure.tip"],{
			buttons: IFA.generateJson(i18n["Entity.Operation.batchDelete"],function() {
				$(this).dialog("close");
				var urlStr = $("#deleteUrl").val();
	     		var urlParam = {"carTypeIds":carTypeIds, "_method":"DELETE"};
	     		$.ajax({
	     			url: urlStr,			//异步请求的地址
	     			type: "POST",			//请求方法
	     			data: urlParam,			//请求中包含的参数
	     			success: function(response, ioArgs) {
	     				$(this).dialog("close");					//请求完成后关闭对话框
	     				$.msg.alert(i18n["Page.title.dialogshow.title"],i18n["Entity.delete.succeed"], function() {
	     							$(this).dialog("close");
	     							window.location.href = urlStr;
	     						}
	     					,{width:350,height:150}
	     				);
	     			}
	     		});
			},i18n["Entity.Operation.cancel"],function() {
				$(this).dialog("close");
				carTypeList.render();
			}),width:350, height:150
		});
	}
	
	function getCheckedIds() {
		var ids = "";
		//将被选中的单选框carTypeId拼接成字符串
		$("#carTypeList").find(":checkbox[name='carTypeId'][checked]").each(function(i, data) {
			ids = ids + data.value.toString()+ ",";
		} );
		return ids;
	}
	
	//查询车型列表
	function searchResult() {
		var url = $("#queryUrl").val();
		carTypeList = $("#carTypeList").grid({
			url:url,
			root: 'carTypeList',
			isREST:false,
			pageTheme:'Theme1',
			defaultParams: $("#carTypeForm").serialize(),
			method:"post",
			fields: [
			         {type:"checkbox", id:"carTypeId"},
			         {name:i18n["Entity.carType.name"], id:"carTypeName", render:function(data) {
			        	var carTypeId = data.carTypeId;
			        	 return $('<a href="#" />').html(data.carTypeName).click(function() {
			        		 //单击链接查看详细信息
			        		 var urlStr = new $.Template($("#detailsUrl").val());
			        		 var urlParam = {"carTypeId":carTypeId};
			        		 var urls = urlStr.expand(urlParam);
					    	 window.location.href = urls;
			        	 });
			         }},
			         {name:i18n["Entity.carType.category"], id:"categoryId", render:function(data) {
			        	 return data.categoryName;
			         }},
			         {name:i18n["Entity.SystemUser.creator"], id:"creatorName"},
			         {name:i18n["Entity.SystemUser.creationDate"],id:"creationDate",style:{width:"15%"},type:"time"},
			         {name:i18n["Entity.SystemUser.modifier"], id:"modifierName"},
			         {name:i18n["Entity.SystemUser.modificationDate"],id:"modificationDate",style:{width:"15%"},type:"time"},
			         {name:i18n["Entity.SystemUser.operate"], style:{width:"10%"}, operation:[
			                                                     	{text:i18n["Entity.dealer.edit"], display:true,action:function(data) {
			                                                     		//进入更新页面
			                                                     		var urlStrEdit = new $.Template($("#editUrl").val());
			                                                     		var urlParamEdit = {"carTypeId":data.carTypeId};
			                                                     		var editUrl = urlStrEdit.expand(urlParamEdit);
			                                                     		window.location.href = editUrl;
			                                                     		
			                                                     		/*var urlStrUpdate = new $.Template($("#updateUrl").val());
			                                                     		var urlParamUpdate = {"carTypeId":data.carTypeId};
			                                                     		var updateUrl = urlStrUpdate.expand(urlParamUpdate);*/
			                                                     		
			         												}},
			         												{text:i18n["Entity.Operation.batchDelete"], display:true,action:function(data) {
			         													//判断车型是否为空
			         													if(checkIsEmpty(data.carTypeId) == 'true') {
			         														//删除
			         														deleteCarTypeForTip(data);
			         													}else {
			         														$.msg.alert(i18n["Entity.carType.isNotEmpty"], function() {
			         															
			         														});
			         													}
			         												}}
			                                                     ]}
			         
			],
		buttons:[],
		callback: function(data) {
			//无任何查询结果
			if(data.total == 0) {
				return $.msg.alert(i18n["Entity.vehicleCategory.noAnyRecords"], function() {
					reset();
				});
				
			}
		}
		});
	}
	//批量删除车型
	function deleteMultiply() {
		var carTypeIds = getCheckedIds();
		if(carTypeIds == '') {
			$.msg.alert(i18n["Page.title.dialogshow.title"], i18n["Entity.carType.pleaseSelectCarType"], {buttons: IFA.generateJson(i18n["Entity.Operation.ok"],function() {
				$(this).dialog('close');
				carTypeList.render();
			}), width:350, height:150});
		}else {
			
	 		//判断车型是否为空
			if(checkIsEmpty(carTypeIds) == 'true') {
				//删除
				deleteCarType(carTypeIds);
			}else {
				$.msg.alert(i18n["Entity.carType.listIsNotEmpty"], function() {
					//重新刷新表格
					carTypeList.render();
				});
			}
		}
	}
	function reset() {
		$("#carTypeName").val('');
		$("#categoryId").val('');
	}
	function checkIsEmpty(carTypeIds) {
		var isEmpty;
		var urlStr = new $.Template($("#isEmptyUrl").val());
		var param = {"carTypeIds":carTypeIds};
		var urlStr = urlStr.expand(param);
		$.ajax({
			url:urlStr,
			async:false,
			success:function(data) {
				isEmpty = data.isEmpty;
			}
		});
		return isEmpty;
	}
});