<?php
include_once("htui_header.php");

if ($_GET['listid']!='' && $_GET['ty']==''){
    $scuser_id=$_SESSION["a_uid"];
	if (check_permission($scuser_id,'userlist.php',3)){
		$listid = flc_request($_GET['listid']);
		$sql = "delete from {$tablepre}scuser where ID in(".$listid.")";
		$db->exec($sql);
		$response_arr = array("status" => 1,"message"  => '操作成功');
	}else{
        $response_arr = array("status" => -1,"message"  => '对不起,您无权限操作!');
	}

    echo json_encode($response_arr);
    exit;
}

if ($_GET['listid']!='' && $_GET['ty']==1){
    $scuser_id=$_SESSION["a_uid"];
	if (check_permission($scuser_id,'userlist.php',4)){
		$listid = flc_request($_GET['listid']);
		$isCheck = intval($_GET['flag']);
		$sql = "update {$tablepre}scuser set isCheck='".$isCheck."' where ID in(".$listid.")";
		$db->exec($sql);
		$response_arr = array("status" => 1,"message"  => '操作成功');
	}else{
        $response_arr = array("status" => -1,"message"  => '对不起,您无权限操作!');
	}

    echo json_encode($response_arr);
    exit;
}

//重置密(要有模板修改权限,同时还要是管理员)
if ($_GET['listid']!='' && $_GET['ty']==2){
    $scuser_id=$_SESSION["a_uid"];
	if (check_permission($scuser_id,'userlist.php',2) && $_SESSION["a_type"]==1){
		$listid = flc_request($_GET['listid']);
		$reset_pwd = md5($reset_pwd);
		$sql = "update {$tablepre}scuser set Password='".$reset_pwd."' where ID='".$listid."'";
		$db->exec($sql);
		$response_arr = array("status" => 1,"message"  => '密码重置成功');
	}else{
        $response_arr = array("status" => -1,"message"  => '对不起,您无权限操作!');
	}

    echo json_encode($response_arr);
    exit;
}

if ($_GET['listid']!='' && $_GET['ty']==3){
    $scuser_id=$_SESSION["a_uid"];
	if (check_permission($scuser_id,'userlist.php',2)){
		$listid = flc_request($_GET['listid']);
		$AdminType = intval($_GET['flag']);
		$sql = "update {$tablepre}scuser set AdminType='".$AdminType."' where ID='".$listid."'";
		$db->exec($sql);
		$response_arr = array("status" => 1,"message"  => '设置成功');
	}else{
        $response_arr = array("status" => -1,"message"  => '对不起,您无权限操作!');
	}

    echo json_encode($response_arr);
    exit;
}

if ($_GET['listid']!='' && $_GET['ty']==3){
    $scuser_id=$_SESSION["a_uid"];
	if (check_permission($scuser_id,'userlist.php',2) && $_SESSION["a_type"]==1){
		$listid = flc_request($_GET['listid']);
		$reset_pwd = md5($reset_pwd);
		$sql = "update {$tablepre}scuser set isLock=0 where ID='".$listid."'";
		$db->exec($sql);
		$response_arr = array("status" => 1,"message"  => '解锁成功');
	}else{
        $response_arr = array("status" => -1,"message"  => '对不起,您无权限操作!');
	}

    echo json_encode($response_arr);
    exit;
}

$page=intval($_GET["page"]);
$group_id=intval($_GET["group_id"]);

if ($page==0) $page=1;
$keyword=flc_request($_POST["keyword"]);

if ($_POST["search-field"]!=""){
    $search_field=flc_request(trim($_POST["search-field"]));
}elseif ($_GET["search-field"]!=""){
    $search_field=flc_request(trim($_GET["search-field"]));
}
  
if ($search_field==''){
	 $where="";
}else{
	 $where=$search_field." like '%".$keyword."%' ";
}

if ($keyword!=""){
    $sql="select * from {$tablepre}scuser where ".$where." order by ID desc";
}else{
    $sql="select * from {$tablepre}scuser order by ID desc"; 
}

