<?php
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++
// PDF creator     +++++++++++++++++++++++++++++++++++++++
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++
// include autoloader
require_once dirname(__FILE__).'/dompdf/autoload.inc.php'; 
// reference the Dompdf namespace
use Dompdf\Dompdf;
// instantiate and use the dompdf class
use Dompdf\Options;

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Generated PDF          ++++++++++++++++++++++++++++++++
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++
function generatedPDF($input,$historyID,$email){
  $options = new Options();
  $options->set('isRemoteEnabled', true);
  $dompdf = new Dompdf($options);
  $dompdf->loadHtml($input);
  
  // (Optional) Setup the paper size and orientation
  $dompdf->setPaper('A4', 'portrait');
  // Render the HTML as PDF
  $dompdf->render();
  $attachment = $dompdf->output();
  
  $to = $email;
  $subject = 'Servicereport -'.$historyID;
  $message = $input;
  $attachment_name = 'Servicereport -'.$historyID.'.pdf';
  //Send email
  if (!empty($email)){
  send_mail($to, $subject, $message, $attachment, $attachment_name);
  }
}
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++
// include PHP Mailer
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require dirname(__FILE__).'/mail/PHPMailer.php';
require dirname(__FILE__).'/mail/SMTP.php';
require dirname(__FILE__).'/mail/Exception.php';
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Send Mail via PHPMailer++++++++++++++++++++++++++++++++
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++
function send_mail($to, $subject, $message, $attachment, $attachment_name){

  include_once dirname(__FILE__,2).'/settings/config.php';

  $mail = new PHPMailer();
  $mail->CharSet = 'UTF-8';
  //$mail->isSMTP(); // Use SMTP protocol
  $mail->Host = email_host_name; // Specify  SMTP server
  $mail->SMTPAuth = true; // Auth. SMTP
  $mail->Username = email; // Mail who send by PHPMailer
  $mail->Password = email_outgoing_pw; // your pass mail box
  $mail->SMTPSecure = email_outgoing_security; // Accept SSL
  $mail->Port = email_outgoing_port; // port of your out server
  $mail->setFrom(email, mail_from); // Mail to send at
  $mail->addAddress($to); // Add sender
  $mail->addReplyTo(email_reply_to); // Adress to reply
  $mail->isHTML(true); // use HTML message
  $mail->Subject = $subject;
  $mail->Body = $message;
  if (!empty($attachment) || $attachment != ''){
    $mail->AddStringAttachment($attachment, $attachment_name, 'base64', 'application/pdf');
  }

  // SEND
  if( !$mail->send() ){
      // render error if it is
      $tab = array('error' => 'Mailer Error: '.$mail->ErrorInfo );
      echo json_encode($tab);
      exit;
  }
  else{
      // return true if message is send
      return true;
  }

}

//------------------------------------------
// Global functions
//------------------------------------------
function dbConnect($dbcon) {

    include dirname(__FILE__,2).'/settings/settings.php';

    try {
        // Connect to the MySQL database using the PDO interface
    	$pdo = new PDO('mysql:host=' . $db . ';dbname=' . $dbcon . ';charset=utf8', $dbuser, $dbpw);
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        return $pdo;
    } catch (PDOException $exception) {
    	// Could not connect to the MySQL database! If you encounter this error, ensure your db settings are correct in the config file!
    	exit('Failed to connect to database!');
    }
}
//------------------------------------------
// Determine URL function
//------------------------------------------
function url($url) {
    if (rewrite_url) {
        $url = preg_replace('/\&(.*?)\=/', '/', str_replace(['index.php?page=', 'index.php'], '', $url));
    }
    return base_url . $url;
}
//------------------------------------------
// Routing function
//------------------------------------------
function routes($urls) {
    foreach ($urls as $url => $file_path) {
        $url = '/' . ltrim($url, '/');
        $prefix = dirname($_SERVER['PHP_SELF']);
        $uri = $_SERVER['REQUEST_URI'];
        if (substr($uri, 0, strlen($prefix)) == $prefix) {
            $uri = substr($uri, strlen($prefix));
        }
        $uri = '/' . ltrim($uri, '/');
        $path = explode('/', parse_url($uri)['path']);
        $routes = explode('/', $url);
        $values = [];
        foreach ($path as $pk => $pv) {
            if (isset($routes[$pk]) && preg_match('/{(.*?)}/', $routes[$pk])) {
                $var = str_replace(['{','}'], '', $routes[$pk]);
                $routes[$pk] = preg_replace('/{(.*?)}/', $pv, $routes[$pk]);
                $values[$var] = $pv;
            }
        }
        if ($routes === $path && rewrite_url) {
            foreach ($values as $k => $v) {
                $_GET[$k] = $v;
            }
            return file_exists($file_path) ? $file_path : 'dashboard.php';
        }
    }
    if (rewrite_url) {
        header('Location: ' . url('index.php'));
        exit;
    }
    return null;
}

//------------------------------------------
// Menu Builder
//------------------------------------------
function menu($selected,$selected_child){

  include dirname(__FILE__,2).'/settings/settings.php';

  if(isset($_SESSION['country_code'])){
    $api_file_language = dirname(__FILE__,2).'/settings/translations/translations_'.strtoupper($_SESSION['country_code']).'.php';
    if (file_exists($api_file_language)){   
        include $api_file_language; //Include the code
    }
    else {
        include dirname(__FILE__,2).'/settings/translations/translations_US.php';
    }
    }
    else {
        include dirname(__FILE__,2).'/settings/translations/translations_US.php';
    }  
  
  $profile = $_SESSION['profile'];

  //Define Menu
  $menu = '';

  foreach ($main_menu as $menu_item){
      if (str_contains($profile,$item = $menu_item)){
          //Main URL
          $menu .= '<a href="index.php?page='.$urls[$item]['url'].'"' . ($selected == $urls[$item]['selected'] ? ' class="selected"' : '') . '><i class="'.$urls[$item]['icon'].'"></i>'.ucfirst(${$urls[$item]['name']}).'</a>';

          //DEFINE SUBMENU
          $sub_menu = $item.'_sub' ?? '';
          $sub_menu = (isset($$sub_menu) && $$sub_menu !='')? $$sub_menu : 0;
          //CHECK IF SUBMENU EXIST
          if ($sub_menu !=0){
              $menu .= '<div class="sub" ' . ($selected == $urls[$item]['selected'] ? ' class="selected"' : '') . '>';
              foreach($sub_menu as $key){
              //CHECK IF USER IS ALLOWED
                  if (str_contains($profile, $key)){  
                    $menu .= '<a href="index.php?page='.$urls[$key]['url'].'"' . ($selected == $urls[$key]['selected'] && $selected_child == 'view' ? ' class="selected"' : '') . '><span>&#9724;</span>'.ucfirst(${$urls[$key]['name']}).'</a>';
                  }
              }
              $menu .= '</div>';
          }
      }
  }
  return $menu;
  }
