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

use GreenApi\RestApi\GreenApiClient; 

date_default_timezone_set("Asia/Tehran");
$now = date('Y-m-d H:i:s');
$inTwentyMinutes = date('Y-m-d H:i:s', strtotime('+30 minutes'));
$stmt = $db->prepare("SELECT * FROM phone WHERE status = 'pending' AND send_time >= ? AND send_time < ?");
$stmt->execute([$now, $inTwentyMinutes]);



function send_sms($mobile, $massage)
{
    $url = "https://ippanel.com/services.jspd";
    $rcpt_nm = array($mobile);
    $param = array(
        'uname' => 'u09140790345',
        'pass' => 'Faraz@1355484610792125',
        'from' => '5000404223',
        'message' => $massage ,
        'to' => json_encode($rcpt_nm),
        'op' => 'send'
    );

    $handler = curl_init($url);
    curl_setopt($handler, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($handler, CURLOPT_POSTFIELDS, $param);
    curl_setopt($handler, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($handler);

    $response = json_decode($response);
    var_dump($response) ;
}
function send_sms_bypattern($mobile, $patterncode)
{

    $username = "u09140790345";
    $password = 'Faraz@1355484610792125';
    $from = "5000404223";
    $pattern_code = $patterncode;
    $to = array($mobile);
    $input_data = array("");
    $url = "https://ippanel.com/patterns/pattern?username=" . $username . "&password=" . urlencode($password) . "&from=$from&to=" . json_encode($to) . "&input_data=" . urlencode(json_encode($input_data)) . "&pattern_code=$pattern_code";
    $handler = curl_init($url);
    curl_setopt($handler, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($handler, CURLOPT_POSTFIELDS, $input_data);
    curl_setopt($handler, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($handler);
    var_dump($response) ;
}
function send_to_whatsapp($mobile, $massage){
    //The idInstance and apiTokenInstance values are available in your account, double brackets must be removed
$url = "https://7105.api.greenapi.com/waInstance7105227659/sendMessage/b02aea8fd59f4cbdb0ba75e935f9d45888e1299b3d834505a7";

$mobile= ltrim($mobile, "0");
//chatId is the number to send the message to (@c.us for private chats, @g.us for group chats)
$data = array(
'chatId' => '98'.$mobile.'@c.us', 
'message' => $massage
);

$options = array(
    'http' => array(
        'header' => "Content-Type: application/json\r\n",
        'method' => 'POST',
        'content' => json_encode($data)
    )
);

$context = stream_context_create($options);

$response = file_get_contents($url, false, $context);
    var_dump($response);
}
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_sms($_POST['phone'], $_POST['massage']);
       
        
      
    } else {
        send_sms_bypattern($_POST['phone'], $_POST['pattern_code']);
        
       
    }

}


while ($row = $stmt->fetch()) {
    if(!empty($row['id'])){
        if (empty($row['patterncode'])) {
            send_sms($row['phone'], utf8_decode($row['massage']));
            send_to_whatsapp($row['phone'], utf8_decode($row['massage']));
           update_send_status($row['id'] , $db);
        } else {
            send_sms_bypattern($row['phone'], $row['patterncode']);
            update_send_status($row['id'] , $db);
        }

    }
    
}


