<?php
$msg = "";
$errmsg = "";
$recpasswfailed = 0;
$height = 0;
$class1 = "";

if (!isset($username)) {
    $username = "";
}

if ($loginfailed == '1') {
    $class1 = "input_evi";
}

if (isset($ricordapsw) && $ricordapsw == '1') {
    $recuperopassword = "1";

    $sql = "SELECT COUNT(*) as n FROM t_utenti WHERE email = '$emailreg' ";
    $mydb->DoSelect($sql);
    $r = $mydb->GetRow();

    $n = $r['n'];

    if ($n == 0) {
        $recpasswfailed = 1;
        $errmsg = $lang['errore_email_non_trovata'];
    } else {
        $res = inviaEmailRecuperoPsw($emailreg);
        $msg = $lang['messaggioEmail'];
    }
}

$registerUrl = $urlbase . "index.php?sz=registrazione&language=" . urlencode($language);
$homeUrl = $urlbase . "index.php?sz=home&language=" . urlencode($language);
?>

<script>
function espandi_rp() {
    enablescroll_ricorda_passw = 1;
    hidelayer('lnkrecpassw');
    expand_div_v('ricorda_passw', 300, 3, 10);
}

function sendReqPsw() {
    if (document.frmpassw.emailreg.value !== '') {
        document.frmpassw.submit();
    }
}
</script>

<section class="auth-shell">
    <div class="card auth-panel">
        <div class="auth-header">
            <span class="auth-eyebrow">Area riservata</span>
            <h1 class="auth-title"><?= htmlspecialchars($lang['area_riservata'] ?? 'Area riservata', ENT_QUOTES, 'UTF-8') ?></h1>
            <p class="auth-subtitle">
                Inserisci le tue credenziali per accedere alla dashboard. Il recupero password resta disponibile nella stessa pagina.
            </p>
        </div>

        <?php if ($loginfailed == 1) { ?>
        <div class="status-banner is-error">
            <p class="testob evi">Non è stata trovata nessuna corrispondenza tra username e password inseriti.</p>
        </div>
        <?php } ?>

        <?php if ($msg != "") { ?>
        <div class="status-banner is-success">
            <p class="testob"><?= htmlspecialchars($msg, ENT_QUOTES, 'UTF-8') ?></p>
        </div>
        <?php } ?>

        <form id="frmlogin" name="frmlogin" action="<?php echo $action; ?>" method="post" class="form-stack">
            <div class="form-grid form-grid--single">
                <div class="form-field">
                    <label for="username" class="l-label <?= $class1 ?>">Username</label>
                    <input id="username" type="text" name="username" value="<?= htmlspecialchars($username, ENT_QUOTES, 'UTF-8') ?>" />
                </div>

                <div class="form-field">
                    <label for="password" class="l-label <?= $class1 ?>">Password</label>
                    <input id="password" type="password" name="password" value="" />
                </div>
            </div>

            <div class="form-actions">
                <div id="login_entra" class="button1" onclick="Login()">
                    <div><?= htmlspecialchars($lang['invia'] ?? 'Accedi', ENT_QUOTES, 'UTF-8') ?></div>
                </div>
            </div>

            <input type="image" name="submit" src="./main/contents/immagini/trans.gif" style="width:0;height:0;border:0;padding:0;" alt="" />
            <input type="hidden" name="MSID" value="">
            <input type="hidden" name="params" value="">
            <input type="hidden" name="login" value="1">
            <?php if (isset($keyord)) { ?>
            <input type="hidden" name="keyord" value="<?php echo htmlspecialchars($keyord, ENT_QUOTES, 'UTF-8'); ?>">
            <?php } ?>
        </form>

        <div class="auth-links">
            <a href="javascript:void(0);" onclick="espandi_rp()" id="lnkrecpassw"><?= htmlspecialchars($lang['psw_dimenticata'] ?? 'Password dimenticata', ENT_QUOTES, 'UTF-8') ?></a>
            <a href="<?= $registerUrl ?>">Non hai un account? Registrati</a>
        </div>

        <div class="divider"></div>

        <div id="ricorda_passw" style="height:<?= $height ?>px;">
            <div class="card">
                <form id="frmpassw" name="frmpassw" action="<?php echo $action; ?>" method="post" class="form-stack">
                    <div class="form-field">
                        <label for="emailreg" class="l-label"><?= htmlspecialchars($lang['messaggioRecPsw'] ?? 'Recupero password', ENT_QUOTES, 'UTF-8') ?></label>
                        <p class="form-helper"><?= htmlspecialchars($lang['messaggioLogin1'] ?? 'Inserisci il tuo indirizzo email per recuperare le credenziali.', ENT_QUOTES, 'UTF-8') ?></p>
                        <input id="emailreg" type="text" name="emailreg" value="" placeholder="email@dominio.it" />
                    </div>

                    <div class="form-actions">
                        <div class="button2" onclick="sendReqPsw()"><?= htmlspecialchars(ucfirst($lang['invia'] ?? 'Invia'), ENT_QUOTES, 'UTF-8') ?></div>
                    </div>

                    <p id="msg2" class="testo_err"><?php if ($recpasswfailed == '1') echo htmlspecialchars($errmsg, ENT_QUOTES, 'UTF-8'); ?></p>

                    <input type="image" name="submit" src="./main/contents/immagini/trans.gif" style="width:0;height:0;border:0;padding:0;" alt="" />
                    <input type="hidden" name="MSID" value="<?php echo $mysessionid ?>">
                    <input type="hidden" name="ricordapsw" value="1">
                    <input type="hidden" name="sz" value="<?= htmlspecialchars($sz, ENT_QUOTES, 'UTF-8') ?>">
                </form>

                <span class="form-helper"><?= htmlspecialchars($lang['messaggioLogin2'] ?? '', ENT_QUOTES, 'UTF-8') ?></span>
            </div>
        </div>
    </div>

    <aside class="card auth-aside">
        <div class="auth-header">
            <span class="auth-eyebrow">Accesso pubblico</span>
            <h2 class="titolo_big">Login dedicato</h2>
            <p class="auth-subtitle">
                La home è ora la pagina di approdo, mentre questa vista è dedicata esclusivamente all'autenticazione.
            </p>
        </div>

        <ul class="note-list">
            <li>
                <i class="bi bi-check2-circle"></i>
                <span>Accesso separato dalla home page per una navigazione più chiara.</span>
            </li>
            <li>
                <i class="bi bi-shield-lock"></i>
                <span>Recupero credenziali disponibile nello stesso flusso di login.</span>
            </li>
            <li>
                <i class="bi bi-person-plus"></i>
                <span>Registrazione raggiungibile con CTA dedicata e form separato.</span>
            </li>
        </ul>

        <div class="form-actions">
            <a href="<?= $homeUrl ?>" class="btn btn-ghost">Torna alla home</a>
            <a href="<?= $registerUrl ?>" class="btn btn-accent">Registrati</a>
        </div>
    </aside>
</section>

<?php if (isset($recuperopassword)) { ?>
<script>
espandi_rp();
</script>
<?php } ?>
