<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
  <link rel="shortcut icon" href="https://i.postimg.cc/mZJxWkK0/1736248575292.jpg">
  <title>Fancode Web</title>
  <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&family=Montserrat:wght@400;700&display=swap">
  <style>
    body {
      font-family: 'Roboto', sans-serif;
      margin: 0;
      padding: 20px;
      background-color: #000;
      color: #fff;
    }

    h1 {
      text-align: center;
      color: #FFD700;
      margin-bottom: 20px;
      font-family: 'Montserrat', sans-serif;
    }

    .file-container {
      display: flex;
      flex-wrap: wrap;
      gap: 20px;
      justify-content: center;
    }

    .match-item {
      margin: 10px;
      text-align: center;
      border-radius: 8px;
      overflow: hidden;
      background-color: #1a1a1a;
      padding: 20px;
      width: calc(25% - 20px);
      box-sizing: border-box;
      transition: transform 0.3s ease, box-shadow 0.3s ease;
    }

    .match-item img {
      max-width: 100%;
      height: auto;
      margin: 0 auto 10px auto;
    }

    .match-item h3,
    .match-item p {
      font-family: 'Montserrat', sans-serif;
      margin: 0;
      text-transform: uppercase;
    }

    .match-item h3 {
      font-weight: 700;
      margin-bottom: 10px;
    }

    .match-item p {
      font-weight: 400;
      margin-bottom: 10px;
    }

    .status-live {
      color: yellow;
    }

    .status-upcoming {
      color: #FF5733;
    }

    .telegram-toggle {
      background-color: #EE4B2B;
      color: white;
      padding: 10px 15px;
      border-radius: 50px;
      text-decoration: none;
      font-weight: bold;
      display: block;
      margin: 20px auto;
      text-align: center;
      width: fit-content;
    }

    .stream-button {
      padding: 10px 15px;
      margin: 5px;
      border: none;
      border-radius: 5px;
      font-weight: bold;
      cursor: pointer;
      color: white;
    }

    .normal-button {
      background-color: #007bff;
    }

    .normal-button:hover {
      background-color: #0056b3;
    }

    .adfree-button {
      background-color: #28a745;
    }

    .adfree-button:hover {
      background-color: #218838;
    }

    @media (max-width: 768px) {
      .match-item {
        width: calc(50% - 20px);
      }
    }

    @media (max-width: 480px) {
      .match-item {
        width: calc(100% - 20px);
      }
    }
  </style>
</head>

<body>
  <h1>Fancode WEB</h1>
  <a href="https://t.me/CRICSPORTCENTRAL" class="telegram-toggle">Join Telegram</a>
  <div class="file-container" id="matches"></div>

  <script>
    if (confirm("Join Our Telegram Channel @CRICSPORTCENTRAL")) {
      window.location.href = "https://t.me/CRICSPORTCENTRAL";
    }
 
  fetch("https://raw.githubusercontent.com/drmlive/fancode-live-events/main/fancode.json")
  .then(response => response.json())
  .then(data => {
    const container = document.getElementById("matches");

    // Separate live and upcoming matches
    const liveMatches = data.matches.filter(match => match.status.toLowerCase() === "live");
    const upcomingMatches = data.matches.filter(match => match.status.toLowerCase() === "upcoming");

    // Sort upcoming matches by start time
    const sortedUpcomingMatches = upcomingMatches.sort((a, b) => {
      const timeA = new Date(a.startTime); // Convert startTime to Date object
      const timeB = new Date(b.startTime); // Convert startTime to Date object
      return timeA - timeB; // Sort by ascending time
    });

    // Combine live matches on top and sorted upcoming matches
    const allMatches = [...liveMatches, ...sortedUpcomingMatches];

    // Loop through all matches and display them
    allMatches.forEach(match => {
      const matchDiv = document.createElement("div");
      matchDiv.className = "match-item";

      const statusClass = match.status.toLowerCase() === "live" ? "status-live" : "status-upcoming";
      let timeAndDateHTML = ""; // Initialize an empty string for time and date
      let buttonsHTML = "";

      // Add time and date only for upcoming matches
      if (match.status.toLowerCase() === "upcoming") {
        const startTime = match.startTime;
        const [time, period, date] = startTime.split(' '); // Split time, period (AM/PM), and date

        const formattedTime = `${time} ${period}`; // e.g., 06:30 AM

        // Correctly format date as dd-MM-yyyy
        const [day, month, year] = date.split('-'); // Fix date format
        const formattedDate = `${day}-${month}-${year}`; // Convert to dd-MM-yyyy

        timeAndDateHTML = `<p>${formattedTime}, ${formattedDate}</p>`; // Time first, then date
      }

      // Add buttons for live matches
      if (match.status.toLowerCase() === "live") {
        buttonsHTML = `
          <button class="stream-button normal-button" 
            onclick="window.location.href='https://shz.al/~cricsportcentral-nplay.html?id=${match.match_id}'">
            Normal
          </button>
          <button class="stream-button adfree-button" 
            onclick="window.location.href='https://shz.al/~cricsportcentral-aplay.html?id=${match.match_id}'">
            Ad-Free
          </button>
        `;
      }

      matchDiv.innerHTML = `
        <img src="${match.src}" alt="${match.title}">
        <h3>${match.event_name}</h3>
        <p>${match.team_1} vs ${match.team_2}</p>
        <p class="${statusClass}">${match.status}</p>
        ${timeAndDateHTML} <!-- Add time and date only for upcoming -->
        <div>${buttonsHTML}</div>
      `;

      container.appendChild(matchDiv);
    });
  })
  .catch(error => {
    console.error("Error fetching match data:", error);
  });
</script>
</body>

</html>