//------------------------------------------
// Template Header
//------------------------------------------
function template_header($title, $selected = 'assets', $selected_child = 'view') {
    
  $base_url = 'https://'.$_SERVER['SERVER_NAME'].'/';
  $site_name = site_name;
  $site_name_footer = site_name_footer;
  $icon_image = icon_image;
  $admin_links = menu($selected,$selected_child);

  if (isset($_SESSION['id'])){$id = $_SESSION['id'];} else{$id='';}

  if(isset($_SESSION['country_code'])){
    $api_file_language = dirname(__FILE__,2).'/settings/translations/translations_'.strtoupper($_SESSION['country_code']).'.php';
    if (file_exists($api_file_language)){   
        include $api_file_language; //Include the code
    }
    else {
        include dirname(__FILE__,2).'/settings/translations/translations_US.php';
    }
    }
    else {
        include dirname(__FILE__,2).'/settings/translations/translations_US.php';
    }  

    $profile = $general_profile;
    $logout = $general_logout;

echo <<<EOT
<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,minimum-scale=1">
		<title>$title</title>
        <link rel="icon" type="image/png" href="$icon_image">
		<link href="./style/admin.css" rel="stylesheet" type="text/css">
		<link rel="stylesheet" href="https://use.fontawesome.com/releases/v6.0.0/css/all.css">
	</head>
	<body class="admin">
        <aside class="responsive-width-100 responsive-hidden">
            <h1>$site_name</h1>
            $admin_links
            <div class="footer">
            $site_name_footer
            </div>
        </aside>
        <main class="responsive-width-100">
            <header>
                <a class="responsive-toggle" href="#">
                    <i class="fas fa-bars"></i>
                </a>
                <div class="space-between"></div>
                <div class="dropdown right">
                    <i class="fas fa-user-circle"></i>
                    <div class="list">
                        <a href="index.php?page=profile">$profile</a>
                        <a href="index.php?page=logout">$logout</a>
                    </div>
                </div>
            </header>
EOT;
}
//------------------------------------------
// Template footer
//------------------------------------------
function template_footer($js_script = '') {
        $js_script = $js_script ? '<script>' . $js_script . '</script>' : '';
        $lancode = $_SESSION['language'] ?? 'US';
        $user_mail = $_SESSION['username'] ?? '';
        $veliti_cim = '';
        if (veliti_cim){
          $veliti_cim = '<iframe src="https://change.veliti.nl/request_popup.php?language='.$lancode.'&email='.$user_mail.'" style="border: solid 1px;border-radius: 5px;min-width:400px;min-height:400px;"></iframe>';
        } 

// DO NOT INDENT THE BELOW CODE
echo <<<EOT
        </main>
        <button id="support_btn" class="btn" style="opacity: 0.8;position: fixed;bottom: 23px;right: 28px;background:#4a79b400;font-size:36px;" onclick="openForm()"><img src="https://www.totalsafetysolutions.nl/assets/svg/tss-persoon.svg" alt="tss-persoon" height="115"></button>
          <div class="form-popup" id="request">$veliti_cim 
          <div class="close">
              <button type="button" style="border: solid 1px;" onclick="closeForm()">X</button>
          </div>
        </div>
        <script src="./assets/admin.js"></script>
        {$js_script}
        <script>
        function openForm() {
        document.getElementById("request").style.display = "block";
        }

        function closeForm() {
        document.getElementById("request").style.display = "none";
        }
        </script>
    </body>
</html>
EOT;
}

//------------------------------------------
// Secure Payload
//------------------------------------------
function generate_payload($payload) {

    include dirname(__FILE__,2).'/settings/settings.php';
	
    $headers = array('alg'=>'HS256','typ'=>'payload');
    $payload = array('payload'=>$payload,'exp'=>(time() + 1200));

    $headers_encoded = base64url_encode(json_encode($headers));
    $payload_encoded = base64url_encode(json_encode($payload));

    $signature = hash_hmac('SHA256', "$headers_encoded.$payload_encoded", $secret, true);
    $signature_encoded = base64url_encode($signature);

    $payload_input = "$headers_encoded.$payload_encoded.$signature_encoded";

    return $payload_input;
}

//------------------------------------------
// Decode Payload
//------------------------------------------
function decode_payload($payload_input) {
    include dirname(__FILE__,2).'/settings/settings.php';
    // split the jwt
    $tokenParts = explode('.', $payload_input);
    $header = base64_decode($tokenParts[0]);
    $payload = base64_decode($tokenParts[1]);
    $signature_provided = $tokenParts[2];

    $expiration = json_decode($payload)->exp;
    $is_token_expired = ($expiration - time()) < 0;

    // build a signature based on the header and payload using the secret
    $base64_url_header = base64url_encode($header);
    $base64_url_payload = base64url_encode($payload);
    $signature = hash_hmac('SHA256', $base64_url_header . "." . $base64_url_payload, $secret, true);
    $base64_url_signature = base64url_encode($signature);

    // verify it matches the signature provided in the jwt
    $is_signature_valid = ($base64_url_signature === $signature_provided);

    if ($is_token_expired || !$is_signature_valid) {
        return FALSE;
    } else {
            $tokenParts = explode('.', $payload_input);
            $payload = base64_decode($tokenParts[1]);
            return $payload_decoded = json_decode($payload)->payload;
    }
}

function base64url_encode($data) {
    return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}

//------------------------------------------
// JWT Function for CommunicationTOken
//------------------------------------------
function generate_jwt($headers, $payload) {
	
    include dirname(__FILE__,2).'/settings/settings.php';

    $headers_encoded = base64url_encode(json_encode($headers));
	$payload_encoded = base64url_encode(json_encode($payload));

	$signature = hash_hmac('SHA256', "$headers_encoded.$payload_encoded", $secret, true);
	$signature_encoded = base64url_encode($signature);

	$jwt = "$headers_encoded.$payload_encoded.$signature_encoded";

	return $jwt;
}
function is_jwt_valid($jwt) {

    include dirname(__FILE__,2).'/settings/settings.php';
	// split the jwt
	$tokenParts = explode('.', $jwt);
	$header = base64_decode($tokenParts[0]);
	$payload = base64_decode($tokenParts[1]);
	$signature_provided = $tokenParts[2];

	$expiration = json_decode($payload)->exp;
	$is_token_expired = ($expiration - time()) < 0;

	// build a signature based on the header and payload using the secret
	$base64_url_header = base64url_encode($header);
	$base64_url_payload = base64url_encode($payload);
	$signature = hash_hmac('SHA256', $base64_url_header . "." . $base64_url_payload, $secret, true);
	$base64_url_signature = base64url_encode($signature);

	// verify it matches the signature provided in the jwt
	$is_signature_valid = ($base64_url_signature === $signature_provided);

	if ($is_token_expired || !$is_signature_valid) {
		return FALSE;
	} else {
		return TRUE;
	}
}

