<?php
$path = realpath(__DIR__).'/';
$json = file_get_contents($path.'qcm.json');
$data = json_decode($json);
$search = @$_POST['search'];
$wrong = false;
$show = true;
$checked = '';
if (isset($_POST['wrong'])) {
    $wrong = true;
    $checked = ' checked';
}
if ($search) {
    $show = false;
}
$html = '<!doctype html>';
$html .= '
<html lang="fr">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>QCM</title>
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">
    </head>
    <body>
    <div class="container mt-3 mb-4">
        <form class="row row-cols-lg-auto g-3 align-items-center" action="https://rampeur.fr/qcm.php" method="post">
            <div class="col-12">
                <label class="visually-hidden" for="search">Recherche</label>
                <div class="input-group">
                    <input type="text" class="form-control" id="search" name="search" placeholder="Recherche" value="'.$search.'">
                </div>
            </div>
            <div class="col-12">
                <div class="form-check">
                    <input class="form-check-input" type="checkbox" id="wrong" name="wrong"'.$checked.'>
                    <label class="form-check-label" for="wrong">Tout afficher</label>
                </div>
            </div>
            <div class="col-12">
                <button type="submit" class="btn btn-primary">Ok</button>
            </div>
        </form>
    </div>
    ';
if ($data->qcm) {
    foreach ($data->qcm as $qcm) {
        if ($qcm->name == 'bpa') {
            foreach ($qcm->categories as $category) {
                $html .= '
        <div class="container">
            <div class="row">
                <div class="col">';
                if ($wrong) {
                    $html .= '<h3 id="cat_'.$category->num.'">'.$category->num.' '.$category->name.'</h3>';
                }
                $found = false;
                foreach ($category->questions as $question) {
                    if (!$search) {
                        $show = true;
                    } else {
                        $show = false;
                        $pattern = '/'.$search.'/i';
                        if (stripos($question->libelle, $search) !== false) {
                            $show = true;
                        }
                    }
                    if ($show) {
                        $html .= '<h5>'.$question->num.' - '.$question->libelle.'</h5>';
                        $html .= '<p>';
                        foreach ($question->answers as $answer) {
                            if ($answer->correct) {
                                $html .= '<span class="text-success">'.$answer->libelle.'</span>';
                            } else if ($wrong) {
                                $html .= '<span class="text-danger">'.$answer->libelle.'</span>';
                            }
                            if ($wrong) {
                                $html .= '<br />';
                            }
                        }
                        $html .= '</p>';
                    }
                    if ($search && $show) {
                        //break;
                    }
                }
                $html .= '
                </div>
            </div>
        </div>';
            }
        }
    }
} else {
    $html .= '<p class="text-center">0 qcm</p>';
}
$html .= '
    </body>
</html>';
$html .= '';
echo $html;
?>