<?php
if (!isset($_mode)) {
    $_mode = "";
}


$btncontinua = "Continua";
if ($_szalias == "areariservata") {
    $btncontinua = "Salva";
}

if (!isset($usr_group)) {
    $usr_group = "";
}
?>

<section class="section">
    
    <?php if ($errmsg != "") { ?>
        <div class="card status-banner is-error" style="margin-bottom: 20px; max-width: 700px; margin-left: auto; margin-right: auto;">
            <span class="testo_err"><?= $errmsg ?></span>
        </div>
    <?php } ?>

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

        <div class="form-panel__header" style="max-width: 700px; margin: 0 auto 20px; text-align: center;">
            <span class="auth-eyebrow">Registrazione</span>
            <h2 class="titolo_big"><?= Get_TXTArea('registrazioneutente_1', $language); ?></h2>
            <p class="auth-subtitle">Compila i dati richiesti per creare il tuo account nella piattaforma.</p>
        </div>

        <div class="card form-panel" style="max-width: 700px; margin: 0 auto;">
            <h3 class="testo_evi" style="text-align: center; margin-bottom: 20px;">Inserisci i tuoi dati</h3>
            
            <div style="display:none" id="divError" class="testo_err" role="alert">
                Compilare tutti i campi obbligatori!
            </div>

            <div style="margin-bottom: 16px;">
                <label for="usr_email" class="l-label" style="display: block; text-align: left;">Email (Username)*</label>
                <input type="email" 
                       id="usr_email" 
                       name="usr_email" 
                       value="<?= htmlspecialchars($usr_email ?? '') ?>" 
                       style="width: 100%;"
                       required
                       <?php if ($onlylogged == 1) echo 'disabled'; ?> />
            </div>

            <?php if ($_regpassword) { ?>
                <div style="margin-bottom: 16px;">
                    <label for="password" class="l-label" style="display: block; text-align: left;">Password*</label>
                    <input type="password" 
                           id="password" 
                           name="usr_password" 
                           value="" 
                           style="width: 100%;" 
                           required />
                </div>

                <div style="margin-bottom: 16px;">
                    <label for="password2" class="l-label" style="display: block; text-align: left;">Conferma Password*</label>
                    <input type="password" 
                           id="password2" 
                           name="password2" 
                           value=""
                           style="width: 100%;" 
                           required />
                </div>
            <?php } ?>

            <div style="margin-bottom: 16px;">
                <label for="usr_nome" class="l-label" style="display: block; text-align: left;">Nome*</label>
                <input type="text" 
                       id="usr_nome" 
                       name="usr_nome" 
                       value="<?= htmlspecialchars(stripslashes($usr_nome ?? '')) ?>" 
                       style="width: 100%;" 
                       required />
            </div>

            <div style="margin-bottom: 16px;">
                <label for="usr_cognome" class="l-label" style="display: block; text-align: left;">Cognome*</label>
                <input type="text" 
                       id="usr_cognome" 
                       name="usr_cognome" 
                       value="<?= htmlspecialchars(stripslashes($usr_cognome ?? '')) ?>" 
                       style="width: 100%;" 
                       required />
            </div>

            <div style="margin-bottom: 16px;">
                <label for="usr_telefono" class="l-label" style="display: block; text-align: left;">Telefono*</label>
                <input type="tel" 
                       id="usr_telefono" 
                       name="usr_telefono" 
                       value="<?= htmlspecialchars($usr_telefono ?? '') ?>" 
                       style="width: 100%;" 
                       required />
            </div>

            <div style="margin-bottom: 16px;">
                    <label for="usrf_rs" class="l-label" style="display: block; text-align: left;">Azienda / Ragione Sociale*</label>
                    <input type="text" 
                           id="usrf_rs" 
                           name="usrf_rs" 
                           style="width: 100%;"
                           value="<?= htmlspecialchars(stripslashes($usrf_rs ?? '')) ?>"
                           title="Azienda / Ragione Sociale"
                           required />

        </div>

        <?php if (0) { ?>
            <div class="card" style="max-width: 700px; margin: 20px auto 0;">
                <h3 class="testo_evi" style="text-align: center; margin-bottom: 20px;">Dati di fatturazione (opzionali)</h3>

                

                <div style="margin-bottom: 16px;">
                    <label for="usrf_cfiscale" class="l-label" style="display: block; text-align: left;">Codice Fiscale</label>
                    <input type="text" 
                           id="usrf_cfiscale" 
                           name="usrf_cfiscale" 
                           style="width: 100%;"
                           value="<?= htmlspecialchars(stripslashes($usrf_cfiscale ?? '')) ?>" />
                </div>

                <div style="margin-bottom: 16px;">
                    <label for="usrf_piva" class="l-label" style="display: block; text-align: left;">Partita IVA</label>
                    <input type="text" 
                           id="usrf_piva" 
                           name="usrf_piva" 
                           style="width: 100%;"
                           value="<?= htmlspecialchars($usrf_piva ?? '') ?>" />
                </div>

                <div style="margin-bottom: 16px;">
                    <label for="usrf_indirizzo" class="l-label" style="display: block; text-align: left;">Indirizzo</label>
                    <input type="text" 
                           id="usrf_indirizzo" 
                           name="usrf_indirizzo" 
                           style="width: 100%;"
                           value="<?= htmlspecialchars(stripslashes($usrf_indirizzo ?? '')) ?>" />
                </div>

                <div style="margin-bottom: 16px;">
                    <label for="usrf_citta" class="l-label" style="display: block; text-align: left;">Città</label>
                    <input type="text" 
                           id="usrf_citta" 
                           name="usrf_citta" 
                           style="width: 100%;"
                           value="<?= htmlspecialchars(stripslashes($usrf_citta ?? '')) ?>" />
                </div>

                <div style="margin-bottom: 16px;">
                    <label for="usrf_cap" class="l-label" style="display: block; text-align: left;">CAP</label>
                    <input type="text" 
                           id="usrf_cap" 
                           name="usrf_cap" 
                           style="width: 100%;"
                           value="<?= htmlspecialchars($usrf_cap ?? '') ?>" />
                </div>

                <div style="margin-bottom: 16px;">
                    <label for="usrf_provincia" class="l-label" style="display: block; text-align: left;">Provincia</label>
                    <input type="text" 
                           id="usrf_provincia" 
                           name="usrf_provincia" 
                           style="width: 100%;"
                           value="<?= htmlspecialchars(stripslashes($usrf_provincia ?? '')) ?>" />
                </div>

                <div style="margin-bottom: 16px;">
                    <label for="usrf_telefono" class="l-label" style="display: block; text-align: left;">Telefono</label>
                    <input type="tel" 
                           id="usrf_telefono" 
                           name="usrf_telefono" 
                           style="width: 100%;"
                           value="<?= htmlspecialchars($usrf_telefono ?? '') ?>" />
                </div>

                <div style="margin-bottom: 16px;">
                    <label for="usrf_cdest" class="l-label" style="display: block; text-align: left;">
                        Codice Destinatario<br>
                        <span class="testosm">(Fatturazione Elettronica)</span>
                    </label>
                    <input type="text" 
                           id="usrf_cdest" 
                           name="usrf_cdest" 
                           style="width: 100%;"
                           value="<?= htmlspecialchars($usrf_cdest ?? '') ?>" />
                </div>

                <div style="margin-bottom: 16px;">
                    <label for="usrf_pec" class="l-label" style="display: block; text-align: left;">PEC</label>
                    <input type="email" 
                           id="usrf_pec" 
                           name="usrf_pec" 
                           style="width: 100%;"
                           value="<?= htmlspecialchars($usrf_pec ?? '') ?>" />
                </div>
            </div>
        <?php } ?>

        <div class="card form-panel" style="max-width: 700px; margin: 20px auto 0;">
            <div style="text-align: center; margin-bottom: 20px;">
                <h3 class="testob">Informativa Privacy</h3>
                <div style="max-width: 100%; height: 200px; overflow-y: scroll; border: 1px solid #ddd; background: white; padding: 10px; margin: 10px auto;">
                    <iframe src="Privacy.html" 
                            scrolling="no" 
                            style="width: 100%; height: 2000px; border: none; background: white;" 
                            title="Informativa Privacy"></iframe>
                </div>
            </div>

            <div style="margin-bottom: 20px;">
                <label style="display: flex; align-items: flex-start; gap: 8px;">
                    <input type="checkbox" 
                           id="chk_condizioni" 
                           name="chk_condizioni" 
                           value="1" 
                           style="margin-top: 3px; flex-shrink: 0;"
                           required />
                    <span class="testosm">
                        Dichiaro di avere preso visione dell'informativa per il trattamento dei dati personali
                        <a href="?sz=privacy" target="_blank">(visualizza in una nuova finestra)</a>
                    </span>
                </label>
            </div>

            <div style="text-align: center; margin-bottom: 20px;">
                <h3 class="testob">Consenso al trattamento dei dati personali</h3>
                <div style="max-width: 100%; height: 200px; overflow-y: scroll; border: 1px solid #ddd; background: white; padding: 10px; margin: 10px auto;">
                    <iframe src="Privacy2.html" 
                            scrolling="no" 
                            style="width: 100%; height: 190px; border: none; background: white;" 
                            title="Consenso Privacy"></iframe>
                </div>
            </div>

            <div style="margin-bottom: 20px;">
                <fieldset style="border: none; padding: 0;">
                    <legend class="testosm" style="margin-bottom: 10px; text-align: center;">Consenso marketing:</legend>
                    <div style="display: flex; gap: 30px; justify-content: center;">
                        <label style="display: flex; align-items: center; gap: 6px;">
                            <input type="radio" 
                                   name="chk_marketing" 
                                   value="0" 
                                   required />
                            <span class="testo">Acconsento</span>
                        </label>
                        <label style="display: flex; align-items: center; gap: 6px;">
                            <input type="radio" 
                                   name="chk_marketing" 
                                   value="1" 
                                   required />
                            <span class="testo">Non acconsento</span>
                        </label>
                    </div>
                </fieldset>
            </div>

            <div style="text-align: center; margin-top: 30px;">
                <?php if ($onlylogged != 1) { ?>
                    <button type="button" 
                            class="btn btn-accent" 
                            onclick="checkdata()" 
                            style="padding: 12px 40px; font-size: 16px; cursor: pointer;">
                        Continua
                    </button>
                <?php } else { ?>
                    <button type="button" 
                            id="login_entra" 
                            class="btn btn-accent" 
                            onclick="checkdata()" 
                            style="padding: 12px 40px; font-size: 16px; cursor: pointer;">
                        <?= $btncontinua ?>
                    </button>
                <?php } ?>
            </div>
        </div>

        <input type="image" 
               name="submit" 
               src="./main/contents/immagini/trans.gif" 
               style="width:0; height:0; border:0; padding:0; position:absolute; left:-9999px;" 
               alt="" 
               tabindex="-1" />
        
        <input type="hidden" name="MSID" value="<?php echo $mysessionid; ?>">
        <input type="hidden" name="step" value="2">
        <input type="hidden" name="sz" value="<?= $_szalias ?>">
        <input type="hidden" name="azionel" value="">
        <input type="hidden" name="_mode" value="<?= $_mode ?>">
    </form>

