<?php

	require_once 'processes/config.php';
	require_once 'processes/dbh.inc.php';
	require_once 'processes/checkLogin.php';
	
	
	// IF USER IS DOESN'T HAVE ADMINISTRATOR PERMISIONS, EXIT
	if($_SESSION['dbUserData']['admin'] !== '1') {
		echo "You are unauthorized to view this page.";
		exit();
	}
	
	include_once 'header.php';

?>

	<!--START OF users.php -->
	<body>
	
    	<header id="main-header">
        	<a id="logo" href=".">
				TANIA
            	<img src="./img/pen_800x800.png" alt="tania logo pen" width="25" height="25">
        	</a>
        	<div id="account">
            	<a class="nav-link" href="#" onclick="location='home.php'">Go Back</a>
            	<a class="sign-out" href="./processes/logout.php">Sign Out</a>
        	</div>
    	</header>
        
        <div class="container" style="margin-top: 8rem; min-height: calc(100vh - 8rem - 8rem);">
        		
        	<br>
        		
        	<div class="row">
        		<div class="col-sm-12">
        			<h2 class="text-muted"><?php echo $_SESSION['dbUserData']['name']; ?></h2>
<!--     	    		<p>Add new users, view their info and manage permissions!</p> -->
        	    </div>
        	</div>
        	
        	<hr>
        	
        	<div class="row">
        		<div class="col-sm-12">
        			<h3>Add Researcher</h3>
        		</div>
        	</div>
        	
        	<form id="addUser" class="form-horizontal" method="POST" action="processes/addUser.php" onsubmit="addUser(event)">
        	
        		<div class="form-group">
        			<label class="col-sm-2 control-label" for="exampleInputEmail1">Name</label>
        			<div class="col-sm-10">
						<input type="text" name="name" class="form-control text-center" placeholder="i.e. Juan del Pueblo Villa" required autocomplete="off">
					</div>
				</div>
        		
        		<div class="form-group">
	        		<label class="col-sm-2 control-label for="exampleInputEmail1">Email</label>
	        		<div class="col-sm-10">
						<input type="email" name="email" class="form-control text-center" placeholder="i.e. juan.delpueblo@upr.edu" required autocomplete="off">
					</div>
				</div>
				
				<div class="row">
				<div class="col-sm-4 col-sm-offset-5">
					<button type="submit" name="addUser" class="btn btn-primary btn-block">Add User</button>
				</div>
				</div>
        		
        	</form>
        	
        	<br>
        	
        	<!-- ERROR ALERT FOR USER -->			
			<div id="error-addUser" class="row" style="display: none;">
				<div class="col-sm-10 col-sm-offset-2">
					<div class="alert alert-danger mb-1" role="alert">
						<h4 class="error-lead">Error!</h4>
						<p class="error-description"></p>
					</div>
				</div>
			</div>
        	
        	
        	<br>
        
        	<div class="row">
        		
        		<div class="col-sm-12">
	        		<h3>Researchers</h3>
        		</div>
        	
				
				<div class="col-sm-12">
					<table class="table table-hover table-striped table-responsive">
  						<thead>
  							<tr>
  								<th style="text-align: center;"><h4>Name</h4></th>
  								<th style="text-align: center;"><h4>Email</h4></th>
  								<th style="text-align: center;"><h4>Manage</h4></th>
  							</tr>
  						</thead>
  						<tbody>
  							<?php 
  								$queryResearchers = "SELECT * FROM researcher;";
								$resultResearchers = mysqli_query($connection, $queryResearchers);
													
  								while($rowResearchers = mysqli_fetch_assoc($resultResearchers)):
  							?>
  							<tr>
  								<td style="text-align: center;"><h5><?php echo $rowResearchers['name']; ?></h5></td>
  								<td style="text-align: center;"><h5><a class="email" href="mailto:<?php echo $rowResearchers['email']; ?>"><?php echo $rowResearchers['email']; ?></a></h5></td>
  								<td style="text-align: center;">
  									<?php if($rowResearchers['admin'] === '1'): ?>
  									<h5><sm class="text-muted">Already manager...</sm></h5>
  									<?php else: ?>
  									<button class="btn btn-sm btn-default" data-researcher="<?php echo $rowResearchers['id_researcher']; ?>" onclick="makeManager(event)">Make Manager</button>
  									<?php endif; ?>
  								</td>
  							</tr>
  							<?php endwhile; ?>
						</tbody>
  					</table>
  				</div><!--col-->
	
        	</div><!--row-->
        	
        	<br><br><br>
        			
        </div><!--container-->
        			
        			
        			
        			
        	<style>
        	.email {
        		color: #333;
        		transition: color 300ms ease;
        	}
        	
        	.email:hover {
        		color: #999;
        	}
        	</style>
    
    
    
        	
			<script src="js/handleSubmit.js"></script>
        	
        	<script>
        	
        	
			["addUser"].forEach(function(formName) {
			
				var form = document.getElementById(formName);
			
				if(form) {
					form.addEventListener('submit', function(e) {
						handleSubmit(e, formName);
					});
				}
			
			});



        	
        	function foo(e) {
        		e.preventDefault();
        		console.log(e.srcElement);
        		return false;
        	}
        	
        	
        	function makeManager(e) {
        	
        		let button = e.currentTarget;
        		let researcherID = button.getAttribute('data-researcher');
        		
        		// Create loader
        		let loader = document.createElement('div');
        		loader.style.display = "flex";
        		loader.style.alignItems = "center";
        		loader.style.justifyContent = "center";
        		loader.style.height = "35px";
        		let span = document.createElement('span');
        		span.classList.add('loader');
        		loader.appendChild(span);
        		
        		// Insert loader and remove button
        		button.insertAdjacentElement('afterend', loader);
        		button.remove();

        		let URL = document.location.protocol + "//tania.uprrp.edu/admin_nuevo/processes/makeManager.php";
        		let fields = {
        			researcherID: researcherID
        		};
        		
        		$.post(URL, fields)
        			.done(function(data, text) {
        			
        				// Create Done!
        				let h5 = document.createElement('h5');
        				let sm = document.createElement('sm');
        				sm.classList.add('text-success');
        				sm.innerText = "Done!";
        				h5.appendChild(sm);
        			
        				// Insert Done! and remove loader
        				loader.insertAdjacentElement('afterend', h5);
						loader.remove();
        				
        			})
        			.fail(function(request, status, error) {
        			
        				// Create Retry
        				let retry = document.createElement('button');
        				retry.className = "btn btn-sm btn-primary";
        				retry.setAttribute('data-researcher', researcherID);
        				retry.setAttribute('onclick', 'makeManager(event)');
        				retry.innerText = "Retry";
        			
        				// Insert Retry and remove loader
        				loader.insertAdjacentElement('afterend', retry);
        				loader.remove();
        				
        			});
        		
        	}
        	
        	</script>
        	
        		
	<!-- END OF users.php -->

<?php include_once 'footer.php'; ?>