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

$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();
}

$delmail = $_POST['delmail'];
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="admin.css">
    <link rel="stylesheet" href="../ressources/navbaradminhtml.css">
    <link rel="stylesheet" href="../index.css">
    <script src="../inscription/inscription.js"></script>
    <title>Administration</title>
</head>
<body>
<?php require_once("../ressources/navbaradminhtml.php"); ?>
<div id="formulaire">
    <h1>Administration</h1>
    <div>
      <h1>Supprimer un utilisateur</h1>
      <fieldset>
        <form action="admin.php" method="post">
          <table>
            <tr>
              <td><p>mail de l'utilisateur a supprimer :</p></td>
              <td><input type="text" name="delmail"/></td>
            </tr>
            <tr>
              <td><p><input type="submit" name="deluserbutton" value="supprimer l'utilisateur"></td>
            </tr>
            <tr>
              <td><?php if (isset($_POST['delmail'])) { deluser($delmail); }?></td>
            </tr>
          </table>
        </form>
      </fieldset>
    </div>
</body>
</html>
