<?php
if(core::ifGet("add")) {
    $id_form = '<input type="hidden" name="id" value="add">';
    $submit = "Ajouter un profil";
    $titre = "Ajouter un profil";
} else {
    
    $databaseUser = new user();
    
    if(core::ifGet("id")){
        $user = $databaseUser->getUserById(core::getGet("id"));
        $submit = "Modifier ce profil";
        $titre = "Fiche de " . $user["prenom"] . " " . $user["nom"];
    } else {
        $user = $databaseUser->getUserById(session::getId());
        $submit = "Modifier votre profil";
        $titre = "Votre fiche de profil";
    }
    
    $id_form = '<input type="hidden" name="id" value="' . $user["id"] . '">';
}

?>

<header class="d-flex flex-column flex-md-row align-items-md-center p-3 bg-light row">
    <div class="col-11">
        <h2 class="bd-title" id="content">
            <span><?php echo $titre ?></span>
        </h2>
        <?php if(isset($user["id"]) AND session::getId() != $user["id"]){ ?>
            <div style="float:right; margin: -30px;">
                <a href="/submit.php?from=user-delete&id=<?php echo $user["id"] ?>" onclick="return confirm('Voulez-vous supprimer le compte de <?php echo $user["prenom"] . " " . $user["nom"]; ?> ?')" style="position: absolute; right: 0; margin: 0 40px 0 0;"><button type="submit" class="btn btn-outline-danger btn-sm"><span data-feather="trash-2"></span> Supprimer</button></a>
            </div>
        <?php } ?>
        <?php if(isset($user["last_connect"])){ echo '<h6>Dernière connexion : ' . $user["last_connect"] .'</h6>'; } ?>
    </div>
</header>

<br />

<form method="post" action="/submit.php" oninput='password2.setCustomValidity(password2.value != password.value ? "Les mots de passe ne sont pas identiques" : "")'>

    <input type="hidden" name="from" value="user">
    
    <?php
        echo $id_form;
    ?>
    
    <div class="form-group">
        <label>Type de compte</label>
        <select name="id_type" class="form-select">
            <option value="2"<?php if(isset($user["id_type"]) AND $user["id_type"] == 2){ echo " selected"; } ?>>Contrôleur QRCode (émargement)</option>
            <option value="3"<?php if(isset($user["id_type"]) AND $user["id_type"] == 3){ echo " selected"; } ?>>Assitance sociale</option>
            <option value="1"<?php if(isset($user["id_type"]) AND $user["id_type"] == 1){ echo " selected"; } ?>>Administrateur</option>
        </select>
    </div>
    <br />

    <div class="form-group">
        <label>Prénom</label>
        <input type="text" class="form-control" value="<?php
        if (isset($user["prenom"])) {
            echo $user["prenom"];
        }
        ?>" name="prenom" placeholder="" required>
    </div>
    <br />
    
    <div class="form-group">
        <label>nom</label>
        <input type="text" class="form-control" value="<?php
        if (isset($user["nom"])) {
            echo $user["nom"];
        }
        ?>" name="nom" placeholder="" required>
    </div>
    <br />
    
    <div class="form-group">
        <label>Email</label>
        <input type="email" class="form-control" value="<?php
        if (isset($user["email"])) {
            echo $user["email"];
        }
        ?>" name="email" placeholder="" required>
    </div>
    <br />
    
    <div class="form-group">
        <label>Mot de passe</label>
        <input type="password" class="form-control" minlength="8" maxlength="25" value="" name="password" placeholder="" <?php if(core::ifGet("add")){ echo "required"; } ?>>
    </div>
    <br />
    
    <div class="form-group">
        <label>Confirmation du mot de passe</label>
        <input type="password" class="form-control" value="" name="password2" placeholder="" <?php if(core::ifGet("add")){ echo "required"; } ?>>
    </div>
    <br />
    
    <div class="form-group">
        <label>Etat du compte</label>
        <select name="actif" class="form-select">
            <option value="0"<?php if(isset($user["actif"]) AND $user["actif"] == 0){ echo " selected"; } ?>>Compte désactivé</option>
            <option value="1"<?php if(isset($user["actif"]) AND $user["actif"] == 1){ echo " selected"; } ?>>Compte activé</option>
        </select>
    </div>
    <br />
    
    <input class="btn btn-primary btn-lg" style="width: 100%" type="submit" value="<?php echo $submit ?>">
    
</form>