<?php
// Include config file
require_once "Connection.php";
 
// Define variables and initialize with empty values
$name = $code = $password = "";
$name_err = $code_err = $password_err  = "";
 
// Processing form data when form is submitted
if(isset($_POST["id"]) && !empty($_POST["id"])){
    // Get hidden input value
    $id = $_POST["id"];
    
    // Validate Project
    $input_name = trim($_POST["fullname"]);
    if(empty($input_name)){
        $name_err = "Please enter employee name.";
    
    } else{
        $name = $input_name;
    }
    
    // Validate category
    $input_code = trim($_POST["name"]);
    if(empty($input_code)){
        $code_err = "Please enter employee code.";     
    } else{
        $code = $input_code;
    }
    
    // Validate process
    $input_password = trim($_POST["password"]);
    if(empty($input_password)){
        $password_err = "Please Password.";     
    
    } else{
        $password = $input_password;
    }
    
    // Check input errors before inserting in database
    if(empty($name_err) && empty($code_err_err) && empty($password_err) ){
        // Prepare an update statement
        $sql = "UPDATE user_reg_tbl SET fullname=?, name=?, password=? WHERE id=?";
         
        if($stmt = mysqli_prepare($con, $sql)){
            // Bind variables to the prepared statement as parameters
            mysqli_stmt_bind_param($stmt, "sssi", $param_name, $param_code, $param_password, $param_id);
            
            // Set parameters
            $param_name = $name;
            $param_code = $code;
            $param_password = $password;
            $param_id = $id;
            
            // Attempt to execute the prepared statement
            if(mysqli_stmt_execute($stmt)){
                // Records updated successfully. Redirect to landing page
                header("location: add_emp.php");
                exit();
            } else{
                echo "Oops! Something went wrong. Please try again later.";
            }
        }
         
        // Close statement
        mysqli_stmt_close($stmt);
    }
    
    // Close connection
    mysqli_close($con);
} else{
    // Check existence of id parameter before processing further
    if(isset($_GET["id"]) && !empty(trim($_GET["id"]))){
        // Get URL parameter
        $id =  trim($_GET["id"]);
        
        // Prepare a select statement
        $sql = "SELECT * FROM user_reg_tbl WHERE id = ?";
        if($stmt = mysqli_prepare($con, $sql)){
            // Bind variables to the prepared statement as parameters
            mysqli_stmt_bind_param($stmt, "i", $param_id);
            
            // Set parameters
            $param_id = $id;
            
            // Attempt to execute the prepared statement
            if(mysqli_stmt_execute($stmt)){
                $result = mysqli_stmt_get_result($stmt);
    
                if(mysqli_num_rows($result) == 1){
                    /* Fetch result row as an associative array. Since the result set
                    contains only one row, we don't need to use while loop */
                    $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
                    
                    // Retrieve individual field value
                    $name       =   $row["fullname"];
                    $code       =   $row["name"];
                    $password   =   $row["password"];

                } else{
                    // URL doesn't contain valid id. Redirect to error page
                    header("location: error.php");
                    exit();
                }
                
            } else{
                echo "Oops! Something went wrong. Please try again later.";
            }
        }
        
        // Close statement
        mysqli_stmt_close($stmt);
        
        // Close connection
        mysqli_close($con);
    }  else{
        // URL doesn't contain id parameter. Redirect to error page
        header("location: error.php");
        exit();
    }
}
?>
 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Emp_Prod_Benchmark</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <link rel="shortcut icon" href="images/Files_Download.png" type="image/x-icon"/>
    <style>
        .wrapper{
            width: 600px;
            margin: 0 auto;
            border:solid black 2px;
            border-radius: 20px;
            height: 500px;
            margin-top: 50px;
        }
    </style>
</head>
<body>
    <?php include "Nav.php"?>
    <div class="wrapper">
        <div class="container-fluid">
            <div class="row">
                <div class="col-md-12">
                    <h5 class="mt-5">Update Employee </h5>
                        <form action="<?php echo htmlspecialchars(basename($_SERVER['REQUEST_URI'])); ?>" method="post">
                            <div class="form-group">
                                <label>Employee Name:</label>
                                <input type="text" name="fullname" class="form-control <?php echo (!empty($name_err_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $name; ?>">
                                <span class="invalid-feedback"><?php echo $name_err;?></span>
                            </div>
                            <div class="form-group">
                                <label>Employee Code:</label>
                                <textarea name="name" class="form-control <?php echo (!empty($code_err)) ? 'is-invalid' : ''; ?>"><?php echo $code; ?></textarea>
                                <span class="invalid-feedback"><?php echo $code_err;?></span>
                            </div>
                            <div class="form-group">
                                <label>Password:</label>
                                <input type="text" name="password" class="form-control <?php echo (!empty($password_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $password; ?>">
                                <span class="invalid-feedback"><?php echo $password_err;?></span>
                            </div>
                            <input type="hidden" name="id" value="<?php echo $id; ?>"/>
                            <input type="submit" class="btn btn-primary" value="Submit">
                            <a href="add_emp.php" class="btn btn-secondary ml-2">Cancel</a>
                        </form>
                </div>
            </div>        
        </div>
    </div>
</body>
</html>