<?php
check_loggedin($pdo);

if (isset($_POST["RecordPath"]) || isset($_POST["RecordName"])) {
    $RecordPath = $_POST['RecordPath'];
    $RecordName = $_POST['RecordName'];
    $id = $_POST['id']; // Get the ID from the AJAX request

    // Fetch video details from the database
    $stmt = $pdo->prepare('SELECT * FROM DeviceRecordInfoUsers, Devices WHERE DeviceRecordInfoUsers.DeviceId = Devices.DeviceId AND RecordPath = ? AND RecordName = ?');
    $stmt->bindValue(1, $RecordPath);
    $stmt->bindValue(2, $RecordName);
    $stmt->execute();
    $Record_video = $stmt->fetch(PDO::FETCH_ASSOC);
    $Record_video_client_id = $Record_video['ClientId'];

    // Set the video path in the session
    $_SESSION['playvideo'] = "../../clients/" . $Record_video_client_id . "/" . $RecordPath . "/" . $RecordName;

    // Generate the video element with a unique source URL
    $uniqueParam = time(); // Add a timestamp to make the URL unique
    echo '<video id="videoPlayer_' . htmlspecialchars($id) . '" style="width:100%" controls>
              <source src="/playbackvideohome?t=' . $uniqueParam . '" type="video/mp4">
              Your browser does not support the video tag.
          </video>';
}
?>