<?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';
if(isset($_POST['submit'])){
    $fname=$_POST['fname'];
    $lname=$_POST['lname'];
    $mname=$_POST['mname'];
    $email=$_POST['email'];
    $mobile=$_POST['mobile'];
    $role=$_POST['role'];
    $password=$_POST['password'];
    
//Validate if the password is strong
    if (!preg_match('/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[\W_]).{8,}$/', $password)) {
     echo "Password must be at least 8 characters long and include a mix of uppercase letters, lowercase letters, numbers and special characters.";
    exit;
}

    //$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
    
    $sql = "insert into users (fname, mname, lname, email, mobile, password, role) values ('$fname','$mname','$lname','$email','$mobile','$password','$role')";
    $result=mysqli_query($conn, $sql);
    if($result){
        //echo "Data inserted 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">
<link rel="stylesheet" href="fontawesome/css/all.min.css">

    <title>CRUD APPLICATION</title>
  </head>
  <body>
    <div class="container my-5"> 

    <h3>Please fill all the textboxes to add a new user to the system</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" placeholder="Enter your Firstname">
            </div>
            <div class="mb-3">
                <label for="formGroupExampleInput" class="form-label">Middle Name:</label>
                <input type="text" class="form-control" name="mname" autocomplete="off" placeholder="Enter your middlename">
            </div>
            <div class="mb-3">
                <label for="formGroupExampleInput" class="form-label">Last Name:</label>
                <input type="text" class="form-control" name="lname" autocomplete="off" placeholder="Enter your lastname">
            </div>
            <div class="mb-3">
                <label for="formGroupExampleInput2" class="form-label">Email:</label>
                <input type="email" class="form-control" name="email" autocomplete="off" placeholder="Enter your email">
            </div>
            <div class="mb-3">
                <label for="formGroupExampleInput" class="form-label">Mobile:</label>
                <input type="text" class="form-control" name="mobile" autocomplete="off" placeholder="Enter your mobile number">
            </div>
            <div class="mb-3">
                <label for="formGroupExampleInput2" class="form-label">Password:</label>
                <input type="password" class="form-control" name="password"  placeholder="Enter your password" required>
            </div>
            <div class="mb-3">
                
                <label for="ormGroupExampleInput2" class="form-label">Choose Role:</label>

                <select name="role" class="form-control">
                <option value="admin">Admin</option>
                <option value="guest">Guest</option>
                </select>

            </div>

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

    </div>

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