define(function(require) {
	var $ = require('jquery');
	require('grid')($);
	require('dialog')($);
	require('uriTemplate')($);
	var IFA = require('util');
	var saleGroupList;
	require('../../../styles/cim/jquery-ui-1.8.14.custom.css');
	require('../../../styles/cim/cim-grid.css');
	$(function() {
		searchSaleGroup();
		$("#search").bind("click", searchSaleGroup);
		$("#delete").click(function() {
			deleteMultiply();
		});
	});
	//查询条件置空
	function reset() {
		$("#groupName").val('');
	}
	//批量删除
	function deleteMultiply() {
		var groupIds = getCheckedIds();
		if(groupIds == '') {
			$.msg.alert(i18n["Page.title.dialogshow.title"], i18n["Entity.saleGroup.pleaseSelectSaleGroup"], {buttons: IFA.generateJson(i18n["Entity.Operation.ok"],function() {
				$(this).dialog('close');
				saleGroupList.render();
			}), width:350, height:150});
		}else {
			
	 		//判断是否为空
			if(checkIsEmpty(groupIds) == 'true') {
				//删除
    		  deleteSaleGroup(groupIds,saleGroupList);
			}else {
				$.msg.alert(i18n["Entity.saleGroup.listIsNotEmpty"], function() {
					//重新刷新表格
					saleGroupList.render();
				});
			}
		}
	}
	
	//删除
	function deleteSaleGroup(groupIds,grid) {
		$.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 = {"groupIds":groupIds, "_method":"DELETE"};
	     		$.ajax({
	     			url: urlStr,			//异步请求的地址
	     			type: "POST",			//请求方法
	     			data: urlParam,			//请求中包含的参数
	     			success: function(response, ioArgs) {
	     				$(this).dialog("close");					//请求完成后关闭对话框
	     				grid.render();
	     				$.msg.alert(i18n["Page.title.dialogshow.title"],i18n["Entity.delete.succeed"], function() {
	     						}
	     					,{width:350,height:150}
	     				);
	     			}
	     		});
			}, i18n["Entity.Operation.cancel"], function() {
				$(this).dialog("close");
				saleGroupList.render();
			}),width:350, height:150
		});
	}
	
	function deleteSaleGroupForTip(data,grid) {
		var groupIds = data.groupId;
		$('#groupIdInfo').text(groupIds);
		$('#groupNameInfo').text(data.groupName);
		var div = $('#tipInfo');
		$.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 = {"groupIds":groupIds, "_method":"DELETE"};
	     		$.ajax({
	     			url: urlStr,			//异步请求的地址
	     			type: "POST",			//请求方法
	     			data: urlParam,			//请求中包含的参数
	     			success: function(response, ioArgs) {
	     				$(this).dialog("close");					//请求完成后关闭对话框
	     				grid.render();
	     				$.msg.alert(i18n["Page.title.dialogshow.title"],i18n["Entity.delete.succeed"], function() {
	     						}
	     					,{width:350,height:150}
	     				);
	     			}
	     		});
			}, i18n["Entity.Operation.cancel"], function() {
				$(this).dialog("close");
				saleGroupList.render();
			}),width:350, height:150
		});
	}
	//获取被选中chekcbox所代表的车辆ID
	function getCheckedIds() {
		var ids = "";
		//将被选中的单选框groupId拼接成字符串
		$("#saleGroupList").find(":checkbox[name='groupId'][checked]").each(function(i, data) {
			ids = ids + data.value.toString()+ ",";
		} );
		return ids;
	}
	//查询车辆信息 填充grid表格
	function searchSaleGroup() {
		var url = $("#queryUrl").val();
		saleGroupList = $("#saleGroupList").grid({
			url: url,
			root: 'saleGroupList',
			isREST: false,
			pageTheme:'Theme1',
			defaultParams: $("#saleGroupForm").serialize(),
			method:"post",
			fields: [
			         {type:"checkbox", id:"groupId"},
			         {name:i18n["Entity.saleGroup.name"], id:"groupName", render:function(data){
			        	 var groupId = data.groupId;
			        	 return $('<a href="#" />').html(data.groupName).click(function() {
			        		 //单击链接查看详细信息
			        		 var urlStr = new $.Template($("#detailsUrl").val());
			        		 var urlParam = {"groupId":groupId};
			        		 var urls = urlStr.expand(urlParam);
					    	 window.location.href = urls;
			        	 });
			         }},
			         {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 = {"groupId":data.groupId};
			                                                     		var editUrl = urlStrEdit.expand(urlParamEdit);
			                                                     		window.location.href = editUrl;
			                                                     }},
			                                                     {text:i18n["Entity.Operation.batchDelete"], display:true, action:function(data) {
			                                                    	 if(checkIsEmpty(data.groupId) == 'true') {
			         														//删除
			                                                    		 deleteSaleGroupForTip(data,this);
			         													}else {
			         														$.msg.alert(i18n["Entity.saleGroup.isNotEmpty"], function() {
			         															
			         														});
			         													}
			                                                     }}
			                                                     ]}
			         
			],
			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();
					
			}
		});
	}
	//检查销售组是否为空
	function checkIsEmpty(groupIds) {
		var isEmpty;
		var urlStr = new $.Template($("#isEmptyUrl").val());
		var param = {"groupIds":groupIds};
		var urlStr = urlStr.expand(param);
		$.ajax({
			url:urlStr,
			async:false,
			success:function(data) {
				isEmpty = data.isEmpty;
			}
		});
		return isEmpty;
	}
});