<?php

use api\classes\API_servers;
use bin\php\Classes\serverOverviewBuilder;

if (!defined('APP_INIT')) {
    exit;
}

if (!$GLOBALS['modules_enabled']['servers']) {
    echo '405 Not Allowed';
    exit;
}
# IDE Section

# Includes Section
require_once "{$_SERVER['DOCUMENT_ROOT']}/api/classes/API_servers.php";
require_once "{$_SERVER['DOCUMENT_ROOT']}/bin/php/Classes/serverOverviewBuilder.php";

# Check permissions
$API_servers = new API_servers();
if (!$API_servers->checkPermissions('servers', 'RO', true)) {
    echo 'error 401 unauthorized';
    exit;
}

# Page functions

# JS Scripts to load for this page
$jsScriptLoadData['/src/js/sentri/delete_confirmation.js'] = true;
$jsScriptLoadData['/src/js/plugin/datatables/datatables.min.js'] = true;
$jsScriptLoadData['/src/js/sentri/multiFilterSelectServers.js'] = true;

# PageClasses Setup
$serverOverview = new serverOverviewBuilder();

# Retrieve Information for the page
if (!isset($_GET['del'])) {
    if ($GLOBALS['modules_enabled']['customers']) {
        $API_servers->baseQuery = "SELECT * FROM servers LEFT JOIN companies ON companies.company_uuid = servers.company_uuid WHERE servers.server_state != 'deleted'";
    } else {
        $API_servers->baseQuery = "SELECT * FROM servers WHERE servers.server_state != 'deleted'";
    }

} else {
    if ($GLOBALS['modules_enabled']['customers']) {
        $API_servers->baseQuery = "SELECT * FROM servers LEFT JOIN companies ON companies.company_uuid = servers.company_uuid";
    } else {
        $API_servers->baseQuery = "SELECT * FROM servers";
    }
}

$servers_data = $API_servers->getServers(true);

$serverOverview->servers = $servers_data;
$serverOverview->processServerData();
if (!$GLOBALS['modules_enabled']['customers']) {
    $serverOverview->showCompanies = false;
}

# Set breadcrumb data
$_SESSION['breadCrumbArray'][] = ['display' => __('permission'), 'href' => '/accesscontrol/#permissions'];
$_SESSION['breadCrumbArray'][] = ['display' => __('view'), 'href' => ''];

# Start page output
$serverOverview->serverOverviewOutPut();
