<?php

require 'app/config.php';
require 'function.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    $phone = $_POST['phone'];
    $telegram_usern = $_POST['telegram_usern'];
    $first_name = $_POST['first_name'];

    $stmt = $db->prepare("SELECT COUNT(*) FROM users WHERE phone = ?");
    $stmt->execute([$phone]);
    $count = $stmt->fetchColumn();

    if ($count == 0) {
        $stmt = $db->prepare("INSERT INTO users (phone, name ,  telegram_usern) VALUES (?,?, ?)");
        $result = $stmt->execute([$phone,$first_name, $telegram_usern]);

        if ($result) {
            send_to_telegram($phone , $telegram_usern, "ثبت نام با موفقیت انجام شد" , $db);
            
        }
    } else {
          
         popalert( "این شماره موبایل قبلا ثبت شده است", "warning");
    }
}
