<?
$errmsg = "";

if($azione=="save")
{
    $a_usrparams = find_post("usr_");
    $usrparams = serialize($a_usrparams);

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

    $a_usrfparams = find_post("usrf_");
    $usrfparams = serialize($a_usrfparams);

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

    $sql = "SELECT COUNT(*) FROM $tbl_utenti WHERE username='$usr_username' AND cancellato='0'";
    $mydb->DoSelect($sql);
    $rtmp=$mydb->GetRow();
    if($rtmp[0]>0)
    {
        $errmsg = "Lo username scelto &egrave; gi&agrave; utilizzato";
    } else {

        $sql = "INSERT INTO $tbl_utenti (nome, cognome, username, password, email, level, data_ins, tipo, tipo_reg, stato ) VALUES (";
        $sql .= "'$usr_nome', '$usr_cognome', '$usr_username', '$usr_password', '$usr_email', '$usr_level', NOW(), '1', '0', '1') ";

        $mydb->ExecSql($sql);
        $idu = $mydb->LastInsertedId;

        if($idu>1 ){
            $errmsg = "L'Utente &egrave; stato inserito!";
        }
    }
}

$usr_username_val = htmlspecialchars((string)($usr_username ?? ''), ENT_QUOTES, 'UTF-8');
$usr_email_val = htmlspecialchars((string)($usr_email ?? ''), ENT_QUOTES, 'UTF-8');
$usr_nome_val = htmlspecialchars((string)($usr_nome ?? ''), ENT_QUOTES, 'UTF-8');
$usr_cognome_val = htmlspecialchars((string)($usr_cognome ?? ''), ENT_QUOTES, 'UTF-8');
?>

<? if($errmsg!=""){ ?>
<div class="card" style="margin-bottom:14px;border-left:4px solid #2157a6;">
    <div class="testo" 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">Nuovo operatore</h3>
                <p class="card__subtitle">Crea un nuovo account operatore e assegna il profilo.</p>
            </div>
        </div>

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

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

            <div class="field">
                <label class="label">Password*</label>
                <input class="control evi" type="password" name="usr_password" value="" title="Password">
            </div>

            <div class="field">
                <label class="label">Ripeti Password*</label>
                <input class="control evi" type="password" name="usr_password2" value="" title="Ripeti Password">
            </div>

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

            <div class="field">
                <label class="label">Cognome*</label>
                <input class="control evi" type="text" name="usr_cognome" value="<?=$usr_cognome_val?>" 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(isset($usr_level) && $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-person-plus"></i>
                <span>Salva operatore</span>
            </button>
        </div>
    </section>

    <input type="hidden" name="MSID" value="<? echo $mysessionid ?>">
    <input type="hidden" name="id" value="">
    <input type="hidden" name="sezione" value="impostazioni,1">
    <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(document.frmins.usr_password.value!=document.frmins.usr_password2.value)
    {
        myAlert('Attenzione: le password inserite non coincidono.');
        res = 0;
    }

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

</script>