<?php session_start(); ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Ecrire Notifications </title>
<link rel="stylesheet" href="w3.css">
</head>
<body>
<form method="post" action="_notificationecole.php" class="w3-container">
<?php
 include 'header.php';
 include '_serveur.php';
$classeid=0;
$date=$agenda="";
$ctrlErr=""; ?>
<table width=100% border="0">
<tr><td> <input class="w3-check" type="checkbox" name="parents" value="3" />Parents
<input class="w3-check" type="checkbox" name="profs" value="2" />Profs
<input class="w3-check" type="checkbox" name="admin" value="1" />Administration

</td></tr>
<tr><td>Titre:<input class="w3-input" type="text" name="title" value="" />
</td></tr>
<tr><td><label>Date: <input class="w3-input w3-border w3-light-grey" type="date" name="date1" 
value="<?php echo date('Y-m-d'); ?>" /></label></td></tr>
<tr>
    <td width="100%" scope="row"><textarea class="w3-textarea w3-border w3-round" name="message" id="message" cols="50" rows="20" > 
                                 </textarea></td>
  </tr>
  
</table>

<?php
 echo' </p><p> <input name="envoyer" type="submit" class="style19" value="Envoyer" bgcolor="#CC99FF" /></p></form>';
include("sjmSendPush.php");
if (isset ($_POST['envoyer'])){
$title=$_POST['title'];
$message=$_POST['message'];
$date1=$_POST['date1'];
$userType1=$_POST['parents'];
$userType2=$_POST['profs'];
$userType3=$_POST['admin'];
	 if(($userType1<>3)&&($userType2<>2)&&($userType3<>1))
	 {
		 	 $sql1= 'INSERT INTO notifications VALUES(DEFAULT,"'.$title.'","'.$message.'","'.$date1.'",NULL,NULL)';
   if(!(mysqli_query($link, $sql1))){
    echo "ERROR: Could not able to execute $sql1. " . mysqli_error($link);
                                     }
	$sql = 'SELECT * from devices join users on devices.userID=users.id ';
   if(!(mysqli_query($link, $sql))){
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
$req=  mysqli_query($link, $sql);
$i=1; 
$j=1;
echo "<table width='95%' border=1 bordercolor='#D1FAFC'>"; 
while ($data = mysqli_fetch_array($req)) {
	                 $deviceType=$data[2];
					 $pushid=$data[0];
					 $pushid=(string)$pushid;

					 $dataa=array();
					 $regid=array();
				 if($deviceType=="ios") $deviceType="iOS";
                      $payload = array('type' =>$deviceType,
					           'data'=>["title" => $title, "body" => $message],
							   'devices' =>[$pushid]);
					 
                     echo sendPush(json_encode($payload));
}
									 
									 

	 }
	 if($userType1=="3"){
	 $sql1= 'INSERT INTO notifications VALUES(DEFAULT,"'.$title.'","'.$message.'","'.$date1.'","'.$userType1.'",NULL)';
   if(!(mysqli_query($link, $sql1))){
    echo "ERROR: Could not able to execute $sql1. " . mysqli_error($link);
                                     }
	$sql = 'SELECT * from devices join users on devices.userID=users.id where users.type=3 or userID=10001';
   if(!(mysqli_query($link, $sql))){
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
$req=  mysqli_query($link, $sql);
$i=1; 
$j=1;
echo "<table width='95%' border=1 bordercolor='#D1FAFC'>"; 
while ($data = mysqli_fetch_array($req)) {
	                  $deviceType=$data[2];
					 $pushid=$data[0];
					 $pushid=(string)$pushid;

						 if($deviceType=="ios") $deviceType="iOS";
                      $payload = array('type' =>$deviceType,
					           'data'=>["title" => $title, "body" => $message],
							   'devices' =>[$pushid]);
					 
                     echo sendPush(json_encode($payload));
}
									 
	                   }
					   
	 if($userType2=="2"){
	 $sql1= 'INSERT INTO notifications VALUES(DEFAULT,"'.$title.'","'.$message.'","'.$date1.'","'.$userType2.'",NULL)';
   if(!(mysqli_query($link, $sql1))){
    echo "ERROR: Could not able to execute $sql1. " . mysqli_error($link);
                                     }
		$sql = 'SELECT * from devices join users on devices.userID=users.id where users.type=2 or userID=10001';
   if(!(mysqli_query($link, $sql))){
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
$req=  mysqli_query($link, $sql);
$i=1; 
$j=1;
echo "<table width='95%' border=1 bordercolor='#D1FAFC'>"; 
while ($data = mysqli_fetch_array($req)) {
	                 $deviceType=$data[2];
					 $pushid=$data[0];
					 $pushid=(string)$pushid;

						 if($deviceType=="ios") $deviceType="iOS";
                      $payload = array('type' =>$deviceType,
					           'data'=>["title" => $title, "body" => $message],
							   'devices' =>[$pushid]);
					 
                     echo sendPush(json_encode($payload));
}
}
								 
									 
									 
	
	 if($userType3=="1"){
	 $sql1= 'INSERT INTO notifications VALUES(DEFAULT,"'.$title.'","'.$message.'","'.$date1.'","'.$userType3.'",NULL)';
   if(!(mysqli_query($link, $sql1))){
    echo "ERROR: Could not able to execute $sql1. " . mysqli_error($link);
                                     }
									 
		$sql = 'SELECT * from devices join users on devices.userID=users.id where users.type=1 or userID=10001';
   if(!(mysqli_query($link, $sql))){
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
$req=  mysqli_query($link, $sql);
$i=1; 
$j=1;
echo "<table width='95%' border=1 bordercolor='#D1FAFC'>"; 
while ($data = mysqli_fetch_array($req)) {
	                 $deviceType=$data[2];
					 $pushid=$data[0];
					 $pushid=(string)$pushid;

						 if($deviceType=="ios") $deviceType="iOS";
                      $payload = array('type' =>$deviceType,
					           'data'=>["title" => $title, "body" => $message],
							   'devices' =>[$pushid]);
					 
                     echo sendPush(json_encode($payload));
}
								 
	                   }				   				   
  echo ' <div class="w3-panel w3-leftbar w3-sand w3-xxlarge w3-serif">
    <p><i>"Notifications déjà faite"</i></p>
  </div>';

 
}

?>




</body>
</html>