<?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 id 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 = "UPDATE user_master SET username = '$username' WHERE id = ".$rows["id"]."  ";
                    if ($con->query($sql) === TRUE)
                    {
                        $error = "User Created Successfully!!!";   
                    }                    
                    else
                    {
                        $error = "User Name Already Exists!!!"; 
                    }
                }
                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">List 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"> -->
                            <table class="table">
                                <thead>
                                    <tr>
                                    <th scope="col">#</th>
                                    <th scope="col">Company Name</th>
                                    <th scope="col">Broker Customer Code</th>
                                    <th scope="col">Broker Business Code</th>
                                    <th scope="col">CTO</th>
                                    <th scope="col">Shipping Agent</th>
                                    <th scope="col">Action</th>
                                    <th scope="col">Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                <?php  
                                    // $sql_stmt = "SELECT * FROM user_master "; 
                                    $sql_stmt2 = "SELECT * FROM company_master "; 
                                    // $result = mysqli_query($con,$sql_stmt);
                                    $result2 = mysqli_query($con,$sql_stmt2);  
                                    $i = 1;
                                    while($row = mysqli_fetch_array($result2))
                                    {  
                                ?>
                                <tr>
                                    <th scope="col"><?php echo $i++; ?></th>
                                    <th scope="col"><?php echo $row['company_name']; ?></th>
                                    <th scope="col"><?php echo $row['broker_customer_code']; ?></th>
                                    <th scope="col"><?php echo $row['broker_business_code']; ?></th>
                                    <th scope="col"><?php echo $row['cto']; ?></th>
                                    <th scope="col"><?php echo $row['shipping_agent']; ?></th>
                                    <th scope="col"><button class="btn btn-info"><a href = "edit_company.php?id=<?php echo $row['id']; ?>" style="color : azure;">Edit</a></button></th>
                                    <th scope="col"><button class="btn btn-info delete"><input type="hidden" class="user_id" value="<?php echo $row['id']; ?>">Delete</button></th>
                                    </tr>
                                    <?php } ?>
                                </tbody>
                            </table>                             
                            <div class="form-group">
                                <button  class="btn btn-primary"><a href="create_company.php" style="color: azure;">Create New</a></button>
                                <button  class="btn btn-secondary"><a href="create.php" style="color: azure;">Go Back</a></button>
                            </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>
     $(".delete").click(function()
    {
        if(confirm("Do you really want to delete?"))
        {
            var user = $(this).closest("tr").find(".user_id").val();
            $.ajax({
                type : "post",
                data : { user : user , method : 2},
                url : "delete.php",
                success : function(res)
                {
                    alert(res);
                    window.location.reload();
                }
            });
        }
        else
        {
            return false;
        }       
    });
    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>
