<?php
// Database connection information
$servername = "localhost";
$username = "coinlyca_cash";
$password = "AHmed2468#$";
$dbname = "coinlyca_cash";

// Create database connection
try {
    $pdo = new PDO("mysql:host=$servername;dbname=$dbname;charset=utf8", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo "Database connection failed: " . $e->getMessage();
    exit;
}

// Fetch prize data and date range from the top10 table
$sql = "SELECT * FROM top10 ORDER BY id DESC LIMIT 1";
$stmt = $pdo->query($sql);
$prizeData = $stmt->fetch(PDO::FETCH_ASSOC);

if ($prizeData) {
    $startDate = $prizeData['start_date'];
    $endDate = $prizeData['end_date'];
    $firstPrize = $prizeData['first_prize'];
    $secondPrize = $prizeData['second_prize'];
    $thirdPrize = $prizeData['third_prize'];
    $otherPrizes = $prizeData['other_prizes'];

    // Convert dates to UNIX timestamps
    $startTimestamp = strtotime($startDate);
    $endTimestamp = strtotime($endDate) + 86400 - 1; // Add 86400 seconds (24 hours) and subtract 1 second to include the end of the day

    // Query to get amounts within a specified date range
    $sql = "
        SELECT 
            wallet,
            SUM(amount) AS total_amount
        FROM 
            withdraw_history
        WHERE 
            type = 1 AND
            claim_time BETWEEN :startTimestamp AND :endTimestamp
        GROUP BY 
            wallet
        ORDER BY 
            total_amount DESC
        LIMIT 10
    ";

    $stmt = $pdo->prepare($sql);
    $stmt->bindParam(':startTimestamp', $startTimestamp, PDO::PARAM_INT);
    $stmt->bindParam(':endTimestamp', $endTimestamp, PDO::PARAM_INT);
    $stmt->execute();
    $topWallets = $stmt->fetchAll(PDO::FETCH_ASSOC);
} else {
    echo "No prize data available.";
    exit;
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-FNW6P9E8E7"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'G-FNW6P9E8E7');
</script>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Top Wallets by Date Range</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f7f9fc;
            color: #333;
        }
        header {
            background-color: #0056b3;
            color: white;
            padding: 20px;
            text-align: center;
        }
        main {
            padding: 20px;
            max-width: 1200px;
            margin: 0 auto;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            background-color: #ffffff;
            margin-bottom: 20px;
            border-radius: 10px;
            overflow: hidden;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }
        th, td {
            padding: 15px;
            text-align: center;
            border-bottom: 1px solid #e0e0e0;
        }
        th {
            background-color: #f1f1f1;
            color: #333;
            font-weight: bold;
        }
        tr:nth-child(even) {
            background-color: #f9f9f9;
        }
        tr:nth-child(odd) {
            background-color: #ffffff;
        }
        tr:hover {
            background-color: #e2e6ea;
        }
        footer {
            background-color: #0056b3;
            color: white;
            text-align: center;
            padding: 15px;
            position: relative;
            width: 100%;
            bottom: 0;
        }
        #countdown {
            font-size: 2rem;
            color: #ffffff;
            text-align: center;
            margin: 20px auto;
            padding: 10px 20px;
            background-color: #003d7a;
            border-radius: 30px;
            display: inline-block;
        }
        .footer-info {
            font-size: 0.9rem;
            margin-top: 10px;
        }
        .footer-info a {
            color: #ffffff;
            text-decoration: none;
            font-weight: bold;
            border-bottom: 1px solid #ffffff;
            padding-bottom: 2px;
            transition: color 0.3s, border-bottom 0.3s;
        }
        .footer-info a:hover {
            color: #cce5ff;
            border-bottom: 1px solid #cce5ff;
        }
        @media (max-width: 768px) {
            table {
                font-size: 14px;
            }
            th, td {
                padding: 10px;
            }
            #countdown {
                font-size: 1.5rem;
                padding: 8px 16px;
            }
        }
    </style>
</head>
<body>
    <header>
        <h1 id="countdown">Loading...</h1>
    </header>

    <main>
        <table>
            <thead>
                <tr>
                    <th>Rank</th>
                    <th>Wallet</th>
                    <th>Total Amount</th>
                    <th>Prize</th>
                </tr>
            </thead>
            <tbody>
                <?php if (!empty($topWallets)): ?>
                    <?php foreach ($topWallets as $index => $wallet): ?>
                        <tr>
                            <td><?php echo $index + 1; ?></td>
                            <td><?php echo htmlspecialchars($wallet['wallet']); ?></td>
                            <td><?php echo htmlspecialchars(number_format($wallet['total_amount'], 2)); ?></td>
                            <td>
                                <?php
                                    // Assign prizes based on rank
                                    if ($index === 0) {
                                        echo '$' . htmlspecialchars($firstPrize);
                                    } elseif ($index === 1) {
                                        echo '$' . htmlspecialchars($secondPrize);
                                    } elseif ($index === 2) {
                                        echo '$' . htmlspecialchars($thirdPrize);
                                    } else {
                                        echo '$' . htmlspecialchars($otherPrizes);
                                    }
                                ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                <?php else: ?>
                    <tr>
                        <td colspan="4">No data available for the selected date range.</td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </main>

    <footer>
        <div class="footer-info">
            <p><a href="https://t.me/youssef_shaban_0" target="_blank">Dev by Youssef Shaban</a></p>
        </div>
    </footer>

    <script>
        // Countdown timer setup
        function updateCountdown() {
            var countdownElement = document.getElementById('countdown');
            var endDate = new Date('<?php echo $endDate;?>').getTime(); // Final countdown time
            var now = new Date().getTime();
            var timeleft = endDate - now;

            if (timeleft < 0) {
                countdownElement.innerHTML = "Time's up!";
                return;
            }

            var days = Math.floor(timeleft / (1000 * 60 * 60 * 24));
            var hours = Math.floor((timeleft % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
            var minutes = Math.floor((timeleft % (1000 * 60 * 60)) / (1000 * 60));
            var seconds = Math.floor((timeleft % (1000 * 60)) / 1000);

            countdownElement.innerHTML = days + "d " + hours + "h " + minutes + "m " + seconds + "s ";

            setTimeout(updateCountdown, 1000);
        }

        // Start countdown on page load
        window.onload = updateCountdown;
    </script>
</body>
</html>
