<?php
session_start();
require_once('../connexionBD.php');
require_once('../ressources/user.php');

if (!isset($_SESSION['mail'])){
  if (isset($_POST['login']) || isset($_FILES['avatar'])){
    if (isset($_POST['login']) && isset($_POST['password']) && isset($_POST['confirmation']) && isset($_POST['prenom']) && isset($_POST['nom']) && isset($_POST['phone'])){
      $login = htmlentities(pg_escape_string($_POST['login']));
      $password = htmlentities(pg_escape_string($_POST['password']));
      $prenom = htmlentities(pg_escape_string($_POST['prenom']));
      $nom = htmlentities(pg_escape_string($_POST['nom']));
      $dateN = htmlentities(pg_escape_string($_POST['dateN']));
      $mail = strtolower(htmlentities(pg_escape_string($_POST['mail'])));
      $phone = htmlentities(pg_escape_string($_POST['phone']));

      if (!filter_var($mail, FILTER_VALIDATE_EMAIL)){
        header('Location: inscription.php?error=1');
        exit();
      }
      elseif (!isMailUnique($db, $mail)){
        header('Location: inscription.php?error=2');
        exit();
      }
      elseif (!isLoginUnique($db, $login)){
        header('Location: inscription.php?error=3');
        exit();
      }
      else {
        if (isset($_FILES['avatar']) and !empty($_FILES['avatar']['name'])) {
        $fic = $_FILES['avatar'];
        $res = uploadAvatar($mail, $fic);
        switch ($res){
          case -1:
          header('Location: inscription.php?error=4');
          exit();
          case -2:
          header('Location: inscription.php?error=5');
          exit();
          case -3:
          header('Location: inscription.php?error=6');
          exit();
        }
      }
      $result = creerUtilisateur($db, $mail, $password, $prenom, $nom, $login, $phone, $dateN);
      if ($result) {
        header('Location: ../index.php');
        exit();
      }
      else{
        header('Location: inscription.php?error=6');
        exit();
      }
    }
  }
}
}
else {
  header('Location: ../tableauBord/tableauBord.php');
  exit();
}
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="inscription.css">
    <title>Inscription</title>
</head>
<body>
<div id="formulaire">
    <h1>Inscription</h1>
    <form method="post" enctype="multipart/form-data" action="inscription.php">
        <div class="line">
            <label for="mail">Adresse mail : </label>
            <input id="mail" class="form" name="mail" type="email" maxlength="100" required>
        </div>
        <div class="line">
            <label for="login">Login : </label>
            <input id="login" class="form" name="login" type="text" maxlength="50" required>
        </div>
        <div class="line">
            <label for="password">Mot de passe : </label>
            <input class="form" id="password" name="password" type="password" maxlength="50" required>
        </div>
        <div class="line">
            <label for="confirmation">Confirmation : </label>
            <input class="form" id="confirmation" name="confirmation" type="password" maxlength="16">
        </div>
        <div class="line">
            <label for="prenom">Prénom : </label>
            <input id="prenom" class="form" name="prenom" type="text" maxlength="50" required>
        </div>
        <div class="line">
            <label for="nom">Nom : </label>
            <input id="nom" class="form" name="nom" type="text" maxlength="50" required>
        </div>
        <div class="line">
            <label for="dateN">Date de naissance : </label>
            <input id="dateN" class="form" name="dateN" type="date" required>
        </div>
        <div class="line">
            <label for="phone">Numéro de téléphone : </label>
            <input id="phone" class="form" name="phone" type="tel" pattern="[0-9]{10}" maxlength="10" required>
        </div>
        <div class="line">
            <label for="avatar">Image du Profil :</label>
            <input type="file" id="avatar" class="form" name="avatar" accept="image/png, image/jpeg">
        </div>
        <?php getErrorInscription($_GET['error']); ?>
        <input id="submit" class="button" type="submit" value="S'inscrire">
    </form>
    <p id="enregistrer">Si vous avez déjà un compte <a href="index.php" title="S'identifier">cliquez ici</a>.</p>
</div>
<script src="inscription.js"></script>
</body>
</html>
