<!DOCTYPE html> 
<html lang="en"> 

<head>
<?php ob_start(); ?>
<?php   require_once 'head_link.php'; ?> 

</head>

<body> 
 
 
<?php   require_once 'header1.php'; ?> 

<?php  require_once 'sidebar.php'; ?>

<?php

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

  // Check if the smssend flag is set
  if (isset($_POST['smssend'])) {

    $BatchId = $_POST['BatchId'];
	$Text = $_POST['Text'];

    // Use prepared statements with placeholders to prevent SQL injection
    $stmt = $DB_con->prepare("SELECT customer_name, contact_info FROM customer WHERE member_id='".$_SESSION['id']."' and batch_no = :BatchId and status=1   ");
    $stmt->execute(['BatchId' => $BatchId]);
    $students = $stmt->fetchAll(PDO::FETCH_ASSOC);

    foreach ($students as $student) {
      $studentsName = $student['customer_name'];
      $number = $student['contact_info'];
       
      $message = "$Text";
 
      // Initialize curl
      $curl = curl_init();

      // Set curl options
      curl_setopt_array($curl, array(
        CURLOPT_URL => 'https://api.sms.net.bd/sendsms',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => http_build_query(array(
          'api_key' => 'LvpiximdpLedV8UDt5TCZvrY090wBCUeZg6J7j62',
          'msg' => $message,
          'to' => $number
        )),
      ));

      // Execute curl request
      $response = curl_exec($curl);

      // Check for errors
      if ($response === false) {
        echo '<script>alert("Error: ' . curl_error($curl) . '")</script>';
      }

      // Close curl
      curl_close($curl);
    }

   echo '<script>alert("SMS sent successfully."); window.location.href=("batch-wise-sms");</script>';
    exit;
  } else {
    echo '<script>alert("Error: Required fields are missing.")</script>';
  }
}
?>

  <main id="main" class="main">

    <div class="pagetitle bg-light rounded p-2">
      <h1><i class="bi bi-people"></i>   Batch Wise SMS <i class="bi bi-chat-left-dots"></i></h1>
       
    </div> 

<section class="section">
      <div class="row">
	  
        <div class="col-lg-6">

          <div class="card">
            <div class="card-body p-5">
				
				<form class="form-horizontal" enctype="multipart/form-data" method="post">
				
				<div class="row mb-3 mt-5">
                  <label for="CustId1" class="col-sm-3 control-label">Batch Name</label>
                  <div class="col-sm-9">
        <select style="width:100%;" class="form-control chosen-select" Id="BatchId" name="BatchId" >
							<option value="#">Select Batch</option>

							<?php
							$sql = "SELECT id,batch_name FROM batch where user_id='".$_SESSION['id']."'  order by id asc ";
							$result = $con->query($sql);

							while ($row = $result->fetch_array()) {
								echo "<option value='" . $row[0] . "'>" . $row[1] . "</option>";
							}
							?>
						</select> 
				  </div>
                </div>
			    
				<div class="row mb-3">
                  <label for="Text" class="col-sm-3 col-form-label">Text / SMS </label>
                  <div class="col-sm-9">
				   <textarea class="form-control" id="Text" name="Text"   rows="5"></textarea> 
                  </div>
                </div>
				  
				  <div class="text-center mt-5">
				  <button type="submit" name="smssend" class="btn btn-primary">  Send SMS <i class="bi bi-send"></i></button>
                  </div>
				  
				  
				</form>

		
			</div>
          </div>

        </div>

		
	  </div>
    </section>

 
	
	 </main>
	
 

<?php  require_once 'footer1.php'; ?>


<script src="custom/js/report.js"></script>
<script src = "js/chosen.js"></script>
<script type = "text/javascript">
	$('.chosen-select').chosen({width: "100%"});
</script>
 