<!DOCTYPE html>
<html>
<head>
	<?php include('include/head.php'); ?>
	
	<!-- <title>Send SMS to All</title> -->
</head>
<body>
	<?php include('include/header.php'); ?>
	<?php include('include/sidebar.php'); ?>
	<div class="main-container">
		<div class="pd-ltr-20 customscroll customscroll-10-p height-100-p xs-pd-20-10">
			<div class="min-height-200px">
				<div class="page-header">
					<div class="row">
						<div class="col-md-6 col-sm-9">
							<div class="title">
								<h4>Send SMS to Course Students</h4>
							</div>
							<nav aria-label="breadcrumb" role="navigation">
								<ol class="breadcrumb">
									<li class="breadcrumb-item"><a href="dashboard.php">Dashboard</a></li>
									<li class="breadcrumb-item active" aria-current="page">Send SMS to Course Students</li>
								</ol>
							</nav>
						</div>

						<div class="col-md-6 col-sm-3" align="right">
							<a href="sms_to_course_view.php"><button type="button" class="btn btn-primary" align="right">View Send SMS to Course Students</button></a>
						</div>
						
					</div>
				</div>


				
<div class="pd-20 bg-white border-radius-4 box-shadow mb-30">
					<!-- <div class="clearfix">
						<div class="pull-left">
								<h4 class="text-blue">Add Seller</h4>
						</div>
						
					</div><br>  -->
						
					<form method="post" enctype="multipart/form-data">

						<?php
								// $query="select sr.*,c.* from tbl_student_reg sr,tbl_course c where c.fld_course_id=sr.fld_course_id and sr.fld_stud_reg_delete='0' group by sr.fld_stud_reg_id order by sr.fld_stud_reg_id desc";
        //                             $row=mysqli_query($connect,$query) or die(mysqli_error($connect));

        //                             $count=mysqli_num_rows($row);
                                   
						?>

						<!-- <div class="form-group row">
							<label class="col-sm-12 col-md-2 col-form-label">Total Students : </label>
							<div class="col-sm-12 col-md-10">
								<input class="form-control" type="text" name="student_count" placeholder="Total count" readonly value="<?php //echo $count; ?>">
							</div>
						</div> -->

						<div class="form-group row">
							<label class="col-sm-12 col-md-2 col-form-label">Message Code <span class="text-danger">*</span> : </label>
							<div class="col-sm-12 col-md-10">
								<input class="form-control" type="text" name="Message_code" placeholder="Message Code" required="" value="GBSOLU" readonly=""  maxlength="6" minlength="6" onkeyup="myFunction()" id="Message_code" onchange="myFunction()">
								<!-- <p class="help-block" style="color: red">Note : Exactly 6 Characters can be enter.</p> -->
							</div>
						</div>
			<script type="text/javascript">
			    function myFunction() {
			  var str = document.getElementById("Message_code").value;
			  var res = str.toUpperCase();
			  document.getElementById("Message_code").value = res;
			}
			</script>						
						<div class="form-group row">
							<label class="col-sm-12 col-md-2 col-form-label">Course Name <span class="text-danger">*</span> : </label>
							<div class="col-sm-12 col-md-10">
								<select class="form-control" name="course_name" required="">
									<option value="">Select Course</option>
									<?php 
				                        $query= mysqli_query($connect,"select distinct fld_course_name,fld_course_id from tbl_course where fld_course_delete='0' order by fld_course_id desc");

				                        while ($row=mysqli_fetch_assoc($query)) {
				                          extract($row);

				                    ?>
				                          <option value="<?php echo $row['fld_course_id']?>"><?php echo $row['fld_course_name']?></option>
				                        <?php } ?>
								</select>
							</div>
						</div>
						<div class="form-group row">
							<label class="col-sm-12 col-md-2 col-form-label">Message <span class="text-danger">*</span> : </label>
							<div class="col-sm-12 col-md-10">
								<!-- <textarea class="textarea_editor form-control border-radius-0"  name="Alert_message" placeholder="Enter Message " required=""></textarea> -->
								<textarea class="form-control"  name="Alert_message" placeholder="Enter Message " required="" maxlength="157"></textarea>
								<p class="help-block" style="color: red">Note : Please do not use apostrophe (') or single quote (' ') or double quote (" ") and also do not press Enter Key. Upto 157 Characters can be enter.</p>
							</div>
						</div>
						
						
						<div class="form-group row">
<!-- 							<label class="col-sm-12 col-md-2 col-form-label">Submit</label>
 -->							<div class="col-sm-12 col-md-10">
							<center><input class="btn btn-success" value="Send" type="submit" name="submit" onclick="return confirm('Are You Sure To Send Message')">
							</center>
							</div>
						</div>
					</form>

<?php


if(isset($_POST['submit']))
{
	extract($_POST);
	 $asas="insert into tbl_message(fld_message,fld_student_count,fld_message_code,fld_course_id,fld_message_status) values('$Alert_message', '$student_count','$Message_code','$course_name','Course')";
	$asdf=mysqli_query($connect,$asas) or die(mysqli_query($connect));

	 if ($asdf) 
     {

    	$query1="select * from tbl_student_reg where fld_stud_reg_delete='0' and fld_course_id='".$_POST['course_name']."'  group by fld_stud_reg_id order by fld_stud_reg_id desc";
            $row=mysqli_query($connect,$query1) or die(mysqli_error($connect));            
            while($fetch=mysqli_fetch_array($row)) {

            extract($fetch);

            	$msg=$_POST['Alert_message'];
					        

					$mobile=$fld_mobile;

					$ch = $_POST['Message_code'];

			        $apiurl =
			"http://sms.happysms.in/api/sendhttp.php?authkey=242446A57b8VBMQpOd5bc06387&mobiles=$mobile&message=$msg&sender=$ch&route=4&country=91";

					      $ch = curl_init($apiurl);

					      if($method == "GET")
					      {
					        curl_setopt($ch, CURLOPT_POST,1);
					        curl_setopt($ch, CURLOPT_POSTFIELDS,$apiurl);
					      }
					      else
					      {
					        $get_url = $apiurl;
					        curl_setopt($ch, CURLOPT_POST,0);
					        curl_setopt($ch, CURLOPT_URL, $get_url);
					      }

					      curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
					      curl_setopt($ch, CURLOPT_HEADER,0);
					      // DO NOT RETURN HTTP HEADERS
					      curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
					      // RETURN THE CONTENTS OF THE CALL
					      $return_val = curl_exec($ch);

					      if($return_val=="")
					      {
					      echo "Process Failed";
					      } 

        }


        echo "<script>";
        echo "alert('Message Sent');";
        echo "</script>";                 
    }
    else
    {
        echo "<script>";
        echo "alert('Error');";
        echo "</script>";
    }
}

?>

	</div>
			<?php include('include/footer.php'); ?>
		</div>
	</div>
	<?php include('include/script.php'); ?>
</body>
</html>

