<?php
if (!defined('APP_INIT')) {
    exit;
}

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

# Includes Section
use api\classes\API;

require_once "{$_SERVER['DOCUMENT_ROOT']}/api/classes/API_permissions.php";
require_once "{$_SERVER['DOCUMENT_ROOT']}/api/classes/API.php";

# Check permissions
$API = new API();
if (!$API->checkPermissions('customer-companies', '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/multiFilterSelect.js'] = true;
$jsScriptLoadData['datepicker'] = true;
$jsScriptLoadData['/src/js/sentri/activateCompany.js'] = true;

# PageClasses Setup

# Retrieve Information for the page
if (!isset($_GET['all'])) {
    $query = "SELECT companies.*, COUNT(servers.company_uuid) AS server_count FROM companies LEFT JOIN servers ON companies.company_uuid = servers.company_uuid WHERE company_state = 'active' GROUP BY companies.company_uuid  ORDER BY companies.company_name ASC;";
} else {
    $query = "SELECT companies.*, COUNT(servers.company_uuid) AS server_count FROM companies LEFT JOIN servers ON companies.company_uuid = servers.company_uuid GROUP BY companies.company_uuid ORDER BY companies.company_name ASC;";
}

$stmt = $GLOBALS['conn']->query($query);
$stompjes = array();

$companies = [];
while ($row = $stmt->fetch_assoc()) {
    $companies[$row['company_uuid']] = $row;
}

# Start page output
?>


<div class="form-group form-show-validation row mb-3">
    <div class="col-5">
        <h2>
            <i class="<?php echo $GLOBALS['pages']['customers']['companies']['page_icon'] ?>"></i> &nbsp; <?php echo __('companies') ?>
        </h2>
    </div>
    <div class="col d-flex justify-content-end px-1">
        <div class="col-lg-auto col-md-auto col-sm-auto">
            <?php
            if (!isset($_GET['all'])) { ?>
                <a class="btn btn-secondary" href="?all">
                    <i class="fa-solid fa-filter"></i> &nbsp; <?php echo __('show_all') ?>
                </a>
            <?php } else { ?>
                <a class="btn btn-secondary" href="?">
                    <i class="fa-solid fa-filter"></i> &nbsp; <?php echo __('show_active') ?>
                </a>
            <?php } ?>
        </div> &nbsp;
        <form method="post" action="/api/v1/sources/inserve/sync-companies/">
            <input type="hidden">
            <div class="col-lg-auto col-md-auto col-sm-auto">
                <button class="btn btn-primary">
                    <i class="fa-solid fa-arrow-rotate-right"></i> &nbsp;<?php echo __('sync') ?>
                </button>
            </div>
        </form>
    </div>
</div>

<div class="card">
    <div class="card-body">
        <div class="table-responsive">
            <table class="multi-filter-select display table table-striped table-hover" data-skip-columns="0,1,2,4" data-page-length="50">
                <thead>
                <tr>
                    <th><?php echo __('company_name') ?></th>
                    <th><?php echo __('company_id') ?></th>
                    <th><?php echo __('company_debtor') ?></th>
                    <th><?php echo __('company_state') ?></th>
                    <?php if ($GLOBALS['modules_enabled']['servers']) { ?>
                        <th><?php echo __('server_count') ?></th>
                    <?php } ?>
                    <th><?php echo __('actions') ?></th>
                </tr>
                </thead>
                <tfoot>
                <tr>
                    <th><?php echo __('company_name') ?></th>
                    <th><?php echo __('company_id') ?></th>
                    <th><?php echo __('company_debtor') ?></th>
                    <th><?php echo __('company_state') ?></th>
                    <?php if ($GLOBALS['modules_enabled']['servers']) { ?>
                        <th><?php echo __('server_count') ?></th>
                    <?php } ?>
                    <th><?php echo __('actions') ?></th>
                </tr>
                </tfoot>
                <tbody>

                <?php foreach ($companies as $company) { ?>
                    <tr data-item-id="<?php echo $company['company_uuid'] ?>">
                        <td class="text-nowrap"><?php echo $company['company_name'] ?></td>
                        <td class="text-nowrap"><?php echo $company['company_source_id'] ?></td>
                        <td class="text-nowrap"><?php echo $company['company_source_id2'] ?></td>
                        <td class="text-nowrap"><?php echo $company['company_state'] ?></td>
                        <?php if ($GLOBALS['modules_enabled']['servers']) { ?>
                            <td class="text-nowrap"><?php echo $company['server_count'] ?></td>
                        <?php } ?>
                        <td>
                            <a href="/companies?view=<?php echo $company['company_uuid'] ?>" class="btn btn-info btn-sm btn-rounded" data-item-uuid="<?php echo $company['company_uuid'] ?>"><i class="fa-solid fa-eye"></i></a>
                            <?php if ($API->checkPermissions('customer-companies', 'RW', true) && $company['server_count'] == 0) { ?>
                                <a class="btn btn-<?php echo ($company['company_state'] != 'active') ? 'success' : 'danger' ?> btn-sm btn-rounded" data-item-company-state="" data-item-uuid="<?php echo $company['company_uuid'] ?>" data-item-state="<?php echo $company['company_state'] ?>"><i class="fa-solid <?php echo ($company['company_state'] != 'active') ? 'fa-plus' : 'fa-xmark' ?>"></i></a>
                            <?php } ?>
                        </td>
                    </tr>
                <?php } ?>
                </tbody>
            </table>
            <br>
        </div>
    </div>
</div>
