<?php
if(!isset($tab) || $tab=="") $tab=0;

if(!isset($isubsezione) || $isubsezione=="") $isubsezione=0;
if(empty($nxpage)) $nxpage=50;
if(empty($npage)) $npage=0;

if($azione=="delUser")
{
    $sql = "DELETE FROM t_utenti WHERE id_utenti=$id";
    $mydb->ExecSql($sql);
}

?>
<!-- @impostazioni -->
<script>

function setTab(t)
{
    if (document.frmmenu) {
        document.frmmenu.tab.value = t;
        document.frmmenu.tab2.value = '';
        document.frmmenu.azione.value = '';
        document.frmmenu.id.value = '';
        document.frmmenu.sezione.value = 'impostazioni,0';
        waitpage();
        document.frmmenu.submit();
        return;
    }

    if (document.frmins) {
        document.frmins.tab.value = t;
        document.frmins.sezione.value = 'impostazioni,0';
        document.frmins.submit();
    }
}

function delUser(id){
    if (!confirm('Confermi eliminazione operatore?')) return;
    document.frmins.azione.value='delUser';
    document.frmins.id.value=id;
    waitpage();
    document.frmins.submit();
}

function editUser(id){
    document.frmmenu.sezione.value='impostazioni,2';
    document.frmmenu.id.value=id;
    waitpage();
    document.frmmenu.submit();
}

function back(){
    document.frmmenu.sezione.value='impostazioni,0';
    document.frmmenu.id.value=0;
    waitpage();
    document.frmmenu.submit();
}
</script>

<div class="box_cnt">

    <section class="card" style="margin-bottom:18px;">
        <div class="card__head">
            <div>
                <h3 class="card__title">Impostazioni</h3>
                <p class="card__subtitle">Gestione operatori, IA e configurazioni modulo.</p>
            </div>
        </div>

        <div class="filters" style="margin-bottom:0;">
            <button type="button" class="filter-pill <? if($tab==0) echo "is-active"; ?>" onclick="setTab(0)">
                <i class="bi bi-people"></i>
                <span>Gestione Operatori</span>
            </button>
            
            <button type="button" class="filter-pill <? if($tab==4) echo "is-active"; ?>" onclick="setTab(4)">
                <i class="bi bi-robot"></i>
                <span>Gestione IA</span>
            </button>
        </div>
    </section>

<?
if($tab==0) {
    if($isubsezione==0)
    {
        $cond = "";
        if(isset($search_str) && $search_str!="") {
            $cond = " AND (nome LIKE '%$search_str%' OR cognome LIKE '%$search_str%' OR email LIKE '%$search_str%') ";
        }

        $sql = "SELECT COUNT(*) FROM t_utenti WHERE level>='$OPERLEVEL' AND level<=$ADMINLEVEL $cond";
        $mydb->DoSelect($sql);
        $rcount = $mydb->GetRow();

        $ntot = $rcount[0];
        $ntotpage = floor($ntot/$nxpage);
        if($ntotpage != $ntot/$nxpage) $ntotpage++;

        $nn = $npage*$nxpage;

        $sql = "SELECT * FROM t_utenti WHERE level>='$OPERLEVEL' AND level<=$ADMINLEVEL $cond ORDER BY cognome ASC";
        $a_utenti = $mydb->DoSelect($sql);
?>
    <form name="frmins" id="frmins" method="post" action="<? echo $action ?>">
        <section class="card" style="margin-bottom:18px;">
            <div class="card__head">
                <div>
                    <h3 class="card__title">Elenco operatori</h3>
                    <p class="card__subtitle">Totale record: <?= (int)$ntot ?></p>
                </div>
                <a class="btn btn-soft" href="?MSID=<?=$MSID?>&tab=0&sz=impostazioni,1">
                    <i class="bi bi-plus-circle"></i>
                    <span>Aggiungi Operatore</span>
                </a>
            </div>

            <div class="table-wrap">
                <table>
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>Cognome</th>
                            <th>Nome</th>
                            <th>Username</th>
                            <th>Email</th>
                            <th>Profilo</th>
                            <th>Azioni</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?
                    $i=0;
                    if(is_array($a_utenti)) {
                    foreach($a_utenti as $k => $r)
                    {
                        $i++;
                        $nome    = mydecodeTxt($r['nome']);
                        $cognome = mydecodeTxt($r['cognome']);
                        $email = mydecodeTxt($r['email']);
                        $username = $r['username'];
                        $idu = $r['id_utenti'];
                        $profilo = $A_USERSDES[$r['level']] ?? $r['level'];
                    ?>
                        <tr>
                            <td><?= $i+$nn ?></td>
                            <td><?= htmlspecialchars($cognome, ENT_QUOTES, 'UTF-8') ?></td>
                            <td><?= htmlspecialchars($nome, ENT_QUOTES, 'UTF-8') ?></td>
                            <td><?= htmlspecialchars($username, ENT_QUOTES, 'UTF-8') ?></td>
                            <td><?= htmlspecialchars($email, ENT_QUOTES, 'UTF-8') ?></td>
                            <td><?= htmlspecialchars($profilo, ENT_QUOTES, 'UTF-8') ?></td>
                            <td>
                                <div class="actions">
                                    <button class="action-icon" type="button" onclick="editUser(<?=$idu?>)" title="Modifica">
                                        <i class="bi bi-pencil"></i>
                                    </button>
                                    <button class="action-icon" type="button" onclick="delUser(<?=$idu?>)" title="Elimina">
                                        <i class="bi bi-trash"></i>
                                    </button>
                                </div>
                            </td>
                        </tr>
                    <?
                    }
                    }
                    ?>
                    </tbody>
                </table>
            </div>

            <input type="hidden" name="MSID" value="<?=$MSID?>">
            <input type="hidden" name="sezione" value="<?=$sezione?>">
            <input type="hidden" name="azione" value="">
            <input type="hidden" name="id" value="">
            <input type="hidden" name="tab" value="<?=$tab?>">
            <input type="hidden" name="tab2" value="<?=$tab2?>">
        </section>
    </form>
<?
    } elseif($isubsezione==1){
?>
    <div class="form-actions" style="margin-bottom:12px;">
        <button type="button" class="btn btn-soft" onclick="back()"><i class="bi bi-arrow-left"></i> Indietro</button>
    </div>
    <? include "nuovooperatore.php"; ?>
<?
    } elseif($isubsezione==2){
?>
    <div class="form-actions" style="margin-bottom:12px;">
        <button type="button" class="btn btn-soft" onclick="back()"><i class="bi bi-arrow-left"></i> Indietro</button>
    </div>
    <? include "operatore.php"; ?>
<?
    }
} else {
?>
    <? include "impostazionicustom.inc.php"; ?>
<? } ?>

</div>