 <?php
    include("admin/connection.php");
    include('include/header.php');
    ?>


 <main class="main">

     <!-- breadcrumb -->
     <div class="site-breadcrumb" style="background: url(assets/img/breadcrumb/01.jpg)">
         <div class="container">
             <h2 class="breadcrumb-title">Video</h2>
             <ul class="breadcrumb-menu">
                 <li><a href="index.php">Home</a></li>
                 <li class="active">Video</li>
             </ul>
         </div>
     </div>
     <!-- breadcrumb end -->


     <!-- gallery-area -->
     <div class="gallery-area py-100">
         <div class="container">

             <div class="row popup-gallery mt-5">
             <?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'];   // uploaded video file
                    $caption    = $row['caption']; // video link (YouTube / Vimeo / Others)
                    $video_path = 'admin/assets/images/gallery/' . $photo;
                ?>
                 <div class="col-md-12 col-lg-3 mb-4">

                     <?php
                        // ✅ Uploaded video file
                        if (!empty($photo) && file_exists($video_path)) { ?>
                         <video style="width: 100%; height: 250px; border-radius: 8px;" controls>
                             <source src="<?php echo $video_path; ?>" type="video/mp4">
                             Your browser does not support the video tag.
                         </video>

                         <?php
                        }
                        // ✅ Embedded YouTube / 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'] ?? '';

                                // handle short links (youtu.be/xxxx)
                                if (!$youtube_id && strpos($caption, 'youtu.be') !== false) {
                                    $youtube_id = basename(parse_url($caption, PHP_URL_PATH));
                                }

                                if ($youtube_id) { ?>
                                 <iframe width="100%" height="250"
                                     src="https://www.youtube.com/embed/<?php echo $youtube_id; ?>"
                                     frameborder="0" allowfullscreen style="border-radius: 8px;"></iframe>
                             <?php } else { ?>
                                 <a href="<?php echo htmlspecialchars($caption); ?>" target="_blank">View Video</a>
                             <?php }
                            }
                            // Vimeo
                            elseif (strpos($caption, 'vimeo.com') !== false) {
                                $vimeo_id = basename(parse_url($caption, PHP_URL_PATH)); ?>
                             <iframe src="https://player.vimeo.com/video/<?php echo $vimeo_id; ?>"
                                 width="100%" height="250" frameborder="0"
                                 allowfullscreen style="border-radius: 8px;"></iframe>
                         <?php
                            }
                            // Other links
                            else { ?>
                             <a href="<?php echo htmlspecialchars($caption); ?>" target="_blank">View Video</a>
                         <?php
                            }
                        }
                        // ✅ No video available
                        else { ?>
                         <p>No video available.</p>
                     <?php } ?>

                 </div>
             <?php } ?>
         </div>
         </div>
     </div>
     <!-- gallery-area end -->

 </main>


 <?php
    include("include/footer.php");
    ?>