<?php 

	$xuid	= $_GET['xuid'];
	$lang 	= $_GET['lang'];	
	$xfdc 	= $_GET['xfdc'];
	
	// CHECK FOR FORMS NOT HAVING xfdc on url
	$nfid = 1;

		include('config.php');
		include('field_names.php'); 
		include('users.php');	


include_once('nf_database.php');
include_once('nf_paginator.class.php');
define('SS_DB_NAME', $data_db);
define('SS_DB_USER', $data_user);
define('SS_DB_PASSWORD', $data_pswd);
define('SS_DB_HOST', $data_server);

$dsn	= 	"mysql:dbname=".SS_DB_NAME.";host=".SS_DB_HOST."";
$pdo	=	"";
try {
	$pdo = new PDO($dsn, SS_DB_USER, SS_DB_PASSWORD);
}catch (PDOException $e) {
	echo "Connection failed: " . $e->getMessage();
}
$db 	=	new Database($pdo);
$pages 	=   new Paginator();

// Change character set to utf8
$pdo->query("SET NAMES 'utf8'");

$gcrud = 'CRUD';

?>

<!doctype html>
<html lang="en-US" class="no-js">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
	<title><?php echo $nusers."-".$nprojects ?></title>
	
    <link rel="stylesheet" href="./bootstrap/css/bootstrap.css" >
    <link rel="stylesheet" href="./fontawesome/css/all.css" >

<?php include('nf_chosen01.php'); ?>

<style>
body {
  background-color: #CCFFFF;
}
</style>
	
</head>

