<?php

function load_contact($date, $category_id, $category){
    $where_ids = " AND main_contact_id IN (SELECT main_contact.id FROM main_contact LEFT JOIN main_contact_department ON main_contact.id = main_contact_department.main_contact_id WHERE main_mandant_id = ".$GLOBALS['main_contact']['current_mandant_id'].")";
    $query = "SELECT * FROM infoboard LEFT JOIN main_contact ON infoboard.main_contact_id = main_contact.id LEFT JOIN main_contact_department ON main_contact.id = main_contact_department.main_contact_id WHERE date = '".$date."' AND category_id = ".$category_id." AND main_mandant_id = ".$GLOBALS['main_contact']['current_mandant_id']."  AND main_contact_department.active = 1";
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);
    if (@mysqli_num_rows($result) == 0) { 
        return;
    }
    while($contact = @mysqli_fetch_array($result)) {
        if(get_permission_state('show_on_infoboard', $GLOBALS['main_contact']['current_mandant_id'], $contact['main_contact_id']) == 1){
            $span = "<span class='infoboard_contact_tooltip'><span class='category'>".$category."</span>".$contact['name']."</span>";
            echo "<div class='infoboard_contact infoboard_category_".$category_id."'>".$span.$contact['shortcut']."</div>";
        }
    }
}

function get_meeting_link($collection_id, $setup_id){
    $query = "SELECT * FROM main_collection_link LEFT JOIN main_collection_setup_content ON main_collection_link.main_collection_setup_content_id = main_collection_setup_content.id WHERE main_collection_link.main_collection_id = ".$collection_id. " AND main_collection_setup_content_id = ".$setup_id;
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);
    $link = @mysqli_fetch_array($result);
    if($link['type_id'] == 6){
        $option_value = unserialize($link['data']);
        return $option_value;
    }else{
        return $link['data'];
    }
}

function load_meetings_from_collection($date){
    $if_query = "main_collection_id IS NOT NULL AND main_mandant_id = ".$GLOBALS['main_contact']['current_mandant_id'];
    $query = "SELECT main_collection.* FROM main_collection LEFT JOIN main_collection_mandant_link ON main_collection.id = main_collection_mandant_link.main_collection_id WHERE  main_collection_setup_id = 4";
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);
    $i = 0;
    while($meeting = @mysqli_fetch_array($result)) {
        $coll_time = get_meeting_link($meeting['id'], 82);
        $coll_time2 = get_meeting_link($meeting['id'], 83);
        $coll_location = get_meeting_link($meeting['id'], 5);
        $time = get_meeting_link($meeting['id'], 4);
        $coll_date = get_meeting_link($meeting['id'], 54);
        $coll_contact = get_meeting_link($meeting['id'], 63);
        if($date == $coll_date){
            // $span = "<span class='infoboard_contact_tooltip'>".$coll_time." - ".$coll_time2."</span>";
            echo "<div class='infoboard_category_meeting'>";
            echo "<div class='infoboard_meeting'>" . $span . $meeting['description'] ."  ". $time . "</div>";
                        if($coll_contact != ""){ 
                $where_ids = " AND main_contact.id IN (SELECT main_contact.id FROM main_contact LEFT JOIN main_contact_department ON main_contact.id = main_contact_department.main_contact_id WHERE main_mandant_id = ".$GLOBALS['main_contact']['current_mandant_id']." AND main_contact_department.active = 1)";
                $query2 = "SELECT * FROM main_contact WHERE main_contact.id IN (".$coll_contact.")". $where_ids;
                $result2 = @mysqli_query($GLOBALS['mysql_con'], $query2);
    
                // while($contact = @mysqli_fetch_array($result2)) {
                //     $span = "<span class='infoboard_contact_tooltip'>".$contact['name']."</span>";
                //     echo "<div class='infoboard_contact'>".$span.$contact['shortcut']."</div>";
                // }
                echo "</div>";
                $i++;
            }
        }
    }
    if($i == 0){
        return false;
    }else{
        return true;
    }
}


function load_board($start_date) {
    echo "<h3 class='termine_title'>Kommende Termine</h3>";
    echo "<div class='inhouse_and_outhouse'>";

    // Iteriere über die nächsten 8 Wochen
    for ($i = 0; $i < 8; $i++) {
        // Berechne das Datum für diese Woche
        $date = clone $start_date;
        $date->modify("+$i week");
        
        // Hier könnten wir die Daten für die Inhouse- und Outhouse-Meetings abrufen
        echo "<div class='outhouse'>";
        load_meetings_from_collection($date->format("Y-m-d"));
        echo "</div>";
    }

    echo "</div>";
}

$this_day = new DateTime();
?>

<div class='infoboard_boxes'>
    <div class="content_day this_day">
        <?= load_board($this_day); ?>
    </div>
</div>

