<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Fusion Trader</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      background-color: #f8f8f8;
      margin: 0;
      padding: 0;
    }

    .container {
      max-width: 380px;
      margin: 20px auto;
      background-color: #ffffff;
      border-radius: 10px;
      box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.1);
      overflow: hidden;
    }

    .header {
      background-color: #e84118;
      color: #fff;
      text-align: center;
      padding: 15px;
      font-size: 20px;
      font-weight: bold;
    }

    .welcome {
      background-color: #fff;
      padding: 10px;
      text-align: center;
      font-size: 14px;
      color: #555;
      font-weight: bold;
    }

    .welcome span {
      display: block;
      font-size: 12px;
      color: #999;
    }

    .content {
      padding: 15px;
    }

    .card {
      border: 1px solid #eee;
      border-radius: 8px;
      padding: 10px;
      margin-bottom: 15px;
      text-align: center;
      background-color: #fef7f7;
    }

    .card h2 {
      font-size: 16px;
      font-weight: bold;
      color: #555;
      margin-bottom: 5px;
    }

    .card p {
      font-size: 20px;
      color: #e84118;
      font-weight: bold;
    }

    .table-container {
      margin-top: 15px;
    }

    table {
      width: 100%;
      border-collapse: collapse;
      margin-top: 10px;
    }

    table th,
    table td {
      border: 1px solid #ddd;
      padding: 8px;
      text-align: center;
      font-size: 14px;
    }

    table th {
      background-color: #e84118;
      color: #fff;
      font-weight: bold;
    }

    table tr:nth-child(even) {
      background-color: #f9f9f9;
    }

    table tr td:nth-child(2) {
      font-weight: bold;
      color: green;
    }

    table tr td:nth-child(2).small {
      color: #e84118;
    }
  </style>
</head>
<body>
  <div class="welcome to">
    <div>Mr Mdx SERVER</div>
    <span>Welcome to FUSION TRADER!</span>
  </div>
  <div class="container">
    <div class="header">FUSION TRADER</div>
    <div class="content">
      <div class="card">
        <h2>Period Number</h2>
        <p id="periodNumber">Loading...</p>
      </div>
      <div class="card">
        <h2>Time Remaining</h2>
        <p id="remainingSeconds">00 : 00</p>
      </div>
      <div class="card">
        <h2>Current Result</h2>
        <p id="result">Loading...</p>
      </div>
      <div class="table-container">
        <table>
          <thead>
            <tr>
              <th>Period</th>
              <th>Big/Small</th>
            </tr>
          </thead>
          <tbody id="historyBody"></tbody>
        </table>
      </div>
    </div>
  </div>

  <script>
    const historySet = new Set(); // To prevent duplicate entries
    const historyBody = document.getElementById("historyBody");
    const periodNumberElement = document.getElementById("periodNumber");
    const resultElement = document.getElementById("result");
    const remainingSecondsElement = document.getElementById("remainingSeconds");

    function calculatePeriodAndResult() {
      const now = new Date();
      const offset = 330; // Offset in minutes (5 hours 30 minutes)
      const totalMinutes = now.getHours() * 60 + now.getMinutes();
      const periodCalculation = 10001 + totalMinutes - offset;

      // Format date as yyyyMMdd
      const currentDate = now.toISOString().slice(0, 10).replace(/-/g, "");

      // Combine date and period calculation
      const periodNumber = `${currentDate}1000${periodCalculation}`;
      periodNumberElement.textContent = periodNumber;

      // Calculate digit sum
      let digitSum = Array.from(String(periodCalculation)).reduce(
        (sum, digit) => sum + parseInt(digit),
        0
      );

      // Reduce digit sum to a single digit
      while (digitSum >= 10) {
        digitSum = Array.from(String(digitSum)).reduce(
          (sum, digit) => sum + parseInt(digit),
          0
        );
      }

      // Determine result
      const result = digitSum >= 4 && digitSum <= 8 ? "Small" : "Big";
      resultElement.textContent = result;
      resultElement.className = result.toLowerCase();

      // Calculate remaining seconds to the next period
      const secondsPastCurrentMinute = now.getSeconds();
      const remainingSeconds = 60 - secondsPastCurrentMinute;
      remainingSecondsElement.textContent = `00 : ${remainingSeconds.toString().padStart(2, "0")}`;

      // Add to history if not already present
      if (!historySet.has(periodNumber)) {
        historySet.add(periodNumber);

        // Append to history table
        const row = document.createElement("tr");
        const periodCell = document.createElement("td");
        const resultCell = document.createElement("td");

        periodCell.textContent = periodNumber;
        resultCell.textContent = result;
        resultCell.className = result.toLowerCase();

        row.appendChild(periodCell);
        row.appendChild(resultCell);
        historyBody.prepend(row); // Add new row to the top
      }
    }

    // Run the calculation initially
    calculatePeriodAndResult();

    // Update every second
    setInterval(calculatePeriodAndResult, 1000);
  </script>
</body>
</html>