<body>
	
	
	<?php
	$condition	=	'';
	
	if(isset($_REQUEST['username']) and $_REQUEST['username']!=""){
		$condition	.=	' AND username LIKE "%'.$_REQUEST['username'].'%" ';
	}
	if(isset($_REQUEST['user_role']) and $_REQUEST['user_role']!=""){
		$condition	.=	' AND user_role LIKE "%'.$_REQUEST['user_role'].'%" ';
	}
	if(isset($_REQUEST['pids']) and $_REQUEST['pids']!=""){
		$condition	.=	' AND pids = "'.$_REQUEST['pids'].'" ';
	}
	// CHECK FOR SUPER ADMINISTRATOR
	if ($admins_check != 1) {
		$condition = $condition." and pids in (select distinct pids from members_projects where username = '".$xuid."') and username not in (select username from members_det where admins = 1)";
	}


	echo $usrole;
	//Main queries
    $pages->default_ipp  =   10; //total records show on per page
    $sql    = $db->getRecFrmQry("SELECT * FROM members_projects WHERE 1 ".$condition."");
    $pages->items_total  =   count($sql);
    $pages->mid_range    =   9;
    $pages->paginate(); 
      
    $userData   =   $db->getRecFrmQry("SELECT * FROM members_projects WHERE 1 ".$condition." ORDER BY pids,username ".$pages->limit."");	
	
	
		
	?>
   	<div class="container">
		
		<div class="card">
		
			<div class="card-header"><i class="fa fa-fw fa-globe"></i> <strong><?php echo $nusers."-".$nprojects ?></strong> 
					<a href="nf_users_projects_rep.php?xuid=<?php echo $xuid ?>&lang=<?php echo $lang ?>" class="float-end btn btn-dark btn-sm" target=_blank ><i class="far fa-file-alt"></i> <?php echo $nreport  ?></a>
			</div>
			<div class="card-header">
					<?php 
					if ($gcrud == "CRUD") { ?>		
					<a href="nf_users_projects_add.php?xuid=<?php echo $xuid ?>&lang=<?php echo $lang ?>" class="float-end btn btn-dark btn-sm"><i class="fa fa-fw fa-plus-circle"></i> <?php echo $nadd  ?></a>
					<?php } ?>
			</div>
			<div class="card-body">
				<?php
				if(isset($_REQUEST['msg']) and $_REQUEST['msg']=="rds"){
					echo	'<div class="alert alert-success"><i class="fa fa-thumbs-up"></i>'.$recdel.'</div>';
				}elseif(isset($_REQUEST['msg']) and $_REQUEST['msg']=="rus"){
					echo	'<div class="alert alert-success"><i class="fa fa-thumbs-up"></i>'.$recupd.'</div>';
				}elseif(isset($_REQUEST['msg']) and $_REQUEST['msg']=="rnu"){
					echo	'<div class="alert alert-warning"><i class="fa fa-exclamation-triangle"></i>'.$recnochange.'</div>';
				}elseif(isset($_REQUEST['msg']) and $_REQUEST['msg']=="rna"){
					echo	'<div class="alert alert-danger"><i class="fa fa-exclamation-triangle"></i>'.$recwrong.' <strong>'.$rectry.'</strong></div>';
				}
				?>
				<div class="col-sm-12">
					<h5 class="card-title"><i class="fa fa-fw fa-search"></i><?php echo $nfind ?></h5>
					<form method="get"  >
						<div class="row">
							<div class="col-sm-3">
								<div class="mb-3">
									<label><?php echo $n_username ?></label>

									<?php
									echo "<select name='username' id='username' class='form-control' > ";
									printf ("<option value = >Enter $n_username</option>");
									include('r_dbc.php');
									if ($admins_check != 1) { $mqry = "and m.username in (select username from members_projects where pids in (select pids from members_projects where username = '".$xuid."'))"; }
									if ($admins_check == 1) { $mqry = ""; }
									
											$sql = "select m.username, d.user_description
											from members m, members_det d
											where d.username = m.username
											".$mqry."
											order by 1";
									$result = mysqli_query($conn, $sql);
									if (mysqli_num_rows($result) > 0) {
										  while($row = mysqli_fetch_assoc($result)) {
											printf ("<option value = %s > %s - %s</option>", $row["username"],$row["username"],$row["user_description"]);
											}
										}
									mysqli_close($conn);
									echo "</select>";
									?>

								</div>
							</div>
							<div class="col-sm-3">
								<div class="mb-3">
									<label><?php echo $nproject ?></label>
									
									<?php
									echo "<select name='pids' id='pids' class='form-control' > ";
									printf ("<option value = >Enter $nproject</option>");
									include('r_dbc.php');
									if ($admins_check != 1) { $pqry = "and pid in (select distinct pids from members_projects where username = '".$xuid."')"; }
									if ($admins_check == 1) { $pqry = ""; }									
											$sql = "select pid,desc_project
											from ccprojects
											where 1 
											".$pqry."
											order by 1";
									$result = mysqli_query($conn, $sql);
									if (mysqli_num_rows($result) > 0) {
										  while($row = mysqli_fetch_assoc($result)) {
											printf ("<option value = %s > %s - %s </option>", $row["pid"], $row["pid"], $row["desc_project"]);
											}
										}
									mysqli_close($conn);
									echo "</select>";
									?>									
									
								</div>
							</div>														
							<div class="col-sm-3">
								<div class="mb-3">
									<label><?php echo $n_user_role ?></label>
									
									<?php
									echo "<select name='user_role' id='user_role' class='form-control' > ";
									printf ("<option value = >Enter $n_user_role</option>");
									include('r_dbc.php');
											$sql = "select user_role,role_description
											from members_roles
											order by role_power";
									$result = mysqli_query($conn, $sql);
									if (mysqli_num_rows($result) > 0) {
										  while($row = mysqli_fetch_assoc($result)) {
											printf ("<option value = %s > %s - %s </option>", $row["user_role"],$row["user_role"],$row["role_description"]);
											}
										}
									mysqli_close($conn);
									echo "</select>";
									?>									
									
								</div>
							</div>							

							
							<input type="hidden" id="xuid" name="xuid" value="<?php echo $xuid ?>" >
							<input type="hidden" id="lang" name="lang" value="<?php echo $lang ?>" >

							<div class="col-sm-3">
								<div class="mb-3">
									<label>&nbsp;</label>
									<div>
										<button type="submit" name="submit" value="search" id="submit" class="btn btn-primary"><i class="fa fa-fw fa-search"></i><?php echo $nsearch ?></button>
										<a href="<?php echo $_SERVER['PHP_SELF']; ?>?xuid=<?php echo $xuid ?>&lang=<?php echo $lang ?>" class="btn btn-danger"><i class="fa fa-fw fa-sync"></i> <?php echo $nclear ?></a>
									</div>
								</div>
							</div>
						</div>
					</form>
				</div>
			</div>
		</div>
		<hr>



		<div class="clearfix"></div>
     
		<div class="row marginTop">
			<div class="col-sm-12 paddingLeft pagerfwt">
				<?php if($pages->items_total > 0) { ?>
					<?php echo $pages->display_pages();?>
					<?php echo $pages->display_items_per_page();?>
					<?php echo $pages->display_jump_menu(); ?>
				<?php }?>
			</div>
			<div class="clearfix"></div>
		</div>
	 
		<div class="clearfix"></div>




		
		<div>
			<table class="table table-striped table-bordered">
				<thead>
					<tr class="bg-primary text-white">
						<th><?php echo $n_username ?></th>
						<th><?php echo $nproject ?></th>						
						<th><?php echo $n_user_role ?></th>
						<th class="text-center"><?php echo $naction ?></th>
					</tr>
				</thead>
				<tbody>
					<?php 
					$s	=	'';
					foreach($userData as $val){
						$s++;
					?>
<?php 						

$usernamex = $val['username'];

include('r_dbc.php');
 		$sql = "select username,email as usermail
		from members
		where username = '".$usernamex."'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
      while($row = mysqli_fetch_assoc($result)) {
		$usernameg = $row["username"];
		$usermailg = $row["usermail"];
		}
	}
