<?php
// templates/landing6.php
$site_settings = get_site_settings($pdo);
$landing_title = $site_settings['landing_title'] ?? 'UTAMU';
$favicon = $site_settings['site_favicon'] ?? 'assets/images/favicon.png';

if (!$featured && !empty($videos)) {
    $featured = $videos[0];
}
?>
<!DOCTYPE html>
<html lang="en">


<!-- Mirrored from michiriziutamu.tanzaniahub.click/ by HTTrack Website Copier/3.x [XR&CO'2014], Wed, 18 Mar 2026 16:28:16 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=utf-8" /><!-- /Added by HTTrack -->
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= htmlspecialchars($landing_title) ?> | Premium</title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&amp;display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css"><!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-WFSQPTZZLJ"></script>

</head>

<body>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
            background: #141414;
            color: #fff;
            min-height: 100vh;
            -webkit-user-select: none;
            user-select: none;
        }

        /* Navbar */
        .navbar {
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            z-index: 100;
            padding: 20px 4%;
            display: flex;
            align-items: center;
            justify-content: space-between;
            background: linear-gradient(180deg, rgba(0, 0, 0, 0.8) 0%, transparent 100%);
            transition: background 0.3s;
        }

        .navbar.scrolled {
            background: #141414;
        }

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

        .logo {
            font-size: 1.8rem;
            font-weight: 800;
            color: #e50914;
            text-decoration: none;
        }

        .nav-links {
            display: flex;
            gap: 25px;
            list-style: none;
        }

        .nav-links a {
            color: #e5e5e5;
            text-decoration: none;
            font-size: 0.9rem;
            transition: color 0.2s;
        }

        .nav-links a:hover {
            color: #fff;
        }

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

        .nav-icon {
            color: #fff;
            font-size: 1.2rem;
            cursor: pointer;
        }

        .avatar {
            width: 32px;
            height: 32px;
            background: #e50914;
            border-radius: 4px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: 600;
            font-size: 0.9rem;
        }

        /* Hero */
        .hero {
            position: relative;
            height: 50vh;
            min-height: 350px;
            background: linear-gradient(to right, rgba(0, 0, 0, 0.7) 0%, rgba(0, 0, 0, 0.3) 50%, rgba(0, 0, 0, 0.1) 100%),
                linear-gradient(to top, #141414 0%, transparent 30%),
                url('../images-wixmp-ed30a86b8c4ca887773594c2.wixmp.com/f/2bf7cf63-597a-4df9-a804-226faf3069d5/dh2zj0w-d328e6b7-ec7e-4759-9348-6cadc92640df.jp/v1/fill/w_360%2ch_540%2cq_7/beautiful_black_lady_wi?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1cm46YXBwOjdlMGQxODg5ODIyNjQzNzNhNWYwZDQxNWVhMGQyNmUwIiwiaXNzIjoidXJuOmFwcDo3ZTBkMTg4OTgyMjY0MzczYTVmMGQ0MTVlYTBkMjZlMCIsIm9iaiI6W1t7ImhlaWdodCI6Ijw9NTQwIiwicGF0aCI6Ii9mLzJiZjdjZjYzLTU5N2EtNGRmOS1hODA0LTIyNmZhZjMwNjlkNS9kaDJ6ajB3LWQzMjhlNmI3LWVjN2UtNDc1OS05MzQ4LTZjYWRjOTI2NDBkZi5qcGciLCJ3aWR0aCI6Ijw9MzYwIn1dXSwiYXVkIjpbInVybjpzZXJ2aWNlOmltYWdlLm9wZXJhdGlvbnMiXX0.2PzsUMbooAmSL7EqBJ9Xev5qB4Lg1eAKbkf1U7oUJo0') center/cover no-repeat;
            background-color: #141414;
        }

        .hero-content {
            position: absolute;
            bottom: 15%;
            left: 4%;
            max-width: 500px;
        }

        .hero-tag {
            display: inline-block;
            background: #e50914;
            padding: 4px 10px;
            font-size: 0.7rem;
            font-weight: 700;
            border-radius: 3px;
            margin-bottom: 12px;
        }

        .hero-title {
            font-size: 2.5rem;
            font-weight: 800;
            line-height: 1.1;
            margin-bottom: 12px;
        }

        .hero-meta {
            display: flex;
            align-items: center;
            gap: 12px;
            margin-bottom: 12px;
            font-size: 0.85rem;
        }

        .hero-meta .match {
            color: #46d369;
            font-weight: 600;
        }

        .hero-meta .year {
            color: #aaa;
        }

        .hero-meta .rating {
            border: 1px solid #aaa;
            padding: 0 5px;
            font-size: 0.75rem;
        }

        .hero-meta .duration {
            color: #aaa;
        }

        .hero-desc {
            font-size: 0.95rem;
            line-height: 1.4;
            color: #ccc;
            margin-bottom: 18px;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
            overflow: hidden;
        }

        .hero-buttons {
            display: flex;
            gap: 10px;
        }

        .btn {
            display: inline-flex;
            align-items: center;
            gap: 8px;
            padding: 10px 22px;
            border-radius: 4px;
            font-size: 0.95rem;
            font-weight: 600;
            cursor: pointer;
            border: none;
            transition: all 0.2s;
        }

        .btn-white {
            background: #fff;
            color: #000;
        }

        .btn-white:hover {
            background: rgba(255, 255, 255, 0.75);
        }

        .btn-gray {
            background: rgba(109, 109, 110, 0.7);
            color: #fff;
        }

        .btn-gray:hover {
            background: rgba(109, 109, 110, 0.5);
        }

        .hero-poster {
            position: absolute;
            right: 8%;
            top: 50%;
            transform: translateY(-50%);
            width: 220px;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 10px 40px rgba(0, 0, 0, 0.6);
            z-index: 2;
        }

        .hero-poster img {
            width: 100%;
            height: auto;
            display: block;
        }

        .hero-fade {
            position: absolute;
            bottom: 0;
            left: 0;
            right: 0;
            height: 150px;
            background: linear-gradient(transparent, #141414);
        }
    </style>
    <style>
        /* Content Row */
        .content-row {
            padding: 0 4%;
            margin-bottom: 50px;
            position: relative;
        }

        .row-header {
            display: flex;
            align-items: center;
            margin-bottom: 15px;
        }

        .row-title {
            font-size: 1.4rem;
            font-weight: 700;
        }

        .row-title i {
            color: #e50914;
            margin-right: 10px;
        }

        /* Movie Grid - 2 columns */
        .movie-grid {
            display: grid;
            grid-template-columns: repeat(2, 1fr);
            gap: 15px;
        }

        /* Movie Card */
        .movie-card {
            border-radius: 6px;
            overflow: hidden;
            cursor: pointer;
            transition: transform 0.3s ease;
            position: relative;
            background: #1a1a1a;
        }

        .movie-card:hover {
            transform: scale(1.08);
            z-index: 10;
        }

        .card-poster {
            position: relative;
            aspect-ratio: 16/10;
        }

        .card-poster video,
        .card-poster img {
            width: 100%;
            height: 100%;
            object-fit: cover;
            background: #000;
        }

        .card-overlay {
            position: absolute;
            inset: 0;
            background: linear-gradient(transparent 40%, rgba(0, 0, 0, 0.95));
            opacity: 0;
            transition: opacity 0.3s;
            display: flex;
            flex-direction: column;
            justify-content: flex-end;
            padding: 15px;
        }

        .movie-card:hover .card-overlay {
            opacity: 1;
        }

        .overlay-buttons {
            display: flex;
            gap: 8px;
            margin-bottom: 10px;
        }

        .circle-btn {
            width: 36px;
            height: 36px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 0.9rem;
            cursor: pointer;
            transition: all 0.2s;
            border: 2px solid rgba(255, 255, 255, 0.7);
            background: rgba(0, 0, 0, 0.6);
            color: #fff;
        }

        .circle-btn.play-btn {
            background: #fff;
            color: #000;
            border-color: #fff;
        }

        .circle-btn:hover {
            transform: scale(1.1);
        }

        .overlay-meta {
            display: flex;
            align-items: center;
            gap: 8px;
            font-size: 0.8rem;
        }

        .overlay-meta .match {
            color: #46d369;
            font-weight: 600;
        }

        .overlay-meta span {
            color: #aaa;
        }

        /* Badges */
        .card-badges {
            position: absolute;
            top: 8px;
            left: 8px;
            right: 8px;
            display: flex;
            justify-content: space-between;
            z-index: 5;
        }

        .badge {
            padding: 4px 10px;
            border-radius: 3px;
            font-size: 0.7rem;
            font-weight: 700;
        }

        .badge-new {
            background: #e50914;
        }

        .badge-hd {
            background: rgba(0, 0, 0, 0.7);
            border: 1px solid #666;
        }

        .badge-unlocked {
            background: #46d369;
            color: #000;
        }

        .preview-timer {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background: rgba(229, 9, 20, 0.95);
            padding: 10px 20px;
            border-radius: 20px;
            font-weight: 600;
            font-size: 0.9rem;
            display: none;
            z-index: 20;
        }

        .card-info {
            padding: 12px 15px;
        }

        .card-info h3 {
            font-size: 0.9rem;
            font-weight: 600;
            margin-bottom: 5px;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }

        .card-info p {
            font-size: 0.8rem;
            color: #888;
        }

        .card-price {
            position: absolute;
            bottom: 60px;
            right: 10px;
            background: #e50914;
            padding: 5px 10px;
            border-radius: 4px;
            font-size: 0.75rem;
            font-weight: 700;
        }
    </style>
    <style>
        /* Modal */
        .modal {
            display: none;
            position: fixed;
            inset: 0;
            z-index: 1000;
            background: rgba(0, 0, 0, 0.8);
            align-items: center;
            justify-content: center;
            padding: 20px;
        }

        .modal.active {
            display: flex;
        }

        .modal-box {
            background: #181818;
            border-radius: 8px;
            max-width: 420px;
            width: 100%;
            overflow: hidden;
            animation: slideUp 0.3s ease;
        }

        @keyframes slideUp {
            from {
                transform: translateY(30px);
                opacity: 0;
            }

            to {
                transform: translateY(0);
                opacity: 1;
            }
        }

        .modal-top {
            background: linear-gradient(135deg, #e50914, #b20710);
            padding: 30px;
            text-align: center;
            position: relative;
        }

        .modal-close {
            position: absolute;
            top: 12px;
            right: 12px;
            width: 32px;
            height: 32px;
            background: rgba(0, 0, 0, 0.5);
            border: none;
            border-radius: 50%;
            color: #fff;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .modal-close:hover {
            background: rgba(0, 0, 0, 0.7);
        }

        .modal-top h2 {
            font-size: 1.3rem;
            margin-bottom: 5px;
        }

        .modal-top p {
            opacity: 0.9;
            font-size: 0.9rem;
        }

        .modal-body {
            padding: 25px;
        }

        .price-box {
            text-align: center;
            margin-bottom: 25px;
        }

        .price-amount {
            font-size: 2.2rem;
            font-weight: 800;
            color: #46d369;
        }

        .price-label {
            color: #888;
            font-size: 0.85rem;
        }

        .form-group {
            margin-bottom: 18px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-size: 0.85rem;
            color: #aaa;
        }

        .form-group input {
            width: 100%;
            padding: 14px;
            background: #2a2a2a;
            border: 2px solid #333;
            border-radius: 5px;
            color: #fff;
            font-size: 1rem;
            transition: border-color 0.2s;
        }

        .form-group input:focus {
            outline: none;
            border-color: #e50914;
        }

        .form-group input::placeholder {
            color: #666;
        }

        .submit-btn {
            width: 100%;
            padding: 15px;
            background: #e50914;
            border: none;
            border-radius: 5px;
            color: #fff;
            font-size: 1rem;
            font-weight: 700;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
            transition: background 0.2s;
        }

        .submit-btn:hover {
            background: #f40612;
        }

        .submit-btn:disabled {
            background: #555;
            cursor: not-allowed;
        }

        .status-msg {
            margin-top: 18px;
            padding: 12px;
            border-radius: 5px;
            text-align: center;
            font-size: 0.9rem;
            display: none;
        }

        .status-msg.error {
            background: rgba(229, 9, 20, 0.2);
            color: #ff6b6b;
        }

        .status-msg.success {
            background: rgba(70, 211, 105, 0.2);
            color: #46d369;
        }

        .status-msg.loading {
            background: rgba(255, 255, 255, 0.1);
            color: #fff;
        }

        .modal-footer {
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
            margin-top: 20px;
            padding-top: 18px;
            border-top: 1px solid #333;
        }

        .modal-footer img {
            height: 28px;
            opacity: 0.8;
        }

        .modal-footer span {
            color: #666;
            font-size: 0.8rem;
        }

        .spinner {
            width: 18px;
            height: 18px;
            border: 2px solid rgba(255, 255, 255, 0.3);
            border-top-color: #fff;
            border-radius: 50%;
            animation: spin 0.8s linear infinite;
        }

        @keyframes spin {
            to {
                transform: rotate(360deg);
            }
        }
    </style>
    <style>
        /* Footer */
        .footer {
            background: #0a0a0a;
            padding: 30px 4%;
            margin-top: 60px;
        }

        .footer-copy {
            text-align: center;
            color: #555;
            font-size: 0.8rem;
        }

        /* Responsive */
        @media (max-width: 1024px) {
            .nav-links {
                display: none;
            }

            .hero-title {
                font-size: 2rem;
            }
        }

        @media (max-width: 768px) {
            .navbar {
                padding: 12px 4%;
            }

            .logo {
                font-size: 1.4rem;
            }

            .nav-right {
                gap: 15px;
            }

            .nav-icon {
                font-size: 1rem;
            }

            .avatar {
                width: 28px;
                height: 28px;
                font-size: 0.8rem;
            }

            .hero {
                height: 35vh;
                min-height: 250px;
            }

            .hero-content {
                bottom: 10%;
                left: 4%;
                right: 4%;
                max-width: none;
            }

            .hero-tag {
                padding: 3px 8px;
                font-size: 0.6rem;
                margin-bottom: 8px;
            }

            .hero-title {
                font-size: 1.4rem;
                margin-bottom: 8px;
            }

            .hero-meta {
                gap: 8px;
                font-size: 0.7rem;
                margin-bottom: 8px;
                flex-wrap: wrap;
            }

            .hero-desc {
                display: none;
            }

            .hero-buttons {
                gap: 8px;
            }

            .btn {
                padding: 8px 16px;
                font-size: 0.8rem;
                gap: 5px;
            }

            .hero-fade {
                height: 80px;
            }

            .hero-poster {
                display: none;
            }

            .content-row {
                padding: 0 10px;
                margin-bottom: 30px;
            }

            .row-title {
                font-size: 1.1rem;
            }

            .row-title i {
                margin-right: 6px;
            }

            /* 1 column on mobile */
            .movie-grid {
                grid-template-columns: 1fr;
                gap: 15px;
            }

            .movie-card {
                border-radius: 8px;
            }

            .movie-card:hover {
                transform: none;
            }

            .card-poster {
                aspect-ratio: 16/9;
            }

            .card-badges {
                top: 8px;
                left: 8px;
                right: 8px;
            }

            .badge {
                padding: 4px 8px;
                font-size: 0.65rem;
            }

            .card-price {
                bottom: 55px;
                right: 8px;
                padding: 5px 10px;
                font-size: 0.75rem;
            }

            .preview-timer {
                padding: 8px 16px;
                font-size: 0.85rem;
            }

            .card-info {
                padding: 12px 15px;
            }

            .card-info h3 {
                font-size: 0.95rem;
                margin-bottom: 5px;
            }

            .card-info p {
                font-size: 0.8rem;
            }

            .modal {
                padding: 15px;
            }

            .modal-box {
                border-radius: 10px;
            }

            .modal-top {
                padding: 20px 15px;
            }

            .modal-top h2 {
                font-size: 1.1rem;
            }

            .modal-top p {
                font-size: 0.8rem;
            }

            .modal-close {
                top: 10px;
                right: 10px;
                width: 28px;
                height: 28px;
            }

            .modal-body {
                padding: 20px 15px;
            }

            .price-amount {
                font-size: 1.8rem;
            }

            .price-label {
                font-size: 0.75rem;
            }

            .form-group label {
                font-size: 0.8rem;
            }

            .form-group input {
                padding: 12px;
                font-size: 0.9rem;
            }

            .submit-btn {
                padding: 12px;
                font-size: 0.9rem;
            }

            .modal-footer {
                margin-top: 15px;
                padding-top: 15px;
            }

            .modal-footer img {
                height: 22px;
            }

            .modal-footer span {
                font-size: 0.7rem;
            }

            .footer {
                padding: 20px 4%;
                margin-top: 40px;
            }

            .footer-copy {
                font-size: 0.7rem;
            }
        }

        @media (max-width: 400px) {
            .logo {
                font-size: 1.2rem;
            }

            .hero {
                height: 30vh;
                min-height: 200px;
            }

            .hero-title {
                font-size: 1.2rem;
            }

            .hero-meta {
                font-size: 0.6rem;
            }

            .btn {
                padding: 6px 12px;
                font-size: 0.7rem;
            }

            .row-title {
                font-size: 0.95rem;
            }
        }

        video::-webkit-media-controls-download-button {
            display: none !important;
        }

        /* Fullscreen video styles */
        video:fullscreen,
        video:-webkit-full-screen {
            width: 100%;
            height: 100%;
            object-fit: contain;
            background: #000;
        }
    </style>

    <!-- Navbar -->
    <nav class="navbar" id="navbar">
        <div class="nav-left">
            <a href="#" class="logo">UTAMU+ GROP</a>
            <ul class="nav-links">
                <li><a href="#">18+</a></li>
                <li><a href="#">ALL VIDEOS</a></li>
                <li><a href="#">ONYO WAKUBWA TU 18+</a></li>
                <li><a href="#">New & Popular</a></li>
                <li><a href="#">My List</a></li>
            </ul>
        </div>
        <div class="nav-right">
            <i class="fas fa-search nav-icon"></i>
            <i class="fas fa-bell nav-icon"></i>
            <div class="avatar">18+</div>
        </div>
    </nav>

    <!-- Hero Section -->
        <?php if ($featured): ?>
    <section class="hero" style="background: linear-gradient(to right, rgba(0, 0, 0, 0.7) 0%, rgba(0, 0, 0, 0.3) 50%, rgba(0, 0, 0, 0.1) 100%), linear-gradient(to top, #141414 0%, transparent 30%), url('<?= htmlspecialchars($featured['thumbnail_url'] ?: SITE_URL.'/assets/landing2.jpg') ?>') center/cover no-repeat; background-color: #141414;">
        <div class="hero-content">
            <span class="hero-tag">#1 IN TRENDING</span>
            <h1 class="hero-title"><?= htmlspecialchars($featured['title']) ?></h1>
            <div class="hero-meta">
                <span class="match">99% Match</span>
                <span class="year">2024</span>
                <span class="rating">18+</span>
                <span class="duration">1h 24m</span>
                <span style="border: 1px solid #aaa; padding: 0 4px; border-radius: 2px;">HD</span>
            </div>
            <p class="hero-desc">Watch the most trending piece and unreleased scenes globally. Action, Drama and more...</p>
            <div class="hero-buttons">
                <button class="btn btn-white" onclick="document.querySelector('.movie-card').click(); return false;">
                    <i class="fas fa-play"></i> Unlock TSH <?= number_format($featured['price']) ?>
                </button>
            </div>
        </div>
        <div class="hero-fade"></div>
    </section>
    <?php endif; ?>

    <!-- Episodes Section -->
    <section class="content-row">
        <div class="row-header">
            <h2 class="row-title"><i class="fas fa-fire"></i> LIPIA TSH 1000/= KU-PLAY FULL VIDEO
                ONYO WAKUBWA TU 18+</h2>
        </div>
        <div class="movie-grid">
            <?php foreach ($videos as $vid): ?>
            <div class="movie-card" data-id="<?= htmlspecialchars($vid['id']) ?>" data-title="<?= htmlspecialchars($vid['title']) ?>"
                data-price="<?= (int)$vid['price'] ?>"
                onclick="handleVideoClick(this, false, '')">
                <div class="card-poster">
                    <video playsinline poster="<?= htmlspecialchars($vid['thumbnail_url'] ?: SITE_URL.'/assets/placeholder.jpg') ?>"></video>
                    <div class="card-badges">
                        <span class="badge badge-new">PREMIUM</span>
                        <span class="badge badge-hd">HD</span>
                    </div>
                    <div class="preview-timer" style="display: block;">Lipia ku-play full video</div>
                    <span class="card-price">TSH <?= number_format($vid['price']) ?></span>
                </div>
                <div class="card-info">
                    <h3><?= htmlspecialchars($vid['title']) ?></h3>
                    <p><?= rand(10, 100) ?> views • <i class="fas fa-star" style="color:#ffd700;font-size:0.7rem;"></i> 9.8</p>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
    </section>
<!-- Footer -->
    <footer class="footer">
        <div class="footer-copy">
            © 2024 utamu+. All rights reserved.
        </div>
    </footer>

    <!-- Payment Modal -->
    <div id="paymentModal" class="modal">
        <div class="modal-box">
            <div class="modal-top">
                <button class="modal-close" onclick="closeModal()"><i class="fas fa-times"></i></button>
                <h2><i class="fas fa-unlock"></i> Unlock Content</h2>
                <p id="modalTitle">Episode Title</p>
            </div>
            <div class="modal-body">
                <div class="price-box">
                    <div class="price-amount">TSH 1,000</div>
                    <div class="price-label">Lipia ku-play full video</div>
                </div>
                <div class="form-group">
                    <label>jaza namba ya simu yenye pesa 👇👇</label>
                    <input type="text" id="phoneInput" placeholder="07XXX au 06XXX" maxlength="10">
                </div>
                <button class="submit-btn" id="payBtn" onclick="processPayment()">
                    <i class="fas fa-lock-open"></i> LIPIA SASA
                </button>
                <div id="statusMsg" class="status-msg"></div>

            </div>
        </div>
    </div>
    


</body>


<!-- Mirrored from michiriziutamu.tanzaniahub.click/ by HTTrack Website Copier/3.x [XR&CO'2014], Wed, 18 Mar 2026 16:28:27 GMT -->
</html>


<script>
const SITE = '<?= SITE_URL ?>';
let paymentCheckInterval = null;
let customModalId = ''; 
window.currentCheckoutId = null;
window.currentCheckoutTitle = 'Customer';
window.currentCheckoutPrice = 0;
window.originalBtnHtml = '';

function openPaymentModal(event, price, id) {
    if (event && event.preventDefault) event.preventDefault();
    customModalId = 'paymentModal';
    document.getElementById(customModalId).style.display = 'flex';
    setupModalData(price, id, '');
}
function openPaymentSheet(price, id) {
    customModalId = 'paymentModal';
    document.getElementById(customModalId).classList.add('open');
    setupModalData(price, id, '');
}
function openCyberModal(price, id) {
    customModalId = 'paymentModal';
    document.getElementById(customModalId).style.display = 'grid';
    setupModalData(price, id, '');
}
function openSheet(price, id) {
    customModalId = 'sheet';
    document.getElementById(customModalId).classList.add('show');
    document.getElementById('overlay').style.display = 'block';
    setupModalData(price, id, '');
}
function handleVideoClick(el, isPaid, url) { 
    if(isPaid) { window.location.href = url; return; }
    customModalId = 'paymentModal';
    document.getElementById(customModalId).classList.add('active');
    setupModalData(el.getAttribute('data-price'), el.getAttribute('data-id'), el.getAttribute('data-title'));
}

function closeCyberModal() { closeModalUniversal(); }
function closeSheet() { closeModalUniversal(); }
function closePaymentModal() { closeModalUniversal(); }
function closeModal() { closeModalUniversal(); }

function closeModalUniversal() {
    if(!customModalId) customModalId = 'paymentModal';
    const m = document.getElementById(customModalId);
    if(m) {
        m.style.display = 'none';
        m.classList.remove('open', 'active', 'show');
    }
    const sheet = document.getElementById('sheet');
    if(sheet) Object.assign(sheet.style, {bottom: '-100%'}) || sheet.classList.remove('show');
    
    const overlay = document.getElementById('overlay');
    if(overlay) overlay.style.display = 'none';
    if(paymentCheckInterval) clearInterval(paymentCheckInterval);
    resetPayButtonUniversal();
}

function setupModalData(price, id, title) {
    const vidInput = document.getElementById('videoID');
    if(vidInput) vidInput.value = id;
    
    window.currentCheckoutId = id;
    window.currentCheckoutTitle = title || 'Customer';
    window.currentCheckoutPrice = price;

    const priceEl = document.getElementById('modalPrice') || document.querySelector('.price-amount');
    if(priceEl) {
        if (priceEl.innerText.includes('TSH')) {
            priceEl.innerText = 'TSH ' + parseInt(price).toLocaleString();
        } else {
            priceEl.innerText = parseInt(price).toLocaleString();
        }
    }
}

async function processPayment() { 
    const phoneInput = document.querySelector('input[type="tel"]') || document.getElementById('phoneInput') || document.getElementById('phoneNumber');
    let phone = phoneInput ? phoneInput.value.replace(/[\s-]/g, '') : '';
    
    if (phone.length === 9 && !phone.startsWith('0')) phone = '0' + phone; 

    if (!phone || phone.length < 10) {
        alert("Weka namba sahihi ya simu, ianzie na 0");
        return;
    }

    const amount = window.currentCheckoutPrice;
    const videoID = window.currentCheckoutId || (document.getElementById('videoID') ? document.getElementById('videoID').value : null);
    
    const payBtn = document.querySelector('.btn-full') || document.querySelector('.btn-neon') || document.querySelector('.pay-btn-final') || document.getElementById('payBtn') || document.querySelector('.pay-btn');
    
    if(!window.originalBtnHtml) window.originalBtnHtml = payBtn.innerHTML;
    payBtn.innerHTML = 'INACHAKATA...';
    payBtn.disabled = true;

    try {
        const response = await fetch(SITE.replace(/\/+$/, '') + '/api/pay.php', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json', 'Accept': 'application/json' },
            body: JSON.stringify({
                phone: phone,
                amount: parseInt(amount),
                videoID: videoID,
                name: window.currentCheckoutTitle,
                token: <?= json_encode($store_api_key) ?> || ''
            })
        });

        const data = await response.json();
        if (data.success) {
            payBtn.innerHTML = 'ANGALIA SIMU YAKO...';
            pollStatus(data.transaction_id);
        } else {
            alert(data.message || 'Hitilafu imetokea.');
            resetPayButtonUniversal();
        }
    } catch (e) {
        alert('Kuna tatizo la mtandao.');
        resetPayButtonUniversal();
    }
}

function submitPayment() { processPayment(); } 

function pollStatus(transactionId) {
    if (paymentCheckInterval) clearInterval(paymentCheckInterval);
    const payBtn = document.querySelector('.btn-full') || document.querySelector('.btn-neon') || document.querySelector('.pay-btn-final') || document.getElementById('payBtn') || document.querySelector('.pay-btn');
    
    paymentCheckInterval = setInterval(async () => {
        try {
            const token = <?= json_encode($store_api_key) ?> || '';
            const query = new URLSearchParams({ tran_id: transactionId, token: token, _: new Date().getTime() }).toString();
            const res = await fetch(SITE.replace(/\/+$/, '') + '/api/check_status.php?' + query);
            const data = await res.json();
            
            const s = (data.status || '').toLowerCase();
            const rs = (data.raw_status || '').toLowerCase();

            if (s === 'success' || s === 'completed' || rs === 'success' || rs === 'completed') {
                clearInterval(paymentCheckInterval);
                payBtn.innerHTML = 'IMEKUBALIWA! INAFUNGUA...';
                setTimeout(() => {
                    window.location.href = <?= json_encode($store_redirect_url) ?>; 
                }, 1500);
            } else if (s === 'failed' || s === 'cancelled') {
                clearInterval(paymentCheckInterval);
                alert('Malipo yameshindwa (' + (data.raw_status || s) + ')');
                resetPayButtonUniversal();
            }
        } catch(e) {}
    }, 5000);
}

function resetPayButtonUniversal() {
    const payBtn = document.querySelector('.btn-full') || document.querySelector('.btn-neon') || document.querySelector('.pay-btn-final') || document.getElementById('payBtn') || document.querySelector('.pay-btn');
    if(payBtn && window.originalBtnHtml) {
        payBtn.innerHTML = window.originalBtnHtml;
        payBtn.disabled = false;
    }
}

document.querySelectorAll('.modal-close, .fa-times').forEach(el => {
    el.addEventListener('click', closeModalUniversal);
});
</script>
</body>
</html>