define(function(require) {
	//加载JS库
	var $ = require('jquery');
	require('grid')($);
	require('dialog')($);
	require('uriTemplate')($);
	require('demoAssets/../../style/jquery-ui-1.8.14.custom.css');
	require('../../../styles/cim/cim-grid.css');  
	var IFA = require('util');
	$(function() {
		queryDetail();
		$("#edit").click(function() {
			var urlStrEdit = new $.Template($("#editUrl").val());
     		var urlParamEdit = {"groupId":$("#groupId").val()};
     		var editUrl = urlStrEdit.expand(urlParamEdit);
     		window.location.href = editUrl;
		});
		 $("#delete").click(function(){batchDelete();});
	});
	
	function queryDetail(){
	var groupId = $("#groupId").val();
	var params = {'groupId':groupId};
	var url =$("#querySalesUrl").val();
	$("#result").grid({
		url: url,
		root: 'sales',
		pageTheme:'Theme1',
		isREST:false,  
		method:"POST",
		defaultParams:params,
		fields:[
		        {type:"checkbox", id:"peopleId",style:'width:5%'},
		        {
		        name:i18n['Entity.saleGroup.sale.name'],style:'width:25%',render:function(data){
		        	return data.cnName+"("+data.enName+")";
		        }
		        },
		        {
		        name:i18n['Entity.saleGroup.sale.jobNumber'],style:'width:30%',id:"jobNumber"
		        },
		        {
		        name:i18n['Entity.Operation'],operation:[
                       {text:i18n["Entity.dealer.edit"], display:true, action:function(data) {
                   		//进入更新页面
                    		var urlStrEdit = new $.Template($("#editSaleUrl").val());
                    		var urlParamEdit = {"peopleId":data.peopleId};
                    		var editUrl = urlStrEdit.expand(urlParamEdit);
                    		window.location.href = editUrl;
                    }},
                    {text:i18n["Entity.Operation.batchDelete"], display:true, action:function(data) {
								//删除
                   		deleteInfo(data.peopleId,this);
                    }}
                    ]
		        }
		      ]
	});
	}
	
	// 删除
	  function deleteInfo(peopleIds,grid){
		  $.msg.alert(i18n["Page.title.dialogshow.title"], i18n["Entity.Sure.tip"],{
				buttons:IFA.generateJson(i18n["Entity.page.YES"],function() {
		    		   var deleteUrl = $("#deleteSaleUrl").val();
		    		   var param={'peopleId':peopleIds,'_method':'DELETE'};
			    	   $.ajax({
			    		   url:deleteUrl,
			    		   type:"POST",
			    		   data:param,
			    		   dataType:'json',
			    		   success:function(response,ioArgs){
			    		     $(this).dialog("close");
			    		     if(response.flag){
			    		    	 $.msg.alert(i18n["Page.title.dialogshow.title"], i18n["Entity.delete.succeed"],{
			    		    		 buttons: IFA.generateJson(i18n["Entity.Operation.ok"], function() {
									        $(this).dialog("close");
									        queryDetail();
									        }),
									   width:350,height:150
							 	 });// $.msg.alert()结束
			    		      }else{
			    		    	  $.msg.alert(i18n["Page.title.dialogshow.title"],i18n["Entity.delete.failed.tip"],{
			    		    		  buttons: IFA.generateJson(i18n["Entity.Operation.ok"], function() {
									        $(this).dialog("close");
									        queryDetail();
									        }), //buttons结束
									   width:350,height:150
							 	 });// $.msg.alert()结束
			    		      }
			    	        },
			    	        error:function(response,ioArgs){
			    	        	$(this).dialog("close");
								 $.msg.alert(i18n["Page.title.dialogshow.title"], i18n["Entity.delete.failed"],{
									 buttons: IFA.generateJson(i18n["Entity.Operation.ok"], function() {
									        $(this).dialog("close");
									        }), //buttons结束
									   width:350,height:150
							 	 });// $.msg.alert()结束
			    	        }
			    	   });
		    	       $(this).dialog("close");
				   },i18n["Entity.page.No"],function() {$(this).dialog("close");}
	 		  ),
			width:350,height:150
	 		}
	 	);
	  }
	  // 批量删除
	  function batchDelete(){
	  	var peopleIds = getCheckedIds();
	  	// 用户未选择删除项
	  	if(peopleIds.length==0){
	  		$.msg.alert(i18n["Page.title.dialogshow.title"], i18n["Page.warnning.tips"],{
		  			buttons: IFA.generateJson(i18n["Entity.Operation.ok"], function() {
			            $(this).dialog("close");
			        }),
	  		        width:350,height:150
	  			});
	  	}else{
	  		deleteInfo(peopleIds);
	  	}
	  }
	  // 获取用户选择的Id(数组形式),(通过选择checkbox)
	  function getCheckedIds(){
	  	var ids = "";
	  	$("#result").find(":checkbox[name='peopleId'][checked]").each(function(i,data){
	  		ids = ids + data.value.toString()+",";
	  	});
	  	return ids;
	  }
});