<?php
   if(isset($_GET['SS']))
   {
   $ses=$_GET['SS'];
   }
   
   $query=mysqli_query($con,"select * from SS where Ses='$ses'");
   while($row=mysqli_fetch_array($query))
   {
   $msg=$row["Msg"];
   }
   
   if(isset($_POST['SENDNOW']))
   {
       $msg=addslashes($_POST["themsg"]);
       
       $query=mysqli_query($con,"select * from SS where Ses='$ses' AND Sent='No'");
       while($row=mysqli_fetch_array($query))
       {
           $cont=$row["Contact"]; 
         
           $baseurl = "https://sms.textsms.co.ke/api/services/sendsms/";
           $ch = curl_init($baseurl);
           $data= array(
           "apikey"=>"dd831d08b4bef652dbb8dbafabdab40b",
           "response_type"=> "json",
           "partnerID"=>"11288",
           "message"=>$msg,
           "shortcode"=>"BISHOPKAIRO",
           "service_id"=>0,
           "mobile"=>$cont,
           );
           
           $payload = json_encode($data);
           curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
           curl_setopt($ch, CURLOPT_HTTPHEADER, array('content-Type: application/json','h_api_key:dd831d08b4bef652dbb8dbafabdab40b'));
           curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
           $result = json_encode(curl_exec($ch));
         
           $sql=mysqli_query($con,"UPDATE SS SET Sent='Yes',Msg='$msg' WHERE Contact='$cont' AND Ses='$ses'");   
       }
   }
   ?>
<div class="content-wrapper">
   <!-- Content Header (Page header) -->
   <div class="content-header">
      <div class="container-fluid">
         <div class="row mb-2">
            <div class="col-sm-6">
               <h4 class="m-0">Messaging</h4>
            </div>
            <!-- /.col -->
         </div>
         <!-- /.row -->
      </div>
      <!-- /.container-fluid -->
   </div>
   <section class="content">
      <div class="container-fluid">
         <div class="row">
            <div class="col-md-3 col-sm-6 col-12">
               <div class="info-box">
                  <div class="info-box-content">
                     <a href="index.php?page=Messaging"><i class="fa fa-long-arrow-left"></i> Back </a>
                  </div>
               </div>
            </div>
            <div class="col-md-2 col-sm-6 col-12">
               <div class="info-box">
                  <span class="info-box-icon bg-info"><i class="far fa-envelope"></i></span>
                  <?php
                     include('../include/config.php');
                     $result = mysqli_query($con,"select * from SS where Ses='$ses'");
                     $num_rows1 = mysqli_num_rows($result);
                     {
                      $sts=htmlentities($num_rows1); 
                     }
                     ?>
                  <div class="info-box-content">
                     <span class="info-box-text">All Recipients</span>
                     <h4 class="info-box-number budge"><?php echo $sts; ?></h4>
                  </div>
                  <!-- /.info-box-content -->
               </div>
               <!-- /.info-box -->
            </div>
            <div class="col-md-3 col-sm-6 col-12">
               <div class="info-box">
                  <span class="info-box-icon bg-success"><i class="far fa-flag"></i></span>
                  <?php
                     include('../include/config.php');
                     $result = mysqli_query($con,"select * from SS where Ses='$ses' AND Sent='Yes'");
                     $num_rows1 = mysqli_num_rows($result);
                     {
                      $tea=htmlentities($num_rows1); 
                     }
                     ?>
                  <div class="info-box-content">
                     <span class="info-box-text">Sent to</span>
                     <span class="info-box-number text-danger"><?php echo $tea.' of '.$sts; ?></span>
                  </div>
                  <!-- /.info-box-content -->
               </div>
               <!-- /.info-box -->
            </div>
         </div>
      </div>
   </section>
   <section class="content">
      <div class="row">
         <div class="col-md-3">
            <form method="POST" action="" enctype="multipart/form-data">
               <div class="row">
                  <div class="col-md-12">
                     <div class="card card-primary">
                        <div class="card-header">
                           <div class="card-tools">
                              <button type="button" class="btn btn-tool" data-card-widget="collapse" title="Collapse">
                              <i class="fas fa-minus"></i>
                              </button>
                           </div>
                        </div>
                        <div class="card-body">
                           <div class="form-group">
                              <label for="inputProjectLeader">Message</label>
                              <textarea class="form-control" name="themsg" rows='4'> <?php echo $msg; ?></textarea>
                           </div>
                           <div class="form-group">
                              <center><label id="feedback1"></label></center>
                           </div>
                           <div class="form-group">
                              <button class="btn btn-primary pull-right btn-sm" name="SENDNOW"> Send <i class="fa fa-paper-plane"></i> </button>
                           </div>
                        </div>
                        <!-- /.card-body -->
                     </div>
                     <!-- /.card -->
                  </div>
               </div>
            </form>
         </div>
         <div class="col-md-9">
            <div class="card ">
               <div class="card-header">
                  <h3 class="card-title">All Recipients</h3>
                  <div class="card-tools">
                     <button type="button" class="btn btn-tool" data-card-widget="collapse" title="Collapse">
                     <i class="fas fa-minus"></i>
                     </button>
                  </div>
                  <div class="card-body">
                     <div class="table-responsive" id='divstaffss'>
                        <table class="table table-hover table-success table-sm" id="tablestaffsss">
                           <thead>
                              <tr>
                                 <th>#</th>
                                 <th>Recipient</th>
                                 <th>Contact</th>
                                 <th>Child's Class</th>
                                 <th>Sent</th>
                              </tr>
                           </thead>
                           <tbody>
                              <?php
                                 $cnt=1;
                                 $query=mysqli_query($con,"select SS.*,pupils.PName as pnm,pupils.Names as lname from SS left join pupils ON pupils.Contact=SS.Contact where SS.Ses='$ses'");
                                 while($row=mysqli_fetch_array($query))
                                 {
                                 ?> 
                              <tr>
                                 <td><?php echo $cnt; ?></td>
                                 <td><?php echo $row["pnm"]; ?> <i class="fa fa-long-arrow-right"></i> <?php echo $row["lname"]; ?></td>
                                 <td><?php echo $row["Contact"]; ?></td>
                                 <td><?php echo $row["Grade"]; ?></td>
                                 <td><?php echo $row["Sent"]; ?></td>
                              </tr>
                              <?php
                                 $cnt=$cnt+1;
                                     }
                                 ?>
                           </tbody>
                        </table>
                     </div>
                  </div>
               </div>
            </div>
         </div>
      </div>
   </section>
</div>