<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

include "main.php";

isconnected();

if(isset($_REQUEST['action']))
{
	$action = $_REQUEST['action'];
	if($action == 1)
	{
		$groupe = AntiInjectionSQL($_REQUEST['groupe']);
		$message = AntiInjectionSQL($_REQUEST['message']);
		
		$clientid = getParametre("clientid");
		$clientsecret = getParametre("secretid");
		$phonesender = getParametre("phonesender");
		$phonesender = 'tel:'.$phonesender;
		
		require "api/Orange/Osms.php";
		//use \Osms\Osms;

		$config = array('clientId' => $clientid, 'clientSecret' => $clientsecret);
		
		$osms = new Osms($config);
		$osms->setVerifyPeerSSL(false);
		$response = $osms->getTokenFromConsumerKey();
		$config1 = array('token' => $response['access_token']);
		
		$count = 0;

		$SQL = "SELECT * FROM numero WHERE idgroupe = $groupe";
		$r = $pdo->query($SQL);
		while($rr = $r->fetch())
		{
			$tel = $rr['telephone'];
			$tel = 'tel:+221'.$tel;
			$osms1 = new Osms($config1);
			$response1 = $osms1->sendSms($phonesender,$tel,$message);
			$count++;
		}
		
		header("Location: expedition.php?valid=1&count=$count");
		exit;
	}
}

?>

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

<head>
    <meta charset="UTF-8">
    <meta name="description" content="">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <!-- The above 4 meta tags *must* come first in the head; any other head content must come *after* these tags -->

    <!-- Title -->
    <title>SMS Sender</title>

    <!-- Favicon -->
    <link rel="icon" href="img/core-img/favicon.png">

    <!-- Master Stylesheet [If you remove this CSS file, your file will be broken undoubtedly.] -->
    <link rel="stylesheet" href="style.css">

</head>

<body>
    <!-- Preloader Start -->
    <div id="preloader">
        <div class="preload-content">
            <div id="hasro-load"></div>
        </div>
    </div>
    <!-- Preloader End -->

    <!-- ======================================
    ******* Page Wrapper Area Start **********
    ======================================= -->
    <div class="ecaps-page-wrapper">
        <!-- Sidemenu Area -->
        <?php
		
		include "menu.php";
		
		?>

            <!-- Main Content Area -->
            <div class="main-content">
                <div class="container-fluid">
                    <div class="row justify-content-center">
                        <div class="col-sm-12 col-lg-12">
							<div class="card box-margin">
								<div class="card-body">
									<h3><i class="bx bx-envelope"></i> Expedition de SMS</h3>
									<div class="alert bg-secondary" style="color:#fff;">
									Lors de l'expédition de SMS, selectionnez un groupe, inserez le message que vous souhaitez envoyer et attendez la fin de l'opération.
									Ne fermez pas cette page.
									</div>
									<?php
									if(isset($_REQUEST['valid']))
									{
										?>
										<div class="alert bg-success" style="color:#fff;">
                                        <?php echo $_REQUEST['count']; ?> SMS envoyé(s) !
                                        </div>
										<?php
									}
									?>
									<form method="POST">
										<div class="form-group">
											<label for="exampleInputEmail111">Selectionner le groupe qui recevra le SMS</label>
											<select name="groupe" class="form-control">
												<?php
												
												$SQL = "SELECT * FROM groupe";
												$r = $pdo->query($SQL);
												while($rr = $r->fetch())
												{
													?>
													<option value="<?php echo $rr['id']; ?>"><?php echo $rr['nom']; ?></option>
													<?php
												}
												
												?>
											</select>
										</div>
										<div class="form-group">
											<label for="exampleInputEmail111">Votre message par SMS</label>
											<textarea name="message" maxlength="160" class="form-control" id="msg" onkeyup="updatemsg();"></textarea>
										<div id="nbrcar">0 caractère(s)</div>
										</div>
										<input type="hidden" name="action" value="1">
										<button type="submit" class="btn btn-primary mr-2"><i class="zmdi zmdi-collection-plus"></i> Envoyer</button>
									</form>
								</div>
							</div>
                        </div>
                        
                    </div>

                    <div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-modal="false">
                        <div class="modal-dialog modal-lg">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <h5 class="modal-title mt-0 font-16" id="myLargeModalLabel">Add New Customer</h5><button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                                </div>
                                <div class="modal-body">
                                    <form>
                                        <div class="row">
                                            <div class="col-md-6">
                                                <div class="form-group"><label for="LeadName">Name</label> <input type="text" class="form-control" id="LeadName" required=""></div>
                                            </div>
                                            <div class="col-md-6">
                                                <div class="form-group"><label for="LeadEmail">Email</label> <input type="email" class="form-control" id="LeadEmail" required=""></div>
                                            </div>
                                        </div>
                                        <div class="row">
                                            <div class="col-md-6">
                                                <div class="form-group"><label for="PhoneNo">Phone No</label> <input type="text" class="form-control" id="PhoneNo" required=""></div>
                                            </div>
                                            <div class="col-md-6">
                                                <div class="form-group"><label for="status-select" class="mr-2">Country</label> <select class="custom-select" id="status-select">
                                                        <option selected="">Select</option>
                                                        <option value="1">India</option>
                                                        <option value="2">USA</option>
                                                        <option value="3">Japan</option>
                                                        <option value="4">China</option>
                                                        <option value="5">Germany</option>
                                                    </select></div>
                                            </div>
                                        </div><button type="button" class="btn btn-sm btn-primary">Save</button> <button type="button" class="btn btn-sm btn-danger">Delete</button>
                                    </form>
                                </div>
                            </div><!-- /.modal-content -->
                        </div><!-- /.modal-dialog -->
                    </div>

                </div>

                <!-- Footer Area -->
            </div>
        </div>
    </div>
    <script>
    function updatemsg()
    {
        var nmessage = $('#msg').val();
        $('#nbrcar').html(nmessage.length+' caractère(s)');
    }
    </script>

    <!-- ======================================
    ********* Page Wrapper Area End ***********
    ======================================= -->

    <!-- Must needed plugins to the run this Template -->
    <script src="js/jquery.min.js"></script>
    <script src="js/popper.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/bundle.js"></script>
    <script src="js/default-assets/setting.js"></script>

    <!-- Active JS -->
    <script src="js/default-assets/active.js"></script>

    <script src="js/default-assets/ammap.min.js"></script>
    <script src="js/default-assets/radar.js"></script>
    <script src="js/default-assets/widget-page-chart-active.js"></script>

    <script src="js/default-assets/apexchart.min.js"></script>
    <script src="js/default-assets/dashboard-active.js"></script>


</body>

</html>