<?php
include("connection.php");
include("header.php");
?>
<div class="main-content">
    <div class="page-content">
        <div class="container-fluid">
            <!-- start page title -->
            <div class="row">
                <div class="col-12">
                    <div class="page-title-box d-sm-flex align-items-center justify-content-between">
                        <h4 class="mb-sm-0 font-size-18">View gallery/video </h4>
                    </div>
                </div>
            </div>
            <!-- end page title -->

            <div class="row">
                <div class="col-lg-12">
                    <div class="card">
                        <div class="card-body">
                            <div class="row mb-2">
                                <div class="col-sm-6 mt-3">
                                    <div class="search-box me-2 d-inline-block">
                                        <div class="position-relative">
                                            <input type="text" class="form-control" autocomplete="off" id="searchInput" onkeyup="searchTable()" placeholder="Search...">
                                            <i class="bx bx-search-alt search-icon"></i>
                                        </div>
                                    </div>

                                </div>
                                <div class="col-sm-6 mt-3">
                                    <div class="text-sm-end">
                                        <a href="addgallery.php"> <button type="button" data-bs-toggle="modal" data-bs-target="#newContactModal" class="btn btn-success btn-rounded waves-effect waves-light addContact-modal mb-2">
                                                <i class="mdi mdi-plus me-1"></i> New Contact
                                            </button></a>
                                    </div>
                                </div>
                                <!-- end col-->
                            </div>
                            <!-- end row -->
                            <div class="table-responsive">
                                <table class="table align-middle table-nowrap table-hover dt-responsive nowrap w-100" id="userList-table">
                                    <thead class="table-light">
                                        <tr>
                                            <th scope="col">S.no</th>
                                            <th scope="col">Type</th>
                                            <th scope="col">Caption</th>
                                            <th scope="col">Media</th>
                                            <!-- <th scope="col">Projects</th> -->
                                            <th scope="col" style="width: 200px">Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                        $i = 0;
                                        $query = "SELECT * FROM media ORDER BY id DESC";
                                        $result = mysqli_query($con, $query);

                                        while ($row = mysqli_fetch_assoc($result)) {
                                            $i++;
                                            $page_name = $row['page_name'];
                                            $photo     = $row['photo'];
                                            $caption   = $row['caption'];

                                            $media_path = "assets/images/gallery/" . $photo;
                                        ?>
                                            <tr>
                                                <td><?= $i; ?></td>

                                                <td><?= htmlspecialchars($page_name); ?></td>

                                                <td><?= htmlspecialchars($caption); ?></td>

                                                <td>
                                                    <?php
                                                    /** -------------------------
                                                     *  MANUFACTURING / PROJECT IMAGE VIEW
                                                     * -------------------------- */
                                                    if ($page_name === 'Manufacturing' || $page_name === 'Project') {

                                                        if (!empty($photo) && file_exists($media_path)) {
                                                            echo '<img src="' . $media_path . '" width="150" height="120" style="object-fit:cover;border-radius:6px;">';
                                                        } else {
                                                            echo "Image not found";
                                                        }
                                                    }

                                                    /** -------------------------
                                                     *  VIDEO UPLOAD + URL SUPPORT
                                                     * -------------------------- */
                                                    elseif ($page_name === 'Video') {

                                                        /** 1. Show uploaded video */
                                                        if (!empty($photo) && file_exists($media_path)) {

                                                            echo '
                    <video width="200" height="150" controls style="border-radius:6px;">
                        <source src="' . $media_path . '" type="video/mp4">
                        Your browser does not support the video tag.
                    </video>';
                                                        }
                                                        /** 2. If caption contains YouTube/Vimeo/external URL */
                                                        elseif (!empty($caption)) {

                                                            /** YouTube Support */
                                                            if (strpos($caption, 'youtube.com') !== false || strpos($caption, 'youtu.be') !== false) {

                                                                // Extract YouTube ID
                                                                parse_str(parse_url($caption, PHP_URL_QUERY), $yt_params);
                                                                $youtube_id = $yt_params['v'] ?? '';

                                                                // youtu.be link
                                                                if (!$youtube_id && strpos($caption, 'youtu.be') !== false) {
                                                                    $youtube_id = basename(parse_url($caption, PHP_URL_PATH));
                                                                }

                                                                if ($youtube_id) {
                                                                    echo '<iframe width="200" height="150" 
                                src="https://www.youtube.com/embed/' . $youtube_id . '" 
                                frameborder="0" allowfullscreen style="border-radius:6px;"></iframe>';
                                                                } else {
                                                                    echo "Invalid YouTube URL";
                                                                }
                                                            }
                                                            /** Vimeo Support */
                                                            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="200" height="150" frameborder="0" allowfullscreen style="border-radius:6px;"></iframe>';
                                                            }
                                                            /** Normal video link */
                                                            else {
                                                                echo '<a href="' . htmlspecialchars($caption) . '" target="_blank">View Video</a>';
                                                            }
                                                        }
                                                        /** 3. Nothing available */
                                                        else {
                                                            echo "No video available.";
                                                        }
                                                    }
                                                    ?>
                                                </td>

                                                <td>
                                                    <a href="editgallery.php?id=<?= $row['id']; ?>">
                                                        <button class="btn btn-primary btn-rounded mb-2">
                                                            <i class="far fa-edit"></i> Edit
                                                        </button>
                                                    </a>

                                                    <a onclick="return confirm('Are you sure you want to delete?')"
                                                        href="deletegallery.php?id=<?= $row['id']; ?>">
                                                        <button class="btn btn-danger btn-rounded mb-2">
                                                            <i class="fas fa-trash"></i> Delete
                                                        </button>
                                                    </a>
                                                </td>
                                            </tr>
                                        <?php } ?>
                                    </tbody>


                                </table>
                                <!-- end table -->
                            </div>
                            <!-- end table responsive -->
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!-- container-fluid -->
    </div>
    <!-- End Page-content -->




    <?php
    include("footer.php");
    ?>
</div>