//------------------------------------------
// createCommunicationToken
//------------------------------------------
function createCommunicationToken($input){
    $headers = array('alg'=>'HS256','typ'=>'JWT');
    $payload = array('token'=>$input, 'exp'=>(time() + 2100));
    $token = generate_jwt($headers, $payload);
    return $token;
}

//------------------------------------------
// getUserKey
//------------------------------------------
function getUserKey($jwt){

    include dirname(__FILE__,2).'/settings/settings.php';
	$tokenParts = explode('.', $jwt);
	$payload = base64_decode($tokenParts[1]);
	$token = json_decode($payload)->token;

	return $token;
}

//------------------------------------------
// get_bearer_token
//------------------------------------------

function get_authorization_header(){
	$headers = null;

	if (isset($_SERVER['Authorization'])) {
		$headers = trim($_SERVER["Authorization"]);
	} else if (isset($_SERVER['HTTP_AUTHORIZATION'])) {
		$headers = trim($_SERVER["HTTP_AUTHORIZATION"]);
	} else if (function_exists('apache_request_headers')) {
		$requestHeaders = apache_request_headers();
		$requestHeaders = array_combine(array_map('ucwords', array_keys($requestHeaders)), array_values($requestHeaders));
		if (isset($requestHeaders['Authorization'])) {
			$headers = trim($requestHeaders['Authorization']);
		}
	}

	return $headers;
}
function get_bearer_token() {
    $headers = get_authorization_header();

    if (!empty($headers)) {
        if (preg_match('/Bearer\s(\S+)/', $headers, $matches)) {
            return $matches[1];
        }
    }
    return null;
}

