<?php
$translation = \DynCom\mysyde\common\classes\Registry::get("translation");
$page_id = (int)$GLOBALS["navigation"]['forward_page_id'];
$header_id = (int)$GLOBALS['navigation']['sitepart_header_id'];
?>
<div class="faq">
    
    <div class="faq_nearest_search">
        <input type="text" width="300px" name="nearest_faq_search" onkeyup="liveSearch(this)" placeholder="Suchbegriff eingeben">
    </div>

    <div class = "search_result"></div>


  <?
    $query = "SELECT main_faq_setup_group.id, main_faq_setup_group.description FROM main_faq_setup_group RIGHT JOIN main_faq_header_group_link ON main_faq_setup_group.id = main_faq_header_group_link.faq_group_id WHERE active = 1 AND header_id = ".$header_id." ORDER BY sorting asc";
    $result = mysqli_query($GLOBALS['mysql_con'], $query);
    if (@mysqli_num_rows($result) > 0) {

        while ($faq_group = @mysqli_fetch_array($result)) {
            $group_link = "SELECT * FROM main_faq_group_link WHERE main_faq_setup_group_id = '".$faq_group['id']."' AND active = 1 ORDER BY sorting ASC";
            $result_group_link = @mysqli_query($GLOBALS['mysql_con'], $group_link);
            echo "\n<div class=\"faq_group_container\">\n";
            echo "<div class=\"faq_headline\"><h2>".$faq_group['description']."</h2></div>\n";

            while ($questions = @mysqli_fetch_array($result_group_link)) {

                $faq_line = "SELECT * FROM main_faq_line LEFT JOIN main_faq ON main_faq.id = main_faq_line.main_faq_id WHERE main_faq_id = '".$questions['main_faq_id']."'";
                $result_faq_line = @mysqli_query($GLOBALS['mysql_con'], $faq_line);

                while ($questions_result =  @mysqli_fetch_array($result_faq_line)) {
                    echo "<div class=\"faq_question_box\"><h3>{$questions_result[description]}</h3></div>\n<div class=\"faq_answer_box\">{$questions_result['text2']}</div>";
                }

            }
            echo "\n</div>\n";
        }

    }

  ?>
</div>

<script>

    function liveSearch(str) {

        var txt = str.value;
        
        $.ajax({ 
            url: '/module/faq/search.php',
            data: {
                action: 'liveSearch',
                search: txt
            },
            type: 'post',
            success: function(data) {
                      $('.search_result').html(data);
                  }
            });
    }


</script>