<?php
session_start();
if (!isset($_SESSION['username'])) {
    echo "<script>window.top.location.href='index.php';</script>"; // Redirect parent window to login page
    exit();
}

include 'connect.php';

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!--<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.rtl.min.css">-->
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="fontawesome/css/all.min.css">
    <title>USER CRUD OPERATION</title>
</head>
<body>
    <div class="container my-5">
    <button class="btn btn-primary"><a href="users.php" class="text-light">Add user<a></button>
    </div>
    <div class="container my-5">
    <table class="table table-hover">
  <thead>
    <tr>
      <th scope="col">SNo.</th>
      <th scope="col">First Name</th>
      <th scope="col">Middle Name</th>
      <th scope="col">Last Name</th>
      <th scope="col">Email</th>
      <th scope="col">Mobile</th>
      <!--<th scope="col">Password</th>-->
      <th scope="col">Role</th>
      <th scope="col">Operations</th>
    </tr>
  </thead>
  <tbody>

  <?php
    $sql= "select * from users";
    $result= mysqli_query($conn,$sql);
    if($result){

      //Initialize serila number
      $serialNo = 1;
       
        while($row=mysqli_fetch_assoc($result)){
            $userid=$row['userid'];
            $fname=$row['fname'];
            $lname=$row['lname'];
            $mname=$row['mname'];
            $email=$row['email'];
            $mobile=$row['mobile'];
            //$password=$row['password'];
            $role=$row['role'];

            echo '<tr>
      <th scope="row">'.$serialNo++.'</th>
      <td>'.$fname.'</td>
      <td>'.$mname.'</td>
      <td>'.$lname.'</td>
      <td>'.$email.'</td>
      <td>'.$mobile.'</td>
      <td>'.$role.'</td>
      <td>
        <button class="btn btn-primary"><a href="update.php?updateid='.$userid.'" class="text-light"><i class="fas fa-edit"></i><a></button>
        '.($_SESSION['username'] != $email ? '<button class="btn btn-danger"><a href="delete.php?deleteid='.$userid.'" class="text-light"><i class="fas fa-trash"></i></a></button>' : '').'

    </td>
    </tr>';
            
        }

    }




   ?>
    
    
  </tbody>
</table>

    </div>
    
</body>
</html>