<?php
if(core::ifGet("add") AND access::ifAccesss("add-user")) {
    $id_form = '<input type="hidden" name="id" value="add">';
    $submit = "Ajouter un profil";
    $titre = "Ajouter un profil";
    $protect = 0;
} else {
    
    if( core::ifGet("id") == FALSE OR  (core::ifGet("id") AND session::getId() == core::getGet("id"))){
        $user = user::getUserById(session::getId());
        $submit = "Modifier votre profil";
        $titre = "Votre fiche de profil";
        $protect = 1;
        if (core::isDebug()) {
            debug::log($user, "Données brutes user");
        }
    } else {
        if(access::ifAccesss("add-user")){
            $user = user::getUserById(core::getGet("id"));
            if(is_array($user)){
                $submit = "Modifier ce profil";
                $titre = "Fiche de " . $user["prenom"] . " " . $user["nom"];
                if($user["deleted"] == 1){
                    $titre .= " (Supprimée)";
                    $protect = 2;
                } else {
                    $protect = 0;
                }
            } else {
                get::page("unknow"); exit();
            }
        } else {
            get::page("unknow"); exit();
        }
    } 

    $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; if(isset($user["last_connect"])){ echo '<span style="margin-left: 15px; font-size: .875rem;">Dernière connexion : ' . $user["last_connect"] ."</span>"; } ?></span>
        </h2>        
        <?php if(isset($user["id"]) AND session::getId() != $user["id"] AND $protect != 2){ ?>
            <div class="fix-container-button-nav">
                <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"]; ?> ?')"><button type="submit" class="btn btn-outline-danger btn-sm"><?php icon::getFont(["icon" => "bi-trash-fill"]) ?></i> Supprimer</button></a>
            </div>
        <?php } elseif($protect == 2){ ?>
            <div class="fix-container-button-nav">
                <a href="/submit.php?from=user-restore&id=<?php echo $user["id"] ?>" onclick="return confirm('Voulez-vous restaurer le compte de <?php echo $user["prenom"] . " " . $user["nom"]; ?> ?')"><button type="submit" class="btn btn-outline-danger btn-sm"><?php echo icon::getFont(["icon" => "bi bi-box2-fill"]) ?> Restaurer</button></a>
            </div>
        <?php } ?>
        
    </div>
</header>

<br />
<?php if($protect != 2): ?>
    
<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;
        endif;
    ?>
    
    <div class="form-group">
        <label>Type de compte</label>
        <?php if($protect == 0): ?>
            <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"; } ?>>Assistance sociale</option>
                <option value="4"<?php if(isset($user["id_type"]) AND $user["id_type"] == 4){ echo " selected"; } ?>>Modérateur du CMS</option>
                <option value="5"<?php if(isset($user["id_type"]) AND $user["id_type"] == 5){ echo " selected"; } ?>>Membre du Bureau du CSE</option>
                <option value="6"<?php if(isset($user["id_type"]) AND $user["id_type"] == 6){ echo " selected"; } ?>>Elu du CSE</option>
                <option value="7"<?php if(isset($user["id_type"]) AND $user["id_type"] == 7){ echo " selected"; } ?>>Comptable</option>
                <option value="1"<?php if(isset($user["id_type"]) AND $user["id_type"] == 1){ echo " selected"; } ?>>Administrateur</option>
            </select>
        <?php endif; ?>
        <?php if($protect == 1 OR $protect == 2): ?>
            <input type="text" class="form-control" value="<?php 
                if($user["id_type"] == 1){ echo "Administrateur"; }  
                elseif($user["id_type"] == 2){ echo "Contrôleur QRCode (émargement)"; } 
                elseif($user["id_type"] == 3){ echo "Assistance sociale"; } 
                elseif($user["id_type"] == 4){ echo "Modérateur du CMS"; }
                elseif($user["id_type"] == 5){ echo "Membre du Bureau du CSE"; } 
                elseif($user["id_type"] == 6){ echo "Elu du CSE"; } 
                ?>" readonly="readonly" />
            <input type="hidden" name="id_type" value="<?php echo $user["id_type"] ?>">
        <?php endif; ?>
    </div>
    <br />

    <?php 
        if(access::ifAccesss("add-user") AND core::ifGet("id")) { ?>
            <div class="form-group">
                <label>Rôles aditionnels</label>
                <input type="text" value="<?php
                if (isset($user["tags"])) {
                    echo $user["tags"];
                }
                ?>" id="tags" name="tags" />
            </div>
            <br />
    <?php 
        } elseif(isset($user["tags"])) { ?>
        <div class="form-group">
        <label>Rôles aditionnels</label>
            <input type="text" class="form-control" name="tags" value="<?php echo $user["tags"] ?>" readonly="readonly" />
        </div>
        <br />
    <?php 
        } else {
            echo '<input type="hidden" name="tags" value="" />';
        }
    ?>

    <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="" <?php if($protect == 2){ echo 'readonly="readonly"'; } else { echo '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="" <?php if($protect == 2){ echo 'readonly="readonly"'; } else { echo '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="" <?php if($protect == 2){ echo 'readonly="readonly"'; } else { echo 'required'; } ?>>
    </div>
    <br />
    
    <?php if($protect != 2): ?>

    <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>Google Authenticator</label>
        <select name="googleAuthenticator" class="form-select">
            <option value="0"<?php if(isset($user["googleAuthenticator"]) AND $user["googleAuthenticator"] == 0){ echo " selected"; } ?>>Désactivée</option>
            <option value="1"<?php if(isset($user["googleAuthenticator"]) AND $user["googleAuthenticator"] == 1){ echo " selected"; } ?>>Activée</option>
        </select>
    </div>
    <br />

    <?php endif; ?>

    <?php if(isset($user["googleAuthenticator"]) AND $user["googleAuthenticator"] == 1 AND $protect == 1): ?>
        <div class="card text-center" style="width: 18rem;">
                <?php
                    $qrCodeUrl = googleAuthenticator::getGoogleUrl("CMS CSE Invent: " . ENVIRONNEMENT,  user::getMyGoogleAuthenticator(session::getId()));
                    myQrcode::printQRCode($qrCodeUrl);
                ?>
            <div class="card-footer text-body-secondary">
                QRCode à scanner dans votre application Google Authenticator<br />
                <a href="https://play.google.com/store/apps/details?id=com.google.android.apps.authenticator2" target="_blank">android</a>&nbsp;&nbsp;&nbsp;<a href="https://apps.apple.com/fr/app/google-authenticator/id388497605" target="_blank">iOS</a>
            </div>
        </div>  
        <br />
    <?php endif; ?>

    <?php if($protect == 0): ?>
    <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 />
    <?php endif; ?>

    <?php if($protect == 1): ?>
        <input type="hidden" name="actif" value="<?php echo $user["actif"] ?>">
    <?php endif; ?>

    <input class="btn btn-primary btn-lg" style="width: 100%; margin-bottom:20px;" type="submit" value="<?php echo $submit ?>">
    
</form>

<script>
    $(document).ready(function () {
        $('#tags').inputTags({
            autocomplete: {
                values: <?php echo tags::getJquery(1) ?>,
                only: true
            },
            max: 3
        });
    });
</script>