<?php
// /registration/index.php
// ─── Entry Point — Wizard config-driven ───

$promozione     = $_GET['promozione'] ?? '';
$codice_amico   = $_GET['codice_amico'] ?? '';
$affiliate_code = $_GET['affiliate_code'] ?? '';
$label          = $_GET['label'] ?? '';
$btag           = $_GET['btag'] ?? '';

// Carica configurazione step
$wizardSteps = require __DIR__ . '/wizard_steps.php';
?>
<!DOCTYPE html>
<html lang="it" data-bs-theme="dark">
<head>
    <meta name="robots" content="noindex">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <link rel="icon" type="image/png" sizes="16x16" href="https://www.promovt.info/frames2/assets/img/favicon16.png">
    <title>Registrazione Vincitù</title>
    <link href="./css/bootstrap.min.css" rel="stylesheet">
    <link href="./css/style.css" rel="stylesheet">
    <script src="./js/cf_autocomplete.js" defer></script>
    <script src="./js/form_handler.js" defer></script>
    <script type="module" src="./js/wizard/steps.js"></script>
    <script type="module" src="./js/wizard/promo_param_loader.js"></script>
    <script type="module" src="./js/wizard/password_strength.js"></script>
    <script type="module" src="./js/face_validation/doc_upload_wizard.js"></script>
</head>
<body>

<nav class="navbar bg-body-tertiary px-3">
    <div class="container-fluid d-flex justify-content-between align-items-center">
        <a class="navbar-brand" href="#">
            <img src="./assets/img/logo.svg" alt="Logo" width="200" class="d-inline-block align-text-top">
        </a>

        <div class="d-flex align-items-center gap-3">
            <!-- Switch Tema -->
            <div class="form-check form-switch m-0">
                <input class="form-check-input" type="checkbox" role="switch" id="themeSwitch">
                <label class="form-check-label small" for="themeSwitch">
                    <i class="bi bi-sun"></i>/<i class="bi bi-moon"></i>
                </label>
            </div>

            <!-- Pulsante Chiudi -->
            <a class="btn btn-outline-success" type="button">Chiudi</a>
        </div>
    </div>
</nav>

<div class="container my-5">
    <div class="card shadow-lg p-4 mb-3">

        <!-- Progress pills (generato dinamicamente) -->
        <ul class="nav nav-pills d-flex justify-content-between mb-4" id="wizardNav">
            <?php foreach ($wizardSteps as $stepId => $stepDef): ?>
                <li class="nav-item">
                    <button class="nav-link border border-2 rounded-5 fw-bold<?= $stepId === 1 ? ' active' : '' ?>"
                            data-wizard-target="<?= $stepId ?>"
                            <?= $stepId > 1 ? 'disabled' : '' ?>>
                        <?= $stepId ?>
                    </button>
                </li>
            <?php endforeach; ?>
        </ul>

        <form id="registrationForm" novalidate>
            <?php foreach ($wizardSteps as $stepId => $stepDef): ?>
                <section data-step="<?= $stepId ?>" class="wizard-step<?= $stepId > 1 ? ' d-none' : '' ?>">

                    <?php // Includi tutti i moduli di questo step
                    foreach ($stepDef['modules'] as $module):
                        $modulePath = __DIR__ . "/modules/{$module}.php";
                        if (file_exists($modulePath)):
                            include $modulePath;
                        else:
                            echo "<!-- Modulo non trovato: {$module} -->";
                        endif;
                    endforeach; ?>

                    <?php // Genera navigazione
                    $nav = $stepDef['navigation']; ?>
                    <div class="d-flex gap-2 mt-3 justify-content-<?= !empty($nav['prev']) ? 'between' : 'end' ?>">
                        <?php if (!empty($nav['prev'])): ?>
                            <button type="button" class="btn btn-outline-secondary" data-wizard-prev>Indietro</button>
                        <?php endif; ?>

                        <?php if (!empty($nav['next'])): ?>
                            <button type="button" class="btn btn-vincitu" data-wizard-next
                                <?= !empty($nav['next_hidden']) ? 'style="display:none;"' : '' ?>
                                <?= !empty($nav['next_disabled']) ? 'disabled' : '' ?>>
                                <?= $nav['next_label'] ?? 'Avanti' ?>
                            </button>
                        <?php endif; ?>

                        <?php if (!empty($nav['submit'])): ?>
                            <button type="submit" class="btn btn-success">Invia Registrazione</button>
                        <?php endif; ?>
                    </div>
                </section>
            <?php endforeach; ?>
        </form>

        <div id="result" class="alert mt-4 d-none"></div>
    </div>
</div>

<!-- Bootstrap e jQuery -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
    (function(){
        const root = document.documentElement;
        const switchEl = document.getElementById('themeSwitch');

        const saved = localStorage.getItem('theme');
        if (saved) {
            root.setAttribute('data-bs-theme', saved);
            switchEl.checked = (saved === 'dark');
        }

        switchEl.addEventListener('change', () => {
            const mode = switchEl.checked ? 'dark' : 'light';
            root.setAttribute('data-bs-theme', mode);
            localStorage.setItem('theme', mode);
        });
    })();
</script>

</body>
</html>
