<?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';

?>

<?php 
if(isset($_REQUEST['submit']) and $_REQUEST['submit']!=""){
	extract($_REQUEST);
	if($username==""){
		header('location:'.$_SERVER['PHP_SELF'].'?msg=un');
		exit;
	}
	elseif($user_role==""){
		header('location:'.$_SERVER['PHP_SELF'].'?msg=ue');
		exit;
	}
	elseif($pids==""){
		header('location:'.$_SERVER['PHP_SELF'].'?msg=ue');
		exit;		
	}
	else{
		
		$userCount	=	$db->getQueryCount('members_det','cid');
		if($userCount[0]['total']<2000000){
			$data	=	array(
							'username'=>$username,
							'pidS'=>$pids,
							'user_role'=>$user_role
						);
			$insert	=	$db->insert('members_projects',$data);
			if($insert){
				header('location:nf_users_projects.php?msg=ras&xuid='.$xuid.'&lang='.$lang);
				exit;
			}else{
				header('location:nf_users_projects.php?msg=rna&xuid='.$xuid.'&lang='.$lang);
				exit;
			}
		}else{
			header('location:'.$_SERVER['PHP_SELF'].'?msg=dsd&xuid='.$xuid.'&lang='.$lang);
			exit;
		}
	}
}
?>

<!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 ?></title>

	

    <link rel="stylesheet" href="./bootstrap/css/bootstrap.css" >
    <link rel="stylesheet" href="./fontawesome/css/all.css" >
	

</head>



<body>

	

  	<div class="container">


		<?php

		if(isset($_REQUEST['msg']) and $_REQUEST['msg']=="un"){

			echo	'<div class="alert alert-danger"><i class="fa fa-exclamation-triangle"></i> $field_mand </div>';

		}elseif(isset($_REQUEST['msg']) and $_REQUEST['msg']=="ue"){

			echo	'<div class="alert alert-danger"><i class="fa fa-exclamation-triangle"></i> $field_mand </div>';

		}elseif(isset($_REQUEST['msg']) and $_REQUEST['msg']=="up"){

			echo	'<div class="alert alert-danger"><i class="fa fa-exclamation-triangle"></i> $field_mand </div>';

		}elseif(isset($_REQUEST['msg']) and $_REQUEST['msg']=="ras"){

			echo	'<div class="alert alert-success"><i class="fa fa-thumbs-up"></i> $recadded </div>';

		}elseif(isset($_REQUEST['msg']) and $_REQUEST['msg']=="rna"){

			echo	'<div class="alert alert-danger"><i class="fa fa-exclamation-triangle"></i> $recaddedno <strong> $tryagain </strong></div>';

		}elseif(isset($_REQUEST['msg']) and $_REQUEST['msg']=="dsd"){

			echo	'<div class="alert alert-danger"><i class="fa fa-exclamation-triangle"></i> Please delete a user and then try again <strong>We set limit for security reasons!</strong></div>';

		}

		?>

		<div class="card">

			<div class="card-header"><i class="fa fa-fw fa-plus-circle"></i> <strong><?php echo $nadd." ".$nuser;  ?></strong> <a href="nf_users_projects.php?&xuid=<?php echo $xuid ?>&lang=<?php echo $lang ?>" class="float-end btn btn-dark btn-sm"><i class="fa fa-fw fa-globe"></i> <?php echo $nbrowse." ".$nusers;  ?></a></div>

			<div class="card-body">

				

				<div class="col-sm-6">

					<h6 class="card-title"><?php echo $nfields_with ?> <span class="text-danger">*</span> <?php echo $nmandatory ?></h6>

					<form method="post">
					
						<div class="mb-3">

							<label><?php echo $n_username ?><span class="text-danger">*</span></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 d.admins = 0 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 class="mb-3">

							<label><?php echo $nproject ?><span class="text-danger">*</span></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 class="mb-3">

							<label><?php echo $n_user_role ?><span class="text-danger">*</span></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');
									if ($admins_check != 1) { $rlqry = "and role_power > 1 "; }
									if ($admins_check == 1) { $rlqry = ""; }																		
											$sql = "select user_role,role_description
											from members_roles
											where 1
											".$rlqry."
											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>

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

						<div>
							<button type="submit" name="submit" value="submit" id="submit" class="btn btn-primary"><i class="fa fa-fw fa-plus-circle"></i><?php echo $nadd." ".$nuser ?></button>

						</div>

					</form>

				</div>

			</div>

		</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>