<?php
require_once __DIR__ . "/../MaintenanceCheck.php";
session_start();

require "../Auth/db.php";

$username = $_SESSION["user"]["username"] ?? "Guest";

$avatar = "../Assets/Logo.png";

if (
    !empty($_SESSION["user"]["avatar"]) &&
    !empty($_SESSION["user"]["discord_id"])
) {
    $avatar = "https://cdn.discordapp.com/avatars/" .
        $_SESSION["user"]["discord_id"] . "/" .
        $_SESSION["user"]["avatar"] . ".png";
}

$selectedCategory = $_GET["category"] ?? "Most Popular";

if ($selectedCategory === "Most Popular") {

    $stmt = $pdo->query("
        SELECT *
        FROM media_ppv
        WHERE category != '24/7'
        AND (
            start_time_bst IS NULL
            OR start_time_bst <= NOW()
        )
        ORDER BY id DESC
    ");

    $ppvs = $stmt->fetchAll(PDO::FETCH_ASSOC);

} elseif ($selectedCategory === "Most Recent") {

    $stmt = $pdo->query("
        SELECT *
        FROM media_ppv
        ORDER BY created_at DESC
    ");

    $ppvs = $stmt->fetchAll(PDO::FETCH_ASSOC);

} else {

    $stmt = $pdo->prepare("
        SELECT *
        FROM media_ppv
        WHERE category = ?
        ORDER BY id DESC
    ");

    $stmt->execute([$selectedCategory]);

    $ppvs = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">

<title>PPV - Rewire</title>

<link rel="icon" type="image/png" href="../Assets/Logo.png">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<script src="https://unpkg.com/lucide@latest"></script>

<style>

*{
    margin:0;
    padding:0;
    box-sizing:border-box;
    font-family:Arial,sans-serif;
}

body{
    background:#050505;
    color:white;
}

/* HEADER */

.header{
    position:fixed;
    top:0;
    left:0;
    width:100%;
    height:82px;
    background:rgba(5,5,5,0.96);
    backdrop-filter:blur(12px);
    border-bottom:1px solid rgba(255,255,255,0.06);
    z-index:999;

    padding:15px 25px;

    display:flex;
    align-items:center;
    justify-content:space-between;
}

.logo-img {
    height: 70px;
}

.right {
    display: flex;
    align-items: center;
    gap: 30px;
}

.nav {
    display: flex;
    align-items: center;
    gap: 25px;
}

.home-btn,
.browse-btn {
    background: none;
    border: none;
    color: #ccc;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    text-decoration: none;
    transition: 0.25s ease;
}

.home-btn:hover,
.browse-btn:hover {
    color: #00c2ff;
    transform: scale(1.08);
}

.home-btn i {
    width: 22px;
    height: 22px;
}

.browse-btn {
    gap: 6px;
}

.browse-btn i {
    width: 21px;
    height: 21px;
}

.browse-arrow {
    width: 16px !important;
    height: 16px !important;
}

/* PROFILE */

.profile-dropdown {
    position: relative;
    cursor: pointer;
}

.profile-avatar {
    width: 36px;
    height: 36px;
    border-radius: 10px;
    object-fit: cover;
    border: 1px solid rgba(255,255,255,0.25);
}

.profile-menu {
    position: absolute;
    top: 48px;
    right: 0;
    width: 170px;
    background: rgba(8,10,13,0.96);
    border: 1px solid rgba(255,255,255,0.14);
    border-radius: 12px;
    padding: 10px;
    display: none;
    box-shadow: 0 12px 35px rgba(0,0,0,0.55);
}

.profile-menu.active {
    display: block;
}

.profile-menu-name {
    color: white;
    font-size: 0.85rem;
    font-weight: bold;
    padding: 8px 10px;
    border-bottom: 1px solid rgba(255,255,255,0.1);
    margin-bottom: 8px;
}

.profile-menu a {
    display: block;
    padding: 9px 10px;
    border-radius: 8px;
    color: #00c2ff;
    text-decoration: none;
    font-size: 0.9rem;
    font-weight: bold;
}

.profile-menu a:hover {
    background: rgba(0,194,255,0.12);
}

/* PAGE */

.ppv-page{
    max-width:1400px;
    margin:0 auto;
    padding:130px 35px 60px;
}

/* TABS */

.category-tabs{
    display:flex;
    gap:30px;
    overflow-x:auto;
    padding-bottom:14px;
    margin-bottom:35px;
    border-bottom:1px solid rgba(255,255,255,0.08);
}

.category-tabs::-webkit-scrollbar{
    display:none;
}

.cat-tab{
    background:none;
    border:none;
    color:#8f9aa5;
    font-weight:800;
    cursor:pointer;
    white-space:nowrap;
    padding-bottom:10px;
    position:relative;
    transition:0.25s ease;
    font-size:0.95rem;
    text-decoration:none;
}

.cat-tab:hover,
.cat-tab.active{
    color:white;
}

.cat-tab.active::after{
    content:"";
    position:absolute;
    left:0;
    bottom:-15px;
    width:100%;
    height:2px;
    background:#00c2ff;
    box-shadow:0 0 14px rgba(0,194,255,0.8);
}

/* GRID */

.ppv-grid{
    display:grid;
    grid-template-columns:repeat(auto-fill,minmax(320px,1fr));
    gap:26px;
}

.ppv-card{
    text-decoration:none;
    color:white;
    transition:0.3s ease;
}

.ppv-card:hover{
    transform:translateY(-4px);
}

.ppv-banner{
    width:100%;
    height:190px;
    border-radius:18px;
    background-size:cover;
    background-position:center;
    background-repeat:no-repeat;
    border:1px solid rgba(255,255,255,0.08);
    position:relative;
    overflow:hidden;
}

.ppv-banner::after{
    background-color:#000;
    content:"";
    position:absolute;
    inset:0;
    background:linear-gradient(to top, rgba(0,0,0,0.65), transparent);
}

.ppv-logo{
    position:absolute;
    bottom:14px;
    left:14px;
    max-width:140px;
    max-height:60px;
    object-fit:contain;
    z-index:2;
}

.live-pill{
    position:absolute;
    top:14px;
    right:14px;
    z-index:2;

    height:32px;
    padding:0 14px;

    border-radius:999px;

    background:#ff2b2b;

    display:flex;
    align-items:center;
    justify-content:center;

    font-size:0.78rem;
    font-weight:900;

    box-shadow:0 0 18px rgba(255,43,43,0.4);
}

.ppv-info{
    padding-top:14px;
}

.ppv-title{
    font-size:1.05rem;
    font-weight:800;
    margin-bottom:8px;
}

.ppv-meta{
    color:#8f9aa5;
    font-size:0.82rem;
    line-height:1.5;
}

.ppv-meta span{
    color:#00c2ff;
}

.empty-message{
    color:#8f9aa5;
    font-weight:800;
    padding:40px 0;
}

/* RESPONSIVE */

@media (max-width:760px){

    .ppv-page{
        padding:120px 18px 40px;
    }

    .ppv-grid{
        grid-template-columns:1fr;
    }

}

.live-viewers{
    color:#ff3b3b;
    font-size:0.9rem;
    font-weight:900;
}

.start-time{
    color:#d8dde5;
    font-size:1rem;
    font-weight:800;
    letter-spacing:1px;
}

.search-icon-btn {
    background: none;
    border: none;
    color: #ccc;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: 0.25s ease;
}

.search-icon-btn:hover {
    color: #00c2ff;
    transform: scale(1.08);
}

.search-icon-btn i,
.search-icon-btn svg {
    width: 22px;
    height: 22px;
}

.browse-dropdown {
    position: relative;
    display: flex;
    align-items: center;
}

.browse-menu {
    position: absolute;
    top: 34px;
    left: 50%;
    transform: translateX(-50%);
    width: 320px;
    background: rgba(8,10,13,0.96);
    border: 1px solid rgba(255,255,255,0.12);
    border-radius: 20px;
    padding: 18px 14px;
    display: none;
    box-shadow: 0 18px 45px rgba(0,0,0,0.6);
    z-index: 999;
}

.browse-menu.active {
    display: block;
}

.browse-title {
    text-align: center;
    font-weight: 900;
    margin-bottom: 20px;
}

.browse-section-label {
    color: #8f9aa5;
    font-size: 0.72rem;
    font-weight: 900;
    letter-spacing: 1px;
    margin: 16px 0 10px;
}

.browse-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 12px;
}

.browse-personal {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
}

.browse-menu a {
    width: auto;
    height: 76px;
    padding: 0;
    border-radius: 12px;
    color: #ccc;
    text-decoration: none;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 7px;
    font-size: 0.72rem;
    font-weight: 800;
    position: relative;
    transition: 0.3s;
}

.browse-menu a i {
    color: #00c2ff;
    width: 24px;
    height: 24px;
}

.browse-menu a::after {
    content: "";
    position: absolute;
    bottom: 10px;
    left: 50%;
    transform: translateX(-50%);
    width: 0%;
    height: 2px;
    background: #00c2ff;
    transition: 0.3s;
    border-radius: 999px;
}

.browse-menu a:hover {
    color: white;
    background: rgba(0,194,255,0.10);
}

.browse-menu a:hover::after {
    width: 55%;
}

/* GRID */

.ppv-grid{
    display:grid;
    grid-template-columns:repeat(auto-fill,minmax(220px,1fr));
    gap:26px 20px;
}

.ppv-card{
    width:220px;
    text-decoration:none;
    color:white;
    transition:0.3s ease;
    display:block;
}

.ppv-card:hover{
    transform:translateY(-4px);
}

.ppv-banner{
    width:220px;
    height:330px;
    border-radius:10px;
    background-size:cover;
    background-position:center;
    background-repeat:no-repeat;
    border:1px solid rgba(255,255,255,0.08);
    position:relative;
    overflow:hidden;
    background-color:#111;
}

.ppv-info{
    padding-top:11px;
}

.ppv-title{
    font-size:0.95rem;
    font-weight:700;
    margin-bottom:5px;
    color:white;
}

.ppv-meta{
    color:#8f9aa5;
    font-size:0.78rem;
    line-height:1.4;
}

.ppv-meta span{
    color:#00c2ff;
}

/* SEARCH MODAL */

.search-modal {
    position: fixed;
    inset: 0;
    z-index: 2000;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    pointer-events: none;
    transition: 0.35s ease;
}

.search-modal.active {
    opacity: 1;
    pointer-events: auto;
}

.search-blur {
    position: absolute;
    inset: 0;
    background: rgba(0,0,0,0.78);
    backdrop-filter: blur(0px);
    transition: 0.35s ease;
}

.search-modal.active .search-blur {
    backdrop-filter: blur(10px);
}

.search-box {
    position: relative;
    z-index: 2;
    width: 620px;
    max-width: 92%;
    transform: translateY(40px) scale(0.95);
    opacity: 0;
    transition: 0.35s cubic-bezier(.2,.8,.2,1);
}

.search-modal.active .search-box {
    transform: translateY(0) scale(1);
    opacity: 1;
}

.search-top {
    display: flex;
    align-items: center;
    margin-bottom: 12px;
}

.search-top h2 {
    margin-right: auto;
}

.close-search {
    width: 42px;
    height: 42px;
    background: #101216;
    color: white;
    border: 1px solid rgba(255,255,255,0.18);
    border-radius: 9px;
    cursor: pointer;
}

.search-input-wrap {
    height: 54px;
    background: #080a0d;
    border: 1px solid rgba(255,255,255,0.18);
    border-radius: 13px;
    display: flex;
    align-items: center;
    padding: 0 16px;
    gap: 12px;
}

.search-input-wrap input {
    flex: 1;
    background: transparent;
    border: none;
    outline: none;
    color: white;
    font-size: 1rem;
}

.clear-btn {
    background: none;
    border: none;
    color: #aaa;
    cursor: pointer;
}

.search-results {
    margin-top: 14px;
    background: rgba(5,5,5,0.94);
    border: 1px solid rgba(255,255,255,0.12);
    border-radius: 16px;
    padding: 12px;
    display: none;
    max-height: 420px;
    overflow-y: auto;
}

.search-results.active {
    display: block;
}

.result-card {
    display: flex;
    gap: 14px;
    padding: 12px;
    border-radius: 12px;
    transition: 0.25s;
    text-decoration: none;
    color: inherit;
}

.result-card:hover {
    background: rgba(255,255,255,0.06);
}

.result-card img {
    width: 64px;
    height: 96px;
    object-fit: cover;
    object-position: center;
    border-radius: 6px;
}

.result-info h3 {
    font-size: 1rem;
    margin-bottom: 6px;
}

.result-info p {
    color: #8c95a3;
    font-size: 0.82rem;
}
</style>
</head>

<body>
<?php include "../Preloader.php"; ?>

<header class="header">
    <div class="left">
        <img src="../Assets/Logo.png" class="logo-img">
    </div>
    <div class="right">
        <div class="nav">
            <a href="../Index.php" class="home-btn">
                <i data-lucide="house"></i>
            </a>
            <div class="browse-dropdown">
                <button type="button" class="browse-btn" onclick="toggleBrowseMenu(event)">
                    <i data-lucide="grid-2x2"></i>
                    <i data-lucide="chevron-down" class="browse-arrow"></i>
                </button>
                <div class="browse-menu" id="browseMenu">
                    <div class="browse-title">
                        Browse
                    </div>
                    <div class="browse-section-label">
                        CONTENT
                    </div>
                    <div class="browse-grid">
                        <a href="../Movies/Movies.php">
                            <i data-lucide="clapperboard"></i>
                            Movies
                        </a>
                        <a href="../Series/Series.php">
                            <i data-lucide="tv"></i>
                            TV Shows
                        </a>
                        <a href="PPV.php">
                            <i data-lucide="ticket"></i>
                            PPV
                        </a>

                    </div>

                    <div class="browse-section-label">
                        PERSONAL
                    </div>

                    <div class="browse-personal">

                        <a href="../History.php">
                            <i data-lucide="history"></i>
                            History
                        </a>

                        <a href="../Watchlist.php">
                            <i data-lucide="heart"></i>
                            Watchlist
                        </a>
                    </div>
                </div>
            </div>
        </div>
        <button type="button" class="search-icon-btn" onclick="openSearch()">
            <i data-lucide="search"></i>
        </button>
        <div class="profile-dropdown" onclick="toggleProfileMenu()">
        
            <img src="<?php echo htmlspecialchars($avatar); ?>" class="profile-avatar">
        
            <div class="profile-menu" id="profileMenu">
        
                <div class="profile-menu-name">
                    <?php echo htmlspecialchars($username); ?>
                </div>
        
                <a href="../Index.php">
                    Back To Home
                </a>
        
                <a href="../Auth/logout.php">
                    Logout
                </a>
        
            </div>
        
        </div>
    </div>
</header>
<div class="ppv-page">

    <div class="category-tabs">

        <?php
        $categories = [
            "Most Popular",
            "Football",
            "Basketball",
            "American Football",
            "Hockey",
            "Combat Sports",
            "Motorsports",
            "24/7"
        ];
        ?>

        <?php foreach ($categories as $cat): ?>

            <a
                href="PPV.php?category=<?php echo urlencode($cat); ?>"
                class="cat-tab <?php echo $selectedCategory === $cat ? 'active' : ''; ?>"
            >
                <?php echo htmlspecialchars($cat); ?>
            </a>
        <?php endforeach; ?>
    </div>
    <div class="ppv-grid">
    <?php foreach ($ppvs as $ppv): ?>
    
        <?php
    
        date_default_timezone_set('Europe/London');
    
        $banner = !empty($ppv["banner"])
            ? "../" . $ppv["banner"]
            : "https://picsum.photos/900/500?random=" . $ppv["id"];
    
        $isLive = false;
    
        if (!empty($ppv["start_time_bst"])) {
    
            $startTimestamp = strtotime($ppv["start_time_bst"]);
            $currentTimestamp = time();
    
            if ($currentTimestamp >= $startTimestamp) {
                $isLive = true;
            }
        }
    
        ?>
    
        <a
            href="Preset.php?id=<?php echo $ppv["id"]; ?>"
            class="ppv-card"
        >
    
            <div
                class="ppv-banner"
                style="background-image:url('<?php echo htmlspecialchars($banner); ?>')"
            >
    
                <?php if ($isLive): ?>
                    <div class="live-pill">
                        LIVE
                    </div>
                <?php endif; ?>
    
            </div>
    
            <div class="ppv-info">
    
                <div class="ppv-title">
                    <?php echo htmlspecialchars($ppv["title"]); ?>
                </div>
    
                <div class="ppv-meta">
    
                    <?php if ($isLive): ?>
    
                        <?php
    
                        $viewerStmt = $pdo->prepare("
                            SELECT COUNT(*)
                            FROM active_ppv_viewers
                            WHERE ppv_id = ?
                            AND last_seen >= NOW() - INTERVAL 30 SECOND
                        ");
    
                        $viewerStmt->execute([$ppv["id"]]);
    
                        $liveViewerCount = (int)$viewerStmt->fetchColumn();
    
                        ?>
    
                        <span class="live-viewers">
                            ● <?php echo number_format($liveViewerCount); ?> Watching
                        </span>
    
                    <?php else: ?>
    
                    <span class="start-time">
                        <?php
                        echo date(
                            "F j • g:i A",
                            strtotime($ppv["start_time_bst"])
                        );
                        ?>
                    </span>
    
                    <?php endif; ?>
    
                </div>
    
            </div>
    
        </a>
    
    <?php endforeach; ?>
        <?php if (empty($ppvs)): ?>

            <div class="empty-message">
                No PPV Events Found.
            </div>
        <?php endif; ?>
    </div>
</div>

<div class="search-modal" id="searchModal">

    <div class="search-blur" onclick="closeSearch()"></div>

    <div class="search-box">

        <div class="search-top">
            <h2>Search</h2>

            <button class="close-search" onclick="closeSearch()">
                ✕
            </button>
        </div>

        <div class="search-input-wrap">

            <i data-lucide="search"></i>

            <input
                type="text"
                id="searchInput"
                placeholder="Type here to search..."
                oninput="showResults()"
            >

            <button onclick="clearSearch()" class="clear-btn">
                ✕
            </button>

        </div>

        <div class="search-results" id="searchResults">

            <?php foreach ($ppvs as $ppv): ?>

                <?php
                $searchThumb = !empty($ppv["banner"])
                    ? "../" . $ppv["banner"]
                    : "https://picsum.photos/220/330?random=" . $ppv["id"];
                ?>

                <a
                    href="Preset.php?id=<?php echo $ppv["id"]; ?>"
                    class="result-card"
                >

                    <img src="<?php echo htmlspecialchars($searchThumb); ?>">

                    <div class="result-info">

                        <h3>
                            <?php echo htmlspecialchars($ppv["title"]); ?>
                        </h3>

                        <p>
                            PPV |
                            <?php echo htmlspecialchars($ppv["category"] ?? "Unknown"); ?>
                        </p>

                    </div>

                </a>

            <?php endforeach; ?>

        </div>

    </div>

</div>

<script>
lucide.createIcons();
function toggleProfileMenu() {

    const menu = document.getElementById("profileMenu");

    if (menu) {
        menu.classList.toggle("active");
    }

}

document.addEventListener("click", function(e) {
    const dropdown = document.querySelector(".profile-dropdown");
    const menu = document.getElementById("profileMenu");
    if (dropdown && menu && !dropdown.contains(e.target)) {
        menu.classList.remove("active");
    }
});
</script>

<script>
lucide.createIcons();

const searchModal = document.getElementById("searchModal");
const searchInput = document.getElementById("searchInput");
const searchResults = document.getElementById("searchResults");

function openSearch() {

    if (!searchModal) return;

    searchModal.classList.add("active");

    setTimeout(() => {
        if (searchInput) {
            searchInput.focus();
        }
    }, 100);
}

function closeSearch() {

    if (!searchModal) return;

    searchModal.classList.remove("active");

    setTimeout(() => {

        if (searchResults) {
            searchResults.classList.remove("active");
        }

        if (searchInput) {
            searchInput.value = "";
        }

    }, 300);
}

function showResults() {

    const term = searchInput.value.trim().toLowerCase();

    const cards = document.querySelectorAll("#searchResults .result-card");

    searchResults.classList.toggle("active", term.length > 0);

    cards.forEach(card => {

        const title = card.querySelector("h3").textContent.toLowerCase();

        const meta = card.querySelector("p").textContent.toLowerCase();

        if (title.includes(term) || meta.includes(term)) {

            card.style.display = "flex";

        } else {

            card.style.display = "none";

        }

    });
}

function clearSearch() {

    searchInput.value = "";

    searchResults.classList.remove("active");

    document.querySelectorAll("#searchResults .result-card").forEach(card => {
        card.style.display = "flex";
    });

    searchInput.focus();
}

function toggleProfileMenu() {

    const menu = document.getElementById("profileMenu");

    if (menu) {
        menu.classList.toggle("active");
    }
}

function toggleBrowseMenu(event) {

    event.stopPropagation();

    const menu = document.getElementById("browseMenu");

    if (menu) {
        menu.classList.toggle("active");
    }
}

document.addEventListener("click", function(e) {

    const dropdown = document.querySelector(".profile-dropdown");
    const menu = document.getElementById("profileMenu");

    if (dropdown && menu && !dropdown.contains(e.target)) {
        menu.classList.remove("active");
    }

    const browseMenu = document.getElementById("browseMenu");
    const browseDropdown = document.querySelector(".browse-dropdown");

    if (browseMenu && browseDropdown && !browseDropdown.contains(e.target)) {
        browseMenu.classList.remove("active");
    }

});

document.addEventListener("keydown", function(e) {

    if (e.key === "Escape") {
        closeSearch();
    }

});
</script>

<script src="../Security.js"></script>

</body>
</html>