<?php
function resolveUrl( $_url ) {
    //dummy function needed for ckfinder config
}

$siteparts     = \DynCom\mysyde\common\classes\Siteparts::get();
$pageIds       = array();
$sitepartCount = array();
$formname      = "form_edit_sitepart";
$translation   = \DynCom\mysyde\common\classes\Registry::get("translation");

$action = (isset($_GET['action']) ? $_GET['action'] : "");

if ($action != "" && $action != "admin_login") {
    require_once(CMS_PATH . "admin/edit_page.inc.php");
    exit();
}

$adminMenu = \DynCom\mysyde\common\classes\Adminmenu::get();

$subsites       = $adminMenu[$_GET["level_1"]]['subsites'];
$count_subsites = array();

function count_entrys($table){
    $query                   = "SELECT count(*) AS count FROM ".$table;
    $result                  = @mysqli_query($GLOBALS['mysql_con'], $query);
    $row                     = @mysqli_fetch_array($result);
    return $row['count'];
}

// documents_insert();

$GLOBALS["pdo_conn"] = new PDO("mysql:host=".$GLOBALS['myservername'].";dbname=".$GLOBALS['mydb'].';charset=utf8', $GLOBALS['mylogin'], $GLOBALS['mypass']);

function listUsers(){
    $query = "SELECT * FROM main_contact ORDER BY last_interaction DESC LIMIT 20";
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);
    while ($main_contact = @mysqli_fetch_array($result)) {
        $last_interaction = new DateTime($main_contact['last_interaction']);
        $current_datetime = new DateTime();
        $diff = $last_interaction->diff($current_datetime);
        $hours = $diff->h;
        $minutes = $diff->i;
        if($diff->d >= 1){
            $status = "Zuletzt am {$last_interaction->format('d.m.Y')} aktiv";
            $status_color = 'red';
        }else if($hours > 3){
            $status = "Zuletzt um {$last_interaction->format('H:i')} Uhr aktiv";
            $status_color = 'red';
        }else if($hours >= 1){
            $status = "Vor {$hours} Stunde(n) aktiv";
            $status_color = 'yellow';
        }else if($minutes >= 5){
            $status = "Vor {$minutes} Minuten aktiv";
            $status_color = 'yellow';
        }else{
            $status = "Jetzt aktiv";
            $status_color = 'green';
        }
        echo "<div class='user-wrapper'>";
        if($main_contact['image'] == ""){
			echo "<td><div class='img_container'><img class='img-fluid' src='/userdata/intranet/contact/platzhalter.png'></div></td>";
		}else{
			echo "<td><div class='img_container'><img class='img-fluid' src='/userdata/intranet/contact/".$main_contact['image']."'></div></td>";
		}
            echo "<div class='user-info'>";
                echo "<p class='user-name'>".$main_contact['name']."</p>";
                echo "<p class='user-status ".$status_color."_status'>".$status."</p>";
            echo "</div>";
        echo "</div>";
    }
}

function loadNews(){
    $file_server = 'https://breadcrumb-solutions.de/news.xml';

    $xml = simplexml_load_file($file_server);
    $temp = mb_convert_encoding( file_get_contents($file_server), 'UTF-8' );
    $array = simplexml_load_string($temp);

    $i = 1;
    foreach ($array -> news as $news) {
        if($i <= 2){?>
    <div class='news-content'>
        <img class="xml-img"src='<?php echo $news -> image ?>'>
        <p class="xml-category"><?php echo $news -> category ?></p>
        <p class="xml-headline"><?php echo $news -> headline ?></p>
        <p class="xml-teaser"><?php echo $news -> teaser ?></p>
        <a class="xml-link" target='_blank' href="<?php echo $news -> link ?>">Mehr erfahren</a>
    </div>
    <?php } 
        $i++;
    }
}
$GLOBALS['log_site'] = $site;
$GLOBALS['log_language'] = $language;
?>

<div id="mainContent" class="dashboard_intranet">

<div class="dashboard_wrapper">

    <!-- Hero Dashboard -->
    <div class="dashboard_hero">
        <div class="dashboard_intro">
            <div class="always_white_no"><?php echo current_website_language($site, $language); ?></div>
            <h1 class="new-h1">Willkommen zurück, <?php echo $admin_user["name"]; ?></h1>
        </div>
        <div class="dashboard_shortlinks">
            <div class="small_title always_white_no">Aktionen</div>
            <div class="shortlink_group">
                <button class="go_to shortlink"><a href="<?= get_menu_link('intranet/config_contact', $site, $language); ?>">Intranet Benutzer</a></button>
                <button class="go_to shortlink"><a href="<?= get_menu_link('intranet/config_mandant', $site, $language); ?>">Intranet Mandanten</a></button>
                <button class="go_to shortlink"><a href="<?= get_menu_link('intranet/config_department', $site, $language); ?>">Intranet Abteilungen</a></button>
                <button class="add shortlink"><a href="<?= get_menu_link('intranet/config_contact', $site, $language, '?action=open_card'); ?>">Neuer Benutzer</a></button>
                <!-- <button class="add shortlink"><a href="<?= get_menu_link('infoboard', $site, $language); ?>">Infoboard Eintrag</a></button> -->
                <button class="add shortlink"><a href="<?= get_menu_link('collections/3', $site, $language, '?action=open_card_collection'); ?>">Knowledgecenter Beitrag</a></button>
            </div>
        </div>
    </div>
    <!-- Übersicht Leiste mit Linie und Kacheln -->
    <div class="dashboard_overview">
        <h2 class="line always_white_no">Übersicht</h2>
        <div class="dashboard_content">
            <div class="box_wrapper">
                <div class="small_title always_white_no">Intranet Basis</div>
                <div class="box_group">
                    <button class="box"><a href="<?= get_menu_link('intranet/config_contact', $site, $language); ?>">
                        <div class="box_value"><?= count_entrys('main_contact')?></div>
                        <div class="box_label">Benutzer</div>
                    </a></button>
                    <button class="box"><a href="<?= get_menu_link('intranet/config_mandant', $site, $language); ?>">
                        <div class="box_value"><?= count_entrys('main_mandant')?></div>
                        <div class="box_label">Mandanten</div>
                    </a></button>
                    <button class="box"><a href="<?= get_menu_link('intranet/config_department', $site, $language); ?>">
                        <div class="box_value"><?= count_entrys('main_department')?></div>
                        <div class="box_label">Abteilungen</div>
                    </a></button>
                </div>
            </div>
        </div>
    </div>
</div>
