<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {

   // post data
$numbersingle = trim($_POST['number']);
$all = $_POST['group'];
$message = $_POST['message'];
   //end of post dara 

   //server connection
   require 'database.php';
   // end of server connection


   // Message can only be sent to register members and guests
// A connection to the church database is required

   // Switch condition for checkBox
   switch ($all) {
    case 'everyone':
        $allSql = "SELECT per_CellPhone From person_per where trim(per_CellPhone)";

        $results = mysqli_query($conn, $allSql);
    
    $listnumber = mysqli_fetch_all($results, MYSQLI_BOTH);
    
    foreach ($listnumber as $number) {
        //printf("%s\n",  $number[0]);
       printf(implode(",", $number));
      $dataforAll =  `echo $number[0],`;
     echo  $dataforAll;
     //text serverconnection
$ch = curl_init('https://textbelt.com/text');
$data = array(
  'phone' => '$dataforAll',
  'message' => '$message',
  'key' => '_textbelt',
);

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
echo $response;
curl_close($ch);
//End of text Server
     
    }
    break;

     case 'Committee':
        $allCommiteeSQL = "SELECT per_CellPhone From person_per where trim(per_CellPhone)";

        $results3 = mysqli_query($conn, $allCommiteeSql);
    
    $listnumber = mysqli_fetch_all($results3, MYSQLI_BOTH);
    
    foreach ($listnumber as $number) {
        //printf("%s\n",  $number[0]);
       // printf(implode(",", $number));
      $dataforAll =  `echo $number[0],`;
     echo  $dataforAll;

        //text serverconnection
$ch = curl_init('https://textbelt.com/text');
$data = array(
  'phone' => '$dataforAll',
  'message' => '$message',
  'key' => '_textbelt',
);

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
echo $response;
curl_close($ch);
//End of text Server

     
    }
    break;

     case 'individual':
        // we need to add a condition to check if the number enters match any number in the db system before sending the text

       // $singleNumberSQL = "SELECT * from person_per where per_CellPhone like '%$numbersingle%'";
       $singleNumberSQL = "SELECT * from person_per where per_CellPhone like '%$numbersingle%'";



    $results2 = mysqli_query($conn, $singleNumberSQL);

    while ($phoneNumber = mysqli_fetch_array($results2)) {
       $individual = ` echo $phoneNumber[per_CellPhone]`;
       $formatIndividual = preg_replace('~.*(\d{3})[^\d]{0,7}(\d{3})[^\d]{0,7}(\d{4}).*~', '($1) $2-$3', $individual). "\n";
      //  print_r($individual) ;
      //  print_r($message) ;
       
       //text serverconnection
// $ch = curl_init('http://172.233.15.218:9090/text');
//  $data = array(
//   'phone' => $individual,
//    'message' => $message,
//   //'key' => '',
//   );

$ch = curl_init('https://textbelt.com/text');
$data = array(
'phone' => $numbersingle,
'message' => $message,
'key' => 'b340b66096d1b828aa09e474d06c928d3897f49bVF38B84MywezRt60G885S9A6M',
 );

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
echo $response;
curl_close($ch);
//End of text Server
    } 

    break;
    

   default:
   echo "No Phone number was entered or no group was selected";
   //end of checkBox Switch





}

}