<?php
// All Errors दिखाने के लिए (Debugging Mode)
error_reporting(E_ALL);
ini_set('display_errors', 1);

require("top.php"); // Database Connection और Header

$msg = "";

// ✅ जब फॉर्म सबमिट हो
if (isset($_POST['submit'])) {
    if (!empty($_POST['user_id']) && !empty($_POST['video_id'])) {
        $user_id = mysqli_real_escape_string($con, $_POST['user_id']);
        
        // Check if 'All Videos' is selected
        if ($_POST['video_id'] == 'all') {
            // 🛠 Fetch all videos
            $videos_res = mysqli_query($con, "SELECT id FROM course_video WHERE status = 1");
            
            // 🛠 Loop through all videos and grant access
            while ($video = mysqli_fetch_assoc($videos_res)) {
                $video_id = $video['id'];
                
                // 🛠 Check: क्या user और video पहले से exist करता है?
                $check = mysqli_query($con, "SELECT * FROM user_video_access WHERE user_id='$user_id' AND video_id='$video_id'");
                if (mysqli_num_rows($check) == 0) {
                    // ✅ Video Access Grant करने के लिए Query
                    $sql = "INSERT INTO user_video_access (user_id, video_id) VALUES (?, ?)";
                    if ($stmt = mysqli_prepare($con, $sql)) {
                        mysqli_stmt_bind_param($stmt, "ii", $user_id, $video_id);
                        if (mysqli_stmt_execute($stmt)) {
                            $msg = "✅ All videos access granted successfully!";
                        } else {
                            $msg = "❌ Error inserting data: " . mysqli_error($con);
                        }
                        mysqli_stmt_close($stmt);
                    } else {
                        $msg = "❌ Error preparing the query.";
                    }
                } else {
                    $msg = "⚠ This user already has access to this video.";
                }
            }
        } else {
            $video_id = mysqli_real_escape_string($con, $_POST['video_id']);
            
            // 🛠 Check: क्या user और video पहले से exist करता है?
            $check = mysqli_query($con, "SELECT * FROM user_video_access WHERE user_id='$user_id' AND video_id='$video_id'");
            if (mysqli_num_rows($check) == 0) {
                // ✅ Video Access Grant करने के लिए Query
                $sql = "INSERT INTO user_video_access (user_id, video_id) VALUES (?, ?)";
                if ($stmt = mysqli_prepare($con, $sql)) {
                    mysqli_stmt_bind_param($stmt, "ii", $user_id, $video_id);
                    if (mysqli_stmt_execute($stmt)) {
                        $msg = "✅ Video access granted successfully!";
                    } else {
                        $msg = "❌ Error inserting data: " . mysqli_error($con);
                    }
                    mysqli_stmt_close($stmt);
                } else {
                    $msg = "❌ Error preparing the query.";
                }
            } else {
                $msg = "⚠ This user already has access to this video.";
            }
        }
    } else {
        $msg = "⚠ Please select both a user and a video.";
    }
}
?>

<!-- 🖥 UI for Granting Access -->
<div class="container" style="max-width: 600px; margin: 0 auto;">
    <form method="post">
        <h3 class="text-center">Grant Video Access</h3>
        
        <!-- 🟢 Select User -->
        <div class="form-group">
            <label for="user_id">Select User</label>
            <select name="user_id" class="form-control">
                <option value="">-- Select User --</option>
                <?php
                $users_res = mysqli_query($con, "SELECT id, username FROM user_register WHERE status = 1 ORDER BY username ASC");
                while ($user = mysqli_fetch_assoc($users_res)) {
                    echo "<option value='".$user['id']."'>".$user['username']."</option>";
                }
                ?>
            </select>
        </div>

        <!-- 🟢 Select Video -->
        <div class="form-group">
            <label for="video_id">Select Video</label>
            <select name="video_id" class="form-control">
                <option value="">-- Select Video --</option>
                <option value="all">-- All Videos --</option> <!-- Option for all videos -->
                <?php
                $videos_res = mysqli_query($con, "SELECT id, video_title FROM course_video WHERE status = 1 ORDER BY video_title ASC");
                while ($video = mysqli_fetch_assoc($videos_res)) {
                    echo "<option value='".$video['id']."'>".$video['video_title']."</option>";
                }
                ?>
            </select>
        </div>

        <!-- 🟢 Submit Button -->
        <div class="form-group text-center">
            <button type="submit" name="submit" class="btn btn-primary">Grant Access</button>
        </div>

        <!-- 🛠 Debugging Message -->
        <?php if ($msg != ""): ?>
            <div class="mt-3 alert alert-info"><?php echo $msg; ?></div>
        <?php endif; ?>
    </form>
</div>

<!-- 🎨 Custom Styling -->
<style>
    .container { margin-top: 50px; }
    .form-group { margin-bottom: 20px; }
    .btn { width: 100%; }
</style>
