define(function(require){
	var $ = require('jquery');
	require('validation')($);
	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'); 
	
	 $(function(){
		   query(); // 加载页面时,做一次查询
	  	   // 绑定click事件,点击按钮(链接)'新建',触发此事件
	  	   $("#new").click(function(){newPc();});
	  	   //绑定click事件,点击按钮(链接)'查询',触发此事件
	  	   $("#query").click(function(){query();});
	  	   $("#delete").click(function(){batchDelete();});
	  });
	  // 查询
	  function query(){
		    var queryUrl = $("#queryUrl").val();
		  	$("#pcList").grid({
		  		url:queryUrl,
		  		root:"pcList",  // 从ModleMap传递过来的数据
		  		method:"POST",  // 默认为get方法
		  		isREST:false,   // 默认为true,当method是POST时,设为false
		  		pageTheme:'Theme1',
		  		defaultParams:IFA.convertQueryStringToJson($("#pcForm").serialize()),
		  		fields:[
		  				{id:"pcId",style:'width:10px',type:"checkbox"},
		  				{id:"pcId",style:'width:100px', name:i18n["Entity.pc.pcId"],render:function(data){
		  					     return $("<a href='#' />").html(data.pcId).attr("title",data.pcId).click(function(){
		  					    	  showDetail(data.pcId);
		  						  }); // return结束
		  					}}, // render对应的函数结束
		  				{id:"creatorName",name:i18n["Entity.creator"]},
		  				{id:"creationDate",name:i18n["Entity.createdDate"],type:"time"},
		  				{id:"modifierName",name:i18n["Entity.modifier"]},
		  				{id:"modificationDate",name:i18n["Entity.modificationDate"],type:"time"},
		  				{name:i18n["Entity.Operation"],operation:[
		  				      {text:i18n["Entity.Operation.detail"],display:true,action:function(data){showDetail(data.pcId);}},                                 
		  				      {text:i18n["Entity.page.edit"],display:true,action:function(data){edit(data.pcId);}},
		  				      {text:i18n["Entity.Operation.delete"],display:true,action:function(data){deleteInfo(data.pcId);}}
		  				    ]}		
		  				] // Fields结束
		  	});
	  }
	   
	  // 新增
	  function newPc(){
		var newUrl = $("#newUrl").val();
	  	window.location = newUrl;
	  }
	  // 显示详细信息
	  function showDetail(pcId){
		  var showUrl = new $.Template($("#showUrl").val());
		  var params = {"pcId":pcId};
	      window.location= showUrl.expand(params);
	  }
	  // 编辑
	  function edit(pcId){
	  	  // 进入编辑页面URL
	      var params = {"pcId":pcId};
		  var editUrl = new $.Template($("#editUrl").val());
	      window.location= editUrl.expand(params);
	  }
	  // 删除
	  function deleteInfo(pcIds){
		  $.msg.alert(i18n["Page.title.dialogshow.title"], i18n["Entity.Sure.tip"],{
			  buttons:IFA.generateJson(i18n["Entity.page.YES"],function() {
		    		   var deleteUrl = $("#deleteUrl").val();
		    		   var param={'pcId':pcIds,'_method':'DELETE'};
			    	   $.ajax({
			    		   url:deleteUrl,
			    		   type:"POST",
			    		   data:param,
			    		   dataType:'json',
			    		   success:function(response,ioArgs){
			    		     $(this).dialog("close");
							 $.msg.alert(i18n["Page.title.dialogshow.title"], i18n["Entity.delete.succeed"],{
								   buttons: IFA.generateJson(i18n["Entity.Operation.ok"], function() {
								        $(this).dialog("close");
								        query();
								    }), //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");
									        query();
									    }), //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 pcIds = getCheckedIds(this);
	  	// 用户未选择删除项
	  	if(pcIds.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(pcIds);
	  	}
	  }
	  // 获取用户选择的Id(数组形式),(通过选择checkbox)
	  function getCheckedIds(context){
	  	var ids = "";
	  	$("#pcList").find(":checkbox[name='pcId'][checked]").each(function(i,data){
	  		ids = ids + data.value.toString()+",";
	  	});
	  	return ids;
	  }
});