$p       = new htui_page;
$myrow   = $db->get_all($sql);
$numrows = count($myrow);

$p->setvar(array("search_field" => $search_field, "keyword" => $keyword)); 
$p->set($site_admin_num,$numrows,$psize); 

$ListArray = $db -> get_all($sql." limit ".$p->limit());
if(!empty($ListArray) && is_array($ListArray)){
   foreach($ListArray as $k => $v){

	  $str="select * from scuser_role sr inner join role r on r.role_id=sr.role_id where scuser_id='".$v['ID']."'"; 
	  
      $role_arr  = $db->get_all($str);
      $role_name='';
	  if(!empty($role_arr) && is_array($role_arr)){
         foreach($role_arr as $val){
            $role_name.=$val['role_name'].',';
         }
		 $ListArray[$k]['role_name'] = substr($role_name,0,strlen($role_name)-1);
      }else{
         $ListArray[$k]['role_name'] = "默认角色";
	  }
	  $ListArray[$k]['LastLoginTime']   = dateFormat($v['LastLoginTime'],'Y-m-d H:i');
   }
}
?>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8">
<meta id="scale" name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
<title>用户列表 - 飞天素材网</title>
<meta name="author" content="ftsucai.com">
<link href="../css/bootstrap.min.css" rel="stylesheet">
<link href="../css/fonts.css" rel="stylesheet">
<link href="../css/htstyle.css" rel="stylesheet">
<link href="../css/jquery-confirm.min.css" rel="stylesheet">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
  
