<?php include 'includes/session.php'; ?>
<?php include 'includes/sessions.php'; ?>
<?php include 'includes/header.php'; ?>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">

  <?php include 'includes/navbar.php'; ?>
  <?php include 'includes/menubar.php'; ?>

  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper"> 
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <h1>
        Dashboard
      </h1>
      <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
        <li class="active">Dashboard</li>
      </ol>
    </section>

    <!-- Main content -->
    <section class="content">
      <!-- Small boxes (Stat box) -->
      <div class="row" id="user-monitoring">
        <!-- User monitoring cards will be dynamically loaded here -->
      </div>
    </section>   
  </div>

  <?php include 'includes/footer.php'; ?>
  <?php include 'includes/users_modal.php'; ?>
</div>
<?php include 'includes/scripts.php'; ?>

<script>
// Function to load user monitoring data via AJAX
function loadUserData() {
    $.ajax({
        url: 'get_user_data.php', // PHP script that returns the users' data as JSON
        method: 'GET',
        success: function(data) {
            const userData = JSON.parse(data);
            let content = '';
            let userCount = 0;

            userData.forEach(user => {
                if (userCount % 2 === 0) {
                    // Start a new row for every two users
                    content += `<div class="row mb-3">`;
                }

                // Get the last photo and screenshot
                const lastPhoto = user.photos.length > 0 ? user.photos[0].photo_path : 'images/default.jpg';
                const lastScreenshot = user.screenshots.length > 0 ? user.screenshots[0].file_path : 'images/default_screenshot.jpg';

                // Build the content for each user
                content += `
                    <div class="col-md-6  mx-6" >
                        <div class="row">
                            <!-- First Column: Last Photo and Screenshot -->
                            <div class="col-md-4">
                                <h6><b>${user.user}'s Last Photo</b></h6>
                                <img src="https://xpydernetwork.com/user/user/${lastPhoto}" alt="Last User Photo" style="width:130%; height:150%;" class="thumbnail card-img">
                                <h6><b>${user.user}'s Last Screenshot</b></h6>
                                <img src="https://xpydernetwork.com/user/user/${lastScreenshot}" alt="Last Screenshot" style="width:130%; height:150%;" class="thumbnail card-img">
                            </div>
                            
                            <!-- Second Column: 4 User Photos and 4 Screenshots -->
                            <div class="col-md-4">
                            <br>
                            <br>
                                <div class="d-flex flex-column mb-3">`;
                user.photos.slice(0, 4).forEach(photo => {
                    content += `<img src="https://xpydernetwork.com/user/user/${photo.photo_path}" style="width:60px; height:60px; margin-bottom: 5px;margin-left:20px;" alt="User Photo" class="small-img">`;
                });
                content += `
                                </div>
                                <br>
                                <br>
                                <br>
                                <div class="d-flex flex-column">`;
                user.screenshots.slice(0, 4).forEach(screenshot => {
                    content += `<img src="https://xpydernetwork.com/user/user/${screenshot.file_path}" style="width:60px; height:60px; margin-bottom: 5px;margin-left:20px;" alt="Screenshot" class="small-img">`;
                });
                content += `
                                </div>
                            </div>
                            
                            <!-- Third Column: Running Apps and Disconnect Button -->
                            <div class="col-md-4">
                                <h6><b>Running Apps (${user.user})</b></h6>
                                <ul class="list-unstyled">`;
                user.apps.forEach(app => {
                    const appName = app.name.replace('.exe', '');
                    content += `<li class="m-1"><button class="btn btn-info btn-sm">${appName}</button></li>`;
                });
                content += `
                                </ul>
                                <button class="btn btn-primary mt-3 disconnect-btn" data-id="${user.id}">Disconnect</button>
<a href="see_all_photos.php?user_id=${user.id}" class="btn btn-primary mt-3">See All Photos</a>
    <a href="see_all_ss.php?user_id=${user.id}" class="btn btn-primary mt-3">See All Screenshots</a>
                            </div>
                        </div>
                    </div>`;

                userCount++;

                if (userCount % 2 === 0) {
                    // Close the row after two users
                    content += `</div>`;
                }
            });

            // If there are any users remaining, close the row
            if (userCount % 2 !== 0) {
                content += `</div>`;
            }

            // Inject the content into the div
            $('#user-monitoring').html(content);
        },
        error: function(xhr, status, error) {
            console.error("Error loading user data: ", error);
        }
    });
}

// Load user data every 30 seconds
setInterval(loadUserData, 30000);

// Initial load
loadUserData();
</script>

<script>
    // Handle disconnect user functionality
    $(document).on('click', '.disconnect-btn', function() {
        const userId = $(this).data('id'); // Get the user ID from the data attribute

        $.ajax({
            url: 'disconnect_user.php', // PHP script to handle the update
            method: 'POST',
            data: { id: userId }, // Send the user ID in the POST request
            success: function(response) {
                alert('User disconnected successfully.');
                loadUserData(); // Reload the data to update the view
            },
            error: function(xhr, status, error) {
                console.error("Error disconnecting user: ", error);
            }
        });
    });
</script>

</body>
</html>
