 <?php
    include("admin/connection.php");
    include('include/header.php');
    ?>
 <div class="breadcumb-wrapper" data-bg-src="assets/img/hero/banner5.webp">
     <div class="container">
         <div class="breadcumb-content">
             <h1 class="breadcumb-title">Our Video</h1>
             <ul class="breadcumb-menu">
                 <li><a href="<?php path(); ?>/index.php">Home</a></li>
                 <li>Our Video</li>
             </ul>
         </div>
     </div>
 </div>
 <section class="gallery-inner-sec space">
     <div class="container">

         <div class="row gy-4 justify-content-center filter-active">

             <?php
                $gallery = mysqli_query($con, "SELECT * FROM media WHERE page_name = 'Video' ORDER BY id DESC");

                while ($row = mysqli_fetch_array($gallery)) {
                    $photo = $row['photo'];
                    $caption = $row['caption']; // For video links
                    $video_path = 'admin/assets/images/gallery/' . $photo;

                    echo '<div class="col-6 col-lg-3 col-xl-3 col-md-6 col-xxl-auto filter-item cat3 cat2 cat4">';


                    // Display uploaded video file
                    if (!empty($photo) && file_exists($video_path)) {
                        echo '<video style="width: 100%; height: 250px; border-radius: 8px;" controls>
                            <source src="' . $video_path . '" type="video/mp4">
                            Your browser does not support the video tag.
                        </video>';
                    }
                    // Display embedded YouTube or Vimeo links
                    elseif (!empty($caption)) {
                        // YouTube
                        if (strpos($caption, 'youtube.com') !== false || strpos($caption, 'youtu.be') !== false) {
                            parse_str(parse_url($caption, PHP_URL_QUERY), $youtube_params);
                            $youtube_id = $youtube_params['v'] ?? '';

                            if (!$youtube_id && strpos($caption, 'youtu.be') !== false) {
                                $youtube_id = basename(parse_url($caption, PHP_URL_PATH));
                            }

                            if ($youtube_id) {
                                echo '<iframe width="100%" height="250" src="https://www.youtube.com/embed/' . $youtube_id . '" frameborder="0" allowfullscreen style="border-radius: 8px;"></iframe>';
                            } else {
                                echo '<a href="' . htmlspecialchars($caption) . '" target="_blank">View Video</a>';
                            }
                        }
                        // Vimeo
                        elseif (strpos($caption, 'vimeo.com') !== false) {
                            $vimeo_id = basename(parse_url($caption, PHP_URL_PATH));
                            echo '<iframe src="https://player.vimeo.com/video/' . $vimeo_id . '" width="100%" height="250" frameborder="0" allowfullscreen style="border-radius: 8px;"></iframe>';
                        }
                        // Other link
                        else {
                            echo '<a href="' . htmlspecialchars($caption) . '" target="_blank">View Video</a>';
                        }
                    }
                    // No video
                    else {
                        echo '<p>No video available.</p>';
                    }


                    echo '</div>'; // .filter-item
                }
                ?>
         </div>
     </div>
 </section>
 <?php
    include("include/footer.php");
    ?>