<body>
    <!--页面主要内容-->
    <main class="ftdms-layout-content">
      
      <div class="container-fluid" style="margin-bottom:90px;">
        
        <div class="row" style="margin-top:15px;">
          <div class="col-lg-12">
            <div class="card">
              <div class="card-toolbar clearfix">
                <form class="pull-right search-bar" method="post" action="" role="form" id="search">
                  <div class="input-group">
                    <div class="input-group-btn">
                      <input type="hidden" name="search_field" id="search-field" value="UserName">
                      <button class="btn btn-default dropdown-toggle" id="search-btn" data-toggle="dropdown" type="button" aria-haspopup="true" aria-expanded="false">
                      帐号 <span class="caret"></span>
                      </button>
                      <ul class="dropdown-menu">
                        <li> <a tabindex="-1" href="javascript:void(0)" data-field="UserName">帐号</a> </li>
                        <li> <a tabindex="-1" href="javascript:void(0)" data-field="JFM">激活码</a> </li>
                      </ul>
                    </div>
                    <input type="text" class="form-control" value="" name="keyword" placeholder="请输入关键字">
                  </div>
                </form>
                <div class="toolbar-btn-action">
				  <!--primary-->
				  <!--<a id="post" class="btn btn-default" href="" data-id="link556" data-index="556"> 添加用户</a>-->
				  <a id="sh" class="btn btn-default submenuitem"> 批量审核</a>
				  <a id="del" class="btn btn-default submenuitem"> 批量删除</a>
                </div>
              </div>
              <div class="card-body">
                
                <div class="table-responsive">
                  <table class="table table-bordered">
                    <thead>
                      <tr>
                        <th width="5">
                          <label class="ftdms-checkbox checkbox-success">
                            <input type="checkbox" id="check-all"><span></span>
                          </label>
                        </th>
                        <th>登录帐号</th>
                        <th>激活码</th>
						<th>激活状态</th>
                        <th>所属角色</th>
						<th>积分</th>
                        <th>最后登录IP</th>
						<th>最后登录时间</th>
						<th>付费</th>
						<th>管理员</th>
                        <th>审核状态</th>
                        <th>操作</th>
                      </tr>
                    </thead>
                    <tbody>
					  <?php
					  foreach ($ListArray as $k=>$v){
                          $chkbox='否';$paymsg='否';$checked="";$flag=1;$jfstatus='未激活';$ATchecked='';$ATflag=1;
						  if ($v['isCheck']==1){
                             $checked ="checked"; //选中状态
							 $flag=0;
						  }

						  if ($v['UserType'] == 1){
                             $paymsg="是";
						  }

						  if ($v['jfmsign'] == 1){
                             $jfstatus="已激活";
						  }

						  if ($v['AdminType']==1){
                             $ATchecked ="checked"; //选中状态
							 $ATflag=0;
						  }
					  ?>
                      <tr>
                        <td>
                          <label class="ftdms-checkbox checkbox-success">
                            <input type="checkbox" name="ids" value="<?php echo $v['ID']?>" data_id="<?php echo $v['ID']?>"><span></span>
                          </label>
                        </td>
                        <td><?php echo $v['UserName']?></td>
                        <td><?php echo $v['JFM']?></td>
						<td><?php echo $jfstatus;?></td>
                        <td><?php echo $v['role_name'];?></td>
						<td><?php echo $v['Money'];?></td>
                        <td><?php echo $v['LastLoginIP']?></td>
						<td><?php echo $v['LastLoginTime']?></td>
						<td><?php echo $paymsg;?></td>
						<td>
						   <label class="ftdms-switch switch-solid switch-success">
							 <input type="checkbox" value="<?php echo $ATflag;?>" name="AdminType" <?php echo $ATchecked;?> onClick="chkbox(<?php echo $v['ID']?>,<?php echo $ATflag;?>,3);">
							 <span></span>
						   </label>
						</td>
                        <td>
						   <label class="ftdms-switch switch-solid switch-success">
							 <input type="checkbox" value="<?php echo $chkbox;?>" name="isCheck" <?php echo $checked;?> onClick="chkbox(<?php echo $v['ID']?>,<?php echo $flag?>,1);">
							 <span></span>
						   </label>
						</td>
                        <td>
                          <div class="btn-group">
							<a class="btn btn-xs btn-default submenuitem" title="解锁" data-toggle="tooltip" onclick="chkbox(<?php echo $v['ID']?>,<?php echo $flag?>,3);"><i class="ftsucai-53"></i></a>
							<a class="btn btn-xs btn-default submenuitem" title="重置密码" data-toggle="tooltip" onclick="chkbox(<?php echo $v['ID']?>,<?php echo $flag?>,2);"><i class="ftsucai-60"></i></a>
                            <a class="btn btn-xs btn-default submenuitem" href="edit_user.php?id=<?php echo $v['ID'];?>" target= "_self" title="修改资料" data-toggle="tooltip"><i class="ftsucai-edit-2"></i></a>
                            <a class="btn btn-xs btn-default submenuitem del" title="删除用户" data-toggle="tooltip" onclick="del_data(<?php echo $v['ID']?>)"><i class="ftsucai-del"></i></a>
                          </div>
                        </td>
                      </tr>
					  <?php
					  }
					  ?>
                    </tbody>
                  </table>
                </div>

				<nav>
                  <ul class="pagination no-border">
                    <?php echo $pager=$p->output(1);?>
                  </ul>
                </nav>
       
              </div>
            </div>
          </div>
          
        </div>
        
      </div>
      
    </main>
    <!--End 页面主要内容-->

<script type="text/javascript" src="../js/jquery.min.js"></script>
<script type="text/javascript" src="../js/bootstrap.js"></script>
<script type="text/javascript" src="../js/perfect-scrollbar.min.js"></script>
<script type="text/javascript" src="../js/main.min.js"></script>
<script type="text/javascript" src="../js/jquery-confirm.min.js"></script>
<script type="text/javascript" src="../js/bootstrap-notify.min.js"></script>
<script type="text/javascript" src="../js/tips.js"></script>

<script type="text/javascript">
$(function(){
    $('.search-bar .dropdown-menu a').click(function() {
        var field = $(this).data('field') || '';
        $('#search-field').val(field);
        $('#search-btn').html($(this).text() + ' <span class="caret"></span>');
    });
});

$(document).keyup(function(event){
   if(event.keyCode ==13){
      $("#search").trigger("click");
   }
});

