<?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';
$userid=$_GET['updateid'];
$sql="select * from users where userid=$userid";
$result=mysqli_query($conn,$sql);
$row=mysqli_fetch_assoc($result);
$fname=$row['fname'];
$lname=$row['lname'];
$mname=$row['mname'];
$email=$row['email'];
$mobile=$row['mobile'];
$password=$row['password'];
$role=$row['role'];

if(isset($_POST['submit'])){
    $fname=$_POST['fname'];
    $mname=$_POST['mname'];
    $lname=$_POST['lname'];
    $email=$_POST['email'];
    $mobile=$_POST['mobile'];
    $password=$_POST['password'];
    $role=$_POST['role'];
    
    //tuone
    if (!empty($password)){
        // Hash the new password using bcrypt
        //$hashedPassword = password_hash($password, PASSWORD_DEFAULT);

        // Update the user with the new password
        $sql = "update users set fname='$fname',mname='$mname', lname='$lname', email='$email', mobile='$mobile', password='$password', role='$role' where userid=$userid";
        $result=mysqli_query($conn, $sql);
        if($result){
            //echo "Data updated succesfully";
           header('location: display.php');
        }else{
            die(mysqli_error($conn));
        }
    } else {
        //update without changing the password
        $sql = "update users set fname='$fname',mname='$mname', lname='$lname', email='$email', mobile='$mobile', role='$role' where userid=$userid";
        $result=mysqli_query($conn, $sql);
        if($result){
            //echo "Data updated succesfully";
           header('location: display.php');
        }else{
            die(mysqli_error($conn));
        }
    }
            //tuone tena
            //Hii iliyokuwa commented hapa chini inafanya kazi lakini kama umeencrypt pwd inakuwa shida
        /* $sql = "update users set fname='$fname',mname='$mname', lname='$lname', email='$email', mobile='$mobile', password='$password', role='$role' where userid=$userid";
            //$sql = "insert into users (name, email, mobile, password) values ('$name','$email','$mobile','$password')";
            $result=mysqli_query($conn, $sql);
            if($result){
                //echo "Data updated succesfully";
            header('location: display.php');
            }else{
                die(mysqli_error($conn));
            } */
}

?>

<!doctype html>
<html lang="en" dir="ltr">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
<!--<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">

    <title>CRUD APPLICATION</title>
  </head>
  <body>
    <div class="container my-5"> 
        <h3>Change the information filled in the textboxes to update the user</h3>

        <form method="POST">
            <div class="mb-3">
                <label for="formGroupExampleInput" class="form-label">First Name:</label>
                <input type="text" class="form-control" name="fname" autocomplete="off" value=<?php echo $fname; ?>>
            </div>
            <div class="mb-3">
                <label for="formGroupExampleInput" class="form-label">Middle Name:</label>
                <input type="text" class="form-control" name="mname" autocomplete="off" value=<?php echo $mname; ?>>
            </div>
            <div class="mb-3">
                <label for="formGroupExampleInput" class="form-label">Last Name:</label>
                <input type="text" class="form-control" name="lname" autocomplete="off" value=<?php echo $lname; ?>>
            </div>
            <div class="mb-3">
                <label for="formGroupExampleInput2" class="form-label">Email:</label>
                <input type="email" class="form-control" name="email" autocomplete="off" value=<?php echo $email; ?>>
            </div>
            <div class="mb-3">
                <label for="formGroupExampleInput" class="form-label">Mobile:</label>
                <input type="text" class="form-control" name="mobile" autocomplete="off" value=<?php echo $mobile; ?>>
            </div>
            <div class="mb-3">
                <label for="formGroupExampleInput2" class="form-label">New Password (Leave blank to keep current password):</label>
                <input type="password" class="form-control" name="password"  placeholder="Enter new password" ?>
            </div>

            <div class="mb-3">
                
                <label for="formGroupExampleInput2" class="form-label">Choose Role:</label>

                <select name="role" class="form-control" value=<?php echo $role;  ?>>
                <option value="admin">Admin</option>
                <option value="guest">Guest</option>
                </select>

            </div>

            <button type="submit" class="btn btn-primary" name="submit">Update</button>
        </form>    

    </div>

    <script src="js/jquery.min.js"></script>
<script src="js/bootstrap.bundle.min.js"></script>
  </body>
</html>