<?php

use api\classes\API_usergroups;
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_usergroups.php";

# Check permissions
$API = new API_usergroups();
if (!$API->checkPermissions('admin-access-control-user-groups', 'RO', true)) {
    echo 'error 401 unauthorized';
    exit;
}

# Page functions

# JS Scripts to load for this page
$jsScriptLoadData['/src/js/sentri/breadCrumbs.js'] = true;
$jsScriptLoadData['/src/js/sentri/updatePermissions.js'] = true;

# PageClasses Setup
$pageNavbar = new pageNavbar(true);

# Retrieve Information for the page

$user_group_uuid = htmlspecialchars($_GET['user_group_view'], ENT_QUOTES, 'UTF-8');

$_GET['user_group_uuid'] = $user_group_uuid;
$API_usergroups = new API_usergroups();
$_GET['builder'] = [1 => ['where' => [0 => 'user_group_uuid', 1 => $user_group_uuid]]];
$requiredFields = ['user_group_uuid' => ['type' => 'uuid']];
$API_usergroups->validateData($requiredFields);
$user_group = $API_usergroups->getUsergroup()[0];

$query = "SELECT * FROM system_user_group_permissions 
INNER JOIN system_permissions ON system_user_group_permissions.permission_uuid = system_permissions.permission_uuid
WHERE user_group_uuid = ?";
$stmt = $GLOBALS['pdo']->prepare($query);
$stmt->execute([$user_group_uuid]);
$group_permissions = $stmt->fetchAll(PDO::FETCH_ASSOC);

$toggleDisabled = false;
if ($user_group['user_group_weight'] < $_SESSION['user']['user_group_weight']) {
    $toggleDisabled = true;
}

if (!$API->checkPermissions('admin-access-control-permissions', 'RW', true)) {
    $toggleDisabled = true;
}

# Set breadcrumb data
$_SESSION['breadCrumbArray'][] = ['display' => __('user_groups'), 'href' => '/accesscontrol/#user-groups'];
$_SESSION['breadCrumbArray'][] = ['display' => $user_group['user_group_name'], 'href' => ''];

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

<div class="card">
    <div class="card-body">
        <div class="row">
            <h1>
                <i class="fa-solid fa-user-group"></i> <?php echo __('user_group') . ': ' . $user_group['user_group_name'] ?>
            </h1>
        </div>
        <div class="col-md-6 col-lg-6">
            <table>
                <tr>
                    <td>user_group_uuid:</td>
                    <td><?php echo $user_group['user_group_uuid'] ?></td>
                </tr>
                <tr>
                    <td>user_group_name:</td>
                    <td><?php echo $user_group['user_group_name'] ?></td>
                </tr>
                <tr>
                    <td>user_group_slugify:</td>
                    <td><?php echo $user_group['user_group_slugify'] ?></td>
                </tr>
                <tr>
                    <td>user_group_type:</td>
                    <td><?php echo $user_group['user_group_type'] ?></td>
                </tr>
                <tr>
                    <td>user_group_weight:</td>
                    <td><?php echo $user_group['user_group_weight'] ?></td>
                </tr>
                <tr>
                    <td>user_group_create_timestamp:</td>
                    <td><?php echo $user_group['user_group_create_timestamp'] ?></td>
                </tr>
                <tr>
                    <td>user_group_modified_timestamp:</td>
                    <td><?php echo $user_group['user_group_modified_timestamp'] ?></td>
                </tr>
            </table>
        </div>
    </div>
</div>
<div class="card">
    <div class="card-body">
        <h1>
            <i class="fa-solid fa-lock"></i> <?php echo __('permission') ?>
        </h1>
        <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>
                    <th></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>
                    <th></th>
                </tr>
                </tfoot>
                <tbody>


                <?php if ($toggleDisabled) { ?>
                    <div class="alert alert-warning" role="alert">
                        <?php echo __('not_allowed_to_change_due_to_group_weight') ?>
                    </div>
                <?php }

                foreach ($group_permissions as $group_permissions_data) { ?>
                    <tr>
                        <td><?php echo $group_permissions_data['permission_name'] ?> </td>
                        <td>
                            <label class="switch">
                                <input type="checkbox" class="checkbox" data-permission-uuid="<?= $group_permissions_data['permission_uuid'] ?>" data-user-group-uuid="<?= $group_permissions_data['user_group_uuid'] ?>" data-value="NA" data-api-url="/api/v1/portal-management/group-permissions/" <?php echo(($group_permissions_data['permission_value'] == 'NA') ? 'checked' : '') ?>
                                        <?php echo ($toggleDisabled) ? 'disabled' : '' ?>>
                                <span class="slider"></span>
                            </label>
                        </td>
                        <td>
                            <label class="switch">
                                <input type="checkbox" class="checkbox" data-permission-uuid="<?= $group_permissions_data['permission_uuid'] ?>" data-user-group-uuid="<?= $group_permissions_data['user_group_uuid'] ?>" data-value="RO" data-api-url="/api/v1/portal-management/group-permissions/" <?php echo(($group_permissions_data['permission_value'] == 'RO') ? 'checked' : '') ?>
                                        <?php echo ($toggleDisabled) ? 'disabled' : '' ?>>
                                <span class="slider"></span>
                            </label>
                        </td>
                        <td>
                            <label class="switch">
                                <input type="checkbox" class="checkbox" data-permission-uuid="<?= $group_permissions_data['permission_uuid'] ?>" data-user-group-uuid="<?= $group_permissions_data['user_group_uuid'] ?>" data-value="RW" data-api-url="/api/v1/portal-management/group-permissions/" <?php echo(($group_permissions_data['permission_value'] == 'RW') ? 'checked' : '') ?>
                                        <?php echo ($toggleDisabled) ? 'disabled' : '' ?>>
                                <span class="slider"></span>
                            </label>
                        </td>
                        <td>
                            <a href="#" class="btn btn-info btn-sm btn-rounded" data-bs-toggle="modal" data-bs-target="#infoModal<?php echo $group_permissions_data['permission_uuid'] ?>"><i class="fa-solid fa-circle-info"></i></a>
                        </td>
                    </tr>
                    <div class="modal fade" id="infoModal<?php echo $group_permissions_data['permission_uuid'] ?>" tabindex="-1" aria-labelledby="infoModalLabel" aria-hidden="true">
                        <div class="modal-dialog modal-dialog-centered modal-lg">
                            <div class="modal-content bg-black2">
                                <div class="modal-header">
                                    <h5 class="modal-title" id="infoModalLabel">
                                        <i class="fas fa-info-circle"></i> <?php echo __('information') ?>
                                    </h5>
                                    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
                                </div>
                                <div class="modal-body">
                                    <?php echo $group_permissions_data['permission_description'] ?>
                                </div>
                            </div>
                        </div>
                    </div>
                <?php } ?>
                </tbody>
            </table>
        </div>
    </div>
</div>