<?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 (debug::isFile("debug")) {
            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><?= $titre ?></span>
        </h2>        
        <?php if (isset($user["id"]) && session::getId() != $user["id"] && $protect != 2): ?>
            <div class="fix-container-button-nav">
                <a href="/submit.php?from=user-delete&id=<?= $user['id']; ?>" onclick="return confirm('Voulez-vous supprimer le compte de <?= $user["prenom"] . " " . $user["nom"]; ?> ?')">
                    <button type="submit" class="btn btn-outline-danger btn-sm"><?= icon::getFont(["icon" => "bi-trash-fill"]); ?> Supprimer</button>
                </a>
            </div>
        <?php elseif ($protect == 2): ?>
            <div class="fix-container-button-nav">
                <a href="/submit.php?from=user-restore&id=<?= $user['id']; ?>" onclick="return confirm('Voulez-vous restaurer le compte de <?= $user["prenom"] . " " . $user["nom"]; ?> ?')">
                    <button type="submit" class="btn btn-outline-danger btn-sm"><?= icon::getFont(["icon" => "bi bi-box2-fill"]); ?> Restaurer</button>
                </a>
            </div>
        <?php endif; ?>
    </div>
</header>
<?php   
    if(core::getGet("id")){
        echo core::filAriane(array(
            "current" => $titre, 
            "arbo" => array( 
                "Administration" => NULL,
                "Utilisateurs" => "/parametre-users.html",
                $titre => "#")
        ));
    }
        ?>
        <?php if (isset($user["last_connect"])): ?>
        <?php callout::print([
            "type" => "info",
            "size" => "tiny",
            "style" => "margin:-5px 0;",
            "p" => "Dernière connexion le " . core::convertDate($user["last_connect"]),
        ]); ?>
    <?php endif; ?>
<br />
<?php if($protect != 2): ?>
    
<form id="form-user" method="post" action="/submit.php" oninput='password2.setCustomValidity(password2.value != password.value ? "Les mots de passe ne sont pas identiques" : "")' onsubmit="return(false);">

    <input type="hidden" name="from" value="user">
    
    <?php
        echo $id_form;
        endif;
    ?>
    
    <div class="form-group">
        <label>Type de compte</label>
        <?php
            $id_type = [
                2 => "Contrôleur QRCode (émargement)",
                3 => "Assistance sociale",
                4 => "Modérateur du CMS",
                5 => "Membre du Bureau du CSE",
                6 => "Elu du CSE",
                7 => "Comptable",
                1 => "Administrateur"
            ];
        ?>
        <?php if($protect == 0): ?>
            <?php
                html::printSelect('name="id_type" class="form-select"', $id_type, @$user["id_type"]);
            ?>
        <?php endif; ?>
        <?php if($protect == 1 OR $protect == 2): ?>
            <?php 
                html::printSelect('class="form-control" disabled', $id_type, @$user["id_type"]);
                html::printInput('type="hidden" name="id_type"', $user["id_type"]);
            ?>
        <?php endif; ?>
    </div>
    <br />

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

    <div class="form-group">
        <label>Prénom</label>
        <?php
            $confPrenom = 'type="text" name="prenom" class="form-control" ';
            $confPrenom .= $protect == 2 ? 'readonly="readonly"' : 'required';
            html::printInput($confPrenom, @$user["prenom"]);
        ?>
    </div>
    <br />
    
    <div class="form-group">
        <label>nom</label>
        <?php
            $confNom = 'type="text" name="nom" class="form-control" ';
            $confNom .= $protect == 2 ? 'readonly="readonly"' : 'required';
            html::printInput($confNom, @$user["nom"]);
        ?>
    </div>
    <br />
    
    <div class="form-group">
        <label>Email</label>
        <?php
            $confEmail = 'type="text" name="email" class="form-control" ';
            $confEmail .= $protect == 2 ? 'readonly="readonly"' : 'required';
            html::printInput($confEmail, @$user["email"]);
        ?>
    </div>
    <br />
    
    <?php if($protect != 2): ?>

    <div class="form-group">
        <label>Mot de passe</label>
        <?php
            $confPassword = 'type="password" class="form-control" minlength="8" maxlength="25" name="password" ';
            $confPassword .= core::ifGet("add") ? 'required"' : NULL;
            html::printInput($confPassword);
        ?>
    </div>
    <br />
    
    <div class="form-group">
        <label>Confirmation du mot de passe</label>
        <?php
            $confPassword2 = 'type="password" class="form-control" minlength="8" maxlength="25" name="password2" ';
            $confPassword2 .= core::ifGet("add") ? 'required"' : NULL;
            html::printInput($confPassword2);
        ?>
    </div>
    <br />

    <div class="form-group">
        <label>Google Authenticator</label>
        <?php
            $googleAuthenticator = [
                    0 => "Désactivé",
                    1 => "Activé",
                ];
            html::printSelect('name="googleAuthenticator" class="form-select"', $googleAuthenticator, @$user["googleAuthenticator"]);
        ?>
    </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"><?= icon::getFont(["type" => "bi bi-android2", "size" => "40px"]) ?></a>&nbsp;&nbsp;&nbsp;<a href="https://apps.apple.com/fr/app/google-authenticator/id388497605" target="_blank"><?= icon::getFont(["type" => "bi bi-apple", "size" => "40px"]) ?></a>
            </div>
        </div>  
        <br />
    <?php endif; ?>

    <?php if($protect == 0): ?>
    <div class="form-group">
        <label>Etat du compte</label>
        <?php
            $actif = [
                    0 => "Compte désactivé",
                    1 => "Compte activé",
                ];
            html::printSelect('name="actif" class="form-select"', $actif, @$user["actif"]);
        ?>
    </div>
    <br />
    <?php endif; ?>

    <?php if($protect == 1): ?>
        <?= html::printInput('type="hidden" name="actif"', $user["actif"]); ?>
    <?php endif; ?>

    <?php if((isset($user["deleted"]) AND $user["deleted"] == 0) OR core::ifGet("add")) : ?>
    <input class="btn btn-primary btn-lg" style="width: 100%; margin-bottom:20px;" type="button" value="<?php echo $submit ?>"  onclick="validateAndSubmit()">
    <?php endif; ?>

</form>

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

<?php
    get::javascript("user");
?>