<?php

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

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

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

# Check permissions
$API = new API();
if (!$API->checkPermissions('admin-access-admins', 'RW', true)) {
    echo 'error 401 unauthorized';
    exit;
}

# Page functions

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

# PageClasses Setup
$pageNavbar = new pageNavbar(true);
$pageNavbar->AddHTMLButton('<button class="btn btn-danger mx-3" type="button" disabled><i class="fas fa-trash-alt"></i>' . __('delete') . '</button>');
$pageNavbar->AddHTMLButton('<a href="?admin_add="><button class="btn btn-primary"><i class="fas fa-plus text-success"></i>' . __('add_admin') . '</button></a>');
$formBuilder = new formBuilder('edit_admin', '<i class="fas fa-plus"></i>', '/accesscontrol/#user-groups');

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

$user_uuid = $_GET['admin_edit'];
$stmt = $GLOBALS['conn']->prepare("SELECT * FROM system_users INNER JOIN system_user_groups ON system_users.user_group_uuid = system_user_groups.user_group_uuid WHERE user_uuid = ?");
$stmt->bind_param("s", $user_uuid);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows == 1) {
    $admin_data = $result->fetch_assoc();
}

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

# Start page output
$pageNavbar->outPutNavbar();
if ($admin_data) {
    $formBuilder->startForm(); ?>
    <form id="FormValidation" enctype="multipart/form-data" method="post" action="/api/v1/portal-management/users/">
        <input type="hidden" name="X-HTTP-Method-Override" value="PUT">
        <input type="hidden" name="_return" value="/accesscontrol/?admin_view=<?php echo $user_uuid ?>">
        <input type="hidden" name="user_uuid" value="<?php echo $user_uuid; ?>"/>
        <div class="card-body">
            <!-- User Group -->
            <div class="form-group form-show-validation row">
                <label for="user_group_uuid" class="col-lg-3 col-md-3 col-sm-4 mt-sm-2"><?php echo __('user_group') ?></label>

                <div class="col-lg-9 col-md-12 col-sm-10">
                    <select id="user_group_uuid" name="user_group_uuid" class="form-control" required>
                        <?php foreach ($user_groups as $user_group) {
                            if ($user_group['user_group_weight'] >= $_SESSION['user']['user_group_weight']) { ?>?>
                                <option <?php echo(($admin_data['user_group_uuid'] == $user_group['user_group_uuid']) ? 'selected' : '') ?> value="<?php echo $user_group['user_group_uuid'] ?>"><?php echo $user_group['user_group_name'] ?></option>
                            <?php }
                        } ?>
                    </select>
                </div>
            </div>

            <div class="form-group form-show-validation row">
                <label for="user_email" class="col-lg-3 col-md-3 col-sm-4 mt-sm-2"><?php echo __('user_email') ?></label>
                <div class="col-lg-9 col-md-12 col-sm-10">
                    <input type="email" class="form-control" id="user_email" name="user_email" value="<?php echo $admin_data['user_email'] ?>" placeholder="user@example.xxx" required autocomplete="off"/>
                </div>
            </div>

            <div class="form-group form-show-validation row">
                <label for="user_first_name" class="col-lg-3 col-md-3 col-sm-4 mt-sm-2"><?php echo __('first_name') ?></label>
                <div class="col-lg-9 col-md-12 col-sm-10">
                    <input type="text" class="form-control" id="user_first_name" name="user_first_name" value="<?php echo $admin_data['user_first_name'] ?>" required/>
                </div>
            </div>

            <!-- Last Name -->
            <div class="form-group form-show-validation row">
                <label for="user_last_name" class="col-lg-3 col-md-3 col-sm-4 mt-sm-2"><?php echo __('last_name') ?></label>
                <div class="col-lg-9 col-md-12 col-sm-10">
                    <input type="text" class="form-control" id="user_last_name" name="user_last_name" value="<?php echo $admin_data['user_last_name'] ?>" required/>
                </div>
            </div>

            <!-- Phone Number -->
            <div class="form-group form-show-validation row">
                <label for="user_phone_number" class="col-lg-3 col-md-3 col-sm-4 mt-sm-2"><?php echo __('phone_number') ?></label>
                <div class="col-lg-9 col-md-12 col-sm-10">
                    <input type="text" class="form-control" id="user_phone_number" name="user_phone_number" placeholder="+1234542069" value="<?php echo $admin_data['user_phone_number'] ?>"/>
                </div>
            </div>

            <!-- User Status -->
            <div class="form-group form-show-validation row">
                <label for="user_status" class="col-lg-3 col-md-3 col-sm-4 mt-sm-2"><?php echo __('user_status') ?></label>
                <div class="col-lg-9 col-md-12 col-sm-10">
                    <select id="user_status" name="user_status" class="form-control" required>
                        <option value="active" <?php echo(($admin_data['user_status'] == 'active') ? 'selected' : '') ?> ><?php echo __('active') ?></option>
                        <option value="inactive" <?php echo(($admin_data['user_status'] == 'inactive') ? 'selected' : '') ?>><?php echo __('inactive') ?></option>
                        <option value="banned" <?php echo(($admin_data['user_status'] == 'banned') ? 'selected' : '') ?>><?php echo __('banned') ?></option>
                        <option value="pending" <?php echo(($admin_data['user_status'] == 'pending') ? 'selected' : '') ?>><?php echo __('pending') ?></option>
                    </select>
                </div>
            </div>

            <!-- Preferred Language -->
            <div class="form-group form-show-validation row">
                <label for="user_pref_language" class="col-lg-3 col-md-3 col-sm-4 mt-sm-2"><?php echo __('preferred_language') ?></label>
                <div class="col-lg-9 col-md-12 col-sm-10">
                    <select id="user_pref_language" name="user_pref_language" class="form-control">
                        <option value="none" selected>Browser language</option>
                        <?php foreach (scandir($_SERVER['DOCUMENT_ROOT'] . '/bin/locales/') as $file) {
                            if (pathinfo($file, PATHINFO_EXTENSION) === 'php') {
                                $language = str_replace('.php', '', $file); ?>
                                <option <?php echo(($admin_data['user_pref_language'] == $language) ? 'selected' : '') ?> value="<?php echo $language ?>"><?php echo __($language) ?></option>
                            <?php }
                        } ?>
                    </select>
                </div>
            </div>

            <?php if ($GLOBALS['modules_enabled']['office']) { ?>
                <!-- Stompbable -->
                <div class="form-group form-show-validation row">
                    <label for="user_stompable" class="col-lg-3 col-md-3 col-sm-4 mt-sm-2"><?php echo __('stompable') ?></label>
                    <div class="col-lg-9 col-md-12 col-sm-10">
                        <select id="user_stompable" name="user_stompable" class="form-control">
                            <option <?php echo(($admin_data['user_stompable'] == 0) ? 'selected' : '') ?> value="0">Disabled</option>
                            <option <?php echo(($admin_data['user_stompable'] == 1) ? 'selected' : '') ?> value="1">Enabled</option>
                        </select>
                    </div>
                </div>
            <?php } ?>
        </div>
        <?php $formBuilder->formFooter(); ?>
    </form>
    <?php $formBuilder->endForm(); ?><?php } else { ?>
    <p>no admin with this uuid found.</p>
<?php } ?>
