<?php
    session_start();
    include '../config.php';
    $id = $_GET['id'];
    // var_dump($id);die;
    $submit = $_POST['submit'];
    if(!$_SESSION)
    {
        header("Location: index.php");
    }
    else
    {
        $uid = $_SESSION['userid'];
        $uname = $_SESSION['uname'];
    }
    if(isset($submit))
    {
        // var_dump($_POST);
        $username = $_POST['username'];
        $password = $_POST['password'];
        $company_code = $_POST['company'];
        
        if($username != "" || $password != "" || $company_code != "" )
        {
            // if(!$result)
            // {
            //     $error = die("Database access failed: " . mysqli_error()); 
            // }
            // else
            // {
                
                $sql = "UPDATE user_master SET username = '$username' ,password = '$password' ,company_code = '$company_code'  WHERE id = $id ";
                // var_dump($sql);
                if ($con->query($sql) === TRUE)
                {
                    $error = "User Edited Successfully!!!";   
                }                    
                else
                {
                    $error = "User Couldnt Be Edited!!!"; 
                }                
            // }
        }
        else
        {
            $error = "Please Enter All Fields!!!";   
        }
    }
    
?>
<!DOCTYPE html>
<html>
    <head>
        <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        <title>Edit User</title>
        <style>
            body 
            {
                margin: 0;
                padding: 0;
                background-color: #9c9c9ca3;
                height: 100vh;
            }
            #login .container #login-row #login-column #login-box
            {
                margin-top: 42px;
                max-width: 600px;
                height: 500px;
                /* border: 1px solid #9C9C9C; */
                background-color: #eaeaead4;
            }
            #login .container #login-row #login-column #login-box #login-form {
                padding: 20px;
            }
            #login .container #login-row #login-column #login-box #login-form #register-link {
                margin-top: -85px;
            }
            footer 
            {
                position: fixed;
                left: 0;
                bottom: 0;
                width: 100%;
                background-color: #255a89ba;
                /* #6c757d   ; */
                color: white;
                text-align: center;
            }
        </style>
    </head>

    <body>
        <div id="login">
            <h3 class="text-center text-white pt-5">Edit User</h3>
            <div class="container">
                <div id="login-row" class="row justify-content-center align-items-center">
                    <div id="login-column" class="col-md-6">
                        <div id="login-box" class="col-md-12">
                            <form id="login-form" class="form" action="" method="post">
                                <span style="color: red; font-size: x-large;margin-left: 111px;"><?php echo $error; ?></span>
                                <?php 
                                    $sql_stmt = "SELECT * FROM user_master where id = $id "; 
                                    $result = mysqli_query($con,$sql_stmt); 
                                    $row = $result->fetch_assoc();
                                ?>
                                <div class="form-group">
                                    <label for="username" class="text-info">Username:</label><br>
                                    <input type="text" name="username" id="username" class="form-control" value = "<?php echo $row['username']; ?>" required>
                                </div>
                                <div class="form-group">
                                    <label for="password" class="text-info">Password:</label><br>
                                    <input type="text" name="password" id="password" class="form-control" value = "<?php echo $row['password']; ?>" required>
                                </div>
                                <div class="form-group">
                                    <label for="password" class="text-info">Confirm Password:</label><br>
                                    <input type="text" name="c_password" id="c_password" class="form-control" value = "<?php echo $row['password']; ?>" required>
                                </div>
                                <div class="form-group">
                                    <label for="company" class="text-info">Company:</label><br>
                                    <select name="company" id="company" class="form-control ">
                                        <?php
                                            $sql_stmt1 = "SELECT * FROM company_master";
                                            $result1 = mysqli_query($con,$sql_stmt1);
                                            while($row1 = mysqli_fetch_array($result1))
                                            {
                                        ?>
                                        <option value="<?php echo $row1['id']; ?>" <?php if($row1['id'] == $row['company_code']){ echo "selected = 'selected'"; }?>><?php echo $row1['company_name']; ?></option>
                                        <?php } ?>
                                    </select>
                                </div>
                                <?php //} ?>
                                <div class="form-group">
                                    <input type="submit" name="submit" class="btn btn-info btn-md" value="Change">
                                    <button  class="btn btn-secondary"><a href="list_user.php" style="color: azure;">Go Back</a></button>

                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </body>
    <footer class="page-footer font-small blue">
        <div class="footer-copyright text-center py-3">© <?php echo date('Y'); ?> Copyright: m2clearance.com . Powered By
            <a href="http://m2clearance.com/" style="color: darkblue;"> m2clearance.com</a>
        </div>
    </footer>
    <script>
    // document.onkeydown = function(e)
    // {
    //     if(event.keyCode == 123)
    //     {
    //         return false;
    //     }
    //     if(e.ctrlKey && e.shiftKey && e.keyCode == 'I'.charCodeAt(0)) 
    //     {
    //         return false;
    //     }
    //     if(e.ctrlKey && e.shiftKey && e.keyCode == 'C'.charCodeAt(0)) 
    //     {
    //         return false;
    //     }
    //     if(e.ctrlKey && e.shiftKey && e.keyCode == 'J'.charCodeAt(0)) 
    //     {
    //         return false;
    //     }
    //     if(e.ctrlKey && e.keyCode == 'U'.charCodeAt(0))
    //     {
    //         return false;
    //     }
    // }
    </script>
</html>
