<?php include "../db.php" ?>
<?php
$connection=new mysqli($dbhost,$dbusername,$dbpassword,$dbname);
if($_SERVER["REQUEST_METHOD"]=="POST" && isset($_POST["add_user_btn"])){
    $name=$_POST["add_user_name"];
    $username=$_POST["add_user_username"];
    $password=$_POST["add_user_password"];
    $role=$_POST["add_user_role"];
    if(empty($name) || empty($username) || empty($password) || empty($role)){
        session_start();
        $_SESSION["user_add_error"]="All Fields Are Required!!";
        header("Location: ../user.php");
        exit();
    }
    else{
        $addUserQuery="INSERT into user (Name,Username,Password,Role) VALUES ('$name','$username','$password','$role')";
        $connection->query($addUserQuery);
        session_start();
        $_SESSION["user_add_alert"]="User Added Successfully!!";
        header("Location: ../user.php");
        exit();

    }
}
if($_SERVER["REQUEST_METHOD"]=="POST" && isset($_POST["edit_user_btn"])){
    $id=$_POST["edit_user_id"];
    $name=$_POST["edit_user_name"];
    $username=$_POST["edit_user_username"];
    $password=$_POST["edit_user_password"];
    $role=$_POST["edit_user_role"];
    if(empty($name) && empty($username) && empty($password) && empty($role)){
        session_start();
        $_SESSION["user_add_error"]="All Fields Are Required!!";
        header("Location: ../user.php");
        exit();
    }
    else{
        $editUserQuery="UPDATE user SET Name='$name',Username='$username',Password='$password',Role='$role' WHERE ID='$id'";
        $connection->query($editUserQuery);
        session_start();
        $_SESSION["user_add_alert"]="User Added Successfully!!";
        header("Location: ../user.php");
        exit();

    }
}
if($_POST["id"]){
    $id= $_POST["id"];
    $dltUserQuery="DELETE FROM user WHERE ID='$id'";
    $connection->query($dltUserQuery);
}
?>