define(function(require) {
	var $ = require('jquery');
	require('grid')($);
	require('dialog')($);
	require('uriTemplate')($);
	var IFA = require('util');
	var vehicleList;
	require('../../../styles/cim/jquery-ui-1.8.14.custom.css');
	require('../../../styles/cim/cim-grid.css');
	$(function() {
		searchVehicle();
		$("#search").bind("click", searchVehicle);
		$("#delete").click(function() {
			deleteMultiply();
		});
	});
	//查询条件置空
	function reset() {
		$("#vehicleCode").val('');
		$("#plateNumber").val('');
		$("#color").val('');
		$("#leasingType").val('');
		$("#carTypeId").val('');
		$("#districtId").val('');
	}
	//删除
	function deleteVehicle(vehicleIds) {
		var urlStr = $("#deleteUrl").val();
 		var urlParam = {"vehicleIds":vehicleIds, "_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 = $("#indexUrl").val();
 						}
 					,{width:350,height:150}
 				);
 			}
 		});
	}
	//获取被选中chekcbox所代表的车辆ID
	function getCheckedIds() {
		var ids = "";
		//将被选中的单选框vehicleId拼接成字符串
		$("#vehicleList").find(":checkbox[name='vehicleId'][checked]").each(function(i, data) {
			ids = ids + data.value.toString()+ ",";
		} );
		return ids;
	}
	//批量删除车辆
	function deleteMultiply() {
		var vehicleIds = getCheckedIds();
		if(vehicleIds == '') {
			$.msg.alert(i18n["Page.title.dialogshow.title"], i18n["Entity.vehicle.pleaseSelectVehicle"], {buttons: IFA.generateJson(i18n["Entity.Operation.ok"],function() {
				$(this).dialog('close');
				vehicleList.render();
			}), width:350, height:150});
		}else {
			var vehicleIds = getCheckedIds();
			 checkBeforeDelete(vehicleIds);
		}
	}
	
	function checkBeforeDelete(vehicleIds){
		var url = $("#checkUrl").val();
		var data = {'vehicleIds':vehicleIds};
		$.post(url,data,function(res){
			if(res.flag){
				$.msg.alert(i18n["Page.title.dialogshow.title"],i18n['Entity.vehicle.tips1'],function(){},{
					buttons:IFA.generateJson(
					i18n['Entity.Enquire.Opereation.ok'],function(){
					$(this).dialog('close');
					deleteVehicle(vehicleIds);
					},
					i18n['Entity.Enquire.Opereation.cancel'],function(){
					$(this).dialog('close');
					}
				)
				});
			}else{
				$.msg.alert(i18n["Page.title.dialogshow.title"],i18n["Entity.Sure.tip"],{
					buttons:IFA.generateJson(i18n["Entity.Operation.batchDelete"],function() {
						$(this).dialog("close");
						deleteVehicle(vehicleIds);
					},i18n["Entity.Operation.cancel"],function() {
						$(this).dialog("close");
					}) ,width:350, height:150
				});
			}
		});
	}
	
	function DeleteForTipInfo(dataInfo){
		var vehicleIds = dataInfo.vehicleId;
		$('#vehicleNameInfo').text(dataInfo.vehicleCode);
		$('#plateNumberInfor').text(dataInfo.plateNumber);
		$('#carTypeNameInfo').text(dataInfo.carTypeName);
		$('#districtNameInfo').text(dataInfo.districtName);
		var div = $('#vehicleTipInfo');
		var url = $("#checkUrl").val();
		var data = {'vehicleIds':vehicleIds};
		$.post(url,data,function(res){
			if(res.flag){
				$('#checkInfo').text(i18n['Entity.vehicle.tips1']);
				$.msg.alert(i18n["Page.title.dialogshow.title"],div,function(){},{
					buttons:IFA.generateJson(
							i18n['Entity.Enquire.Opereation.ok'],function(){
					$(this).dialog('close');
					deleteVehicle(vehicleIds);
					},
					i18n['Entity.Enquire.Opereation.cancel'],function(){
					$(this).dialog('close');
					}
				)
				});
			}else{
				$('#checkInfo').text(i18n['Page.title.deleteSure']);
				$.msg.alert(i18n["Page.title.dialogshow.title"],div,{
					buttons:IFA.generateJson(i18n["Entity.Operation.batchDelete"],function() {
						$(this).dialog("close");
						deleteVehicle(vehicleIds);
					},i18n["Entity.Operation.cancel"],function() {
						$(this).dialog("close");
					}) ,width:350, height:200
				});
			}
		});
	}
	
	//查询车辆信息 填充grid表格
	function searchVehicle() {
		var url = $("#queryUrl").val();
		vehicleList = $("#vehicleList").grid({
			url: url,
			root: 'vehicleList',
			isREST: false,
			pageTheme:'Theme1',
			defaultParams: $("#vehicleForm").serialize(),
			method:"post",
			fields: [
			         {type:"checkbox", id:"vehicleId"},
			         {name:i18n["Entity.vehicle.plateNumber"], id:"plateNumber", render:function(data){
			        	 var vehicleId = data.vehicleId;
			        	 return $('<a href="#"/>').html(data.plateNumber).click(function() {
			        		 //单击链接查看详细信息
			        		 var urlStr = new $.Template($("#detailsUrl").val());
			        		 var urlParam = {"vehicleId":vehicleId};
			        		 var urls = urlStr.expand(urlParam);
					    	 window.location.href = urls;
			        	 });
			         }},
			         {name:i18n["Entity.vehicle.vehicleCode"], id:"vehicleCode"},
			         {name:i18n["Entity.vehicle.carType"], render:function(data) {
			        	return  data.carTypeName;
			         }},
			         {name:i18n["Entity.vehicle.district"], render:function(data) {
			        	 return data.districtName;
			         }},
			         {name:i18n["Entity.vehicle.color"], id:"color"},
			         {name:i18n["Entity.vehicle.leasingType"], render:function(data) {
			        	 return data.leasingType == "L"?i18n["Entity.vehicle.leasingLong"]:i18n["Entity.vehicle.leasingShort"];
			         }},
			         /*{name:"投入运营时间", render:function(data) {
			        	 return $.format.formatTime(new Date(data.putIntoOperation));
			         }},*/
			         {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 = {"vehicleId":data.vehicleId};
			                                                     		var editUrl = urlStrEdit.expand(urlParamEdit);
			                                                     		window.location.href = editUrl;
			                                                     }},
			                                                     {text:i18n["Entity.Operation.batchDelete"], display:true, action:function(data) {
			                                                    		//checkBeforeDelete(data.vehicleId);
			                                                    	 DeleteForTipInfo(data);
			                                                     }},
			                                                     {text:i18n["Entity.Operation.audits"], display:true, action:function(data) {
			                                                     		var urlStrEdit = new $.Template($("#auditUrl").val());
			                                                     		var urlParamEdit = {"id":data.vehicleId};
			                                                     		var auditUrl = urlStrEdit.expand(urlParamEdit);
			                                                     		window.location.href=auditUrl;
			                                                     }}
			                                                     ]}
			         
			],
			buttons: [],
			callback: function(data) {
				if(data.total == 0) {
					return $.msg.alert(i18n["Page.title.dialogshow.title"],i18n["Entity.vehicleCategory.noAnyRecords"],{
						buttons: IFA.generateJson(i18n["Entity.Operation.ok"], function() {
							$(this).dialog("close");
						})
					});
				}
				//查询动作结束后，清空上一次查询条件
					reset();
					
			}
		});
	}
});