<!DOCTYPE html>

<html lang="th">



<head>

  <meta charset="utf-8" />

  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

  <link rel="apple-touch-icon" sizes="76x76" href="https://www.cdn.niendsitekungz.com/finance/assets/img/apple-icon.png">

  <link rel="icon" type="image/png" href="https://www.cdn.niendsitekungz.com/finance/assets/img/favicon556.png">

  

  <title>

   Countdown Online | NiendsiteKunGz

  </title>

    <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700" rel="stylesheet" />
  <link href='https://fonts.googleapis.com/css?family=Kanit' rel='stylesheet'>
  <link href="https://www.cdn.niendsitekungz.com/finance/assets/css/nucleo-icons.css" rel="stylesheet" />
  <link href="https://www.cdn.niendsitekungz.com/finance/assets/css/nucleo-svg.css" rel="stylesheet" />
  <link href="https://www.cdn.niendsitekungz.com/finance/assets/css/nucleo-svg.css" rel="stylesheet" />
  <link id="pagestyle" href="https://www.cdn.niendsitekungz.com/finance/assets/css/soft-ui-dashboard.css?v=1.0.6" rel="stylesheet" />
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.css">
  
  <script src="https://kit.fontawesome.com/42d5adcbca.js" crossorigin="anonymous"></script>
  <script src="https://code.jquery.com/jquery-3.6.1.min.js"></script>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/sweetalert2@11">
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
 
  

<style>



body {
    font-family: 'Kanit';font-size: 22px;
}

li {
   font-family: 'Kanit';font-size: 22px;
}

div {
    font-family: 'Kanit';font-size: 15px;
}

.html {
   font-family: 'Kanit';font-size: 20px;
}
    
    script {
      font-family: 'Kanit';
      font-size: 17px;
    }
    swal {
      font-family: 'Kanit';
      font-size: 17px;
    if {
      font-family: 'Kanit';
      font-size: 17px;

      /* กำหนดความสูงของ container ให้ Card อยู่กึ่งกลางหน้าจอ */
        .full-height {
            height: 100vh;
        }



</style>

</head>
<body>
    <h1>Countdown Online</h1>

    <div id="countdown">
        <h2>Countdown: <span id="timer"></span></h2>
    </div>

    <hr>
    <h3>Comments</h3>
    <div id="comments">
        <?php foreach ($comments as $comment): ?>
            <p><strong><?= $comment['username'] ?>:</strong> <?= $comment['message'] ?></p>
        <?php endforeach; ?>
    </div>

    <hr>
    <h3>Add a Comment</h3>
    <form id="commentForm">
        <input type="text" name="username" placeholder="Username" required>
        <textarea name="message" placeholder="Your comment" required></textarea>
        <button type="submit">Post Comment</button>
    </form>

    <script>
        // Set countdown date
    const countdownDate = new Date("2024-12-31T23:59:59").getTime();

    function updateCountdown() {
        const now = new Date().getTime();
        const distance = countdownDate - now;

        if (distance < 0) {
            document.getElementById("countdown").innerHTML = "Event has started!";
            return;
        }

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

        document.getElementById("countdown").innerHTML = `${days}d ${hours}h ${minutes}m ${seconds}s`;
    }

    setInterval(updateCountdown, 1000);

        // Fetch comments in real-time
        function fetchComments() {
            $.get("/countdown/fetchComments", function(data) {
                $("#comments").html("");
                data.forEach(comment => {
                    $("#comments").append(`<p><strong>${comment.username}:</strong> ${comment.message}</p>`);
                });
            });
        }

        setInterval(fetchComments, 3000);

        // Handle comment form submission
        $("#commentForm").submit(function(e) {
            e.preventDefault();
            $.post("/countdown/postComment", $(this).serialize(), function(data) {
                if (data.status === 'success') {
                    fetchComments();
                    $("#commentForm")[0].reset();
                } else {
                    alert(data.message);
                }
            }, 'json');
        });
    </script>
</body>
</html>
