<?php
    session_start();
    include '../config.php';
    $submit = $_POST['submit'];
    if(!$_SESSION)
    {
        header("Location: index.php");
    }
    else
    {
        $uid = $_SESSION['userid'];
        $uname = $_SESSION['uname'];
    }
    if(isset($submit))
    {
        $username = $_POST['username'];
        $password = $_POST['password'];
        $company_code = $_POST['company_code'];
        
        if($username != "" || $password != "" || $company_code != "" )
        {
            $sql_stmt = "SELECT username FROM user_master where username = '$username' "; 
            $result = mysqli_query($con,$sql_stmt);            
            if(!$result)
            {
                $error = die("Database access failed: " . mysqli_error()); 
            }
            else
            {
                $rows = mysqli_num_rows($result); 
                if($rows == 0)
                {
                    $sql = "INSERT INTO user_master ( username, password, company_code )
                                VALUES ('$username','$password',$company_code)";
                    // var_dump($sql);die;
                    if ($con->query($sql) === TRUE)
                    {
                        $error = "User Created Successfully!!!";   
                    }                    
                    else
                    {
                        $error = "Some Error Occured".  $con->error;
                    }
                }
                else
                {
                    $error = "User Name Already Exists!!!";   
                }
            }
        }
        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>Change Module</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">Create 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>
                                <div class="form-group">
                                    <label for="username" class="text-info">Username:</label><br>
                                    <input type="text" name="username" id="username" class="form-control">
                                </div>
                                <div class="form-group">
                                    <label for="password" class="text-info">Password:</label><br>
                                    <input type="password" name="password" id="password" class="form-control">
                                </div>
                                <div class="form-group">
                                    <label for="password" class="text-info">Confirm Password:</label><br>
                                    <input type="password" name="c_password" id="c_password" class="form-control">
                                </div>
                                <div class="form-group">
                                    <label for="company" class="text-info">Company:</label><br>
                                    <select name="company_code" id="company_code" class="form-control ">
                                        <option value="">--Select Company--</option>
                                        <?php
                                            $sql_stmt = "SELECT * FROM company_master";
                                            $result = mysqli_query($con,$sql_stmt);
                                            while($row = mysqli_fetch_array($result))
                                            {
                                        ?>
                                        <option value="<?php echo $row['id']; ?>"><?php echo $row['company_name']; ?></option>
                                        <?php } ?>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <input type="submit" name="submit" class="btn btn-info btn-md" value="Save">
                                    <button  class="btn btn-secondary"><a href="create.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>
