<?php
session_start();
if(isset($_SESSION['pseudo']) OR !empty($_SESSION['pseudo']) OR isset($_SESSION['password']) OR !empty($_SESSION['password']))
{
    //header('Location: accueil.php');
}
include_once('Library/Basededonnees.php');

$idEtablissement = $_POST['interface'];

include('/home/iitech/progiciel.geststudent.net/configuration_etabli2.php');

$database = new Basededonnees($MYSQL_DATABASE);
$bdd = $database->getBdd();

$DB_DATABASEG = 'iitech_universite';
$databaseg = new Basededonnees($DB_DATABASEG);
$bddg = $databaseg->getBdd();


include('notifierErreur2.php');
//include('../cryptage.php');
//$chiffrer=new chiffrement();

if(isset($_POST['login']) && isset($_POST['datenaiss']) && isset($_POST['motpass1'])){
	$password=htmlspecialchars($_POST['motpass1']);
	$password=password_hash($password, PASSWORD_DEFAULT);//crypt($password, 'superhashcode');	
	$matricule 	= $_POST['matri'];
	$email 		= $_POST['adresse'];
	$username 	= $_POST['login'];
	$phone  	= $_POST['phone'];
	$phoneDad 	= $_POST['phoneT'];
	$date_nais 	= $_POST['datenaiss'];

	$query = "SELECT e.nom, e.prenom, e.date_nais, i.ajourGest FROM  tabetudiant AS e 
	INNER JOIN tabinscription AS i ON i.matricule = e.matricule WHERE e.matricule = :matri 
	AND e.date_nais = :date AND i.annee_acad = (SELECT MAX(`annee_acad`) AS `annee_acad` 
	FROM `tabinscription` WHERE matricule = :matricule)";

	$statement = $bdd->prepare($query);
	$statement->bindParam(':matri', $matricule, PDO::PARAM_STR);
	$statement->bindParam(':date', $date_nais, PDO::PARAM_STR);
	$statement->bindParam(':matricule', $matricule, PDO::PARAM_STR);
	$statement->execute();
	$n = $statement->rowCount();	

	$tab=$statement->fetch(PDO::FETCH_ASSOC);
	$ajourGest  = $tab['ajourGest'];
			
	$query1 = "SELECT * 	FROM `users_student` WHERE username = :username";
	$statement1 = $bddg->prepare($query1);
	$statement1->bindParam(':username', $username, PDO::PARAM_STR);
	$statement1->execute();
	$nr1 = $statement1->rowCount();

	$query2 = "SELECT * FROM `users_student` WHERE email = :email";
	$statement2 = $bddg->prepare($query2);
	$statement2->bindParam(':email', $email, PDO::PARAM_STR);
	$statement2->execute();
	$nr2 = $statement2->rowCount();

	$query3 = "SELECT * FROM `users_student` WHERE phone = :phone";
	$statement3 = $bddg->prepare($query3);
	$statement3->bindParam(':phone', $phone, PDO::PARAM_STR);
	$statement3->execute();
	$nr3 = $statement3->rowCount();

	$query4 = "SELECT * FROM `users_student` WHERE phoneDad = :phoneDad";
	$statement4 = $bddg->prepare($query4);
	$statement4->bindParam(':phoneDad', $phoneDad, PDO::PARAM_STR);
	$statement4->execute();
	$nr4 = $statement4->rowCount();

    if($nr1==1){
        echo "Le username $nr2 existe déjà. Veuillez choisir un autre nom d'utilistaeur.";	
        exit();
    }else if($nr2==1){
        echo "Email existe déjà. Veuillez choisir un autre email.";	
        exit();
    }else if($nr3==1){
        echo "Le téléphone existe déjà. Veuillez choisir un autre téléphone.";
        exit();
    }else if($nr4==1){
        echo "Le téléphone  existe déjà. Veuillez choisir un autre téléphone.";
        exit();
    }else if($n==0){
		echo "Vérifier votre matricule et / ou votre date de naissance...";	
	}else{ 
		date_default_timezone_set('Africa/Porto-Novo');
		$date = date('Y-m-d H:i:s', time());
		try{
			$sql  = 'INSERT INTO `users_student` (username, password, email, matricule, phone, created_at, updated_at, phoneDad, idEtablissement)';
			$sql .= 'VALUES (:username, :password, :email, :matricule, :phone, :created_at, :updated_at, :phoneDad, :id)';
			$statement = $bddg->prepare($sql);
			$statement->bindParam(':username', 		$username, 	PDO::PARAM_STR);
			$statement->bindParam(':password', 		$password,	PDO::PARAM_STR);
			$statement->bindParam(':email',  		$email, 	PDO::PARAM_STR);
			$statement->bindParam(':matricule',  	$matricule, PDO::PARAM_STR);
			$statement->bindParam(':phone',  		$phone, 	PDO::PARAM_STR);
			$statement->bindParam(':created_at',  	$date, 		PDO::PARAM_STR);
			$statement->bindParam(':updated_at',  	$date, 		PDO::PARAM_STR);
			$statement->bindParam(':phoneDad',  	$phoneDad, 	PDO::PARAM_STR);
			$statement->bindParam(':id',        	$_POST['interface'], 	PDO::PARAM_STR);
			echo $result = $statement->execute();
		}catch(PDOException  $e ){
			echo $e->getMessage( );
			die();
		}
	}
}else{
}

/*
else if($ajourGest=='NO'){
		echo "Vous n'avez pas de droit de création de compte...";	
	}
*/
?>

