<?
$msg = "";
$errmsg = "";

if ($logged != 1) {
    echo "Sessione scaduta!<br/>";
} else {

    if ($azione == "savep") {

        $pass = md5($password);

        $sql = "UPDATE t_utenti SET password = '$pass', last_changed_password = NOW() WHERE id_utenti = $_idutente";
        $mydb->ExecSql($sql);

        $msg = "La password &egrave; stata modificata.";
    }

    if ($azione == "save") {

        $nome = myencodeTxt($nome);
        $cognome = myencodeTxt($cognome);

        $sql = "UPDATE t_utenti SET nome='$nome', cognome='$cognome' WHERE id_utenti = $_idutente";
        $mydb->ExecSql($sql);

        if ($email != $emailold) {
            $sql = "UPDATE t_utenti SET email='$email' WHERE id_utenti = $_idutente";
            $mydb->ExecSql($sql);
        }

        if ($username != $usernameold) {
            $sql = "SELECT COUNT(*) as n FROM t_utenti WHERE username='$username' AND cancellato='0'";
            $mydb->DoSelect($sql);
            $rtmp = $mydb->GetRow();

            if ($rtmp['n'] == 0) {
                $sql = "UPDATE t_utenti SET username='$username' WHERE id_utenti = $_idutente";
                $mydb->ExecSql($sql);
            } else {
                $errmsg = "Lo username scelto non &egrave; disponibile.";
            }
        }
    }

    $sql = "SELECT * FROM t_utenti WHERE id_utenti = $_idutente";
    $mydb->DoSelect($sql);
    $ru = $mydb->GetRow();

    $tipoutente = $ru['tipo'];
    $usr_level = $ru['level'];

    if ($onlywarning > 0) {
        $msg = "ATTENZIONE: E' necessario modificare la propria password!";
    }
    ?>
<!--@account-->

<script>
    function checkdata() {
        var res = convalidaForm3(document.frmins, '<?= $language ?>', 'input_evi');
        if (!res) return false;

        document.frmins.azione.value = 'save';
        waitpage();
        document.frmins.submit();
    }

    function changeProfilo(p) {
        document.frmins.azione.value = 'changeprofilo';
        document.frmins.profilo.value = p;
        waitpage();
        document.frmins.submit();
    }

    function checkdata2() {
        var res = false;

        if (document.frmins.password.value != '' && document.frmins.password.value != document.frmins.password2.value)
            alert('Le password inserite non coincidono');

        if (document.frmins.password.value != '' && document.frmins.password.value == document.frmins.password2.value)
            res = true;

        if (!res) return false;

        document.frmins.azione.value = 'savep';
        waitpage();
        document.frmins.submit();
    }
</script>

<div class="box_in">

    <? if ($msg != "") { ?>
    <div class="card" style="margin-bottom:14px;border-left:4px solid #067647;">
        <div class="testo" style="padding:12px 14px;"><?= $msg ?></div>
    </div>
    <? } ?>

    <? if ($errmsg != "") { ?>
    <div class="card" style="margin-bottom:14px;border-left:4px solid #b42318;">
        <div class="testo_err" style="padding:12px 14px;"><?= $errmsg ?></div>
    </div>
    <? } ?>

    <? if ($usr_level > $MAXUSERSLEVEL) { ?>
    <form name="frmins" id="frmins" method="post" action="<? echo $urlbase ?>index.php" enctype="multipart/form-data">

        <section class="account-grid" style="margin-bottom:18px;">
            <article class="card">
                <div class="card__head">
                    <div>
                        <h3 class="card__title">Il tuo account</h3>
                        <p class="card__subtitle">Aggiorna dati personali e contatti.</p>
                    </div>
                </div>

                <p class="testosm" style="margin-bottom:16px;">I dati contrassegnati con * sono obbligatori</p>

                <div class="form-grid">
                    <? if (!$_supervisione) { ?>
                    <div class="field">
                        <label class="label"><span class="required">*</span> Cognome</label>
                        <input class="control input_evi" type="text" value="<?= mydecodeTxt($ru['cognome']) ?>" name="cognome" title="Cognome">
                    </div>
                    <div class="field">
                        <label class="label"><span class="required">*</span> Nome</label>
                        <input class="control input_evi" type="text" value="<?= mydecodeTxt($ru['nome']) ?>" name="nome" title="Nome">
                    </div>
                    <? } ?>

                    <div class="field field--full">
                        <label class="label">Username</label>
                        <input class="control" type="text" value="<?= htmlspecialchars($ru['username'], ENT_QUOTES, 'UTF-8') ?>" readonly>
                        <input type="hidden" value="<?= htmlspecialchars($ru['username'], ENT_QUOTES, 'UTF-8') ?>" name="usernameold">
                        <input type="hidden" value="<?= htmlspecialchars($ru['username'], ENT_QUOTES, 'UTF-8') ?>" name="username">
                    </div>

                    <div class="field field--full">
                        <label class="label"><span class="required">*</span> Email</label>
                        <input class="control input_evi" type="text" value="<?= htmlspecialchars($ru['email'], ENT_QUOTES, 'UTF-8') ?>" name="email" title="Email">
                        <input type="hidden" value="<?= htmlspecialchars($ru['email'], ENT_QUOTES, 'UTF-8') ?>" name="emailold">
                    </div>
                </div>

                <div class="form-actions">
                    <button class="btn btn-primary" type="button" onclick="checkdata()">
                        <i class="bi bi-check2-circle"></i>
                        <span>Salva dati</span>
                    </button>
                </div>
            </article>

            <aside class="card profile-card">
                <div class="profile-card__top">
                    <div class="profile-card__avatar"><?= strtoupper(substr(trim($ru['nome']), 0, 1) . substr(trim($ru['cognome']), 0, 1)) ?></div>
                    <div>
                        <div class="profile-card__name"><?= htmlspecialchars(trim($ru['nome'] . ' ' . $ru['cognome']), ENT_QUOTES, 'UTF-8') ?></div>
                        <div class="profile-card__role"><?= $A_USERSDES[$usr_level] ?></div>
                    </div>
                </div>

                <div class="kv">
                    <span class="kv__label">Username</span>
                    <span class="kv__value"><?= htmlspecialchars($ru['username'], ENT_QUOTES, 'UTF-8') ?></span>
                </div>
                <div class="kv">
                    <span class="kv__label">Email</span>
                    <span class="kv__value"><?= htmlspecialchars($ru['email'], ENT_QUOTES, 'UTF-8') ?></span>
                </div>
            </aside>
        </section>

        <section class="card" style="margin-bottom:18px;">
            <div class="card__head">
                <div>
                    <h3 class="card__title">Modifica password</h3>
                    <p class="card__subtitle">Inserisci e conferma una nuova password.</p>
                </div>
            </div>

            <div class="form-grid">
                <div class="field">
                    <label class="label">Nuova password</label>
                    <input class="control" type="password" value="" name="password" title="Nuova password">
                </div>
                <div class="field">
                    <label class="label">Ripeti password</label>
                    <input class="control" type="password" value="" name="password2" title="Ripeti password">
                </div>
            </div>

            <div class="form-actions">
                <button class="btn btn-soft" type="button" onclick="checkdata2()">
                    <i class="bi bi-shield-lock"></i>
                    <span>Aggiorna password</span>
                </button>
            </div>
        </section>

        <input type="hidden" name="sz" value="<?= $sz ?>">
        <input type="hidden" name="MSID" value="<? echo $MSID ?>">
        <input type="hidden" name="language" value="<? echo $language; ?>">
        <input type="hidden" name="azione" value="save">
        <input type="hidden" name="frame" value="<? echo $frame ?>">
        <input type="hidden" name="_idutente" value="<? echo $_idutente ?>">

    </form>
    <? } else {
        include $incpath . 'user/account.php';
    } ?>

</div>

<? } ?>