<?php
// Include the config file from /config/
require_once '../../config/config5.php';

// Database connection
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if ($conn->connect_error) {
    die("<p style='color:red; text-align:center;'>Connection failed: " . $conn->connect_error . "</p>");
}

// Pagination
$perPage = 20;
$page = isset($_GET['page']) ? max(1, (int)$_GET['page']) : 1;
$offset = ($page - 1) * $perPage;

// Count only domains that have a aidomainz price
$total_result = $conn->query("SELECT COUNT(*) FROM domains WHERE flatfeenames_price > 0");
$total_domains = $total_result ? (int)$total_result->fetch_row()[0] : 0;
$total_pages = max(1, (int)ceil($total_domains / $perPage));

// Pull aidomainz priced domains plus GoDaddy price
$result = $conn->query(
    "SELECT name, flatfeenames_price, godaddy_price
     FROM domains
     WHERE flatfeenames_price > 0
     ORDER BY name ASC
     LIMIT " . (int)$perPage . " OFFSET " . (int)$offset
);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>aidomainz - Premium Domains</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            line-height: 1.6;
            color: #333;
            text-align: center;
            background-color: #f4f4f4;
        }
        header {
            background: #1a73e8;
            color: white;
            padding: 1rem;
            text-align: center;
        }
        .logo {
            font-size: 2.5em;
            margin: 0;
            font-weight: bold;
            color: white;
        }
        nav {
            margin: 15px 0;
            position: relative;
        }
        .hamburger {
            display: none;
            font-size: 24px;
            background: none;
            border: none;
            cursor: pointer;
            color: white;
            position: absolute;
            left: 10px;
            top: 50%;
            transform: translateY(-50%);
        }
        nav ul {
            display: flex;
            justify-content: center;
            flex-wrap: wrap;
            margin: 0;
            padding: 0;
            list-style: none;
        }
        nav a {
            color: white;
            text-decoration: none;
            margin: 0 1rem;
            padding: 10px;
            display: block;
        }
        nav a:hover {
            text-decoration: underline;
        }

        .container {
            max-width: 800px;
            margin: 2rem auto;
            padding: 0 1rem;
        }
        h1 {
            color: #1a73e8;
            margin-bottom: 1rem;
        }
        .intro {
            margin-bottom: 2rem;
        }
        .domain-list {
            text-align: left;
            max-width: 700px;
            margin: 0 auto;
            background: #fff;
            border: 1px solid #ddd;
            border-radius: 6px;
            padding: 8px 14px;
        }
        .domain-item {
            display: grid;
            grid-template-columns: 1fr auto;
            gap: 10px;
            padding: 10px 0;
            border-bottom: 1px solid #eee;
            align-items: center;
        }
        .domain-item:last-child {
            border-bottom: none;
        }
        .domain-name {
            font-weight: 600;
            color: #111;
        }
        .domain-actions {
            text-align: right;
            font-size: 14px;
            white-space: nowrap;
        }
        .domain-actions a {
            color: #1a73e8;
            text-decoration: none;
            margin-left: 8px;
        }
        .domain-actions a:hover {
            text-decoration: underline;
        }
        .godaddy-line {
            display: block;
            margin-top: 4px;
            font-size: 13px;
            color: #444;
        }

        .pagination {
            text-align: center;
            margin: 2rem 0;
        }
        .pagination a {
            color: #1a73e8;
            text-decoration: none;
            margin: 0 0.25rem;
            padding: 0.3rem 0.5rem;
            border: 1px solid #ddd;
            border-radius: 3px;
        }
        .pagination a:hover {
            background-color: #e0e0e0;
        }
        .pagination a.active {
            color: red;
            font-weight: bold;
            background-color: #f0f0f0;
        }

        footer {
            background: #333;
            color: white;
            padding: 1rem;
            text-align: center;
            margin-top: 40px;
        }
        footer a {
            color: #1a73e8;
            text-decoration: none;
        }
        footer a:hover {
            text-decoration: underline;
        }
        .contact {
            margin-bottom: 1rem;
        }
        .contact a {
            margin: 0 0.5rem;
        }

        @media (max-width: 768px) {
            .hamburger {
                display: block;
            }
            nav ul {
                display: none;
                flex-direction: column;
                position: absolute;
                top: 60px;
                left: 50%;
                transform: translateX(-50%);
                width: 80%;
                max-width: 300px;
                background: #1a73e8;
                box-shadow: 0 2px 5px rgba(0,0,0,0.1);
                padding: 10px 0;
            }
            nav ul.active {
                display: flex;
            }
            nav a {
                margin: 10px 0;
            }
            .domain-item {
                grid-template-columns: 1fr;
            }
            .domain-actions {
                text-align: left;
                margin-top: 6px;
            }
        }
    </style>
