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

$mail = $_SESSION['mail'];
$matricule = isset($_POST['matricule']) ? $_POST['matricule'] : NULL; //recuperation valeur formulaire, isset pour eviter l'erreur "Notice: Undefined index"
$marque = isset($_POST['marque']) ? $_POST['marque'] : NULL;
$modele = isset($_POST['modele']) ? $_POST['modele'] : NULL;
$nbplaces = isset($_POST['nbplaces']) ? $_POST['nbplaces'] : NULL;
$anneefab = isset($_POST['anneefab']) ? $_POST['anneefab'] : NULL;

if (!isset($_SESSION['mail'])) {
    header('Location: index.php');
    exit();
}

if (isset($_POST['submitadd'])) {
    addVoiture($db, $matricule, $marque, $modele, $nbplaces, $anneefab);
}
if (isset($_POST['submitchange'])) {
    changeVoiture($db, $matricule, $marque, $modele, $nbplaces, $anneefab);
}

$user = getUser($db, $mail);

if (!$user){
    header('Location: profil.php?error=3');
    exit();
}

if (isset($_POST['login']) || isset($_FILES['avatar'])){
    if (isset($_FILES['avatar']) and !empty($_FILES['avatar']['name'])) {
        $fic = $_FILES['avatar'];
        supprimerAvatar($mail);
        $res = uploadAvatar($mail, $fic);
        switch ($res){
            case -1:
                header('Location: profil.php?error=4');
                exit();
            case -2:
                header('Location: profil.php?error=5');
                exit();
            case -3:
                header('Location: profil.php?error=6');
                exit();
        }
    }
    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']));
        $confirmation = htmlentities(pg_escape_string($_POST['confirmation']));
        $prenom = htmlentities(pg_escape_string($_POST['prenom']));
        $nom = htmlentities(pg_escape_string($_POST['nom']));
        $phone = htmlentities(pg_escape_string($_POST['phone']));

        if (loginChange($user, $login)) {
            if (!isLoginUniqueModifier($db, $mail, $login)) {
                header('Location: profil.php?error=1');
                exit();
            } else {
                if (!modifierLogin($db, $mail, $login)) {
                    header('Location: profil.php?error=6');
                    exit();
                }
            }
        }

        if (strcmp($password, $confirmation) != 0) {
            header('Location: profil.php?error=2');
            exit();
        }

        if (!modifierUtilisateur($db, $mail, $prenom, $nom, $password, $phone)) {
            header('Location: profil.php?error=6');
            exit();
        }
    }
    sleep(0.85);
    header('Location: profil.php');
    exit();
}
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="profil.css">
    <link rel="stylesheet" href="../ressources/navbarhtml.css">
    <link rel="stylesheet" href="../index.css">
    <script src="../inscription/inscription.js"></script>
    <title>Profil</title>
