<?php include ("header.php");?>

                <div class="dashboard-body">
                    <div class="d-flex justify-content-between mb-3 align-items-center dashboardBodyNav">
                        
                        <!---->

                        
                    </div>
                                            <div class="row justify-content-between align-items-center gy-4">
        <div class="col-lg-4">
            <h4 class="mb-0">Login History</h4>
        </div>
        <div class="col-md-12">
            <div class="table-wrapper">
                <table class="table table--responsive--lg">
                    <thead>
                        <tr>
                    
                            <th>Login At</th>
                            <th>Ip Address</th>
                            <th>Location</th>
                            <th> Browser | Os</th>
                           
                        </tr>
                    </thead>
                    <tbody>
                                  
                            <?php
                        // SQL query to select login activity
                        $sql = "SELECT `time`, `ip_address`, `location`, `browser` FROM `activities` WHERE user_id = '$user_id' ORDER BY `time` DESC";
                        $result = $conn->query($sql);
                        
                        // Check if there are any results
                        if ($result->num_rows > 0) {
                            // Loop through the results and display each row in the table
                            while ($row = $result->fetch_assoc()) {
                                $loginTime = $row['time'];  // Assuming it's stored in `YYYY-MM-DD HH:MM:SS` format
                                $ipAddress = $row['ip_address'];
                                $location = $row['location'];
                                $browser = $row['browser'];
                        
                                // Calculate time ago
                                $timeAgo = getTimeAgo($loginTime);  // Function to calculate time difference
                        ?>
                                <tr>
                                    <td data-label="Login At">
                                        <?= $loginTime; ?> <br> <?= $timeAgo; ?>
                                    </td>
                                    <td data-label="Ip Address">
                                        <?= $ipAddress; ?>
                                    </td>
                                    <td data-label="Location">
                                        <?= $location; ?>
                                    </td>
                                    <td data-label="Browser | Os">
                                        <?= $browser; ?> 
                                    </td>
                                </tr>
                        <?php
                            }
                        } else {
                            echo "<tr><td colspan='4'>No login activity found.</td></tr>";
                        }
                        
                        function getTimeAgo($timestamp) {
                            $time = strtotime($timestamp);
                            $current_time = time();
                            $time_difference = $current_time - $time;
                        
                            $seconds = $time_difference;
                            $minutes      = round($seconds / 60);           // value 60 is seconds
                            $hours        = round($seconds / 3600);         // value 3600 is 60 minutes * 60 sec
                            $days         = round($seconds / 86400);        // value 86400 is 24 hours * 60 minutes * 60 sec
                            $weeks        = round($seconds / 604800);       // value 604800 is 7 days * 24 hours * 60 minutes * 60 sec
                            $months       = round($seconds / 2629440);      // value 2629440 is ((365+365+365+365)/4/12) * 24 * 60 * 60
                            $years        = round($seconds / 31553280);     // value 31553280 is (365+365+365+365)/4 * 24 * 60 * 60
                        
                            if ($seconds <= 60) {
                                return "Just Now";
                            } else if ($minutes <= 60) {
                                if ($minutes == 1) {
                                    return "one minute ago";
                                } else {
                                    return "$minutes minutes ago";
                                }
                            } else if ($hours <= 24) {
                                if ($hours == 1) {
                                    return "an hour ago";
                                } else {
                                    return "$hours hours ago";
                                }
                            } else if ($days <= 7) {
                                if ($days == 1) {
                                    return "yesterday";
                                } else {
                                    return "$days days ago";
                                }
                            } else if ($weeks <= 4.3) { // 4.3 == 30/7
                                if ($weeks == 1) {
                                    return "one week ago";
                                } else {
                                    return "$weeks weeks ago";
                                }
                            } else if ($months <= 12) {
                                if ($months == 1) {
                                    return "one month ago";
                                } else {
                                    return "$months months ago";
                                }
                            } else {
                                if ($years == 1) {
                                    return "one year ago";
                                } else {
                                    return "$years years ago";
                                }
                            }
                        }
                        ?>

                                  
                            
                       </tbody>
                </table>
            </div>
                    </div>
    </div>
<?php include "modals.php";?>