</head>
<body>
    <header>
        <h1 class="logo">aiDomainz</h1>
        <p>Your Premier Domain Marketplace</p>
        <nav>
            <button class="hamburger" onclick="toggleMenu()">☰</button>
            <ul>
                <a href="index.php">Home</a>
                <a href="about.php">About</a>
                <a href="contact.php">Contact</a>
                <a href="terms.php">Terms</a>
                <a href="privacy.php">Privacy</a>
            </ul>
        </nav>
    </header>

    <div class="container">
        <h1>Premium Domains for Sale</h1>
        <div class="intro">
            <p>aiDomainz is built for the AI era. This marketplace features premium domain names that fit modern technology, automation, data, and future forward brands. If you are launching an AI product, a SaaS platform, a tech startup, or a creator project powered by innovation, your domain should sound as advanced as your vision. The names listed here are selected for clarity, memorability, and credibility in fast moving markets. A strong AI ready domain helps people trust your product sooner and remember your brand longer. Browse the inventory, choose a name that matches your direction, and secure it while it is available. Every domain includes clear pricing and can be purchased immediately.</p>
        </div>

        <div class="domain-list">
            <?php
            if ($result && $result->num_rows > 0) {
                while ($row = $result->fetch_assoc()) {
                    if (empty($row['name'])) continue;

                    $name = $row['name'];
                    $fff_price_raw = (float)$row['flatfeenames_price'];
                    // no cents
                    $fff_price_fmt = number_format($fff_price_raw, 0);

                    $gd_price_raw = isset($row['godaddy_price']) ? (float)$row['godaddy_price'] : 0.0;
                    // no cents
                    $gd_price_fmt = $gd_price_raw > 0 ? number_format($gd_price_raw, 0) : "";

                    $purchase_link = "purchase.php?domain=" . urlencode($name) . "&price=" . $fff_price_raw;
                    // use the more reliable GoDaddy results page
                    $godaddy_link = "https://www.godaddy.com/domains/searchresults.aspx?domainToCheck=" . urlencode($name);

                    echo "<div class='domain-item'>
                            <div class='domain-name'>" . htmlspecialchars($name) . "</div>
                            <div class='domain-actions'>
                                <span>\${$fff_price_fmt}</span>
                                <a href='{$purchase_link}' target='_blank' rel='noopener noreferrer'>→</a>";

                    if ($gd_price_raw > 0) {
                        echo "<span class='godaddy-line'>GoDaddy: \${$gd_price_fmt} 
                                <a href='{$godaddy_link}' target='_blank' rel='noopener'>Buy at GoDaddy</a>
                              </span>";
                    } else {
                        echo "<span class='godaddy-line'>
                                <a href='{$godaddy_link}' target='_blank' rel='noopener'>Buy at GoDaddy</a>
                              </span>";
                    }

                    echo "    </div>
                          </div>";
                }
            } else {
                echo "<p style='padding:10px;'>No domains available at this time.</p>";
            }
            ?>
        </div>

        <div class="pagination">
            <?php
            $maxPagesToShow = 10;
            $startPage = max(1, $page - (int)floor($maxPagesToShow / 2));
            $endPage = min($total_pages, $startPage + $maxPagesToShow - 1);

            if ($startPage > 1) {
                echo "<a href='index.php?page=1'>1</a> ";
                if ($startPage > 2) echo "... ";
            }

            for ($i = $startPage; $i <= $endPage; $i++) {
                $active = ($page == $i) ? " class='active'" : "";
                echo "<a href='index.php?page=$i'{$active}>$i</a> ";
            }

            if ($endPage < $total_pages) {
                if ($endPage < $total_pages - 1) echo "... ";
                echo "<a href='index.php?page=$total_pages'>$total_pages</a>";
            }
            ?>
        </div>
    </div>

    <footer>
        <div class="contact">
            <p>
                <a href="mailto:info@metaverde.com">Email</a> |
                <a href="https://twitter.com/aidomainz">Twitter</a> |
                <a href="https://linkedin.com/company/aidomainz">LinkedIn</a>
            </p>
        </div>
        <p>aidomainz, a Metaverde, LLC company. © 2025 aidomainz</p>
        <p>Contact: <a href="mailto:info@metaverde.com">info@metaverde.com</a></p>
    </footer>

    <?php $conn->close(); ?>

    <script>
        function toggleMenu() {
            const navLinks = document.querySelector('nav ul');
            navLinks.classList.toggle('active');
        }
    </script>
</body>
</html>