</head>
<body>
<?php require_once("../ressources/navbarhtml.php"); ?>
<div id="formulaire">
    <h1>Profil de <?php echo $user['prenom']; ?></h1>
    <div id="divImgProfil">
        <img id="imgProfil" src="<?php echo getPathImgProfil($mail); ?>">
    </div>
    <script type="text/javascript">
      function visibilite(thingId){
        var targetElement;
        var elements;
        targetElement = document.getElementById(thingId) ;
        elements = document.getElementsByClassName("Element")
        for (var i = 0; i < elements.length; i++){
          if(!targetElement.isSameNode(elements[i])){
            elements[i].style.display = "none" ;
          }
        }
        if (targetElement.style.display == "none"){
          targetElement.style.display = "";
        }
        else{
          targetElement.style.display = "none" ;
        }
      }
    </script>
    <ul class="profil-nav">
      <li class="FT"><a href="" onclick="javascript:visibilite('profilgeneral'); return false;">Profil général</a></li>
      <li class="FT"><a href="" onclick="javascript:visibilite('profilvoiture'); return false;">Profil voiture</a></li>
    </ul>
    <div id="profilgeneral" class="Element" style="display:none;">
      <form method="post" action="profil.php" enctype="multipart/form-data">
          <div class="line">
              <label for="mail">Adresse mail : </label>
              <input id="mail" class="form" type="email" maxlength="100" readonly value="<?php echo $user['mail']; ?>">
          </div>
          <div class="line">
              <label for="login">Login : </label>
              <input id="login" class="form" name="login" type="text" maxlength="50"
                     value="<?php echo $user['login']; ?>" required>
          </div>
          <div class="line">
              <label for="password">Mot de passe : </label>
              <input class="form" id="password" name="password" type="password" maxlength="50"
                     value="<?php echo $user['passwd']; ?>" required>
          </div>
          <div class="line">
              <label for="confirmation">Confirmation : </label>
              <input class="form" id="confirmation" name="confirmation" type="password" maxlength="16"
                     value="<?php echo $user['passwd']; ?>" required>
          </div>
          <div class="line">
              <label for="prenom">Prénom : </label>
              <input id="prenom" class="form" name="prenom" type="text" maxlength="50"
                     value="<?php echo $user['prenom']; ?>" required>
          </div>
          <div class="line">
              <label for="nom">Nom : </label>
              <input id="nom" class="form" name="nom" type="text" maxlength="50" value="<?php echo $user['nom']; ?>"
                     required>
          </div>
          <div class="line">
              <label for="dateN">Date de naissance : </label>
              <input id="dateN" class="form" readonly="readonly" type="date"
                     value="<?php echo $user['datenaissance']; ?>" 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"
                     value="<?php echo $user['telephone']; ?>" 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 getErrorProfil($_GET['error']); ?>
          <input id="submit" class="button" type="submit" value="Modifier">
      </form>
    </div>
    <div id="profilvoiture" class="Element" style="display:none;">
      <script type="text/javascript">
          function ShowTab(E) {
              document.getElementById("addVoiture").style.display = (E == 1) ? 'block' : 'none';
              document.getElementById("changeVoiture").style.display = (E == 2) ? 'block' : 'none';
              document.getElementById("showVoiture").style.display = (E == 3) ? 'block' : 'none';
          }
      </script>
      <form id="choixFormeTestVoiture">
          <div>
              <input type="radio" name="TabCheck" onclick="ShowTab(1)"/>Ajouter une voiture
              <input type="radio" name="TabCheck" onclick="ShowTab(2)"/>Modifier ma voiture
              <input type="radio" name="TabCheck" onclick="ShowTab(3)"/>Afficher ma voiture
          </div>
      </form>
      <div id="addVoiture" style="display: none;">
          <h1>Ajouter une voiture</h1>
          <fieldset>
              <form action="profil.php" method="post">
                  <table>
                      <tr>
                          <td><p>matricule :</p></td>
                          <td><input type="text" name="matricule"/></td>
                      </tr>
                      <tr>
                          <td><p>marque :</p></td>
                          <td><input type="text" name="marque"/></td>
                      </tr>
                      <tr>
                          <td><p>modele :</p></td>
                          <td><input type="text" name="modele"/></td>
                      </tr>
                      <tr>
                          <td><p>nombre de places :</p></td>
                          <td><input type="number" maxlength="1" name="nbplaces"/></td>
                      </tr>
                      <tr>
                          <td><p>année de fabrication :</p></td>
                          <td><input type="number" nmaxlength="4" name="anneefab"/></td>
                      </tr>
                      <tr>
                          <td><p><input type="submit" name="submitadd" value="ajouter une voiture"></td>
                      </tr>
                  </table>
              </form>
          </fieldset>
      </div>
      <div id="changeVoiture" style="display: none;">
          <h1>Modifier une voiture</h1>
          <fieldset>
              <form action="profil.php" method="post">
                  <table>
                      <tr>
                          <td><p>matricule :</p></td>
                          <td><input type="text" name="matricule"/></td>
                      </tr>
                      <tr>
                          <td><p>marque :</p></td>
                          <td><input type="text" name="marque"/></td>
                      </tr>
                      <tr>
                          <td><p>modele :</p></td>
                          <td><input type="text" name="modele"/></td>
                      </tr>
                      <tr>
                          <td><p>nombre de places :</p></td>
                          <td><input type="number" maxlength="1" name="nbplaces"/></td>
                      </tr>
                      <tr>
                          <td><p>année de fabrication :</p></td>
                          <td><input type="number" nmaxlength="4" name="anneefab"/></td>
                      </tr>
                      <tr>
                          <td><p><input type="submit" name="submitchange" value="modifier une voiture"></td>
                      </tr>
                  </table>
              </form>
          </fieldset>
      </div>
      <div id="showVoiture" style="display: none;">
          <h1>Afficher ma voiture</h1>
          <fieldset>
              <form action="profil.php" method="post">
                  <table>
                      <tr>
                        <td><p><input type="submit" name="submitshow" value="afficher ma voiture"></p></td>
                      </tr>
                      <tr>
                        <td><?php if (isset($_POST['submitshow'])) { showVoiture($db); }?></td>
                      </tr>
                  </table>
              </form>
          </fieldset>
      </div>
</div>
</body>
</html>
