<?
$errmsg = "";

if($azione=="save")
{
    $_idutente = $id;

    $a_usrparams = find_post("usr_");
    $usrparams = serialize($a_usrparams);

    foreach ($a_usrparams as $k => $v){
        $a_usrparams[$k] = myencodeTxt($v);
    }
    extract($a_usrparams);

    $sql = "UPDATE t_utenti SET nome='$usr_nome', cognome='$usr_cognome', level='$usr_level', email='$usr_email' WHERE id_utenti=$_idutente";
    $mydb->ExecSql($sql);

} elseif($azione=="susp") {

    $sql = "UPDATE t_utenti SET stato='0' WHERE id_utenti=$id";
    $mydb->ExecSql($sql);

} elseif($azione=="attiva") {

    $sql = "UPDATE t_utenti SET stato='1' WHERE id_utenti=$id";
    $mydb->ExecSql($sql);
}

$_idutente = $id;

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

$usr_email    = $ru['email'];
$usr_username = $ru['username'];
$usr_nome     = $ru['nome'];
$usr_cognome  = $ru['cognome'];
$usr_level    = $ru['level'];

$stato = $ru['stato'];
$strstato = ($stato=='1') ? 'Attivo' : 'Non attivo';
$statoChip = ($stato=='1') ? 'chip chip--success' : 'chip chip--warning';

$usr_nome_view = htmlspecialchars(stripslashes((string)$usr_nome), ENT_QUOTES, 'UTF-8');
$usr_cognome_view = htmlspecialchars(stripslashes((string)$usr_cognome), ENT_QUOTES, 'UTF-8');
$usr_email_view = htmlspecialchars((string)$usr_email, ENT_QUOTES, 'UTF-8');
$usr_username_view = htmlspecialchars((string)$usr_username, ENT_QUOTES, 'UTF-8');
?>

<? 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>
<? } ?>

<form id="frmins" name="frmins" action="<? echo $action; ?>" method="post">

    <section class="card" style="margin-bottom:18px;">
        <div class="card__head">
            <div>
                <h3 class="card__title">Modifica operatore</h3>
                <p class="card__subtitle">Aggiorna dati anagrafici e stato account.</p>
            </div>
            <div class="actions">
                <span class="<?=$statoChip?>"><?=$strstato?></span>
                <? if($stato=='1'){ ?>
                <button class="btn btn-danger" type="button" onclick="sospendi()">
                    <i class="bi bi-pause-circle"></i>
                    <span>Sospendi</span>
                </button>
                <? } else { ?>
                <button class="btn btn-soft" type="button" onclick="attiva()">
                    <i class="bi bi-check2-circle"></i>
                    <span>Attiva</span>
                </button>
                <? } ?>
            </div>
        </div>

        <div class="form-grid">
            <div class="field">
                <label class="label">Username</label>
                <input class="control" type="text" name="usr_username" value="<?=$usr_username_view?>" title="Username" readonly>
            </div>

            <div class="field">
                <label class="label">Email</label>
                <input class="control" type="text" name="usr_email" value="<?=$usr_email_view?>" title="Email">
            </div>

            <div class="field">
                <label class="label">Nome*</label>
                <input class="control evi" type="text" name="usr_nome" value="<?=$usr_nome_view?>" title="Nome">
            </div>

            <div class="field">
                <label class="label">Cognome*</label>
                <input class="control evi" type="text" name="usr_cognome" value="<?=$usr_cognome_view?>" title="Cognome">
            </div>

            <div class="field field--full">
                <label class="label">Profilo*</label>
                <select class="select evi" name="usr_level">
                    <? foreach($A_USERSDES as $k => $v) {
                        if($k <= $MAXOPERLEVEL){ ?>
                    <option value="<?=$k?>" <? if($usr_level==$k) echo "selected" ?>><?=$v?></option>
                    <? }
                    } ?>
                </select>
            </div>
        </div>

        <div class="form-actions">
            <button class="btn btn-primary" type="button" onclick="checkdata()">
                <i class="bi bi-save"></i>
                <span>Salva modifiche</span>
            </button>
        </div>
    </section>

    <input type="hidden" name="MSID" value="<? echo $mysessionid ?>">
    <input type="hidden" name="id" value="<? echo $id ?>">
    <input type="hidden" name="sezione" value="impostazioni,2">
    <input type="hidden" name="tab" value="0">
    <input type="hidden" name="azione" value="">

</form>

<script type="text/javascript" charset="utf-8">
function checkdata(){
    var res = convalidaForm3(document.frmins,'<?=$language?>','evi');
    if(res)
    {
        document.frmins.azione.value='save';
        waitpage();
        document.frmins.submit();
    }
}

function sospendi()
{
    document.frmins.azione.value='susp';
    waitpage();
    document.frmins.submit();
}

function attiva()
{
    document.frmins.azione.value='attiva';
    waitpage();
    document.frmins.submit();
}


</script>