<?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> -->
        <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
        <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.10.22/datatables.min.css"/>
        <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.22/css/jquery.dataTables.min.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>
        <script type="text/javascript" src="https://cdn.datatables.net/v/dt/dt-1.10.22/datatables.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">
                    <hr />
                        <div class="col-sm-12">
                            <div class="row">
                                <div class="col-md-3"><h5>Sort By:</h5></div>
                                <div class="col-md-3"><input type="date" class="form-control" name="from_date" id="from_date" value="<?php echo date('Y-m-d'); ?>"><p>Start Date</p></div>
                                <div class="col-md-3"><input type="date" class="form-control" name="to_date" id="to_date" value="<?php echo date('Y-m-d'); ?>"><p>End Date</p></div>
                                <div class="col-md-3">
                                    <select class="form-control" id="company_id">
                                    <option value="">--Select--</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>
                                    <p>Company Name</p>
                                </div>
                                <div class="col-md-3">
                                    <select class="form-control" id="user_id">
                                    <option value="">--Select All--</option>
                                        <?php  
                                            $sql_stmt = "SELECT * FROM user_master "; 
                                            $result = mysqli_query($con,$sql_stmt);
                                            while($row = mysqli_fetch_array($result))
                                            {  
                                        ?>
                                            <option value="<?php echo $row['id']; ?>"><?php echo $row['username']; ?></option>
                                            <?php } ?>
                                    </select>
                                    <p>User Name</p>
                                </div>
                                <div class="col-md-3">
                                    <select class="form-control" id="module">
                                        <option value="">--Select All--</option>
                                        <option value="1">Import</option>                                        
                                        <option value="2">Export</option>
                                    </select>
                                    <p>Module Type</p>
                                </div>
                                <div class="col-md-4">
                                    <button class="btn btn-primary pull-right col-xs-6" name="search" value="search" id="search" style="height: 37px;">Search</button>
                                    <button id="export" data-export="export" class="btn btn-primary">Generate CSV</button> 
                                    <button  class="btn btn-secondary"><a href="create.php" style="color: azure;">Go Back</a></button>                       
                                </div>                                        
                            </div>
                        </div>
                    <hr/>
                    <div id="login-column" class="col-md-12">
                            <table class="table" id="example">
                                <thead>
                                    <tr>
                                    <th scope="col">#</th>
                                    <th scope="col">User Name</th>
                                    <th scope="col">Company Name</th>
                                    <th scope="col">Module Selected</th>
                                    <th scope="col">Login Date</th>
                                    <th scope="col">IP Address</th>
                                    <th scope="col">CSV Generated </th>
                                    <th scope="col">No. of Shipments</th>
                                    </tr>
                                </thead>
                                <tbody>
                                <?php  
                                    // $sql_stmt1 = "SELECT * FROM user_master "; 
                                    // $result1 = mysqli_query($con,$sql_stmt1); 

                                    // $sql_stmt2 = "SELECT * FROM activity_log "; 
                                    // $result2 = mysqli_query($con,$sql_stmt2); 

                                    // $sql_stmt3 = "SELECT * FROM company_master"; 
                                    // $result3 = mysqli_query($con,$sql_stmt3);

                                    // $i = 1;$s = "";$t = "";

                                    // while($row = mysqli_fetch_array($result2))
                                    // {  
                                    //     while($row1 = mysqli_fetch_array($result1))
                                    //     { 
                                    //         if($row1['id'] == $row['user_id'])
                                    //         { 
                                    //             $s = $row1['username']; 
                                    //         }
                                    //     }
                                        
                                    //     while($row1 = mysqli_fetch_array($result3))
                                    //     { 
                                    //         if($row1['id'] == $row['company_id'])
                                    //         { 
                                    //             $t = $row1['company_name']; 
                                    //         }
                                    //     }
                                ?>
                                <tr>
                                    <th scope="col"><?php echo $i++; ?></th>
                                    <th scope="col"><?php echo $s; ?></th>
                                    <th scope="col"><?php echo $t; ?></th>
                                    <th scope="col">
                                        <?php
                                            // if($row['module'] == 1)
                                            // { 
                                            //     echo "Import"; 
                                            // }        
                                            // else
                                            // {
                                            //     echo "Export"; 
                                            // }                                    
                                        ?>
                                    </th>
                                    <th scope="col"><?php echo $row['login_date_time']; ?></th>
                                    <th scope="col"><?php echo $row['public_ip']; ?></th>
                                    <th scope="col"><?php echo $row['csv_generated']; ?></th>
                                    <th scope="col"><?php echo $row['no_of_shipments']; ?></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> -->
                                    </tr>
                                    <?php //} ?>
                                </tbody>
                            </table>      
                        <!-- </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() {
            $('#example').DataTable();
        });
        $('#search').click(function(){
            var from_date = $('#from_date').val();
            
            var to_date = $('#to_date').val();
            
            var company_id = $('#company_id').children('option:selected').val();

            var user_id = $('#user_id').children('option:selected').val();

            var module = $('#module').children('option:selected').val();                    
            if(company_id == "")
            {
                alert("Please select all the fields");
                return false;
            }
            else
            {
                $.ajax({
                    type : "POST",
                    data : {"f_date":from_date , "t_date":to_date, "company_id":company_id , "user_id":user_id , "module":module },
                    url  : "search_activity.php",
                    success : function(data)
                    {
                        $('.table tbody').html(data);
                    }
                });
            }           
        });
        </script>
        <script>
            jQuery.fn.tableToCSV = function() 
            {    
                var clean_text = function(text){
                    text = text.replace(/"/g, '\\"').replace(/'/g, "\\'");
                    return '"'+text+'"';
                };                
                $(this).each(function(){
                        var table = $(this);
                        
                        var title = [];
                        var rows = [];

                        $(this).find('tr').each(function(){
                            var data = [];
                            $(this).find('th').each(function(){
                                var text = clean_text($(this).text());
                                title.push(text);
                                });
                            $(this).find('td').each(function(){
                                var text = clean_text($(this).text());
                                data.push(text);
                                });
                            data = data.join(",");
                            rows.push(data);
                            });
                        title = title.join(",");
                        rows = rows.join("\n");

                        var csv = title + rows;
                        var uri = 'data:text/csv;charset=utf-8,' + encodeURIComponent(csv);
                        var download_link = document.createElement('a');
                        download_link.href = uri;
                        var ts = new Date().getTime();
                        // if(caption==""){
                            download_link.download = ts+'-'+'Activity Report'+".csv";
                        // } else {
                        //     download_link.download = caption+"-"+ts+".csv";
                        // }
                        document.body.appendChild(download_link);
                        download_link.click();
                        document.body.removeChild(download_link);
                });
                
            };
            $("#export").click(function()
            {               
                $(".table").tableToCSV();
            }); 
            
    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>
