<?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))
    {
        $company_name = $_POST['company_name'];
        $broker_customer_code = $_POST['broker_customer_code'];
        $broker_business_code = $_POST['broker_business_code'];
        $cto = $_POST['cto'];
        $shipping_agent = $_POST['shipping_agent'];
        $credit_acc = $_POST['credit_acc'];
        if($company_name !==  " " || $broker_customer_code != "" || $broker_business_code != "" || $cto != "" || $shipping_agent != "" || $credit_acc != "" )
        {
            // if(!$result)
            // {
            //     $error = die("Database access failed: " . mysqli_error()); 
            // }
            // else
            // {
                
                $sql = "UPDATE company_master SET company_name = '$company_name' , broker_customer_code = '$broker_customer_code' ,
                        broker_business_code = '$broker_business_code' ,cto = '$cto' ,shipping_agent = '$shipping_agent', credit_acc ='$credit_acc' WHERE id = $id ";
                // var_dump($sql);
                if ($con->query($sql) === TRUE)
                {
                    $error = "Company Edited Successfully!!!";   
                }                    
                else
                {
                    $error = "Company 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: 800px;
                /* 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">
                                <?php 
                                    $sql_stmt = "SELECT * FROM company_master where id = $id "; 
                                    $result = mysqli_query($con,$sql_stmt); 
                                    $row = $result->fetch_assoc();
                                ?>
                                <span style="color: red; font-size: x-large;"><?php echo $error; ?></span>
                                <div class="form-group">
                                    <label for="company_name" class="text-info">Company Name:</label><br>
                                    <input type="text" name="company_name" id="company_name" class="form-control" value = "<?php echo $row['company_name']; ?>" required>
                                </div>
                                <div class="form-group">
                                    <label for="broker_customer_code" class="text-info">Broker Customer Code:</label><br>
                                    <input type="text" name="broker_customer_code" id="broker_customer_code" class="form-control" value = "<?php echo $row['broker_customer_code']; ?>" required>
                                </div>
                                <div class="form-group">
                                    <label for="broker_business_code" class="text-info">Broker Business Code:</label><br>
                                    <input type="text" name="broker_business_code" id="broker_business_code" class="form-control" value = "<?php echo $row['broker_business_code']; ?>" required>
                                </div>
                                <div class="form-group">
                                    <label for="cto" class="text-info">CTO:</label><br>
                                    <input type="text" name="cto" id="cto" class="form-control" value = "<?php echo $row['cto']; ?>" required>
                                </div>
                                <div class="form-group">
                                    <label for="    shipping_agent" class="text-info">Shipping Agent:</label><br>
                                    <input type="text" name="shipping_agent" id="shipping_agent" class="form-control" value = "<?php echo $row['shipping_agent']; ?>" required>
                                </div>
                                <div class="form-group">
                                    <label for="credit_acc" class="text-info">Payment Mode:</label><br>
                                    <input type="text" name="credit_acc" id="credit_acc" class="form-control" value = "<?php echo $row['credit_acc']; ?>" 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="list_company.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>
