<?php
require 'app/config.php';
require './vendor/autoload.php';

function send_to_telegram($phone , $username, $massage ,$db){
    $stmt = $db->prepare("SELECT * FROM users  WHERE telegram_usern = ? AND  phone = ?");
    $user_exist= $stmt->execute([$username , $phone ]);
    $myuser = $stmt->fetchAll(PDO::FETCH_ASSOC);
   
   
    if($myuser[0]["username"]==$username){
        
         $telegram = new Telegram('7570859490:AAFR8LgStPfUVNKhbFZ7LN0FSYBNkaU2iQs');
       $content = array('chat_id' => $myuser[0]["tl_id"] , 'text' => $massage);
     $send_result=$telegram->sendMessage($content);
    }else{
    
     
     
     $url="https://api.telegram.org/bot7570859490:AAFR8LgStPfUVNKhbFZ7LN0FSYBNkaU2iQs/getUpdates";
    $ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);

$vars = json_decode($result, true)["result"];
 if(empty($vars)){
      popalert( "در ربات ثبت نام نکرده اید لطفا مجدد تلاش کنید", "warning");
     }
foreach($vars as $var){
     $user=$var["message"]["from"]["username"];
     $chat_id = $var["message"]["from"]["id"];
     $tl_phone = $phone;
     
     
     
     if($username==$user && $chat_id){
      $tl_stmt = $db->prepare("UPDATE users SET tl_id = ? WHERE phone = ?");
     $save_result=$tl_stmt->execute([$chat_id , $phone]);  
     $telegram = new Telegram('7570859490:AAFR8LgStPfUVNKhbFZ7LN0FSYBNkaU2iQs');
       $content = array('chat_id' => $chat_id , 'text' => $massage);
     $send_result=$telegram->sendMessage($content);
      popalert( "ثبت نام با موفقیت انجام شد", "success");
     
     }else{
         popalert( "در ربات ثبت نام نکرده اید لطفا مجدد تلاش کنید", "warning");
     }
}
   

         
        
        
    }      
    
     
return $send_result;
}


function update_send_status($id , $db)
{

    $stmt = $db->prepare("UPDATE phone SET status = 'completed' WHERE id = ?");
    $stmt->execute([$id]);
}

if($_POST['action']=="send-test"){
    
    if (empty($_POST['pattern_code'])) {
         send_to_whatsapp($_POST['phone'], $_POST['massage']);
         send_to_telegram($_POST['telegram_usern'], $_POST['massage'], $db);
        send_sms($_POST['phone'], $_POST['massage']);
       
       
      
    } else {
        send_sms_bypattern($_POST['phone'], $_POST['pattern_code']);
        send_to_whatsapp($_POST['phone'], $_POST['massage']);
         send_to_telegram($_POST['telegram_usern'], $_POST['massage'], $db);
        
        
    }

}
// return result
function popalert($message, $status = 'info') {
   echo json_encode(array($message , $status));
}
// get users
if($_POST['action']=="getuser"){
    
     $stmt = $db->prepare("SELECT name, id FROM users ");
      $user_exist= $stmt->execute();
    $alluser = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo json_encode($alluser);

}
// get_reminder_status
if($_POST['action']=="showstatus"){
    
     $stmt = $db->prepare("SELECT reminder_date, status FROM  reminder_date  ");
      $stmt->execute();
    $alldate = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo json_encode($alldate);

}

