<div class="container">
    <div class="text-center">
        <div class="card shadow mt-3">
            <h5 class="card-header text-dark text-center font-weight-bold">Edit Videos</h5>
            <div class="card-body">
                <div style="text-align: start;">
                    <button class="toggle-btn btn btn-info mb-5" onclick="toggleDivVideo()">Click To View
                        Videos</button>
                </div>
                <div id="contentVideo" style="display: none;">
                    <div class="row justify-content-center">
                        <?php 
                            $note_title = $edit_row['n_title'];
                            $n_id = $edit_row['n_id'];
                            $video_sql = "SELECT * FROM notes_img WHERE ni_type = 'video' AND ni_ref = '$edit_row[n_id]' ORDER BY ni_sl ASC";
                            $video_res = mysqli_query($con, $video_sql);
                            if ($video_total_rows = mysqli_num_rows($video_res) > 0) {
                                while ($video_row = mysqli_fetch_assoc($video_res)) {
                                    $video_id = $video_row['ni_sl'];
                                    $video_url = $video_row['ni_name'];
                        ?>
                        <div class="col-md-4 col-xs-4 mb-5">
                            <form method="POST">
                                <div class="note-video-card">
                                    <iframe width="100%" height="200"
                                        src="https://www.youtube.com/embed/<?php echo $video_url; ?>" frameborder="0"
                                        loading="lazy" allowfullscreen></iframe>
                                    <br>
                                    <input type="hidden" name="video_id" value="<?php echo $video_id; ?>"
                                        class="form-control" />
                                    <input type="hidden" name="note_title" value="<?php echo $note_title; ?>"
                                        class="form-control" />
                                    <input type="text" name="new_video_url" placeholder="Enter new YouTube URL"
                                        class="form-control mt-3" />
                                    <input type="submit" value="Update Video" name="updateVideo"
                                        class="btn btn-success text-sm-center mt-4" />
                                    <input type="submit" value="Delete Video" name="deleteVideo"
                                        class="btn btn-danger text-sm-center mt-4" />
                                </div>
                            </form>
                        </div>
                        <?php
                                }
                            }
                        ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
function toggleDivVideo() {
    var div = document.getElementById("contentVideo");
    if (div.style.display === "none" || div.style.display === "") {
        div.style.display = "block";
    } else {
        div.style.display = "none";
    }
}
</script>

<?php

// Check if form is submitted to update a video URL
if (isset($_POST['updateVideo'])) {
    $video_id = $_POST['video_id'];
    $new_video_url = $_POST['new_video_url'];
    
    $update_sql = "UPDATE notes_img SET ni_name = '$new_video_url' WHERE ni_sl = '$video_id'";
    $update_result = mysqli_query($con, $update_sql);

    if ($update_result) {
        echo '<script>alert("Video updated successfully.");</script>';
        echo '<script>window.location = "edit-notes.php?edit_ref=' . $_GET['edit_ref'] . '";</script>';
        exit;
    } else {
        echo '<script>alert("Error updating video in the database.");</script>';
    }
}

// Check if form is submitted to delete a video
if (isset($_POST['deleteVideo'])) {
    $video_id = mysqli_real_escape_string($con, $_POST['video_id']);
    $note_id = mysqli_real_escape_string($con, $_POST['note_title']);

    $delete_sql = "DELETE FROM notes_img WHERE ni_sl = '$video_id'";
    $delete_result = mysqli_query($con, $delete_sql);

    if ($delete_result) {
        echo '<script>alert("Video deleted successfully.");</script>';
    } else {
        echo '<script>alert("Error deleting video.");</script>';
    }

    echo '<script>window.location = "edit-notes.php?edit_ref=' . $_GET['edit_ref'] . '";</script>';
    exit;
}
?>