<?php
ini_set('display_errors', 0);

include_once("../inc/db_config.php");

date_default_timezone_set('Asia/Dhaka');

$today=date('Y-m-d');
$notification_date=date('Y-m-d', strtotime($today. ' + 1 days'));

$settingsSQL = mysqli_query($connection, "SELECT * FROM `settings` where name='sms_api' ");
$settingsSQL = mysqli_fetch_object($settingsSQL);
$sms_api = $settingsSQL->value;


$settingsSQL = mysqli_query($connection, "SELECT * FROM `settings` where name='event_sms_no' ");
$settingsSQL = mysqli_fetch_object($settingsSQL);
$event_sms_no = $settingsSQL->value;
$countSQL = mysqli_query($connection, "SELECT count(t1.id) total_event FROM `events` as t1 left join bookings as t2 on t1.temp_id=t2.id where (t2.status!=0 AND t2.status!=2) AND t1.date='$notification_date'  ");
$countSQL = mysqli_fetch_object($countSQL);
$total_event = $countSQL->total_event;


if($total_event>0){
	$sms_for_notification="আগামি কাল আপনার (".$total_event."টি) ইভেন্ট আছে। "; 
 
   $sms_for_notification=urlencode($sms_for_notification);
   $orginal_text = ["%text%", "%number%"];
		  $replace_text = [$sms_for_notification, $event_sms_no];
		   $curl_url = str_replace($orginal_text, $replace_text, $sms_api);
		    $curl = curl_init();
		  curl_setopt_array($curl, array(
   CURLOPT_CONNECTTIMEOUT => 20,
  CURLOPT_TIMEOUT => 520,
  CURLOPT_RETURNTRANSFER => 1,
  CURLOPT_URL =>$curl_url,
   CURLOPT_USERAGENT =>'jadukor IT Browser'
 ));
 
$resp = curl_exec($curl);
curl_close($curl);
echo "sms sent".$event_sms_no.$curl_url;

}
if($total_event=='0'){ echo "no event tomorrow";
}




//$settingsSQL = mysqli_query($connection, "SELECT * FROM `settings` where name='sms_api' ");
//$settingsSQL = mysqli_fetch_object($settingsSQL);
//$sms_api = $settingsSQL->value;


?>