<?php

use api\classes\API;

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

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

# Check permissions
$API = new API();

# Page functions

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

# PageClasses Setup

# Retrieve Information for the page
$user_groups_data = $GLOBALS['conn']->query("SELECT * FROM system_user_groups ORDER BY user_group_weight ASC");
$user_groups = array();
while ($user_group = $user_groups_data->fetch_assoc()) {
    $user_groups[] = $user_group;
    $last_weight = $user_group['user_group_weight'];
}

# get all the admins
$stmt = $GLOBALS['conn']->query("SELECT * FROM system_users INNER JOIN system_user_groups ON system_users.user_group_uuid = system_user_groups.user_group_uuid WHERE user_group_type = 'admin'");
$administrators = array();
while ($row = $stmt->fetch_assoc()) {
    $administrators[] = $row;
}


$stmt = $GLOBALS['conn']->query("SELECT * FROM system_permissions INNER JOIN system_modules ON system_permissions.module_uuid = system_modules.module_uuid WHERE system_modules.module_enabled = 1");
$permissions = array();
while ($row = $stmt->fetch_assoc()) {
    $permissions[] = $row;
}

# Set breadcrumb data

# Start page output
?>
<div class="card-body activeTabOnRefresh">
    <div class="row">
        <div class="col-md-1">
            <div class="nav flex-column nav-pills nav-secondary nav-pills-no-bd nav-pills-icons" id="v-pills-tab-with-icon" role="tablist" aria-orientation="vertical">
                <?php if ($API->checkPermissions('admin-access-admins', 'RO', true)) { ?>
                    <a class="nav-link active" id="administrators-tab" data-bs-toggle="pill" href="#administrators" role="tab" aria-controls="administrators" aria-selected="true">
                        <i class="fa-solid fa-user-tie"></i><?php echo __('administrators'); ?>
                    </a>
                <?php } ?>
                <?php if ($API->checkPermissions('admin-access-control-permissions', 'RO', true)) { ?>
                    <a class="nav-link" id="permissions-tab" data-bs-toggle="pill" href="#permissions" role="tab" aria-controls="permissions">
                        <i class="fa-solid fa-lock"></i><?php echo __('permission'); ?>
                    </a>
                <?php } ?>
                <?php if ($API->checkPermissions('admin-access-control-user-groups', 'RO', true)) { ?>
                    <a class="nav-link" id="user-groups-tab" data-bs-toggle="pill" href="#user-groups" role="tab" aria-controls="user-groups">
                        <i class="fa-solid fa-user-group"></i><?php echo __('user_groups'); ?>
                    </a>
                <?php } ?>

            </div>

        </div>
        <div class="col-md-11">
            <div class="tab-content" id="v-pills-with-icon-tabContent">
                <?php if ($API->checkPermissions('admin-access-admins', 'RO', true)) { ?>
                    <div class="tab-pane fade show active" id="administrators" role="tabpanel" aria-labelledby="administrators-tab">
                        <div class="row mb-3">
                            <div class="col-5">
                                <h2>
                                    <i class="fa-solid fa-user-tie"></i> &nbsp; <?php echo __('administrators') ?>
                                </h2>
                            </div>
                            <div class="col d-flex justify-content-end">
                                <?php if ($API->checkPermissions('admin-access-admins', 'RW', true)) { ?>
                                    <a href="?admin_add=">
                                        <button class="btn btn-primary">
                                            <i class="fas fa-plus text-success"></i> &nbsp; <?php echo __('add_admin') ?>
                                        </button>
                                    </a>
                                <?php } ?>
                            </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">
                                        <thead>
                                        <tr>
                                            <th><?php echo __('avatar') ?></th>
                                            <th><?php echo __('full_name') ?></th>
                                            <th><?php echo __('user_email') ?></th>
                                            <th><?php echo __('user_state') ?></th>
                                            <th><?php echo __('last_login') ?></th>
                                            <th><?php echo __('user_group_name') ?></th>
                                            <th><?php echo __('actions') ?></th>
                                        </tr>
                                        </thead>
                                        <tfoot>
                                        <tr>
                                            <th><?php echo __('avatar') ?></th>
                                            <th><?php echo __('full_name') ?></th>
                                            <th><?php echo __('user_email') ?></th>
                                            <th><?php echo __('user_state') ?></th>
                                            <th><?php echo __('last_login') ?></th>
                                            <th><?php echo __('user_group_name') ?></th>
                                            <th><?php echo __('actions') ?></th>
                                        </tr>
                                        </tfoot>
                                        <tbody>
                                        <?php foreach ($administrators as $administrator) {
                                            if ($administrator['user_email'] != 'superuser') { ?>
                                                <tr data-item-id="<?php echo $administrator['user_uuid']; ?>">
                                                    <td class="text-nowrap">
                                                        <div class="avatar-sm ">
                                                            <img class="avatar-img rounded-circle" src="data:image/png;base64,<?php echo str_replace("'", '', $administrator['user_profile_picture']) ?>" height="50px" alt="">
                                                        </div>
                                                    </td>
                                                    <td class="text-nowrap"><?php echo $administrator['user_full_name'] ?></td>
                                                    <td class="text-nowrap"><?php echo $administrator['user_email'] ?></td>
                                                    <td class="text-nowrap"><?php echo $administrator['user_status'] ?></td>
                                                    <td class="text-nowrap"><?php showTime($administrator['user_last_login_timestamp']); ?></td>
                                                    <td class="text-nowrap"><?php echo $administrator['user_group_name'] ?></td>
                                                    <td class="text-nowrap">
                                                        <?php if ($API->checkPermissions('admin-access-admins', 'RW', true)) { ?>
                                                            <a href="?admin_edit=<?php echo $administrator['user_uuid'] ?>" class="btn btn-primary btn-sm btn-rounded"><i class="fas fa-edit"></i></a>
                                                        <?php } ?>
                                                        <?php if ($API->checkPermissions('admin-access-admins', 'RO', true)) { ?>
                                                            <a href="?admin_view=<?php echo $administrator['user_uuid'] ?>" class="btn btn-info btn-sm btn-rounded"><i class="far fa-eye"></i></a>
                                                        <?php } ?>
                                                        <?php if ($API->checkPermissions('admin-access-admins', 'RW', true)) { ?>
                                                            <a href="#" class="btn btn-danger btn-sm btn-rounded delete-btn" data-item-uuid="<?php echo $administrator['user_uuid'] ?>" data-api-url="/api/v1/portal-management/users/" data-item-name="user_uuid"><i class="fas fa-trash-alt"></i></a>
                                                        <?php } ?>
                                                    </td>
                                                </tr>
                                            <?php }
                                        } ?>
                                        </tbody>
                                    </table>
                                    <br>
                                </div>
                            </div>
                        </div>
                    </div>
                <?php } ?>
                <?php if ($API->checkPermissions('admin-access-control-permissions', 'RO', true)) { ?>
                    <div class="tab-pane fade show active" id="permissions" role="tabpanel" aria-labelledby="permissions-tab">
                        <div class="row mb-3">
                            <div class="col-6">
                                <h2>
                                    <i class="fa-solid fa-lock"></i> &nbsp; <?php echo __('permission') ?>
                                </h2>
                            </div>
                            <div class="col d-flex justify-content-end">
                                <?php if ($API->isSuperuser()) { ?>
                                    <a href="?permission_add=">
                                        <button class="btn btn-primary">
                                            <i class="fas fa-plus text-success"></i> &nbsp; <?php echo __('add_permission') ?>
                                        </button>
                                    </a>
                                <?php } ?>
                            </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="2,3">
                                        <thead>
                                        <tr>
                                            <th><?php echo __('name') ?></th>
                                            <th><?php echo __('module') ?></th>
                                            <th><?php echo __('description') ?></th>
                                            <th><?php echo __('actions') ?></th>
                                        </tr>
                                        </thead>
                                        <tfoot>
                                        <tr>
                                            <th><?php echo __('name') ?></th>
                                            <th><?php echo __('module') ?></th>
                                            <th><?php echo __('description') ?></th>
                                            <th><?php echo __('actions') ?></th>
                                        </tr>
                                        </tfoot>
                                        <tbody>
                                        <?php foreach ($permissions as $key => $permission) { ?>
                                            <tr data-item-id="<?php echo $permission['permission_uuid']; ?>">
                                                <td class="text-nowrap"><?php echo $permission['permission_name'] ?></td>
                                                <td class="text-nowrap"><?php echo __($permission['module_slugify']) ?></td>
                                                <td class=""><?php echo $permission['permission_description'] ?></td>
                                                <td class="text-nowrap">

                                                    <?php if ($API->isSuperuser()) { ?>
                                                        <a href="?permission_edit=<?php echo $permission['permission_uuid'] ?>" class="btn btn-primary btn-sm btn-rounded"><i class="fas fa-edit"></i></a>
                                                    <?php } ?>

                                                    <a href="?permission_view=<?php echo $permission['permission_uuid'] ?>" class="btn btn-info btn-sm btn-rounded"><i class="far fa-eye"></i></a>
                                                    <?php if ($API->isSuperuser()) { ?>
                                                        <a href="#" class="btn btn-danger btn-sm btn-rounded delete-btn" data-item-uuid="<?php echo $permission['permission_uuid'] ?>" data-item-name="permission_uuid" data-api-url="/api/v1/portal-management/permissions/"><i class="fas fa-trash-alt"></i></a>
                                                    <?php } ?>
                                                </td>
                                            </tr>
                                        <?php } ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                <?php } ?>
                <?php if ($API->checkPermissions('admin-access-control-user-groups', 'RO', true)) { ?>
                    <div class="tab-pane fade" id="user-groups" role="tabpanel" aria-labelledby="user-groups-tab">
                        <div class="row mb-3">
                            <div class="col-6">
                                <h2>
                                    <i class="fa-solid fa-user-group"></i> &nbsp; <?php echo __('user_groups') ?>
                                </h2>
                            </div>
                            <?php if ($API->checkPermissions('admin-access-control-user-groups', 'RW', true)) { ?>
                                <div class="col d-flex justify-content-end">
                                    <a href="?user_group_add=">
                                        <button class="btn btn-primary">
                                            <i class="fas fa-plus text-success"></i> &nbsp; <?php echo __('add_user_groups') ?>
                                        </button>
                                    </a>
                                </div>
                            <?php } ?>
                        </div>
                        <div class="card">
                            <div class="card-body">
                                <?php
                                if (count($user_groups) == 0) {
                                    echo __('no_user_groups_found');
                                } else { ?>
                                    <table class="table table-striped">
                                        <thead>
                                        <tr>
                                            <th><?php echo __('weight') ?></th>
                                            <th><?php echo __('user_group_name') ?></th>
                                            <th><?php echo __('user_group_slugify') ?></th>
                                            <th><?php echo __('user_group_type') ?></th>
                                            <th class="text-nowrap text-end"><?php echo __('actions') ?></th>
                                        </tr>
                                        </thead>
                                        <?php foreach ($user_groups as $user_group) { ?>
                                            <tr>
                                                <td>
                                                    <b><?php echo $user_group['user_group_weight']; ?></b>
                                                </td>
                                                <td><?php echo $user_group['user_group_name']; ?></td>
                                                <td><?php echo $user_group['user_group_slugify']; ?></td>
                                                <td><?php echo __($user_group['user_group_type']); ?></td>
                                                <td class="text-nowrap text-end">
                                                    <?php if ($API->checkPermissions('admin-access-control-user-groups', 'RW', true)) { ?>
                                                        <a href="?user_group_edit=<?php echo $user_group['user_group_uuid'] ?>" class="btn btn-primary btn-sm btn-rounded"><i class="fas fa-edit"></i></a>
                                                    <?php } ?>
                                                    <?php if ($API->checkPermissions('admin-access-control-user-groups', 'RO', true)) { ?>
                                                        <a href="?user_group_view=<?php echo $user_group['user_group_uuid'] ?>" class="btn btn-info btn-sm btn-rounded"><i class="far fa-eye"></i></a>
                                                    <?php } ?>
                                                    <?php if ($API->checkPermissions('admin-access-control-user-groups', 'RW', true)) { ?>
                                                        <a href="#" class="btn btn-danger btn-sm btn-rounded delete-btn" data-item-uuid="<?php echo $user_group['user_group_uuid'] ?>" data-api-url="/api/v1/portal-management/user-groups/" data-item-name="user_group_uuid"><i class="fas fa-trash-alt"></i></a>
                                                    <?php } ?>
                                                </td>
                                            </tr>
                                        <?php } ?>
                                    </table>
                                    <?php foreach ($user_groups as $user_group) {
                                        if ($API->checkPermissions('admin-access-control-user-groups', 'RO', true)) { ?>
                                            <div class="modal fade" id="infoModal<?php echo $user_group['user_group_slugify'] ?>" 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">
                                                            <br>
                                                            <table class="table table-sm table-striped-bg-black">
                                                                <tr>
                                                                    <td><?php echo __('uuid') ?>:</td>
                                                                    <td><?php echo $user_group['user_group_uuid'] ?></td>
                                                                </tr>
                                                                <tr>
                                                                    <td><?php echo __('user_group_name') ?>:</td>
                                                                    <td><?php echo $user_group['user_group_name'] ?></td>
                                                                </tr>
                                                                <tr>
                                                                    <td><?php echo __('user_group_slugify') ?>:</td>
                                                                    <td><?php echo $user_group['user_group_slugify'] ?></td>
                                                                </tr>
                                                                <tr>
                                                                    <td><?php echo __('weight') ?>:</td>
                                                                    <td><?php echo $user_group['user_group_weight'] ?></td>
                                                                </tr>
                                                                <tr>
                                                                    <td><?php echo __('create_date') ?>:</td>
                                                                    <td><?php showTime($user_group['user_group_create_timestamp']) ?></td>
                                                                </tr>
                                                                <tr>
                                                                    <td><?php echo __('last_modified_date') ?>:</td>
                                                                    <td><?php showTime($user_group['user_group_modified_timestamp']); ?></td>
                                                                </tr>
                                                            </table>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        <?php }
                                    }
                                } ?>
                            </div>
                        </div>
                    </div>
                <?php } ?>
            </div>
        </div>
    </div>
</div>
