<div class="workspace_group">
    <div class="headline">
        <h2>Mein Status</h2>
        <div class="svg_content">
        <svg id="confetti"xmlns="http://www.w3.org/2000/svg" width="24.75" height="22.5" viewBox="0 0 24.75 22.5">
  <g id="stats-bars" transform="translate(-5.625 -6.75)">
    <path id="Pfad_2" data-name="Pfad 2" d="M12.375,6.75h4.5v22.5h-4.5Z"/>
    <path id="Pfad_3" data-name="Pfad 3" d="M5.625,22.5h4.5v6.75h-4.5Z"/>
    <path id="Pfad_4" data-name="Pfad 4" d="M19.125,18h4.5V29.25h-4.5Z"/>
    <path id="Pfad_5" data-name="Pfad 5" d="M25.875,13.5h4.5V29.25h-4.5Z"/>
  </g>
</svg>

        </div>
    </div>
    <div class='workspace_table'>
        <table>
            <tr>
                <th>Kategorie</th>
                <th>Offen</th>
                <th>Erledigt</th>
                <th>Status</th>
            </tr>
            <?= count_knowledgecenter()?>
            <!-- <?= count_learning()?> -->
            <!-- <?= count_certificate()?> -->
        </table>
    </div>
</div>

<?php

function get_workspace_state($state) {
    if($state == 'green'){
        $return = '<svg xmlns="http://www.w3.org/2000/svg" width="16.892" height="12.669" viewBox="0 0 16.892 12.669">
        <path id="check" d="M8.112,13.223,6,15.335l6.335,6.335L22.892,11.112,20.781,9l-8.446,8.446Z" transform="translate(-6 -9)" fill="#3bc62c" fill-rule="evenodd"/>
      </svg>
      ';
    }else{
        $return = '<svg xmlns="http://www.w3.org/2000/svg" width="4.5" height="18.008" viewBox="0 0 4.5 18.008">
        <g id="exclamation" transform="translate(-15.755 -8.992)">
          <path id="Pfad_1" data-name="Pfad 1" d="M15.755,24.75A2.25,2.25,0,1,1,18.005,27a2.25,2.25,0,0,1-2.25-2.25Zm.22-13.511a2.036,2.036,0,1,1,4.05,0l-.787,7.891a1.242,1.242,0,0,1-2.475,0Z" fill="red"/>
        </g>
      </svg>
      ';
    }
    return $return;
}

function count_knowledgecenter(){
    $opened = 0;
    $closed = 0;
    $read = 0;
    $count = 0;
    $query = "SELECT DISTINCT main_collection.* FROM main_collection LEFT JOIN knowledgecenter_collection_link ON main_collection.id = knowledgecenter_collection_link.main_collection_id LEFT JOIN main_collection_mandant_link ON main_collection.id = main_collection_mandant_link.main_collection_id WHERE ((main_collection_mandant_link.main_collection_id IS NOT NULL AND main_mandant_id = ".$GLOBALS['main_contact']['current_mandant_id'].") OR main_collection_mandant_link.main_collection_id IS NULL) AND main_collection_setup_id = 3 AND (main_collection.is_archived = 0 OR main_collection.is_archived IS NULL);";
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);
    while($posts = @mysqli_fetch_array($result)) {
        $query2 = "SELECT * FROM main_collection_read WHERE main_collection_id = ".$posts['id']. " AND main_contact_id = ".$GLOBALS['main_contact']['id'];
        $result2 = @mysqli_query($GLOBALS['mysql_con'], $query2);
        if (@mysqli_num_rows($result2) == 1) { 
            $read++;
        }
        $count++;
    }
    $opened = $count - $read;
    $closed = $read;

    $state = get_workspace_state('green');
    if($opened >= 1){
        $state = get_workspace_state('red');
    }
    ?>
    <tr>
        <td><a href="/awo/de/knowledgecenter/">AWO Wiki</a></td>
        <td><?= $opened?></td>
        <td><?= $closed?></td>
        <td><?= $state?></td>
    </tr>
    <?php
}

function count_learning(){
    $opened = 0;
    $closed = 0;
    $query_active = "SELECT * FROM learning_unit_contact WHERE main_contact_id = ".$GLOBALS['main_contact']['id']. " AND active = 1";
    $result_active = @mysqli_query($GLOBALS['mysql_con'], $query_active);
    while($row = @mysqli_fetch_array($result_active)) {
        $query2 = "SELECT * FROM learning_unit_submit WHERE main_contact_id = ".$GLOBALS['main_contact']['id']. " AND learning_unit_id = ".$row['learning_unit_id']." AND correct = 1";
        $result2 = @mysqli_query($GLOBALS['mysql_con'], $query2);
        if (@mysqli_num_rows($result2) == 1) { 
            $closed++;
        }else{
            $opened++;
        }
    }

    $state = get_workspace_state('green');
    if($opened >= 1){
        $state = get_workspace_state('red');
    }
    ?>
    <tr>
        <td><a href="/awo/de/e-learning/">AWO Learnings</a></td>
        <td><?= $opened?></td>
        <td><?= $closed?></td>
        <td><?= $state?></td>
    </tr>
    <?php
}

function count_certificate(){
    $query = "SELECT * FROM main_collection WHERE";
    $state = get_workspace_state('green');
    if($opened >= 1){
        $state = get_workspace_state('red');
    }
    ?>
    <tr>
        <td>Zertifikate</td>
        <td><?= $opened?></td>
        <td><?= $closed?></td>
        <td><?= $state?></td>
    </tr>
    <?php
}