//------------------------------------------
// APIto/fromServer
//------------------------------------------
function ioServer($api_call, $data){

    include dirname(__FILE__,2).'/settings/settings.php';
  
    $token = $_SESSION['userkey'] ?? 'authorization_request';
    $bearertoken = createCommunicationToken($token);
        
    $url = $baseurl.$api_call;

    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    
    $headers = array(
            "Authorization: Bearer $bearertoken",
            "Content-Type: application/json",
    );
    
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  
    if (!empty($data)){
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }

    $resp = curl_exec($curl);
    $http_status = curl_getinfo($curl) ?? '200';
    curl_close($curl);
    
    //Check If errorcode is returned
    if($http_status['http_code'] == '403' || $http_status['http_code'] == '400') {$resp = generate_payload('NOK');}
    
    //Response
    return $resp;
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//get user profile||$profile=settings, $permision = userright()
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function getProfile($profile, $permission){
  
  include_once dirname(__FILE__,2).'/settings/config.php';

  if ($profile != ''){
      $profile = (defined($profile)) ? constant($profile) : standard_profile;
  }
  else {
    switch ($permission) {
      case '4': //Admin++
      $profile = admin_profile;
      break;
      case '3': //Admin
      $profile = admin_profile;
      break;
      case '2': //Super User
      $profile = superuser_profile;
      break;
      default: // Readonly & create&update
      $profile = standard_profile;
      break;
    }
  }
  return $profile;
  }
  
  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  //Is allowed  (yes=1)++++++++++++++++++++++++++++++++++++++++
  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  function isAllowed($page,$profile,$permission,$action){    
    
    //Include settingsa
    include dirname(__FILE__,2).'/settings/settings.php';

    //GET ALLOWED ACTIONS
    $user_permission = ${'permission_'.$permission};
    
    //CHECK ALLOWED
    $page_action = str_contains($user_permission,$action) > 0 ? 1 : 0;
    $page_access = str_contains($profile,$page) > 0 ? 1 : 0;
    
    //RETURN CODE
    if ($page_access == 1 && $page_action == 1){
        $user_access = 1;
    } else {
        //Not Allowed
        $user_access = 0;
    }
    return $user_access;
  }
  

//------------------------------------------
// userRight standaridazation helper
//------------------------------------------
function userRights($input){
	
// From User assigned view gets allowed data view level 
// Admin and Admin+ = all - levels
// SuperUser = soldto - and lower
// CreateUpdate = shipto and lower
// Read-only = shipto and lower
$permission = '';

	switch ($input) {
		case '1':
			//SuperUser
			$permission = 2;
			break;
		case '2':
			//create & update
			$permission = 1;
			break;
		case '3':
			//read-only
			$permission = 0;
			break;
		case '4':
			//admin
			$permission = 3;
			break;
		case '5':
			//admin
			$permission = 4;
			break;
	}
	return $permission;
}

//------------------------------------------
// Retrieve all $_GET from URL
//------------------------------------------
function urlGETdetails($input){
    //GET Details from URL
    if(isset($input) && !empty($input)){
        $GET_VALUES = '';
        foreach ($input as $KEY => $VALUE){
            $GET_VALUES .= $KEY.'='.$VALUE;
            $GET_VALUES .= '&';
        }
        return $GET_VALUES = rtrim($GET_VALUES, "&");
    } 
    else {
        return $GET_VALUES = '';
    }

}

//------------------------------------------
// Translate name of Partner
//------------------------------------------
function getPartnerName($str){
    if (!empty($str)){
        $prefix = strtok($str, '-').'-';
        if (str_starts_with($str, $prefix)) {
        $str2 = substr($str, strlen($prefix));
        }
        else {
        $str2 = $str;
        }
        return $str2;  
    }
}
//------------------------------------------
// overview Indicators
//------------------------------------------
function overviewIndicators($warranty, $service, $sw_version, $hw_version){
  include dirname(__FILE__,2).'/settings/settings.php';
  include dirname(__FILE__,2).'/settings/systemfirmware.php';
  $indicator ='';
  //In warranty
  if (!empty($warranty ) && $warranty > $warrantydate){
    $indicator .= '<span class="dot" style="background-color: #13b368;">W</span>';
  } else {
    $indicator .= '<span class="dot" style="background-color: #eb8a0d;">W</span>';
  }
  //Out of Service
  if (!empty($service) && $service < $servicedate){
    $indicator .= '<span class="dot" style="background-color: #eb8a0d;">S</span>';
  } else {
     $indicator .= '<span class="dot" style="background-color: #13b368;">S</span>';
  }

  //Firmware
  if (isset($sw_version) && isset($hw_version)){    
      $sw_version = substr($sw_version, 0, strpos($sw_version, "."));
      $firmware = ${'Firmwarename' . $hw_version} ?? '';
      //Firmware upgrade required
      if (!empty($firmware) && ($sw_version != $firmware)){
          $indicator .= '<span class="dot" style="background-color: #eb8a0d;">F</span>';
      } else {
        $indicator .= '<span class="dot" style="background-color: #13b368;">F</span>';
      }
    } else {
      $indicator .= '<span class="dot" style="background-color: #81848a">F</span>';

    }

  return $indicator;
}


//------------------------------------------
// Check service status
//------------------------------------------

function warrantyStatus($input){

    include dirname(__FILE__,2).'/settings/settings.php';
    //INCLUDE TRANSLATION FILE
    if(isset($_SESSION['country_code'])){
    $api_file_language = dirname(__FILE__,2).'/settings/translations/translations_'.strtoupper($_SESSION['country_code']).'.php';
    if (file_exists($api_file_language)){   
        include $api_file_language; //Include the code
    }
    else {
        include dirname(__FILE__,2).'/settings/translations/translations_US.php';
    }
    }
    else {
        include dirname(__FILE__,2).'/settings/translations/translations_US.php';
    }  

    $warranty_date_due ='<span class="status">Unknown</span>';

    if (!empty($input) && $input < $warrantydate){
        $warranty_date_due = '<span class="status warranty_outdated">'.$warranty_outdated_text.'</span>';
    } else {
        $warranty_date_due ='<span class="status warranty">'.$warranty_recent.' ('.date('Y-m-d', strtotime($input. ' + 365 days')).')</span>';
    }

    return $warranty_date_due;
}

//------------------------------------------
// Check service status
//------------------------------------------

function serviceStatus($input){

    include dirname(__FILE__,2).'/settings/settings.php';

    //INCLUDE TRANSLATION FILE
    if(isset($_SESSION['country_code'])){
      $api_file_language = dirname(__FILE__,2).'/settings/translations/translations_'.strtoupper($_SESSION['country_code']).'.php';
      if (file_exists($api_file_language)){   
          include $api_file_language; //Include the code
      }
      else {
          include dirname(__FILE__,2).'/settings/translations/translations_US.php';
      }
    }
    else {
        include dirname(__FILE__,2).'/settings/translations/translations_US.php';
    }  

    $service_date_due ='<span class="status">Unknown</span>';

    if (!empty($input) && $input < $servicedate){
        $service_date_due = '<span class="status service_renewal">'.$service_renewal_text.'</span>';
    } else {
        $service_date_due ='<span class="status service">'.$service_recent.' ('.date('Y-m-d', strtotime($input. ' + 365 days')).')</span>';
    }

    return $service_date_due;
}
//------------------------------------------
// Check available firmware
//------------------------------------------

function availableFirmware($sw_version, $hw_version){

    $message ='<span class="status">Unknown</span>';

   if (isset($sw_version) && isset($hw_version)){
        include dirname(__FILE__,2).'/settings/systemfirmware.php';
        include dirname(__FILE__,2).'/settings/settings.php';

        //INCLUDE TRANSLATION FILE
        if(isset($_SESSION['country_code'])){
          $api_file_language = dirname(__FILE__,2).'/settings/translations/translations_'.strtoupper($_SESSION['country_code']).'.php';
          if (file_exists($api_file_language)){   
              include $api_file_language; //Include the code
          }
          else {
              include dirname(__FILE__,2).'/settings/translations/translations_US.php';
          }
        }
        else {
            include dirname(__FILE__,2).'/settings/translations/translations_US.php';
        }  

        $sw_version = substr($sw_version, 0, strpos($sw_version, "."));
        $firmware = ${'Firmwarename' . $hw_version} ?? '';

        if (!empty($firmware) && ($sw_version != $firmware)){
            $message = '<span class="status firmware_update">'.$firmware_update_text.'</span>';

        } else {
            $message = '<span class="status firmware_recent">'.$firmware_recent_text.'</span>';
        }
    } 
    return $message;
}

//------------------------------------------
// show serviceEvents available
//------------------------------------------
function serviceEvents ($messages){

    include dirname(__FILE__,2).'/settings/settings.php'; 

    //INCLUDE TRANSLATION FILE
    if(isset($_SESSION['country_code'])){
    $api_file_language = dirname(__FILE__,2).'/settings/translations/translations_'.strtoupper($_SESSION['country_code']).'.php';
      if (file_exists($api_file_language)){   
          include $api_file_language; //Include the code
      }
      else {
          include dirname(__FILE__,2).'/settings/translations/translations_US.php';
      }
    }
    else {
        include dirname(__FILE__,2).'/settings/translations/translations_US.php';
    }  
    
    $view_header = '<table class="sortable">
                <thead>
                <tr>
                    <th>'.$equipment_label2.'</th>
                    <th>'.$equipment_label8.'</th>
                    <th>'.$equipment_label9.'</th>
                    <th>'.$equipment_label3.'</th>
                    <th>'.$view_asset_actions.'</th>
                </tr>
                </thead>
                <tbody>';
    
    $view_data = '';
    
    foreach ($messages as $message){
        
        if ($message->type == $type14 && str_contains($message->description, 'serialnumber')){
       
            //Get service date
            $service_date = (new DateTime($message->created))->format('Y-m-d');
            $service_renewal_date = (new DateTime($message->created))->modify('+365 days')->format('Y-m-d');
            
            $TETS = json_decode($message->description);
            $test = $TETS->maintenance_test ?? '';

            //+++++++++++++++++++++++++++++++++++++++++++++++
            // Maintenance Test
            //++++++++++++++++++++++++++++++++++++++++++++++
            //GET from DB
            $pdo = dbConnect($dbname);
            $sql = 'SELECT description FROM history WHERE rowID = "'.$test.'"';
            $stmt = $pdo->query($sql);
            $stmt->execute();
            $result = $stmt->fetch();

            $test_string = $result['description'] ?? '';
            //+++++++++++++++++++++++++++++++++++++++++++++++++++++++
            // Check if false test found
            // +++++++++++++++++++++++++++++++++++++++++++++++++++++++
          
            if (str_contains($test_string, "false")){
                $service_status = '<span class="status warranty_outdated">'.$service_report_outcome_attention.'</span>';
                $service_renewal_date = $service_report_outcome_attention; // leave service-renewal empty when errors are found
            } else {
                $service_status = '<span class="status warranty">'.$service_report_outcome_good.'</span>';
            }
            
            $view_data .= '<tr>
                        <td>'.$message->serialnumber.'</td>
                        <td>'.$service_date.'</td>
                        <td>'.$service_renewal_date.'</td>
                        <td>'.$service_status.'</td>
                        <td><a href="index.php?page=servicereport&equipmentID='.$message->h_equipmentid.'&historyID='.$message->historyID.'" class="btn_link">'.$general_view.'</a></td>
                        <td><a href="index.php?page=render_service_report&historyID='.$message->historyID.'" class="btn_link">PDF</a></td>
                    </tr>';
        }
    $view_data .= '</tbody>';
    }


    if ($view_data == ''){
        $view_data =  '<tr>
                            <td colspan="4" style="text-align:center;">'.$service_report_no_text.'</td>
                        </tr>';
    }
    
    $view_footer = '</table>';

    $view = $view_header.$view_data.$view_footer;
    
    return $view;
}
//------------------------------------------
// Service report //Input = servicereport
//------------------------------------------

function serviceReport($history, $request)
{

  if(isset($_SESSION['country_code'])){
    $api_file_language = dirname(__FILE__,2).'/settings/translations/translations_'.strtoupper($_SESSION['country_code']).'.php';
      if (file_exists($api_file_language)){   
          include $api_file_language; //Include the code
      }
      else {
          include dirname(__FILE__,2).'/settings/translations/translations_US.php';
      }
  }
  else {
      include dirname(__FILE__,2).'/settings/translations/translations_US.php';
  }  

  include dirname(__FILE__,2).'/settings/settings.php';
  include dirname(__FILE__,2).'/settings/systemservicetool.php';
  
    $json_array = json_decode($history->description);
    $service_exe_date = (new DateTime($history->created))->format('Y-m-d');
    $service_renewal_date = (new DateTime($history->created))->modify('+365 days')->format('Y-m-d');
    
    if (isset($json_array->maintenance_test)) {     
      //Get Maintenance Test directly from DB
      $pdo = dbConnect($dbname);
      $stmt = $pdo->prepare('SELECT description FROM history WHERE rowID = ?');
      $stmt->execute([$json_array->maintenance_test]);
      $maintenance_test = $stmt->fetch();
      $test_fail = $maintenance_test['description'];
      $maintenance_test = json_decode($maintenance_test['description']);

      //check if maintenance test has false test and update renewal date
      if (str_contains($test_fail, "false")){
        $service_renewal_date = $service_report_outcome_attention; ; 
      }
    }
   
    $servicereport = ''; //empty string

    if ($request != 'display'){
    $servicereport .= '
    <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <style>
           @page {margin: 220px 50px; }
        
           body {
            font-family: "DejaVu Sans", system-ui, sans-serif;
            color: #035754;
            font-size: 18px;
            line-height: 1.5;
          }
      
          .servicereport {
            border-collapse: collapse;
            width: 100%;
            border-radius: 5px;
            border-style: hidden;
            margin: auto;
            background-color: #fff;
            margin-top: 20px;
          }
      
          .servicereport.fs-normal td {
            font-size: 18px;
          }
      
          .servicereport.fs-small td {
            font-size: 14px;
          }
      
          .servicereport thead {
            background-color: #2fac66;
            color: #fff;
          }
      
          .servicereport td {
            padding: 5px 15px;
          }
      
          .servicereport thead td {
            font-weight: bold;
          }
      
          .servicereport tbody td.success {
              text-decoration: underline;
              text-decoration-thickness: 2px;
              text-decoration-color: #3ead48;
              text-underline-offset: 2px;
          }
      
          .servicereport tbody td.warning {
              text-decoration: underline;
              text-decoration-thickness: 2px;
              text-decoration-color: #ffc107;
              text-underline-offset: 2px;
          }
      
          .servicereport tbody td.error {
              text-decoration: underline;
              text-decoration-thickness: 2px;
              text-decoration-color: #d1232a;
              text-underline-offset: 2px;
          }
      
          .servicereport .col-key {
            width: 70%;
          }
      
          .servicereport .col-value {
            width: 30%;
          }
      
          .servicereport--details tbody tr {
              border-bottom: 1px solid #035754;
          }
      
          .servicereport--striped tbody tr:nth-child(odd) {
              background-color: #f3fafa;
          }
      
          .servicereport--striped tbody tr:nth-child(even) {
              background-color: #e7f6f6;
          }
      
          .servicereport--striped tbody tr {
              border-bottom: 1px solid #9bdcd9;
          }
      
          .service_summary {
            background-image: linear-gradient(#e7f6f6, #fafdfd);
            margin-top: 20px;
            padding-bottom: 15px;
          }
      
          .service_summary__title {
            background-color: #035754;
            color: #fff;
            font-weight: bold;
            padding: 5px 15px;
          }
      
          .service_notes {
            margin: 15px 15px 0 15px;
            padding: 15px;
            background-color: #fff;
            color: #035754;
          }
      
          #content {
            width: 100%;
            margin: auto;
          }
      
          #header {
            position: fixed;
            left: -50px;
            top: -220px;
            right: -50px;
            height: 200px;
            text-align: center;
            border-radius: 5px;
          }
      
          #header h1 {
            color: #fff;
            font-weight: bold;
            margin: auto;
          }
      
          #header img {
            width: 100%;
          }
      
          #footer {
            position: fixed;
            left: -50px;
            bottom: -280px;
            right: -50px;
            height: 150px;
      
            border-radius: 5px;
          }
      
          #footer img {
            width: 100%;
          }
      
          #footer .page:after {
            content: counter(page, upper-roman);
          }
      
          .page-break {
            page-break-before: always;
          }
      
          .text-center {
            text-align: center;
          }
      
          h4 {
            margin-top: 0;
          }
      
          h4.label {
            padding: 5px 15px;
            background-color: #035754;
            color: #fff;
            margin-top: 20px;
            margin-bottom: 20px;
          }
      
        </style>
      </head>
    <body>
    <div id="header">
      <img src="https://'.$portalURL.'/assets/images/servicereport.png" alt="Service report">
    </div>
    <div id="footer">
      <img src="https://'.$portalURL.'/assets/images/servicereport-footer.png" alt="Service report">
    </div>';        
    }

    $servicereport .= '
    <div id="content">
    <table id="" class="fs-normal servicereport servicereport--details">
    <thead>
      <tr>
        <td class="col-key">'.$service_report_item.'</td>
        <td class="col-value">&nbsp;</td>
      </tr>
    </thead>
    <tbody>
    <tr>
      <td class="col-key">'.$service_report_id.'</td>
      <td class="col-value">'.$history->historyID.'</td>
    </tr>
    <tr>
      <td class="col-key">'.$service_serialnumber.'</td>
      <td class="col-value">'.$json_array->serialnumber.'</td>
    </tr>
    <tr>
      <td class="col-key">'.$service_date.'</td>
      <td class="col-value">'.$service_exe_date.'</td>
    </tr>
    <tr>
      <td class="col-key">'.$service_renewal.'</td>
      <td class="col-value">'.$service_renewal_date.'</td>
    </tr>
    <tr>
      <td class="col-key">'.$service_performed_by.'</td>
      <td class="col-value">'.$history->createdby.'</td>
    </tr>
    </tbody>
    </table>
    <div id="service_summary" class="service_summary">
    <div class="service_summary__title">'.$summary_label1.'</div>
    <ul>
    ';
    
    $actions = 0;
    $repairs = 0;
   
   //Check New Report or Legacy
   if (str_contains($history->description, "service_mandatory_question")) { 
    // New Report
    foreach ($json_array->final as $final) {
      if ($final->id == "service_action_clean" && $final->value == "Yes") {
        $servicereport .= '<li>' . $service_action_clean . '</li>';
        $actions++;
      }
      if ($final->id == "service_action_battery" && $final->value == "Yes") {
        $servicereport .= '<li>' . $service_action_battery . '</li>';
        $actions++;
      }         
    }
  } elseif (str_contains($history->description, "question25") && !str_contains($history->description, "newHistory")) {
    foreach ($json_array->final as $final) {
      if (($final->id == "action_clean" ||$final->id == "action_serial") && $final->value == 'Yes'){
        $servicereport .= '<li>' . ${$final->id . '_description'}. '</li>';
        $actions++;
      }  
    }
  }
  if ($actions == 0) {$servicereport .= '<li>'.$message1.'</li>';}
  
    $servicereport .= '
    </ul>
    <div class="service_summary__title">'.$group_header_8.'</div>
    <ul>
    ';
    if (str_contains($history->description, "service_mandatory_question")) { 
      foreach ($json_array->final as $final) {
        if ($final->id == "service_action_serial" && $final->value == "Yes") {
          $servicereport .=  '<li>' . $service_action_serial . '</li>';
            $repairs++;
        }
        if ($final->id == "service_action_cover" && $final->value == "Yes") {
          $servicereport .=  '<li>' . $service_action_cover . '</li>';
            $repairs++;
        }         
        if ($final->id == "service_action_spudger" && $final->value == "Yes") {
          $servicereport .=  '<li>' . $service_action_spudger . '</li>';
            $repairs++;
        }       
        if ($final->id == "service_action_case" && $final->value == "Yes") {
          $servicereport .=  '<li>' . $service_action_case . '</li>';
            $repairs++;
        }       
        if ($final->id == "service_action_instructions" && $final->value == "Yes") {
          $servicereport .=  '<li>' . $service_action_instructions . '</li>';
            $repairs++;
        }       
      } 
    } elseif (str_contains($history->description, "question25") && !str_contains($history->description, "newHistory")) {
      foreach ($json_array->final as $final) {
        if (strpos($final->id, "action_repaired") !==false && $final->value == 'Yes') {
          $servicereport .= '<li>' . ${$final->id . '_description'}. '</li>';
          $repairs++;
        }
        if (strpos($final->id, "action_replacement") !==false && $final->value == 'Yes') {
          $servicereport .= '<li>' . ${$final->id . '_description'}. '</li>';
          $repairs++;
        }
      }
    }
    
    if ($repairs == 0) {$servicereport .= '<li>' . $message2 . '</li>';}
    $servicereport .='</ul>';
    if (str_contains($history->description, "service_mandatory_question")) { 
    //GET NOTES from JSON
      $notes ='';
      foreach ($json_array->final as $final){
        if ($final->id == "service_notes"){
          $notes = $final->value;
        }
      }
    
      $servicereport .='
      <div class="service_summary__title">'.$group_header_7.'</div>';
      if ($notes != '') {
        $servicereport .= '<div id="service_notes" class="service_notes">'.$notes.'</div>';
      } else {
        $servicereport .= '<div id="service_notes" class="service_notes">'.$service_report_no_comments.'</div>';  }
    }
    $servicereport .= '</div>';
    
    //+++++++++++++++++++++++++++++++++++++++++++++++
    // Maintenance Test
    //++++++++++++++++++++++++++++++++++++++++++++++

      if (isset($json_array->maintenance_test)) {
        
        if (!empty($maintenance_test)) {
    
        $servicereport .='
        <div class="page_break"></div>
        <table id="" class="fs-small servicereport servicereport--striped">
        <thead>
            <tr>
            <td class="col-key">'.$service_report_maintenance_test.'</td>
            <td class="col-value text-center">'.$service_report_maintenance_test_status.'</td>
            </tr>
        </thead>      
        ';
    
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++++
        // Check for measurements       ++++++++++++++++++++++++++
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++++
        // Check if false test found                        +++++++
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++++
        $servicereport .= '<tbody>';

        //Check double test values
        foreach($maintenance_test->doubletestvalues as $key => $value)
            {
            //If test is not passed or not
            if($value->pass == false){
            $text = $service_maintenance_test_fail;
            $style = 'error';
            }
            else 
            {
            $text = $service_maintenance_test_pass;
            $style = 'success';

            }
            
            $servicereport .='
                <tr>
                <td class="col-key">'.$value->name.'</td>
                <td class="col-value text-center '.$style.'">'.$text.'</td>
                </tr>
            ';
            }
        //Check string test values
        foreach($maintenance_test->stringtestvalues as $key => $value)
        {
        //If test is not passed or not
        if($value->pass == false){
        $text = $service_maintenance_test_fail;
        $style = 'error';
        }
        else 
        {
        $text = $service_maintenance_test_pass;
        $style = 'success';

        }
        $servicereport .='
            <tr>
            <td class="col-key">'.$value->name.'</td>
            <td class="col-value text-center '.$style.'">'.$text.'</td>
            </tr>
        ';
        }

        //Check boolean test values
        foreach($maintenance_test->booleantestvalues as $key => $value)
        {
          //If test is not passed or not
          if($value->pass == false){
          $text = $service_maintenance_test_fail;
          $style = 'error';
          }
          else 
          {
          $text = $service_maintenance_test_pass;
          $style = 'success';
          }
        $servicereport .='
            <tr>
            <td class="col-key">'.$value->name.'</td>
            <td class="col-value text-center '.$style.'">'.$text.'</td>
            </tr>
        ';
        }
      
        $servicereport .= '
        </tbody>
    </table>
    ';
     }
            
    }
   //++++++++++++++++++++++++++++++++++++++++++++++
    //GEt visualinspection
    //++++++++++++++++++++++++++++++++++++++++++++++
    if (isset($json_array->visualinspection)) {

        //Get visualinspection directly from DB
        $pdo = dbConnect($dbname);
        $stmt = $pdo->prepare('SELECT description FROM history WHERE rowID = ?');
        $stmt->execute([$json_array->visualinspection]);
        $visualinspection = $stmt->fetch();

        $visualinspection_legacy = $visualinspection['description'];
        $visualinspection = json_decode($visualinspection['description']);
    

        if (!empty($visualinspection)) {
        
        //Check if visualinspection comes from ServiceTool else inhouse
            if (isset($visualinspection->serviceReport->questionItems)) {
            $visualinspection_array = $visualinspection->serviceReport->questionItems;
                        }
            else {
            //inhousetool
            $visualinspection_array = $visualinspection;
            }
        //Check Legacy API
            if (str_contains($visualinspection_legacy, "question1")) {
            //Legacy API Questions
            $question_array = $arrayQuestions_legacy;
            }
            else
            { // New Report
            $question_array = $arrayQuestions_visual;
            }

            $servicereport .= '

            <h4 class="label">' . $summary_label2 . '</h4>';

            foreach ($question_array as $key){
            if ($key['Group_sequence'] == '3'){
                $servicereport .= '<div class="page_break"></div>'; // pagebreak
            }
            $servicereport .= '
            <table id="" class="fs-small servicereport servicereport--striped">
            <thead>
                <tr>
                  <td class="col-key">'.$key['Group'].'</td>
                  <td class="col-value text-center">'.$service_report_maintenance_test_status.'</td>
                </tr>
            </thead>
            <tbody>';
            foreach ($key['Questions_in_group'] as $questions){
                
                $servicereport .=  '<tr>
                                    <td class="col-key">'; 
                        $response = (substr(${$questions['QuestionID']}, -1) == '?') ? substr(${$questions['QuestionID']}, 0, strpos(${$questions['QuestionID']}, "?")) : ${$questions['QuestionID']};
                        //$response = $questions['QuestionID'];
                        $servicereport .=  ' '.$response.'
                        </td>';
                            foreach ($visualinspection_array as $inspection){
                                if ($inspection->id == $questions['QuestionID']){

                                  if (isset($inspection->responseID)){
                                    if (in_array($inspection->responseID,$negative_results)){
                                      $style2 = 'error';
                                      }
                                      else 
                                      {
                                      $style2 = 'success';
                                      }
                                      $translated_value = ${'service_allowed_label'.$inspection->responseID};
                                  }else{
                                    if (in_array($inspection->id,$negative_results)){
                                    $style2 = 'error';
                                    }
                                    else 
                                    {
                                    $style2 = 'success';
                                    }
                                    $translated_value = $inspection->value;
                                  }

                            $servicereport .= '<td class="col-value text-center '.$style2.'">'.$translated_value.'</td>';
                            }
                        }
                        $servicereport .= ' 
                    </tr>';
                }
    
            $servicereport .= '</tbody></table>';
         }
        
        }
    }

    //++++++++++++++++++++++++++++++++++++++++++++++
    //Legacy Code
    //++++++++++++++++++++++++++++++++++++++++++++++
    if (isset($json_array->question25) && isset($json_array->newHistory)) {
      $servicereport .= '
      
      <h4>'.$summary_label2.'</h4>
      <table class="fs-normal servicereport servicereport--details">
      <thead>
      <tr>
          <td>&nbsp;</td>
          <td>'.$summary_label3.'</td>
          <td>'.$summary_label4.'</td>
      </tr>
      </thead>
      <tbody>
      <tr>
          <td>'.$question10.'</td>
          <td>
          '.$service_allowed_label9.'
          </td>
          <td>
              '; if (isset($json_array->{$question10.'b'})){$servicereport .=  $json_array->{$question10.'b'};} else {$servicereport .=  $servicereport5;} $servicereport .= '
            </td>
        </tr>
      <tr>
          <td>'.$question11.'</td>
          <td>
              '; if (isset($json_array->{$question11.'a'})){$servicereport .=  $json_array->{$question11.'a'};} else {$servicereport .=  $servicereport5;} $servicereport .= '
          </td>
          <td>
              '; if (isset($json_array->{$question11.'b'})){$servicereport .=  $json_array->{$question11.'b'};} else {$servicereport .=  $servicereport5;} $servicereport .= '
            </td>
      </tr>
      <tr>
          <td>'.$question12.'</td>
          <td>
              '; if (isset($json_array->{$question12.'a'})){$servicereport .=  $json_array->{$question12.'a'};} else {$servicereport .=  $servicereport5;} $servicereport .= '
          </td>
          <td>
              '; if (isset($json_array->{$question12.'b'})){$servicereport .=  $json_array->{$question12.'b'};} else {$servicereport .=  $servicereport5;} $servicereport .= '
            </td>
      </tr>
      <tr>
          <td>'.$question13.'</td>
          <td>
              '; if (isset($json_array->{$question13.'a'})){$servicereport .=  $json_array->{$question13.'a'};} else {$servicereport .=  $servicereport5;} $servicereport .= '
          </td>
          <td>
              '; if (isset($json_array->{$question13.'b'})){$servicereport .=  $json_array->{$question13.'b'};} else {$servicereport .=  $servicereport5;} $servicereport .= '
            </td>
        </tr>
      <tr>
          <td>'.$question14.'</td>
          <td>
              '; if (isset($json_array->{$question14.'a'})){$servicereport .=  $json_array->{$question14.'a'};} else {$servicereport .=  $servicereport5;} $servicereport .= '
          </td>
          <td>
              '; if (isset($json_array->{$question14.'b'})){$servicereport .=  $json_array->{$question14.'b'};} else {$servicereport .=  $servicereport5;} $servicereport .= '
            </td>
          </tr>
      <tr>
          <td>'.$question15.'</td>
          <td>
          '.$service_allowed_label9.'
          </td>
          <td>
            '; if (isset($json_array->{$question15.'b'})){$servicereport .=  $json_array->{$question15.'b'};} else {$servicereport .=  $servicereport5;} $servicereport .= '
            </td>
        </tr>
      <tr>
          <td>'.$question16.'</td>
          <td>
          '.$service_allowed_label9.'
          </td>
          <td>
          '; if (isset($json_array->{$question16.'b'})){$servicereport .=  $json_array->{$question16.'b'};} else {$servicereport .=  $servicereport5;} $servicereport .= '
            </td>
        </tr>
      <tr>
          <td>'.$question17.'</td>
          <td>
            '.$service_allowed_label9.'
          </td>
          <td>
            '; if (isset($json_array->{$question17.'b'})){$servicereport .=  $json_array->{$question17.'b'};} else {$servicereport .=  $servicereport5;} $servicereport .= '
            </td>
        </tr>
      <tr>
          <td>'.$question18.'</td>
          <td>
          '; if (isset($json_array->{$question18.'a'})){$servicereport .=  $json_array->{$question18.'a'};} else {$servicereport .=  $servicereport5;} $servicereport .= '
          </td>
          <td>
          '; if (isset($json_array->{$question18.'b'})){$servicereport .=  $json_array->{$question18.'b'};} else {$servicereport .=  $servicereport5;} $servicereport .= '
            </td>
        </tr>
        </tbody>
      </table>';
    }
      
    $servicereport .='</div>';
    
    if ($request != 'display'){
      $servicereport .= '
            </body>
            </html>';
    }

  return $servicereport;
}

