<?php
defined(page_security_key) or exit;

if (debug && debug_id == $_SESSION['id']){
    ini_set('display_errors', '1');
    ini_set('display_startup_errors', '1');
    error_reporting(E_ALL);  
}

include_once './assets/functions.php';
include_once './settings/settings_redirector.php';

//Check if allowed
if (isAllowed('dashboard',$_SESSION['profile'],$_SESSION['permission'],'R') === 0){
    header('location: index.php');
    exit;
}
//Return QueryTotal from API
$api_url = '/v1/equipments/totals=';
$query_total_equipment = ioServer($api_url,'');
//Decode Payload
if (!empty($query_total_equipment)){$query_total_equipment = decode_payload($query_total_equipment);}else{$query_total_equipment = null;}

//Return Service from API
$api_url = '/v1/equipments/servicedate='.$servicedate.'&totals=';
$query_total_servicable = ioServer($api_url,'');
//Decode Payload
if (!empty($query_total_servicable)){$query_total_servicable = decode_payload($query_total_servicable);}else{$query_total_servicable = null;}

//Return warranty from API
$api_url = '/v1/equipments/warrantydate='.$warrantydate.'&totals=';
$query_total_warranty = ioServer($api_url,'');
//Decode Payload
if (!empty($query_total_warranty)){$query_total_warranty = decode_payload($query_total_warranty);}else{$query_total_warranty = null;}

//Return warranty from API
$api_url = '/v1/equipments/firmware=update&totals=';
$query_total_software = ioServer($api_url,'');
//Decode Payload
if (!empty($query_total_software)){$query_total_software = decode_payload($query_total_software);}else{$query_total_software = null;}

//Return serviceforecast
$api_url = '/v1/application/totals=/serviceforecast';
$serviceforecast = ioServer($api_url,'');
//Decode Payload
if (!empty($serviceforecast)){$serviceforecast = decode_payload($serviceforecast);}else{$serviceforecast = null;}

//Return warrantyforecast
$api_url = '/v1/application/totals=/warrantyforecast';
$warrantyforecast = ioServer($api_url,'');
//Decode Payload
if (!empty($warrantyforecast)){$warrantyforecast = decode_payload($warrantyforecast);}else{$warrantyforecast = null;}

//GET GEOLOCATIONS
$api_url = '/v1/application/totals=/geolocation';
$geolocation = ioServer($api_url,'');
//Decode Payload
if (!empty($geolocation)){$geolocation = decode_payload($geolocation);}else{$geolocation = null;}

template_header('Dashboard', 'dashboard');
$view = '
<div class="content-title">
    <div class="title">
        <i class="fa-solid fa-house"></i>
        <div class="txt">
            <h2>'.$dashboard_h2.'</h2>
            <p>'.$dashboard_p.'</p>
        </div>
    </div>
</div>';

$view .= '
<div class="dashboard">
    <div class="content-block stat">
        <div class="data" onclick="location.href=\'index.php?page=equipments\'">
            <h3>'.$data_assets_h3.'</h3>
            <p>'.$query_total_equipment.'</p>
        </div>
        <i class="fas fa-box-open"></i>
        <div class="footer">
            <i class="fa-solid fa-rotate fa-xs"></i>'.$data_assets_text.'        </div>
    </div>

    <div class="content-block stat">
    <div class="data" onclick="location.href=\'index.php?page=equipments&warrantydate='.$warrantydate.'\'">
        <h3>'.$data_warranty_h3.'</h3>
        <p>'.$query_total_warranty.'</p>
    </div>
    <i class="fa-solid fa-calendar-check"></i>
    <div class="footer">
        <i class="fa-solid fa-rotate fa-xs"></i>'.$data_warranty_text.'
    </div>
    </div>

    <div class="content-block stat">
        <div class="data" onclick="location.href=\'index.php?page=equipments&servicedate='.$servicedate.'\'">
            <h3>'.$data_service_h3.'</h3>
            <p>'.$query_total_servicable.'</p>
        </div>
        <i class="fas fa-clipboard-check"></i>
        <div class="footer">
            <i class="fa-solid fa-rotate fa-xs"></i>'.$data_service_text.'
        </div>
    </div>

    <div class="content-block stat">
    <div class="data" onclick="location.href=\'index.php?page=equipments&firmware=update\'">
        <h3>'.$data_software_h3.'</h3>
        <p>'.$query_total_software.'</p>
    </div>
    <i class="fa-solid fa-calendar-check"></i>
    <div class="footer">
        <i class="fa-solid fa-rotate fa-xs"></i>'.$data_software_text.'
    </div>
</div>
</div>';

$warranty = serviceforecast(json_decode(json_encode($warrantyforecast), true), 'warranty') ?? '';
$forecast = serviceforecast(json_decode(json_encode($serviceforecast), true), 'service') ?? '';
$view .= '
<div class="content-block-wrapper">
<div class="content-block order-details">
    <div class="block-header">
    <i class="fa-solid fa-bars fa-sm"></i>'.$data_warranty_text_forecast.'
    </div>'.$warranty.'</div>
    <div class="content-block order-details">
    <div class="block-header">
    <i class="fa-solid fa-bars fa-sm"></i>'.$data_service_text_forecast.'
    </div>'.$forecast.'</div>          
</div>
';

//SHOW LOCATION BASED ON GEOLOCATION

if (!empty($geolocation)){
    $add_pos = '';
    foreach ($geolocation as $gloc){
        //$glocation = json_decode($gl,true) ?? '';
        
        if (!empty($gloc->geolocation) || $gloc->geolocation != '' || $gloc->geolocation != null ){
            $glocation = json_decode($gloc->geolocation) ?? '';
            if ($glocation[0] != null){
            $add_pos .= 'L.marker(['.$glocation[0].', '.$glocation[1].'],{icon: TSSemp}).addTo(map);'; 
            }
        }       
    }

    $view .= '<div class="content-block">
                <div id="map" style="height:300px;z-index: 0;">
                </div>
                <script>
                // initialize Leaflet
                var map = L.map(\'map\').setView({lon:  5.1938101, lat: 51.5778742}, 2);

                //Add TSS Avatar to MAP
                var TSSAvatar = L.icon({
                    iconUrl: \'./assets/images/tss-persoon.svg\',      
                    iconSize:     [35, 55], // size of the icon
                });

                //Add TSS Avatar to MAP
                var TSSemp = L.icon({
                    iconUrl: \'./assets/images/EP.png\',
                    iconSize:     [50, 50], // size of the icon
                });
                
                
                L.marker([51.5778742,5.1938101], {icon: TSSAvatar}).addTo(map);
            
                // add the OpenStreetMap tiles
                L.tileLayer(\'https://tile.openstreetmap.org/{z}/{x}/{y}.png\', {
                    maxZoom: 19,
                    attribution: \'&copy; <a href="https://openstreetmap.org/copyright">OpenStreetMap contributors</a>\'
                }).addTo(map);

                // show the scale bar on the lower left corner
                L.control.scale({imperial: true, metric: true}).addTo(map);

                // show a marker on the map
                '.$add_pos.'
                </script>
            </div>
    ';
}


$view .= '<div class="content-block">
<div class="block-header">
    <i class="fa-solid fa-bars fa-sm"></i>'.$dashboard_news.'
</div>
    <textarea id="" name="" placeholder="'.$dashboard_news.'" style="width: 100%;height: 150px;">
    '.((maintenance_mode_communication) ? maintenance_mode_notification : '').'
    '.news.'
    </textarea>  
</div>


';

//OUTPUT
echo $view;

template_footer();
