<?php  
 $connect = mysqli_connect("localhost", "root", "admin", "phssb_rpms_ppr5");  
 session_start();  
 if(isset($_SESSION["username"]))  
 {  
      header("location:entry.php");  
 }  
 /* if(isset($_POST["register"]))  
 {  
      if(empty($_POST["username"]) || empty($_POST["password"]))  
      {  
           echo '<script>alert("Both Fields are required")</script>';  
      }  
      else  
      {  
           $username = mysqli_real_escape_string($connect, $_POST["username"]);  
           $password = mysqli_real_escape_string($connect, $_POST["password"]);  
           $password = password_hash($password, PASSWORD_DEFAULT);  
           echo $query = "INSERT INTO users(username, password) VALUES('$username', '$password')";  
           if(mysqli_query($connect, $query))  
           {  
                echo '<script>alert("Registration Done")</script>';  
           }  
      }  
 } */  
 if(isset($_POST["login"]))  
 {  
      if(empty($_POST["username"]) || empty($_POST["password"]))  
      {  
           echo '<script>alert("Both Fields are required login")</script>';  
      }  
      else  
      {  
           $username = mysqli_real_escape_string($connect, $_POST["username"]);  
           $password = mysqli_real_escape_string($connect, $_POST['password']);  
           $query = "SELECT * FROM users WHERE username = '$username'";  
           $result = mysqli_query($connect, $query);  
           if(mysqli_num_rows($result) > 0)  
           {  
                while($row = mysqli_fetch_array($result))  
                {  
                     if(password_verify($password, $row['password']))  
                     {  
                          //return true;  
                          $_SESSION["username"] = $username;  
                          header("location:entry.php");  
                     }  
                     else  
                     {  
                          //return false;  
                          echo '<script>alert("Wrong User Details1")</script>';  
                     }  
                }  
           }  
           else  
           {  
                echo '<script>alert("Wrong User Details2")</script>';  
           }  
      }  
 }  
 ?>  
 <!DOCTYPE html>  
 <html>  
      <head>  
           <title>Webslesson Tutorial | PHP Login Registration Script by using password_hash() method</title>  
           <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>  
           <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
           <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
      </head>  
      <body>  
           <br /><br />  
           <div class="container" style="width:500px;">  
                <h3 align="center">PHP Login Registration Script by using password_hash() method <?php echo $_GET["action"]?></h3>  
                <br />  
                <?php  
                if(isset($_GET["action"]) == "update")  
                {  
					
			
                ?>  
                <h3 align="center">Update</h3>  
                <br />  
				<p align="center"><a href="index.php">Login</a></p>  
				<table  class="table table-borderless table-striped table-earning">
					<thead>
						<tr>
							<th>#</th>
							<th>Name</th>
							<th>email</th>
							<th>Status</th>
							<th>Username</th>
							<th>Action</th>
							<th>Projects</th>
							<th>Locations</th>
							
						</tr>
					</thead>
					<tbody>
					<?php 
					$sqlUsers = "select users.* from users";
					$runUsers = mysqli_query($connect, $sqlUsers);
					if($runUsers === FALSE){
						die(mysqli_error($connect));
					}
					
					$no = 1;
					
					while($resultUsers = mysqli_fetch_array($runUsers)){ 
						$noResultLocation = 1;
						
						$staff_id = $resultUsers["id"];
						$sqlStaffLocationInfo = "select extra_locations.user_id, extra_locations.location_id, locations.location_name, projects.project_name from  
										extra_locations 
										inner join locations on locations.id = extra_locations.location_id 
										inner join users on extra_locations.user_id =  users.id 
										inner join projects on locations.project_id = projects.id where users.id = $staff_id";
						$runStaffLocationInfo = mysqli_query($connect, $sqlStaffLocationInfo);
						if($runStaffLocationInfo === FALSE){
							die(mysqli_error($connect));
						}
						$countResultStaffLocationInfo = mysqli_num_rows($runStaffLocationInfo);
						
						
					?>
					<tr>
							<td rowspan="<?php echo $countResultStaffLocationInfo?>"><?php echo $no; ?></td>
							<td rowspan="<?php echo $countResultStaffLocationInfo?>"><?php echo $resultUsers["name"]; ?></td>
							<td rowspan="<?php echo $countResultStaffLocationInfo?>"><?php echo $resultUsers["email"]; ?></td>
							<td rowspan="<?php echo $countResultStaffLocationInfo?>"><?php echo $resultUsers["status"]; ?></td>
							<td rowspan="<?php echo $countResultStaffLocationInfo?>"><?php echo $resultUsers["username"]; ?></td>
							<td rowspan="<?php echo $countResultStaffLocationInfo?>" >
								<button type="button"  onclick="Javascript:newPopup('action_new.php?staff_id=<?php echo $staff_id;?>&action=pwd')" class="btn btn-outline-danger btn-sm">Change Password1</button>
								<button type="button"  onclick="Javascript:newPopup('action_new.php?staff_id=<?php echo $staff_id;?>&action=ud')" class="btn btn-outline-warning btn-sm">Update Details1</button>
							</td>
					<?php
						
						if($countResultStaffLocationInfo == 1){
							$resultStaffLocationInfo = mysqli_fetch_assoc($runStaffLocationInfo);
							?>
							<td><?php echo $resultStaffLocationInfo["project_name"]; ?></td>
							<td><?php echo $resultStaffLocationInfo["location_name"]; ?></td>
							
						</tr>
							<?php
						}
						else if($countResultStaffLocationInfo > 1){
							while($resultStaffLocationInfo = mysqli_fetch_array($runStaffLocationInfo)){
							if($noResultLocation == 1){
								?>
								
								<td><?php echo $resultStaffLocationInfo["project_name"]; ?></td>
								<td><?php echo $resultStaffLocationInfo["location_name"]; ?></td>
							</tr>
								<?php
							}
							else if($noResultLocation > 1){
								
								?>
							<tr>
								<td>
									<button type="button"  onclick="Javascript:newPopup('action_new.php?staff_id=<?php echo $staff_id;?>&action=pwd')" class="btn btn-outline-danger btn-sm">Change Password4</button>
									<button type="button"  onclick="Javascript:newPopup('action_new.php?staff_id=<?php echo $staff_id;?>&action=ud')" class="btn btn-outline-warning btn-sm">Update Details4</button>
								</td>
								<td><?php echo "11" . $resultStaffLocationInfo["project_name"]; ?></td>
								<td><?php echo "11" . $resultStaffLocationInfo["location_name"]; ?></td>
							</tr>
								<?php
									$noResultLocation++;
								}
							}
					?>
					   
							
						</tr>
					<?php 
						
					}
					$no++;
					}										?>
					   
					</tbody>
				</table>
				 
                <?php       
                }  
                else  
                {  
                ?>  
                <h3 align="center">Login</h3>  
                <br />  
                <form method="post">  
                     <label>Enter Username</label>  
                     <input type="text" name="username" class="form-control" />  
                     <br />  
                     <label>Enter Password</label>  
                     <input type="password" name="password" class="form-control" />  
                     <br />  
                     <input type="submit" name="login" value="Login" class="btn btn-info" />  
                     <br />  
                     <p align="center"><a href="index.php?action=update">Update</a></p>  
                </form>  
                <?php  
                }  
                ?>  
           </div>  
      </body>  
 </html>  
 <script>
function newPopup(url) {
	popupWindow = window.open(
	//url,'popUpWindow','height=600,width=700,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no,status=yes')
	url,'_blank','left=10,top=10,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no,status=yes')
}
</script>