//------------------------------------------
// LIST PARTNER 
//------------------------------------------
function listPartner($partnertype, $user_right, $input)
{
  include dirname(__FILE__,2).'/settings/settings.php';

//BASED ON USERRIGHT DEFINE SQL AND DATA RETURNED
if ($user_right != 3 || $user_right !=4) {
    //NOT ADMIN USER
    $partner = json_decode($_SESSION['partnerhierarchy']);
    //SoldTo is empty
    if (empty($partner->soldto) || $partner->soldto == ''){$soldto_search = '%';} else {$soldto_search = '-%';}
    //BUILD CONDITION
    $condition = '__salesid___'.$partner->salesid.'___soldto___'.substr($partner->soldto, 0, strpos($partner->soldto, "-")).$soldto_search;
    $whereclause = 'AND salesID like ?';
}
else {//ADMIN USERS
      $whereclause = '';
      }

  $pdo = dbConnect($dbname_users);
  $sql = 'SELECT distinct partnerID, partnername FROM partner WHERE partnertype = ? AND status = 1 '.$whereclause.'';
  $stmt = $pdo->prepare($sql);
  $stmt->execute([$partnertype, $condition]);
  $partners = $stmt->fetchAll(PDO::FETCH_ASSOC);

if ($input !='' && !empty($input)){
    $partner_available  = '<option value="'.$input.'" selected>'.$input.'</option><option value=" "> </option>';
} else {
    $partner_available  = '<option></option>';
}

$view ='
    <input list="'.$partnertype.'" name="'.$partnertype.'" placeholder="'.$input.'" class="datalist">
    <datalist id="'.$partnertype.'">
    '.$partner_available.'
';

foreach ($partners as $row) {
$view .= '<option value="'.$row["partnerID"].'-'.$row["partnername"].'">'.$row["partnerID"].' - '.$row["partnername"].'</option>';
}

$view .= '</datalist>
';
return $view;
}

