/**
 * 审核权限
 */
$(document).ready(function() {
	load();
	
})
function load(){
	$.ajax({
		type : "POST",
		url : "/pqs9900/user/getAllUserList",
		data :"",
		success : function(data) {
			//没有权限的请求
			if(data==300){
				location.href='/pqs9900/auth/unauthorized';
			//没有登陆的请求
			}else if(data==301){
				location.href='/pqs9900';
			}else{
				if (data.code == 500) {
					//没有数据
					var oTable=$('#example').DataTable({
						columnDefs : [ {
							orderable : false,
							targets : 3
						} ],
						data : data.body,
						destroy : true,
						lengthMenu : [ 10 ],
						language : {
							url:'/pqs9900/json/chine.json'
						},
						'fnDrawCallback': function(oTable) {    
						    $("#example_paginate").append("<div style='float:right;padding-top:4px;padding-left:10px'>到第 <input type='text' id='changePage' class='input-text' style='width:50px;height:28px;'> 页  <button class='btn btn-primary' id='dataTable-btn'>确定</button></div>");    
						    var table = $("#example").dataTable();    
						    $('#dataTable-btn').click(function(e) {
						        if($("#changePage").val() && $("#changePage").val() > 0) {    
						            var redirectpage = $("#changePage").val() - 1;    
						        } else {    
						            var redirectpage = 0;    
						        }    
						        table.fnPageChange(redirectpage);
						    });    
						}
					});
					oTable.column(0).visible(false);
					oTable.clear();
				} else {
					var oTable=$('#example').DataTable({
						columnDefs : [ {
							orderable : false,
							targets : 3
						} ],
						data : data.body,
						destroy : true,
						lengthMenu : [ 200 ],
						language : {
							url:'/pqs9900/json/chine.json'
						},
						'fnDrawCallback': function(oTable) {    
						    $("#example_paginate").append("<div style='float:right;padding-top:4px;padding-left:10px'>到第 <input type='text' id='changePage' class='input-text' style='width:50px;height:28px;'> 页  <button class='btn btn-primary' id='dataTable-btn'>确定</button></div>");    
						    var table = $("#example").dataTable();    
						    $('#dataTable-btn').click(function(e) {
						        if($("#changePage").val() && $("#changePage").val() > 0) {    
						            var redirectpage = $("#changePage").val() - 1;    
						        } else {    
						            var redirectpage = 0;    
						        }    
						        table.fnPageChange(redirectpage);
						    });    
						}
					});
					oTable.column(0).visible(false);
					var selectstate=oTable.row( this ).data()[5];
					var selectIndex=oTable.row( this ).data()[0];
					$('#example tbody').on('click', 'tr', function () {
						
						oTable.$('tr.selected').removeClass('selected');
						$(this).addClass('selected');
						getTabData(selectstate);
                        getselect(selectstate,selectIndex);
					})
					
					
				}
			}
		}
	});
}
function getselect(selectstate,selectIndex){
	var roleGpIndex="";
	if(selectstate=='待审核'){
		$("#limitauditBtn").click(function() { //审核
			layer.confirm('确定审核？', function(index){
				$.ajax({
					type : "POST",
					url : "/pqs9900/role/selectAllName",
					success : function(data) {
						$.each(data,function (i,item) {
							roleGpIndex=item.roleGpIndex;
						})
				      $.ajax({
					     type : "POST",
					     url : "/pqs9900/user/updateExaminer",
					     data:{
						    userIndex:selectIndex,
						    roleGpIndex:roleGpIndex,
						    state:1
					       },
					     success : function(data) {
						    var info = data;
						    if (info == 2) {
							//审核异常、或者失败
							layer.msg('审核失败', {
								icon: 5,
								time: 1000
								},function(){
									load();
								});
						} else {
							//审核成功
							layer.msg('审核成功', {
								  icon:6,
								  time: 1000 
								}, function(){
									load();
									document.getElementById("limitauditBtn").style.backgroundImage="url(../images/audit_no.png)"; 
							    	document.getElementById("limitUnlockBtn").style.backgroundImage="url(../images/unlock_no.png)"; 
							    	document.getElementById("limitpmschangeBtn").style.backgroundImage="url(../images/pmschange_no.png)"; 
								}); 
						}
					}
				})
						
					}
				})
			})
        })
	}
}
function getTabData(selectdata){
    if(selectdata == '待审核'){
    	document.getElementById("limitauditBtn").style.backgroundImage="url(../images/audit.png)"; 
    	document.getElementById("limitUnlockBtn").style.backgroundImage="url(../images/unlock_no.png)"; 
    	document.getElementById("limitpmschangeBtn").style.backgroundImage="url(../images/pmschange_no.png)"; 
    }else if(selectdata == '锁定'){
    	document.getElementById("limitUnlockBtn").style.backgroundImage="url(../images/Unlock.png)"; 
    	document.getElementById("limitauditBtn").style.backgroundImage="url(../images/audit_no.png)";
    	document.getElementById("limitpmschangeBtn").style.backgroundImage="url(../images/pmschange_no.png)"; 
    }else{
    	document.getElementById("limitpmschangeBtn").style.backgroundImage="url(../images/pmschange.png)"; 
    	document.getElementById("limitauditBtn").style.backgroundImage="url(../images/audit_no.png)";
    	document.getElementById("limitUnlockBtn").style.backgroundImage="url(../images/unlock_no.png)"; 
    }
}