<div class="future_appointments">
    <div class="headline">
        <h2>Zukünftige Termine</h2>
        <div class="svg_content">
            <svg id="calendar" xmlns="https://www.w3.org/2000/svg" width="20.871" height="20.871" viewBox="0 0 20.871 20.871">
            <path id="Pfad_119" data-name="Pfad 119" d="M1.3,6.163V19.208a1.3,1.3,0,0,0,1.3,1.3H18.262a1.3,1.3,0,0,0,1.3-1.3V6.163Zm1.3-3.913A2.609,2.609,0,0,0,0,4.859V19.208a2.609,2.609,0,0,0,2.609,2.609H18.262a2.609,2.609,0,0,0,2.609-2.609V4.859A2.609,2.609,0,0,0,18.262,2.25Z" transform="translate(0 -0.946)" fill="#5a9c9d" fill-rule="evenodd"/>
            <path id="Pfad_120" data-name="Pfad 120" d="M7.4,0a.652.652,0,0,1,.652.652V1.3a.652.652,0,1,1-1.3,0V.652A.652.652,0,0,1,7.4,0Zm11.74,0a.652.652,0,0,1,.652.652V1.3a.652.652,0,1,1-1.3,0V.652A.652.652,0,0,1,19.142,0Z" transform="translate(-2.837)" fill="#5a9c9d" fill-rule="evenodd"/>
            </svg>
        </div>
    </div>
<?php
    $appointment_type = $sitepart['appointment_type'];
    $appointment_items = $sitepart['sitepart_items'];

    $contact_id = $GLOBALS['main_contact']['id'];

    if(isset($_GET['contact'])){
        $contact_id = $_GET['contact'];
    }

    $current_date = new DateTime();

    $if_query = "main_collection_mandant_link.main_collection_id IS NOT NULL AND main_mandant_id = ".$GLOBALS['main_contact']['current_mandant_id'];
    $query = "SELECT DISTINCT main_collection.*, data FROM main_collection LEFT JOIN main_collection_link ON main_collection.id = main_collection_link.main_collection_id LEFT JOIN main_collection_mandant_link ON main_collection.id = main_collection_mandant_link.main_collection_id WHERE ((".$if_query.") OR main_collection_mandant_link.main_collection_id IS NULL) AND main_collection_setup_id = 4 AND main_collection_setup_content_id = 54 ORDER BY data ASC";

    $result = @mysqli_query($GLOBALS['mysql_con'], $query);
    $i = 0;
    while($row = @mysqli_fetch_array($result)){
        if ($i < $appointment_items){
            if($current_date->format("Y-m-d") < $row["data"] ){
                $persons = get_collection_link($row["id"],63);
                $person_array = explode(",", $persons);
                if($appointment_type == 1) {
                    foreach ($person_array as $person) {
                        if($person == $contact_id){
                            build_appointment($row["id"], $row["description"]);
                            $i++;
                        }
                    }
                }else {
                    build_appointment($row["id"], $row["description"]);
                    $i++;
                }
            }
        }
    }
    if($i == 0){
        echo "<div class='no_appointment'>Aktuell stehen keine Termine an.</div>";
    }
    
    function get_collection_link($collection_id, $content_id) {
        $query = "SELECT * FROM main_collection_link WHERE main_collection_id = ".$collection_id." AND main_collection_setup_content_id = ".$content_id;
        $result = @mysqli_query($GLOBALS['mysql_con'], $query);
        $row = @mysqli_fetch_array($result);
        return $row["data"];

    }

    function get_contact_from_id($main_contact_id){
        $query = "SELECT * FROM main_contact WHERE id = ".$main_contact_id;
        $result = @mysqli_query($GLOBALS['mysql_con'], $query);
        $row = @mysqli_fetch_array($result);
        return $row;    
    }

    function build_appointment($collection_id, $description) {
        $date = new DateTime(get_collection_link($collection_id, 54));
        $time = get_collection_link($collection_id, 4);
        $persons = get_collection_link($collection_id,63);
        $person_array = explode(",", $persons);
        ?>
            <div class="fa_wrapper">
                <div class="fa_list">
                    <a target='_blank' href='/module/calendar/ics.inc.php?calendar=<?= $collection_id?>'>
                        <div class="fa_container">
                            <div class="fa_content">
                                <div class="fa_textconten">
                                    <?php 
                                        echo $description;
                                    ?>
                                </div>
                                <div class="fa_svg_content">
                                    <svg id="info" xmlns="https://www.w3.org/2000/svg" width="16.074" height="16.074" viewBox="0 0 16.074 16.074">
                                        <path id="Pfad_116" data-name="Pfad 116" d="M16.5,15.774a.8.8,0,0,1,1.607,0V20.6a.8.8,0,1,1-1.607,0Z" transform="translate(-9.267 -8.556)"/>
                                        <path id="Pfad_117" data-name="Pfad 117" d="M17.3,9.076a.8.8,0,1,0,.8.8A.8.8,0,0,0,17.3,9.076Z" transform="translate(-9.267 -5.82)"/>
                                        <path id="Pfad_118" data-name="Pfad 118" d="M11.037,3a8.037,8.037,0,1,0,8.037,8.037A8.037,8.037,0,0,0,11.037,3Zm-6.43,8.037a6.43,6.43,0,1,0,6.43-6.43A6.43,6.43,0,0,0,4.607,11.037Z" transform="translate(-3 -3)" fill-rule="evenodd"/>
                                    </svg>
                                </div>
                            </div>
                            <div class="fa_tooltip">
                            <?php
                                echo "<div class='fa_facts'>";
                                echo "<div class='fa_time'>$time</div>";
                                echo "<div class='fa_date'>";
                                echo "</div>";
                                echo $date->format('d.m.Y');
                                echo "</div>";
                                echo "<div class='fa_shortcuts'>";
                                foreach ($person_array as $person) {
                                    $contact = get_contact_from_id($person);
                                    echo "<div class='fa_shortcut'>";
                                    echo $contact['shortcut'];
                                    echo "</div>";
                                    // echo $contact['name'];
                                }
                                echo "</div>";
                            ?>
                            </div>
                        </div>
                    </a>
                </div>
            </div>
        <?php
    }
?>
</div>