<?php
    // session_start();
    include "../db.php";
    require '../auth.php';
    restrictAccess(['Sub Associate']);
    date_default_timezone_set('Asia/Kolkata');
    $currentTime = date('M j, Y g:i a');

    if ( !isset($_SESSION['uid']) && !isset($_SESSION['uref']) && !isset($_SESSION['utype']) ) {
        header("Location: logout.php");
        exit();
    }
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <?php include "include/meta.php"; ?>
    <?php include "include/css.php"; ?>
    <?php include "include/php-functions.php"; ?>
</head>

<body style="background: #1a91c3;">
    <?php
    $tc_sql = mysqli_query($con, "SELECT * FROM users WHERE uid = '{$_SESSION["uid"]}' ");
    if($tc_row = mysqli_fetch_assoc($tc_sql)){ 
        $uTcIp = $tc_row["uTcIp"];
        $uTcDate = $tc_row["uTcDate"];
    }
    if(!empty($uTcIp) && !empty($uTcDate)){
        echo '<script>window.location.href = "index.php";</script>';  
    } else{ 
        // Fetch data from the database
        $query = "SELECT data_long_des FROM data WHERE data_catagory = 't&c'";
        $result = mysqli_query($con, $query);

        $termsConditions = '';
        if ($result && mysqli_num_rows($result) > 0) {
            $row = mysqli_fetch_assoc($result);
            $termsConditions = $row['data_long_des'];
        }
    ?>

    <div class="modal fade" id="defaultModal" tabindex="-1" aria-labelledby="defaultModalLabel" aria-hidden="true"
        data-bs-backdrop="static" data-bs-keyboard="false">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-body">
                    <!-- Close Button -->
                    <button type="button" class="btn-close position-absolute" style="right:10px; top:10px;"
                        aria-label="Close"></button>
                    <div align="justify">
                        <h5>Terms And Conditions</h5>
                        <?php echo $termsConditions; ?>
                    </div>
                    <div class="mt-2 mb-5 text-center">
                        <form action="" method="POST">
                            <button type="submit" class="btn btn-sm btn-success" name="accept">Accept</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <?php
    }
    ?>

    <?php include "include/js.php"; ?>
    <script>
    document.addEventListener('DOMContentLoaded', function() {
        // Show the modal on page load
        var modal = new bootstrap.Modal(document.getElementById('defaultModal'));
        modal.show();

        // Add an event listener to the close button
        document.querySelector('.btn-close').addEventListener('click', function() {
            // Redirect to logout.php on close button click
            window.location.href = 'logout.php';
        });
    });
    </script>

</body>

</html>

<?php
if (isset($_POST["accept"])) {
    // Get device IP address
    $utcIp = $_SERVER['REMOTE_ADDR'];

    // Check if uid exists in the session
    if (isset($_SESSION["uid"])) {
        $uid = $_SESSION["uid"];

        // Prepare and bind SQL update statement
        $stmt = $con->prepare("UPDATE users SET uTcDate = ?, uTcIp = ? WHERE uid = ?");
        $stmt->bind_param("ssi", $currentTime, $utcIp, $uid);

        // Execute the statement
        if ($stmt->execute()) {
            echo '<script>window.location.href = "index.php";</script>';
        } else {
            echo "Error: " . $stmt->error;
        }

        // Close connections
        $stmt->close();
    } else {
        echo "User ID not found in session.";
    }
}
?>