<?php
// Include config file
require_once "Connection.php";
 
// Define variables and initialize with empty values
$project = $category = $process = $benchmark = "";
$project_err = $category_err = $process_err = $benchmark_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_project = trim($_POST["PROJECT"]);
    if(empty($input_project)){
        $project_err = "Please enter a  project name.";
    
    } else{
        $project = $input_project;
    }
    
    // Validate category
    $input_category = trim($_POST["CATEGORY"]);
    if(empty($input_category)){
        $category_err = "Please enter an category.";     
    } else{
        $category = $input_category;
    }
    
    // Validate process
    $input_process = trim($_POST["PROCESS"]);
    if(empty($input_process)){
        $process_err = "Please enter the process.";     
    
    } else{
        $process = $input_process;
    }
    //Validate Benchmark
    $input_benchmark = trim($_POST["BENCHMARK"]);
    if(empty($input_benchmark)){
        $benchmark_err = "Please enter Benchmark Time.";     
    
    } else{
        $benchmark = $input_benchmark;
    }
    // Check input errors before inserting in database
    if(empty($project_err) && empty($category_err) && empty($process_err) && empty($benchmark_err)){
        // Prepare an update statement
        $sql = "UPDATE master_process_copy SET PROJECT=?, CATEGORY=?, PROCESS=?,BENCHMARK=? WHERE ID=?";
         
        if($stmt = mysqli_prepare($con, $sql)){
            // Bind variables to the prepared statement as parameters
            mysqli_stmt_bind_param($stmt, "ssssi", $param_project, $param_category, $param_process,$param_benchmark, $param_id);
            
            // Set parameters
            $param_project = $project;
            $param_category = $category;
            $param_process = $process;
            $param_benchmark = $benchmark;
            $param_id = $id;
            
            // Attempt to execute the prepared statement
            if(mysqli_stmt_execute($stmt)){
                // Records updated successfully. Redirect to landing page
                header("location: add_proj.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 master_process_copy 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
                    $project = $row["PROJECT"];
                    $category = $row["CATEGORY"];
                    $process = $row["PROCESS"];
                    $benchmark = $row["BENCHMARK"];
                } 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 Record</h5>
                        <form action="<?php echo htmlspecialchars(basename($_SERVER['REQUEST_URI'])); ?>" method="post">
                            <div class="form-group">
                                <label>Project:</label>
                                <input type="text" name="PROJECT" class="form-control <?php echo (!empty($project_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $project; ?>">
                                <span class="invalid-feedback"><?php echo $project_err;?></span>
                            </div>
                            <div class="form-group">
                                <label>Category:</label>
                                <textarea name="CATEGORY" class="form-control <?php echo (!empty($category_err)) ? 'is-invalid' : ''; ?>"><?php echo $category; ?></textarea>
                                <span class="invalid-feedback"><?php echo $category_err;?></span>
                            </div>
                            <div class="form-group">
                                <label>Process:</label>
                                <input type="text" name="PROCESS" class="form-control <?php echo (!empty($process_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $process; ?>">
                                <span class="invalid-feedback"><?php echo $process_err;?></span>
                            </div>
                            <div class="form-group">
                                <label>Benchmark:</label>
                                <input type="text" name="BENCHMARK" class="form-control <?php echo (!empty($benchnark_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $benchmark; ?>">
                                <span class="invalid-feedback"><?php echo $benchmark_err;?></span>
                            </div>
                            <input type="hidden" name="ID" value="<?php echo $id; ?>"/>
                            <input type="submit" class="btn btn-primary" value="Submit">
                            <a href="add_proj.php" class="btn btn-secondary ml-2">Cancel</a>
                        </form>
                </div>
            </div>        
        </div>
    </div>
</body>
</html>