/**
 * 系统角色管理 
 * 2018/9/19 by liufang
 */
$(function(){
	var layer;
	var table;
	var util;
	layui.use('layer', function(){
		layer = layui.layer;
	});  
	var ERROR_OK = 200;
	var contextPath = $("#contextPath").val();
	var updateAct = $("#updateAct").val();
	var deleteAct = $("#deleteAct").val();
	var funcid = $("#funcid").val();
	var userType = $("#userType").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,#conditionform select").val("");
          $("#conditionform  #orgId").attr('org__id','')
					_this.againRenderTable();
				});
				//新增
				$(".add-ele-btn").on("click",function(){
					
					window.location.href= contextPath + '/admin/role/form?funcid='+funcid;
				});
        // 模糊搜索
        _this.search_fuzzy()
			},
			// 模糊搜索事件
      search_fuzzy: function(){
        $('body').on("input",'#orgId',function(event){
          var _this = $(this)
          cjqt.search_fuzzy.event_input(_this,'org__id','org__span',contextPath,'/admin/templ/getOrgListByName')
        });
        $('body').on("focus",'#orgId',function(event){
          var _this = $(this)
          cjqt.search_fuzzy.event_focus(_this,'org__id','org__span',contextPath,'/admin/templ/getOrgListByName')
        });
        $('body').on('blur','#orgId',function(event) {
          var _this = $(this)
          cjqt.search_fuzzy.event_blur(_this,'org__id')
        });
        $('body').on('click','.select_wrap>.org__span', function(event) {
          event.preventDefault();
          event.stopPropagation();
          /* Act on the event */
          var _this = $(this)
          cjqt.search_fuzzy.event_click(_this,'org__id','#orgId')
        });
      },
			rendertable: function(){
				var _this = this;
				layui.use(['table','util'], function(){
					  table = layui.table,
					  util = layui.util;
					  //方法级渲染
					  table.render({
					    elem: '#LAY_table_user'
					    ,url: contextPath + "/admin/role/getRoleList"
					    ,skin: 'nob' //行边框风格
				    		  ,even: true //开启隔行背景
				    		  ,size: 'lg' //小尺寸的表格
					    ,cols: [[
						  {
				    		   title: '序号',
				    		   templet: '#indexTpl',
//				    		   width: '80'
				    	  },{
				    		  field:'name',
				    		  title: '名称',
//				    		  minWidth: '200',
			    		  },{
			    			  field:'description', 
			    			  title: '描述', 
//			    			  minWidth: '200',
		    			  },{
			    			  field:'roleType', 
			    			  title: '角色类型', 
//			    			  minWidth: '200',
			    			  templet: function(data){
	    						  var str = '';
	    						  if(data.roleType==1){
	    							return '超级管理员'
	    						  }else if(data.roleType==2){
	    							  return '系统管理员'
	    						  }else if(data.roleType==3){
	    							  return '校区管理员'
	    						  }else if(data.roleType==4){
	    							  return '普通管理员'
	    						  };
	    						  
	    						  
	    					  }
		    			  },{
			    			  field:'statusFlag', 
			    			  title: '角色状态', 
//			    			  minWidth: '280',
			    			  templet: function(data){
	    						  var str = '';
	    						  if(data.statusFlag==1){
	    							return '启动'
	    						  }else if(data.statusFlag==2){
	    							  return '禁用'
	    						  }	  
	    					  }
		    			  },{
			    			  field:'lastUpdatedTime', 
			    			  title: '修改时间',
//			    			  minWidth: '200',
                                  templet:function(data) {
                                      return util.toDateString(data.lastUpdatedTime);
                                  }
		    			  },{
	    					 
	    					  title: '操作', 
	    					  minWidth: '180',
	    					  fixed: 'right', 
	    					  templet: function(data){
	    						  var str = '';
	    						  str += '<button class="t-btn t-red-btn t-check-btn" data-id='+data.id+' >查看</button>';
	    						  //如果当前登录用户为校区管理员  则不让他修改和删除校区管理员角色权限,只能查看
								  if (data.roleType == 3 && userType == 3){

								  }else {
                                      if(updateAct==1){
                                          str += '<button class="t-btn t-red-btn t-edit-btn" data-id='+data.id+' >修改</button>';
                                      };
                                      if(deleteAct==1){
                                          str += '<button class="t-btn t-white-btn t-del-btn" data-id='+data.id+' data-isinitialization='+data.isinitialization+'>删除</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即为你接口返回的信息。
						    	//删除
						    	$(".t-del-btn").on("click",function(){
						    		var id = $(this).data("id");
						    		var isinitialization = $(this).data("isinitialization");
						    		_this.delEles(id,isinitialization)
						    		
						    	});

						    	//修改
						    	$(".t-edit-btn").on("click",function(){
						    		var id = $(this).data("id");
						    		var funcid = $("#funcid").val();
						    		window.location.href= contextPath + '/admin/role/update/'+id+'&'+funcid;
						    	})

                                //查看
						    	$(".t-check-btn").on("click",function(){
						    		var id = $(this).data("id");
						    		var funcid = $("#funcid").val();
						    		window.location.href= contextPath + '/admin/role/info/'+id+'&'+funcid;
						    	})
						    }	
					  });
					  //查询
					  $('#conditionform  .search-btn').on('click', function(){
						  	_this.againRenderTable();
					  });
					});
			},
			/*重新渲染table*/
			againRenderTable: function(){
				table.reload('testReload', {
					where: {
						orgId: $("#conditionform #orgId").attr('org__id')
					} ,
					  page: {
					    curr: 1 //重新从第 1 页开始
					  }
				}); 
			},
			/**
			 * 删除
			 * @param id 元素id
			 * @param isinitialization 
			 */
			delEles: function(id,isinitialization){
				var _this = this;
				if(isinitialization==1){
					alert("初始数据无法删除");
				}else if(isinitialization==0){ 
					layer.confirm('确定删除？(删除之后，该角色用户都会失效)', {
						  btn: ['确定', '取消'] 
						}, function(index, layero){
							 layer.close(index); //如果设定了yes回调，需进行手工关闭
							 window.location.href= contextPath + "/admin/role/delete/"+id+"&"+funcid;
						}, function(index){
							
						});	
				}
				
			},
	};
	elementgroupList.init();
})