mysqli_close($conn);		
		
?>

<?php 						

$pidsx = $val['pids'];

include('r_dbc.php');
 		$sql = "select desc_project
		from ccprojects
		where pid = '".$pidsx."'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
      while($row = mysqli_fetch_assoc($result)) {
		$desc_projectg = $row["desc_project"];
		}
	}
mysqli_close($conn);		
		
?>

<?php 						

$user_rolex = $val['user_role'];

include('r_dbc.php');
 		$sql = "select role_description
		from members_roles
		where user_role = '".$user_rolex."'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
      while($row = mysqli_fetch_assoc($result)) {
		$role_descriptiong = $row["role_description"];
		}
	}
mysqli_close($conn);		
		
?>


					<tr>
						<td><?php echo $val['username']." - ".$usermailg; ?></td>
						<td><?php echo $val['pids']." - ".$desc_projectg; ?></td>						
						<td><?php printf("<img src='user_".$user_rolex.".png' height=auto width=25 /> $role_descriptiong "); ?></td>
						<td align="center">

						<?php 
						if ($gcrud == "CRUD") { ?>						
						<a href="nf_users_projects_delete.php?delId=<?php echo $val['cid']; ?>&xuid=<?php echo $xuid ?>&lang=<?php echo $lang ?>" class="text-danger" onClick="return confirm('<?php echo $areyousure ?>');"><i class="fa fa-fw fa-trash"></i> <?php echo $ndelete ?></a>
						<?php } ?>
						</td>
					</tr>
					<?php } ?>
				</tbody>
			</table>
		</div> <!--/.col-sm-12-->
		
		
		<div class="clearfix"></div>
     
		<div class="row marginTop">
			<div class="col-sm-12 paddingLeft pagerfwt">
				<?php if($pages->items_total > 0) { ?>
					<?php echo $pages->display_pages();?>
					<?php echo $pages->display_items_per_page();?>
					<?php echo $pages->display_jump_menu(); ?>
				<?php }?>
			</div>
			<div class="clearfix"></div>
		</div>
	 
		<div class="clearfix"></div>		
		
	</div>
	
    <script src="./jquery-3.4.1.js"></script>
    <script src="popper.min.js" ></script>
    <script src="./bootstrap/js/bootstrap.min.js" ></script>
    
</body>
</html>
