<?php
try {
    //code...

    

$category_id = 0;
if(isset($_GET['category'])){
    $category_id = $_GET['category'];
}

if(isset($_GET['collection_id'])){
    $unit_id = $_GET['collection_id'];
    require_once("show_unit.inc.php");
    return;
}

function count_posts($category_id){
    $value = array();
    $query = "SELECT * FROM learning_unit_link WHERE learning_category_id = ".$category_id;
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);

    $count = 0;
    $read = 0;
    while($unit = @mysqli_fetch_array($result)) {
        $query_active = "SELECT * FROM learning_unit_contact WHERE learning_unit_id = ".$unit['learning_unit_id']. " AND main_contact_id = ".$GLOBALS['main_contact']['id']. " AND active = 1";
        $result_active = @mysqli_query($GLOBALS['mysql_con'], $query_active);
        if (@mysqli_num_rows($result_active) == 1) { 
            $query2 = "SELECT * FROM learning_unit_submit WHERE learning_unit_id = ".$unit['learning_unit_id']. " AND main_contact_id = ".$GLOBALS['main_contact']['id']. " AND correct = 1";
            $result2 = @mysqli_query($GLOBALS['mysql_con'], $query2);
            if (@mysqli_num_rows($result2) == 1) { 
                $read++;
            }
            $count++;
        }
    }
    if ($count !== 0) {
        $percent = ($read / $count) * 100;
        $percent = round($percent, 2);
        if($count == 0 && $read == 0){
            $percent = 0;
        }
    }
    
    $value['percent'] = $percent;
    $value['count'] = $count;
    return $value;
}

function get_categories($category_id){
    $if_query = "learning_category_id IS NOT NULL AND main_mandant_id = '".$GLOBALS['main_contact']['current_mandant_id']."'";
    $query = "SELECT learning_category.* FROM learning_category LEFT JOIN learning_category_mandant_link ON learning_category.id = learning_category_mandant_link.learning_category_id WHERE active = 1 AND parent_id = ".$category_id." AND ((".$if_query.") OR learning_category_id IS NULL) ORDER BY sorting ASC";
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);
    if (@mysqli_num_rows($result) == 0) { 
        return;
    }
    echo "<div class='el_category_list'>";
    echo "<div class='row'>";
    ?>

    <?php
    echo "<div class='el_category_line_list'>";
    while($category = @mysqli_fetch_array($result)) {
        $count = count_posts($category['id']);
    ?>
        <a class="el_category_link" href='?category=<?= $category['id']?>'>
            <div class='el_category'>
                <img class='el_img' src='/userdata/learning/<?= $category['image']?>'>
                <div class="el_content">
                    <div class="el_textcontent">
                        <h3 class='el_title'><?= $category['description']?></h3>
                        <p class='el_tags'>Beinhaltet <?= $count['count'] ?> Videos</p>
                    </div>
                    <div class="card">
                        <div class="percent">
                            <svg>
                            <circle cx="105" cy="105" r="100"></circle>
                            <circle cx="105" cy="105" r="100" style="--percent: <?= $count['percent']?>"></circle>
                            </svg>
                            <div class="number">
                            <h3><?= $count['percent']?><span>%</span></h3>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </a>
    <?php }
    echo "</div>";
    echo "</div>";
    echo "</div>";
}

function get_posts($category_id){
    $if_query = "learning_unit_mandant_link.learning_unit_id IS NOT NULL AND main_mandant_id = ".$GLOBALS['main_contact']['current_mandant_id'];
    $query = "SELECT learning_unit.* FROM learning_unit LEFT JOIN learning_unit_link ON learning_unit_link.learning_unit_id = learning_unit.id LEFT JOIN learning_unit_mandant_link ON learning_unit.id = learning_unit_mandant_link.learning_unit_id LEFT JOIN learning_unit_contact ON learning_unit_contact.learning_unit_id = learning_unit.id WHERE ((".$if_query.") OR learning_unit_mandant_link.learning_unit_id IS NULL) AND learning_unit_link.learning_category_id = ".$category_id." AND learning_unit_contact.active = 1 AND main_contact_id = ".$GLOBALS['main_contact']['id'];
    // var_dump($query);
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);
    if (@mysqli_num_rows($result) == 0) { 
        return;
    }
    $query_category = "SELECT * FROM learning_category WHERE id = ".$category_id;
    $result_category = @mysqli_query($GLOBALS['mysql_con'], $query_category);
    $category = @mysqli_fetch_array($result_category);
    echo "<div class='el_category_list'>";
    echo "<div class='row'>";
    ?>
    <h3 class='el_current_category'><?= $category['description']?></h3>
    <?php
     echo "<div class='el_category_line_list'>";
    while($post = @mysqli_fetch_array($result)) {
        $link = get_link_to_navigation(7) . get_collection_rewrite($post['title'], $post['id']);
        ?>
        <a class="el_post_link" href='<?= $link?>'>
            <div class='el_post'>
                <h4 class='el_post_title'><?= $post['title']?></h4>
                <svg id="chevron-right" xmlns="https://www.w3.org/2000/svg" width="6.442" height="10.736" viewBox="0 0 6.442 10.736">
                    <path id="Pfad_73" data-name="Pfad 73" d="M15.879,8.467l1.073-1.072,5.368,5.367-5.368,5.368-1.073-1.073,4.294-4.295L15.878,8.467Z" transform="translate(-15.878 -7.395)"/>
                </svg>
            </div>
        </a>
    <?php }
        echo "</div>";
        echo "</div>";
        echo "</div>";
}

echo "<div class='el_group_container'>";
    if(isset($_GET["category"]) || isset($_GET['search'])) {
        echo "<div class='el_back_button'>";
            echo "<button class='back_button' onclick='history.back()'>Zurück</button>";
        echo "</div>";
    }
    if(!isset($_GET['search'])){
        get_posts($category_id);
        get_categories($category_id);
    }
echo "</div>";
} catch (\Throwable $th) {
    echo $th;
}