</section>

<script>
function checkdata() {
    // Nascondi messaggi di errore precedenti
    const divError = document.getElementById('divError');
    if (divError) {
        divError.style.display = 'none';
    }

    // Validazione HTML5 nativa (required fa già il lavoro)
    if (!document.frmins.checkValidity()) {
        document.frmins.reportValidity();
        return false;
    }

    const msgerr2 = '<?= addslashes($lang['errore_passw'] ?? 'Errore password') ?>';
    const msgerr4 = '<?= addslashes($lang['errore_privacy'] ?? 'Errore privacy') ?>';

    // Verifica consenso marketing
    if (!document.frmins.chk_marketing[0].checked && !document.frmins.chk_marketing[1].checked) {
        if (divError) {
            divError.style.display = 'block';
        }
        alert('Attenzione: È necessario scegliere se acconsentire o meno al trattamento dei dati personali per le finalità di marketing');
        return false;
    }

    // Verifica privacy
    if (!document.frmins.chk_condizioni.checked) {
        if (divError) {
            divError.style.display = 'block';
        }
        alert(msgerr4);
        return false;
    }

    <?php if (isset($_usetermini) && $_usetermini) { ?>
        const msgerr5 = '<?= addslashes($lang['errore_contratto'] ?? 'Errore contratto') ?>';
        if (document.frmins.chk_condizioni2 && !document.frmins.chk_condizioni2.checked) {
            alert(msgerr5);
            return false;
        }
    <?php } ?>

    <?php if ($_regpassword) { ?>
        // Verifica password
        if (document.frmins.usr_password.value !== document.frmins.password2.value) {
            const passwordField = document.getElementById('password');
            const password2Field = document.getElementById('password2');
            
            if (passwordField) {
                passwordField.focus();
                passwordField.classList.add('evi');
            }
            if (password2Field) {
                password2Field.focus();
                password2Field.classList.add('evi');
            }
            
            alert(msgerr2);
            return false;
        }
    <?php } ?>

    // Se tutto ok, invia il form
    document.frmins.submit();
}
</script>
