<?php
include('header.php'); 
if (!empty($_GET['case'])) {
$case = make_safe($_GET['case']);	
} else {
$case = '';	
}
switch ($case) {
case 'delete';
$id = abs(intval(make_safe(xss_clean($_GET['id']))));
if (isset($_POST['delete'])) {
$sql = "SELECT * FROM users WHERE id='$id'";
$query = $mysqli->query($sql);
if ($query->num_rows > 0) {
$row = $query->fetch_assoc();
if (!empty($row['avatar']) AND file_exists('../upload/avatars/'.$row['avatar'])) {
@unlink('../upload/avatars/'.$row['avatar']);	
}
}
$mysqli->query("DELETE FROM lists WHERE user_id='$id'");
$mysqli->query("DELETE FROM lists_media WHERE user_id='$id'");
$mysqli->query("UPDATE media SET user_id='1' WHERE user_id='$id'");
$delete = $mysqli->query("DELETE FROM users WHERE id='$id'");
if ($delete) {
$message = notification('success','User Deleted Successfully.');
$done = true;
} else {
$message = notification('danger','Error Happened.');
}
}
?>
			<div class="page-header page-heading">
				<h1>Delete User
				<a href="users.php" class="btn btn-default pull-right"><span class="fa fa-arrow-right"></span></a>
				</h1>
			</div>
			<?php if (isset($message)) {echo $message;} ?>
		  <form role="form" method="POST" action="">
		  <?php if (empty($done)) { ?>
			<div class="alert alert-warning">Are You Sure you want to delete This User ? If you Choose to Delete you Can't Undo this Action Later.</div>
		  <?php } ?>
		  <?php if ($done) { ?>
		  <a href="users.php" class="btn btn-default">Back To Users</a>
		  <?php } else { ?>
		  <button type="submit" name="delete" class="btn btn-danger">Permanent Delete</button>
		  <?php } ?>
		</form>
<?php
break;
case 'search';
$q = make_safe(xss_clean($_GET['q']));
?>
<div class="page-header page-heading">
	<h1 class="row"><div class="col-md-6"><i class="fa fa-user"></i> Search For <?php echo $q; ?> In Users</div>
	<div class="col-md-6">
	<div class="pull-right search-form">
	<form method="GET" action="users.php">
		<div class="input-group">
		  <input type="hidden" name="case" value="search" />
		  <input type="text" name="q" value="<?php echo $q; ?>" class="form-control" placeholder="Search">
		  <span class="input-group-addon"><button class="btn-link"><span class="fa fa-search"></span></button></span>
		</div>
	</form>
	</div>
	<a href="users.php" class="btn btn-default pull-right" data-toggle="tooltip" data-placement="top" title="Active Users"><span class="fa fa-user"></span></a>
	</div>
	</h1>
</div>
<?php
if (isset($message)) {echo $message;}
$page = 1;
$size = 20;
if (isset($_GET['page'])){ $page = (int) $_GET['page']; }
$sqls = "SELECT * FROM users WHERE active='1' AND username LIKE '%$q%' ORDER BY id DESC";
$query = $mysqli->query($sqls);
$total_records = $query->num_rows;
if ($total_records == 0) {
echo notification('warning','There Are No Active Users.');
} else {
$pagination = new Pagination();
$pagination->setLink("?case=search&q=$q&page=%s");
$pagination->setPage($page);
$pagination->setSize($size);
$pagination->setTotalRecords($total_records);
$get = "SELECT * FROM users WHERE active='1' AND username LIKE '%$q%' ORDER BY id DESC ".$pagination->getLimitSql();
$qs = $mysqli->query($get);
?>
<form role="form" method="POST" action="">
<table class="table">
<thead>
<th width="15"><input type="checkbox" class="parentCheckBox" /></th>
<th>Username</th>
<th>Provider</th>
<th>Register Date</th>
<th>Last Login</th>
<th></th>
</thead>
<tbody>
<?php 
while ($row = $qs->fetch_assoc()) {
?>
<tr>
<td><input type="checkbox" name="id[]" class="childCheckBox" value="<?php echo $row['id']; ?>" /></td>
<td><?php echo $row['username']; ?></td>
<td><?php echo $row['provider']; ?></td>
<td><?php echo date('Y-n-j',$row['datetime']); ?></td>
<td><?php echo date('Y-n-j h:i a',$row['last_login']); ?></td>
<td align="right">
<a class="btn btn-danger btn-xs" href="users.php?case=delete&id=<?php echo $row['id']; ?>" data-toggle="tooltip" data-placement="top" title="Delete"><span class="fa fa-close"></span></a>
</td>
</tr>
<?php
}
?>
<tbody>
</table>
<div class="users-actions">
<div class="row">
<div class="col-xs-12"><?php echo $pagination->create_links(); ?></div>
</div>
</div>
</form>
<?php
}
break;
case 'not_active';
if (isset($_POST['activate']) AND isset($_POST['id'])) {
	$ids = $_POST['id'];
	$count= count($ids);
	for($i=0;$i<$count;$i++){
	$delete = $mysqli->query("UPDATE users SET active='1' WHERE id='$del_id'");
	if ($delete) {
	$message = notification('success','The Selected Users Were Activated Successfully.');
	} else {
	$message = notification('error','Error Happened');
	}
	}
}

if (isset($_POST['delete']) AND isset($_POST['id'])) {
	$ids = $_POST['id'];
	$count= count($ids);
	for($i=0;$i<$count;$i++){
	$mysqli->query("DELETE FROM lists WHERE user_id='$del_id'");
	$mysqli->query("DELETE FROM lists_media WHERE user_id='$del_id'");
	$mysqli->query("UPDATE media SET user_id='1' WHERE user_id='$del_id'");
	$delete = $mysqli->query("DELETE FROM users WHERE id='$del_id'");
	if ($delete) {
	$message = notification('success','The Selected Users Was Deleted Permanently.');
	} else {
	$message = notification('error','Error Happened');
	}
	}
}
?>
<div class="page-header page-heading">
	<h1 class="row"><div class="col-md-6"><i class="fa fa-user"></i> Not Active Users</div>
	<div class="col-md-6">
	<div class="pull-right search-form">
	<form method="GET" action="users.php">
		<div class="input-group">
		  <input type="hidden" name="case" value="search" />
		  <input type="text" name="q" class="form-control" placeholder="Search">
		  <span class="input-group-addon"><button class="btn-link"><span class="fa fa-search"></span></button></span>
		</div>
	</form>
	</div>
	<a href="users.php" class="btn btn-default pull-right" data-toggle="tooltip" data-placement="top" title="Active Users"><span class="fa fa-user"></span></a>
	</div>
	</h1>
</div>
<?php
if (isset($message)) {echo $message;}
$page = 1;
$size = 20;
if (isset($_GET['page'])){ $page = (int) $_GET['page']; }
$sqls = "SELECT * FROM users WHERE active='0' ORDER BY id DESC";
$query = $mysqli->query($sqls);
$total_records = $query->num_rows;
if ($total_records == 0) {
echo notification('warning','There Are No Active Users.');
} else {
$pagination = new Pagination();
$pagination->setLink("?page=%s");
$pagination->setPage($page);
$pagination->setSize($size);
$pagination->setTotalRecords($total_records);
$get = "SELECT * FROM users WHERE active='0' ORDER BY id DESC ".$pagination->getLimitSql();
$q = $mysqli->query($get);
?>
<form role="form" method="POST" action="">
<table class="table">
<thead>
<th width="15"><input type="checkbox" class="parentCheckBox" /></th>
<th>Username</th>
<th>Provider</th>
<th>Register Date</th>
<th>Last Login</th>
<th></th>
</thead>
<tbody>
<?php 
while ($row = $q->fetch_assoc()) {
?>
<tr>
<td><input type="checkbox" name="id[]" class="childCheckBox" value="<?php echo $row['id']; ?>" /></td>
<td><?php echo $row['username']; ?></td>
<td><?php echo $row['provider']; ?></td>
<td><?php echo date('Y-n-j',$row['datetime']); ?></td>
<td><?php echo date('Y-n-j h:i a',$row['last_login']); ?></td>
<td align="right">
<a class="btn btn-danger btn-xs" href="users.php?case=delete&id=<?php echo $row['id']; ?>" data-toggle="tooltip" data-placement="top" title="Delete"><span class="fa fa-close"></span></a>
</td>
</tr>
<?php
}
?>
<tbody>
</table>
<div class="users-actions">
<div class="row">
<div class="col-sm-2 col-md-4">
<button type="submit" name="activate" class="btn btn-success"><span class="fa fa-check"></span> Activate</button>
<button type="submit" name="delete" class="btn btn-danger"><span class="fa fa-trash"></span> Delete</button></div>
<div class="col-sm-10 col-md-8"><?php echo $pagination->create_links(); ?></div>
</div>
</div>
</form>
<?php
}
break;
default;
if (isset($_POST['delete']) AND isset($_POST['id'])) {
	$ids = $_POST['id'];
	$count= count($ids);
	for($i=0;$i<$count;$i++){
	$mysqli->query("DELETE FROM lists WHERE user_id='$del_id'");
	$mysqli->query("DELETE FROM lists_media WHERE user_id='$del_id'");
	$mysqli->query("UPDATE media SET user_id='1' WHERE user_id='$del_id'");
	$delete = $mysqli->query("DELETE FROM users WHERE id='$del_id'");
	if ($delete) {
	$message = notification('success','The Selected Users Was Deleted Permanently.');
	} else {
	$message = notification('error','Error Happened');
	}
	}
}
?>
<div class="page-header page-heading">
	<h1 class="row"><div class="col-md-6"><i class="fa fa-user"></i> Active Users</div>
	<div class="col-md-6">
	<div class="pull-right search-form">
	<form method="GET" action="users.php">
		<div class="input-group">
		  <input type="hidden" name="case" value="search" />
		  <input type="text" name="q" class="form-control" placeholder="Search">
		  <span class="input-group-addon"><button class="btn-link"><span class="fa fa-search"></span></button></span>
		</div>
	</form>
	</div>
	<a href="users.php?case=not_active" class="btn btn-default pull-right" data-toggle="tooltip" data-placement="top" title="Not Active Users"><span class="fa fa-user"></span></a>
	</div>
	</h1>
</div>
<?php
if (isset($message)) {echo $message;}
$page = 1;
$size = 20;
if (isset($_GET['page'])){ $page = (int) $_GET['page']; }
$sqls = "SELECT * FROM users WHERE active='1' ORDER BY id DESC";
$query = $mysqli->query($sqls);
$total_records = $query->num_rows;
if ($total_records == 0) {
echo notification('warning','There Are No Active Users.');
} else {
$pagination = new Pagination();
$pagination->setLink("?page=%s");
$pagination->setPage($page);
$pagination->setSize($size);
$pagination->setTotalRecords($total_records);
$get = "SELECT * FROM users WHERE active='1' ORDER BY id DESC ".$pagination->getLimitSql();
$q = $mysqli->query($get);
?>
<form role="form" method="POST" action="">
<table class="table">
<thead>
<th width="15"><input type="checkbox" class="parentCheckBox" /></th>
<th>Username</th>
<th>Media</th>
<th>Provider</th>
<th>Register Date</th>
<th>Last Login</th>
<th></th>
</thead>
<tbody>
<?php 
while ($row = $q->fetch_assoc()) {
?>
<tr>
<td><input type="checkbox" name="id[]" class="childCheckBox" value="<?php echo $row['id']; ?>" /></td>
<td><?php echo $row['username']; ?></td>
<td><?php echo media_by_user($row['id']); ?></td>
<td><?php echo $row['provider']; ?></td>
<td><?php echo date('Y-n-j',$row['datetime']); ?></td>
<td><?php echo date('Y-n-j h:i a',$row['last_login']); ?></td>
<td align="right">
<a class="btn btn-danger btn-xs" href="users.php?case=delete&id=<?php echo $row['id']; ?>" data-toggle="tooltip" data-placement="top" title="Delete"><span class="fa fa-close"></span></a>
</td>
</tr>
<?php
}
?>
<tbody>
</table>
<div class="users-actions">
<div class="row">
<div class="col-sm-2 col-md-3"><button type="submit" name="delete" class="btn btn-danger"><span class="fa fa-trash"></span> Delete</button></div>
<div class="col-sm-10 col-md-9"><?php echo $pagination->create_links(); ?></div>
</div>
</div>
</form>
<?php
} 
} 
include('footer.php');
?>