//------------------------------------------
// UPLOAD PICTURE for PRODUCTS
//------------------------------------------
function uploadProduct($name){

$target_dir = dirname(__FILE__)."/images/products/";
$input_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$imageFileType = strtolower(pathinfo($input_file,PATHINFO_EXTENSION));
$target_file = $target_dir . $name.'.jpg';
$file_input_check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);

//Do when JPG or PNG or JPEG or GIF and smaller than 5MB
if(($imageFileType == "jpg" || $imageFileType == "png" || $imageFileType == "jpeg" || $imageFileType == "gif" || $imageFileType == "png") && $_FILES["fileToUpload"]["size"] < 5000000 && $file_input_check !== false) {
  //Upload picture  
  if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
    echo "Done";
  } else {
    echo "Error";
  }
  //}
}
}
//------------------------------------------
// debuglog
//------------------------------------------
function debuglog($error){
  include_once dirname(__FILE__,2).'/settings/config.php';
  $test = $error.PHP_EOL;
    $filelocation = dirname(__FILE__,2).'/log/log_'.date('d').'.txt';
    error_log($test, 3, $filelocation);
}

//------------------------------------------
// getRowID
//------------------------------------------

function getrowID($database, $id, $table, $where){
  // Connect to DB
  $pdo = dbConnect($database);
  // Define query $where f.e. sn=2423
  $sql = 'SELECT '.$id.' from '.$table.' where '.$where.'';
  $stmt = $pdo->prepare($sql);
  $stmt->execute();
  $rowid = $stmt->fetch();
  //ReturnId
  return $rowid;
}

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++
// downloader       ++++++++++++++++++++++++++++++++++++++
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++
function downloadFile($file) {

	if (!isset($_SESSION['loggedin']) && $_SESSION['loggedin'] !== false) {
		header('location: index.php');
		exit;
	}
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream, application/download, application/force-download');
    header('Content-Disposition: attachment; filename=' . basename($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++
// visual forecast for service and warranty+++++++++++++++
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++
function serviceforecast($messages,$type){
  
  if ($messages){

    $number = (($type == 'warranty')? 1 : 1);
    //GET TOTAL SERVICE COUNT
    $totalcount = 0;
    $year_in_scope = date("Y") - 1;
    foreach ($messages as $message){
      if ($message['year'] >= $year_in_scope){
        $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'] >= $year_in_scope){
          $height = ($message['count'] / $totalcount) * 100;
          $fc_year = $message['year'] + $number;
          $title = 'Q'.$message['quarter'].' '.$fc_year;
          $view .='<li style="text-align:center;">' . $message['count'] . '<span style="height:' . $height . '%" title="' . $title . '"></span></li>';
        }
    }
    $view .='</ul></div>';

    return $view;
  }
}


// +++++++++++++++++++++++++++++++++++++++++++++++++++++++
// changelog
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++
function changelog($database,$object,$objectID,$object_field,$object_value,$createdby){
  /*
  database = database to connect to
  object = data object being changed (table name)
  object_field = field being changed
  object_value = new value
  */
  // Connect to DB
  $pdo = dbConnect($database);
  //Update query
  $sql = 'INSERT INTO changelog (object,objectID,object_field,object_value,createdby) VALUES (?,?,?,?,?)';
  //Prepare SQL AND EXECUTE
  $stmt = $pdo->prepare($sql);
  $stmt->execute([$object,$objectID,$object_field,$object_value,$createdby]);
}
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++
// show changelog (front-end only)
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++
function showlog($object,$objectID){
  
  //INCLUDE TRANSLATIONS
  if(isset($_SESSION['country_code'])){
    $api_file_language = dirname(__FILE__,2).'/settings/translations/translations_'.strtoupper($_SESSION['country_code']).'.php';
      if (file_exists($api_file_language)){   
          include $api_file_language; //Include the code
      }
      else {
          include dirname(__FILE__,2).'/settings/translations/translations_US.php';
      }
  }
  else {
      include dirname(__FILE__,2).'/settings/translations/translations_US.php';
  } 
  include dirname(__FILE__,2).'/settings/settings.php'; 
  
  // Connect to DB
  $pdo = dbConnect($dbname);
  //Update query
  $sql = 'SELECT * FROM changelog WHERE object = ? AND objectID = ?';
  //Prepare SQL AND EXECUTE
  $stmt = $pdo->prepare($sql);
  $stmt->execute([$object,$objectID]);
  $changes = $stmt->fetchAll(PDO::FETCH_ASSOC);

  $view = '<label for="productcode">Changelog</label>';
  foreach($changes as $change){
    
    $object_value = $change['object_value'];

    //UPDATE TO HUMANREADABLE STATUS
    if ($object == 'equipment' && $change['object_field'] == 'status'){
      $object_text = 'status'.$change['object_value'].'_text';
      $object_value = $$object_text;
    }
    $view .= '<input id="name" type="text" value="'.$change['object_field'].' - '.$object_value.' - '.$change['created'].' - '.$change['createdby'].'" readonly>';
  }
  
  return $view;
}

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++
// download to excell function
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++
function downloadToExcel($collection, $data_input){

//Conver to ARRAY
$data_input = json_decode(json_encode($data_input), true);

// Excel file name for download 
$fileName = $collection.date('Y-m-d').".xls"; 

//GET KEYS of first row returned
$header_fields = array_keys($data_input[0]); 

// Display column names as first row 
$excelData = implode("\t", array_values($header_fields)) . "\n"; 
 
// Fetch records from database 
if($data_input){ 
    // Output each row of the data 
    foreach ($data_input as $data_row){ 
        //array_walk($message, 'filterData'); 
        $excelData .= implode("\t", array_values($data_row)) . "\n"; 
    } 
} 

// Headers for download 
header("Content-Type: application/vnd.ms-excel"); 
header("Content-Disposition: attachment; filename=\"$fileName\""); 
 
// Render excel data 
echo $excelData;  
exit;
}