<?php
    session_start();
    include 'config.php';
    $id = $_GET['id'];
    $submit = $_POST['submit'];
    if(!$_SESSION)
    {
        header("Location: index.php");
    }
    else
    {
        $uid = $_SESSION['userid'];
        $uname = $_SESSION['uname'];
    }
    if(isset($submit))
    {
        $username = $_POST['username'];
        $o_password = $_POST['o_password'];
        $n_password = $_POST['n_password'];
        $c_password = $_POST['c_password'];
        $company_code = $_POST['company'];
        
        if($username != "" || $password != "" || $company_code != "" )
        {
            $sql = "Select password from user_master  WHERE id = $id ";
            $res = mysqli_query($con,$sql);
            $r = mysqli_fetch_array($res);
            $pass = $r['password'];
            if($pass === $o_password)
            {
                $sql = "UPDATE user_master SET password = '$n_password' WHERE id = $id ";
                if ($con->query($sql) === TRUE)
                {
                    $error = "Password Changed Successfully!!!";   
                }                    
                else
                {
                    $error = "Password Couldnt Be Changed!!!"; 
                }     
            }
            else
            {
                $error = "Old Password Doesnt Match The Entered Old Password!!!"; 
            }                       
        }
        else
        {
            $error = "Please Enter All Fields!!!";   
        }
    }
    
?>
<!DOCTYPE html>
<head>
	<link href="css/style.css" rel="stylesheet">
        <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>Change Module</title>        
        <style>
             body 
            {
                margin: 0;
                padding: 0;
                background-color: #9c9c9c00;
                height: 100vh;
                font-size: x-small;                
            }
            .form-control
            {
                font-size: larger;             
            }
            th,td,p,input
            {
                font: 14px Verdana;
            }    
            table,th,td
            {
                border: solid 1px #DDD;
                border-collapse: collapse;
                padding: 2px 3px;
                text-align: center;
            }
            .modal-header
            {
                padding: 1px;
                border-bottom: 1px solid #e5e5e5;
                padding-top: 21px;
            }
            hr
            {
                margin-top: 20px;
                margin-bottom: 20px;
                border: 1px solid #204d74;
                border-top: 1px solid #204d74;
            }
            a
            {     
                display: inline-block;     
                position: relative;    
                z-index: 1;     
                padding: 2em;     
                margin: -2em; 
            }
            .red
            {
                background-color : red;
            }
            footer 
            {
                position: fixed;
                left: 0;
                bottom: 0;
                width: 100%;
                background-color: #255a89ba;
                /* #6c757d   ; */
                color: white;
                text-align: center;
            }
            th 
            {
                font-weight: bold;
            }
            .disstyle
            {
                text-align: initial;
                font-weight: 600;
                display: flex;
            }
            .modal-header .close
            {
                margin-top: -43px;
            }
            .modal-header
            {
                padding: 1px;
                border-bottom: 1px solid #e5e5e5;
                padding-top: 21px;
            }
            input[type="number"]::-webkit-outer-spin-button, 
            input[type="number"]::-webkit-inner-spin-button
            {
                -webkit-appearance: none;
                margin: 0;
            }
            input[type="number"] 
            {
                -moz-appearance: textfield;
            }
        </style>
    </head>
    <body>
    <div class="overlay">

        <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">Please Enter 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">Please Enter Old Password:</label><br>
                                    <input type="password" name="o_password" id="o_password" class="form-control" value = "<?php echo $row['password']; ?>" required>
                                </div>
                                <div class="form-group">
                                    <label for="password" class="text-info">Please Enter New Password:</label><br>
                                    <input type="password" name="n_password" id="n_password" class="form-control" value = "" required>
                                </div>
                                <div class="form-group">
                                    <label for="password" class="text-info">Confirm New Password:</label><br>
                                    <input type="password" name="c_password" id="c_password" class="form-control" value = "" required>
                                </div>                                
                                <div class="form-group">
                                    <input type="submit" name="submit" class="btn btn-info btn-md" value="Change">
                                    <button  class="btn btn-secondary"><a href="exceltotext.php" style="color: azure;">Go Back</a></button>

                                </div>
                            </form>
                        </div>
                    </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).ready(function(){
            $("#c_password").blur(function(){
                var n = $("#n_password").val();
                var c = $(this).val();
                if(n !== c)
                {
                    alert("Password Does Not Match!!");
                    $("#c_password").val("");
                    $("#n_password").val("");
                }
            })
        });
    // 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>
