<?php

use api\classes\API;
use api\classes\API_permissions;
use bin\php\Classes\pageNavbar;

if (!defined('APP_INIT')) {
    exit;
}
# IDE Section

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

# Check permissions
$API = new API();
if (!$API->checkPermissions('admin-access-control-permissions', '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/updatePermissions.js'] = true;
$jsScriptLoadData['/src/js/sentri/breadCrumbs.js'] = true;

# PageClasses Setup
$pageNavbar = new pageNavbar(true);
if ($API->isSuperuser()) {
    $pageNavbar->AddHTMLButton('<button class="btn btn-danger" type="button" disabled><i class="fas fa-trash-alt"></i> ' . __('delete') . '</button>');
}
if ($API->checkPermissions('admin-access-control-permissions', 'RW', true)) {
    $permission_view = htmlspecialchars($_GET['permission_view'], ENT_QUOTES, 'UTF-8');
    $pageNavbar->AddHTMLButton('<a class="btn btn-primary mx-3" type="button" href="?permission_edit=' . $permission_view . '"><i class="fas fa-edit text-warning"></i> ' . __('edit') . '</a>');
}
if ($API->isSuperuser()) {
    $pageNavbar->AddHTMLButton('<a href="?permission_add="><button class="btn btn-primary"><i class="fas fa-plus text-success"></i> ' . __('add_permission') . '</button></a>');
}


# Retrieve Information for the page
$permission_uuid = $_GET['permission_view'];
$_GET['permission_uuid'] = $permission_uuid;
$API_permissions = new API_permissions();
$requiredFields = ['permission_uuid' => ['type' => 'uuid']];
$API_permissions->validateData($requiredFields);
$permission_data = $API_permissions->getPermissionRights();

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

# Start page output
$pageNavbar->outPutNavbar();

if ($permission_data) { ?>
    <h2>
        <i class="fa-solid fa-lock"></i> <?php echo $permission_data[0]['permission_name'] ?>
    </h2>
    <div class="card">
        <div class="card-body">
            <table>
                <tr>
                    <td>permission_uuid:</td>
                    <td><?php echo $permission_data[0]['permission_uuid'] ?></td>
                </tr>
                <tr>
                    <td>permission_name:</td>
                    <td><?php echo $permission_data[0]['permission_name'] ?></td>
                </tr>
                <tr>
                    <td>permission_slugify:</td>
                    <td><?php echo $permission_data[0]['permission_slugify'] ?></td>
                </tr>
                <tr>
                    <td>permission_description:</td>
                    <td><?php echo $permission_data[0]['permission_description'] ?></td>
                </tr>
                <tr>
                    <td>module_uuid:</td>
                    <td><?php echo $permission_data[0]['module_uuid'] ?></td>
                </tr>
                <tr>
                    <td>permission_create_timestamp:</td>
                    <td><?php showTime($permission_data[0]['permission_create_timestamp']); ?></td>
                </tr>
                <tr>
                    <td>permission_modified_datetime:</td>
                    <td><?php showTime($permission_data[0]['permission_modified_timestamp']); ?></td>
                </tr>
            </table>
        </div>
    </div>
    <div class="card-body">
        <div class="table-responsive">
            <table class="multi-filter-select display table table-striped table-hover" data-skip-columns="0,5">
                <thead>
                <tr>
                    <th><?php echo __('user_group') ?></th>
                    <th><?php echo __('NA') ?></th>
                    <th><?php echo __('RO') ?></th>
                    <th><?php echo __('RW') ?></th>
                </tr>
                </thead>
                <tfoot>
                <tr>
                    <th><?php echo __('user_group') ?></th>
                    <th><?php echo __('NA') ?></th>
                    <th><?php echo __('RO') ?></th>
                    <th><?php echo __('RW') ?></th>
                </tr>
                </tfoot>
                <tbody>
                <?php
                foreach ($permission_data as $permission_data_group) {
                    if ($permission_data_group['user_group_slugify'] != 'superuser') { ?>
                        <tr>
                            <td><?php echo $permission_data_group['user_group_slugify'] ?> </td>
                            <td>
                                <label class="switch">
                                    <input type="checkbox" class="checkbox" data-permission-uuid="<?= $permission_data_group['permission_uuid'] ?>" data-user-group-uuid="<?= $permission_data_group['user_group_uuid'] ?>" data-value="NA" data-api-url="/api/v1/portal-management/group-permissions/" <?php echo(($permission_data_group['permission_value'] == 'NA') ? 'checked' : '') ?>
                                            <?php echo ($API->checkPermissions('admin-access-control-permissions', 'RW', true)) ? '' : 'disabled' ?>>
                                    <span class="slider"></span>
                                </label>
                            </td>
                            <td>
                                <label class="switch">
                                    <input type="checkbox" class="checkbox" data-permission-uuid="<?= $permission_data_group['permission_uuid'] ?>" data-user-group-uuid="<?= $permission_data_group['user_group_uuid'] ?>" data-value="RO" data-api-url="/api/v1/portal-management/group-permissions/" <?php echo(($permission_data_group['permission_value'] == 'RO') ? 'checked' : '') ?>
                                            <?php echo ($API->checkPermissions('admin-access-control-permissions', 'RW', true)) ? '' : 'disabled' ?>>
                                    <span class="slider"></span>
                                </label>
                            </td>
                            <td>
                                <label class="switch">
                                    <input type="checkbox" class="checkbox" data-permission-uuid="<?= $permission_data_group['permission_uuid'] ?>" data-user-group-uuid="<?= $permission_data_group['user_group_uuid'] ?>" data-value="RW" data-api-url="/api/v1/portal-management/group-permissions/" <?php echo(($permission_data_group['permission_value'] == 'RW') ? 'checked' : '') ?>
                                            <?php echo ($API->checkPermissions('admin-access-control-permissions', 'RW', true)) ? '' : 'disabled' ?>>
                                    <span class="slider"></span>
                                </label>
                            </td>
                        </tr>
                    <?php } ?><?php } ?>
                </tbody>
            </table>
        </div>
    </div>
<?php } else {
    echo '404 not found';
}