<?php

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

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

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

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

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

# Page functions

# JS Scripts to load for this page
$jsScriptLoadData['datepicker'] = true;
$jsScriptLoadData['/src/js/sentri/breadCrumbs.js'] = true;
$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
$pageNavbar = new pageNavbar(true);
$serverOverview = new serverOverviewBuilder();

# Retrieve Information for the page
$company_uuid = htmlspecialchars($_GET['view'], ENT_QUOTES, 'UTF-8');
$stmt = $GLOBALS['conn']->prepare("SELECT * FROM companies WHERE company_uuid = ?");
$stmt->bind_param('s', $company_uuid);
$stmt->execute();

$result = $stmt->get_result();
$company_data = $result->fetch_assoc();

# Set breadcrumb data
$_SESSION['breadCrumbArray'][] = ['display' => __('companies'), 'href' => '/companies/'];
$_SESSION['breadCrumbArray'][] = ['display' => $company_data['company_name'], 'href' => ''];

# Retrieve Information for the page
$company_uuid = htmlspecialchars($_GET['view'], ENT_QUOTES, 'UTF-8');
$_GET['company_uuid'] = $company_uuid;
$_GET['builder'] = [1 => ['where' => [0 => 'company_uuid', 1 => $company_uuid]]];
$requiredFields = ['company_uuid' => ['type' => 'uuid']];
$API_servers->postedData = $_GET;
$API_servers->validateData($requiredFields);
$servers_data = $API_servers->getServers(true);

if (count($servers_data) > 0) {
    $serverOverview->servers = $servers_data;
    $serverOverview->processServerData();
    $serverOverview->showDelBtn = false;
    $serverOverview->showCompanies = false;
}

# Start page output
$pageNavbar->outPutNavbar();
?>
    <div class="card">
        <div class="card-body">
            <div class="row">
                <h1>
                    <i class="<?php echo $GLOBALS['pages']['customers']['companies']['page_icon'] ?>"></i> <?php echo $company_data['company_name'] ?>
                </h1>
            </div>
            <div class="col-md-6 col-lg-6">
                <table>
                    <tr>
                        <td>company_uuid:</td>
                        <td><?php echo $company_data['company_uuid'] ?></td>
                    </tr>
                    <tr>
                        <td>source_uuid:</td>
                        <td><?php echo $company_data['source_uuid'] ?></td>
                    </tr>
                    <tr>
                        <td>company_source_id:</td>
                        <td><?php echo $company_data['company_source_id'] ?></td>
                    </tr>
                    <tr>
                        <td>company_source_id2:</td>
                        <td><?php echo $company_data['company_source_id2'] ?></td>
                    </tr>
                    <tr>
                        <td>company_name:</td>
                        <td><?php echo $company_data['company_name'] ?></td>
                    </tr>
                    <tr>
                        <td>company_state:</td>
                        <td><?php echo $company_data['company_state'] ?></td>
                    </tr>
                    <tr>
                        <td>company_create_timestamp:</td>
                        <td><?php echo $company_data['company_create_timestamp'] ?></td>
                    </tr>
                    <tr>
                        <td>company_modified_timestamp:</td>
                        <td><?php echo $company_data['company_modified_timestamp'] ?></td>
                    </tr>
                </table>
            </div>
        </div>
    </div>

<?php
if ($GLOBALS['modules_enabled']['servers'] && $API_servers->checkPermissions('servers', 'RO', true)) {
    if (count($servers_data) > 0) {
        $serverOverview->serverOverviewOutPut();
    }
}
?>