/**
 * 数据字典配置
 * 2018/9/19 by liufang
 */
$(function(){
	var layer;
	var table;
	layui.use('layer', function(){
		   layer = layui.layer;  
	});  
	var ERROR_OK = 200;
	var contextPath = $("#contextPath").val();
	var updateAct = $("#updateAct").val()
    var permEdit = $("#perm-edit").val();//编辑权限
	var tpl = {
							 
	};
	var elementgroupList = {
			init: function(){
				this.render();
				this.bindEvent();
				
			},
			render: function(){
				this.rendertable();
				
			},
			bindEvent: function(){
				var _this = this;
				var a=$("#orgList").val();
				//重置
				$(".set-btn").on("click",function(){
					$("#conditionform input").val("");
					_this.againRenderTable();
				});
				//新增
				$(".add-ele-btn").on("click",function(){
					window.location.href= contextPath + '/system/elementgroup/preAdd';
				});
				
			},
			
			rendertable: function(){
				var _this = this;
				layui.use('table', function(){
					  table = layui.table;
					  
					  //方法级渲染
					  table.render({
					    elem: '#LAY_table_user'
					    ,url: contextPath + "/system/elementgroup/list"
					    ,skin: 'nob' //行边框风格
				    		  ,even: true //开启隔行背景
				    		  ,size: 'lg' //小尺寸的表格
					    ,cols: [[
			             {
			            	 type: 'checkbox', 
			            	 fixed: 'left',
			            	 width: "5%"
            		 	 } ,     
					     {
				    		   field:'groupcode', 
				    		   title: '组名',
				    		   width: '10%',
				    	  },{
				    		  field:'groupname',
				    		  title: '组名备注',
				    		  width: '12%',
			    		  },{
				    		  field:'funcName',
				    		  title: '所属功能',
				    		  width: '12%',
			    		  },{
				    		  field:'isOrgCustom',
				    		  title: '校区可自定义',
									width: '12%',
									templet: function(data){
	    						  var str = '';
	    						  if(data.isOrgCustom == 1){
	    							  str += '<span>是</span>';
	    						  }else{
											str += '<span>否</span>';
										}
	    						  return str;
	    					  }
			    		  },{
			    			  field:'extraData', 
			    			  title: '成员', 
			    			  width: '65%',
		    			  },{
	    					  field:'id', 
	    					  title: '操作', 
	    					  width: '8%',
	    					  fixed: 'right', 
	    					  templet: function(data){
	    						  var str = '';
	    						  if(permEdit == 1){
	    							  str += '<button class="t-btn t-red-btn t-edit-btn" data-id='+data.id+' >修改</button>';
	    						  }
	    						  return str;
	    					  }
	  					  }
						  ]]
						    ,id: 'testReload'
						    ,page: true,
						    method: 'post',
						    request: {
						        pageName: 'pageNo' //页码的参数名称，默认：page
						        ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
						      },
						    response: {
						    	  statusName: 'code' //数据状态的字段名称，默认：code
						    	  ,statusCode: 200 //成功的状态码，默认：0
						    	  ,countName: "count"//数据总数的字段名称，默认：count
						    	  ,dataName: "data" //数据列表的字段名称，默认：data
						    	}   ,
						    done: function(res, curr, count){
//						        //如果是异步请求数据方式，res即为你接口返回的信息。
						    	//删除
						    	$(".del-ele-btn").on("click",function(){
						    		_this.delEles()
						    	});
						    	//修改
						    	$(".t-edit-btn").on("click",function(){
						    		var id = $(this).data("id");
						    		var funcid = $("#funcid").val();
						    		window.location.href= contextPath + '/system/elementgroup/preModify?id='+id+'&funcid='+funcid;
						    	})
						    }	
					  });
					  //查询
					  $('#conditionform  .search-btn').on('click', function(){
						  	_this.againRenderTable();
					  });
					});
			},
			/*重新渲染table*/
			againRenderTable: function(){
				table.reload('testReload', {
					where: {
						groupcode: $(".groupcode").val(),
						groupname: $(".groupname").val()
					} ,
					  page: {
					    curr: 1 //重新从第 1 页开始
					  }
				}); 
			},
			//删除
			delEles: function(){
				var _this = this;
				var checkStatus = table.checkStatus('testReload'); //test即为基础参数id对应的值
				if(checkStatus.data.length==0){
					layer.msg("请选择需要删除的数据");
					return false;
				}else{
					var delid = [];
					checkStatus.data.forEach(function(item){
						delid.push(item.id)
					})
					console.log(delid);
					layer.confirm('确定删除?', {
						  btn: ['确定', '取消'] 
						}, function(index, layero){
							 layer.close(index); //如果设定了yes回调，需进行手工关闭
							 $.ajax({
								   type: "post",
								   url: $("#contextPath").val()+ "/system/elementgroup/deleteGroup", 
								   data:{id:delid},
								   traditional: true,
								   success: function(data){
									   layer.msg("删除成功"); 
								   		_this.againRenderTable();
								   },
								   error: function(){   
								   }
						   });
						}, function(index){
							
						});	
				}
			}
	
	};
	elementgroupList.init();
})