$('#del').on('click', function(){
	//post字段
	var params =[]; var can_submit = true; var id='';
    $('input[name="ids"]:checked').each(function(){ 
    	//params.push($(this).val());
    	id += ','+this.value;
    });

    //alert(JSON.stringify(params));
    var listid=id.substr(1);
    if (listid==''){
        tips.notify('你还没有选择任何内容！', 'danger', 1000);
		return false;
	}else{
		$.alert({
	        title: '你确定要删除吗？',
	        content: '',
			buttons: {
				confirm: {
					text: '确认',
					btnClass: 'btn-primary',
					action: function(){
			        	tips.loading('show');
			    		$.post('userlist.php?listid='+listid, function(data) {
			                try {
			    				var ret = eval('('+data+')');
			    				if (ret.status == 1) {
			    					//alert(ret.msg);
			    					tips.loading('hide');
			    					tips.notify('操作成功，页面即将自动跳转~', 'success', 3000);
			    					window.location.reload();  //刷新当前页面
			    					//parent.location.reload();  //刷新父亲对象
			    				}else {
			    					tips.notify('操作失败！', 'danger', 1000);
			    				}
			    			} catch (e) {
			    				tips.notify('网络异常,操作失败！', 'danger', 1000);
			    			}
			    		});
					}
				},
				cancel: {
					text: '取消',
					action: function () {
	                    //$.alert('你点击了取消!');
					}
				}
			}
	    });

	}
	
});

$('#sh').on('click', function(){

	var id='';
    $('input[name="ids"]:checked').each(function(){ 
    	id += ','+this.value;
    });

    var listid=id.substr(1);
    if (listid==''){
        tips.notify('你还没有选择任何内容！', 'danger', 1000);
		return false;
	}else{

		$.post('userlist.php?sp=1&flag=1&listid='+listid, function(data) {
			try {
				var ret = eval('('+data+')');
				if (ret.status == 1) {
					//alert(ret.msg);
					tips.loading('hide');
					tips.notify(ret.message, 'success', 3000);
					window.location.reload();  //刷新当前页面
					//parent.location.reload();  //刷新父亲对象
				}else {
					tips.notify(ret.message, 'danger', 1000);
				}
			} catch (e) {
				tips.notify('网络异常,操作失败！', 'danger', 1000);
			}
		});

	}
	
});

function del_data(id) {

	$('input[data_id^=' + id + ']').attr("checked","true");

	$.alert({
		title: '你确定要删除吗？',
		content: '',
		buttons: {
			confirm: {
				text: '确认',
				btnClass: 'btn-primary',
				action: function(){
					tips.loading('show');
					$.post('userlist.php?listid='+id, function(data) {
						try {
							var ret = eval('('+data+')');
							if (ret.status == 1) {
								tips.loading('hide');
								//tips.notify('操作成功，页面即将自动跳转~', 'success', 3000);
								window.location.reload();  //刷新当前页面
								//parent.location.reload();  //刷新父亲对象
							}else {
								tips.loading('hide');
								tips.notify(ret.message, 'danger', 600);
							}
						} catch (e) {
							tips.loading('hide');
							tips.notify('网络异常,操作失败！', 'danger', 1000);
						}
					});
				}
			},
			cancel: {
				text: '取消',
				action: function () {
					//$.alert('你点击了取消!');
				}
			}
		}
	});
}

function chkbox(id,flag,ty) {
	var id;
	var flag;
	var ty;
	$.post('userlist.php?ty='+ty+'&listid='+id+'&flag='+flag, function(data) {
		try {
			var ret = eval('('+data+')');
			if (ret.status == 1) {
				if (ty==2){
					tips.notify(ret.message, 'success', 1000);
				}else{
				   window.location.reload();
				}
			}else {
				tips.notify(ret.message, 'danger', 1000);
			}
		} catch (e) {
			tips.notify('网络异常,操作失败！', 'danger', 1000);
		}
	});
}
</script>
</body>
</html>