<?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';

$page = 'report_usage';
//Check if allowed
if (isAllowed($page,$_SESSION['profile'],$_SESSION['permission'],'R') === 0){
    header('location: index.php');
    exit;
}

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

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

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

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

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

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

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


// +++++++++++++++++++++++++++++++++++++++++++++++++++++++
// visual forecast for service and warranty+++++++++++++++
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++
function usageReportingView($messages){
    
    //GET TOTAL SERVICE COUNT
    $totalcount = 0;
    foreach ($messages as $message){
        $totalcount += $message['count'];
    }
      
    $view = '<div style="margin-bottom: 30px;">
        <ul style="width: 100%;max-width:100%" class="chart">
    ';
    foreach ($messages as $message){

        if ($message['year'] == date("Y") || ($message['year'] == date("Y") - 1 && $message['month'] == "12")){
            $height = ($message['count'] / $totalcount) * 100;
            $fc_year = $message['year'];

            $dateObj   = DateTime::createFromFormat('!m', $message['month']);
            $title = $dateObj->format('F').' '.$fc_year;
            $view .='<li style="text-align:center;">' . $message['count'] . '<span style="height:' . $height . '%" title="' . $title . '"></span></li>';
        } else {


        }
    }
    
    $view .='</ul></div>';
  
    return $view;
  }

template_header('Usage', 'report_usage','view');
$view = '
<div class="content-title">
    <div class="title">
        <i class="fa-solid fa-house"></i>
        <div class="txt">
            <h2>Usage overview</h2>
            <p></p>
        </div>
    </div>
</div>';


$usage_firmware = usageReportingView(json_decode(json_encode($report_usage_firmware), true)) ?? '';
$usage_servicereports = usageReportingView(json_decode(json_encode($report_usage_servicereports), true)) ?? '';
$usage_warranty = usageReportingView(json_decode(json_encode($report_usage_warranty), true)) ?? '';
$usage_other = usageReportingView(json_decode(json_encode($report_usage_other), true)) ?? '';
$usage_equipment = usageReportingView(json_decode(json_encode($report_usage_equipment), true)) ?? '';
$usage_changes = usageReportingView(json_decode(json_encode($report_usage_changes), true)) ?? '';
$usage_users = json_decode(json_encode($report_usage_users), true) ?? '';

$view .= '
<div class="dashboard">
    <div class="content-block stat">
        <div class="data" onclick="location.href=\'index.php?page=equipments\'">
            <h3>Users</h3>
            <p>'.$usage_users['count'].'</p>
        </div>
        <i class="fas fa-box-open"></i>
        <div class="footer">
            <i class="fa-solid fa-rotate fa-xs"></i>Users in contract scope</div>
    </div>
</div>
<div class="content-block-wrapper">
    <div class="content-block order-details">
        <div class="block-header">
            <i class="fa-solid fa-bars fa-sm"></i>Firmware
        </div>'.$usage_firmware.'
    </div>
    <div class="content-block order-details">
        <div class="block-header">
        <i class="fa-solid fa-bars fa-sm"></i>ServiceReports
        </div>'.$usage_servicereports.'
    </div>          
</div>

<div class="content-block-wrapper">
    <div class="content-block order-details">
        <div class="block-header">
            <i class="fa-solid fa-bars fa-sm"></i>Warranty
        </div>'.$usage_warranty.'
    </div>          
    <div class="content-block order-details">
        <div class="block-header">
        <i class="fa-solid fa-bars fa-sm"></i>Other
        </div>'.$usage_other.'
    </div>          
</div>

<div class="content-block-wrapper">
    <div class="content-block order-details">
        <div class="block-header">
            <i class="fa-solid fa-bars fa-sm"></i>Equipment
        </div>'.$usage_equipment.'
    </div>          
    <div class="content-block order-details">
        <div class="block-header">
            <i class="fa-solid fa-bars fa-sm"></i>Changes
        </div>'.$usage_changes.'
    </div>          
</div>
';

//OUTPUT
echo $view;

template_footer();
