<?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/Generatepassword.js'] = true;
$jsScriptLoadData['/src/js/sentri/passwordGenOnLoad.js'] = true;
$jsScriptLoadData['/src/js/sentri/passwordShowHide.js'] = true;
$jsScriptLoadData['/src/js/sentri/passwordRegen.js'] = 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('add_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();
while ($user_group = $user_groups_data->fetch_assoc()) {
    $user_groups[] = $user_group;
    $last_weight = $user_group['user_group_weight'];
}

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

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

    <form id="FormValidation" enctype="multipart/form-data" method="POST" action="/api/v1/portal-management/users/">
        <div class="card-body">
            <div class="col">
                <p><?php echo __('admin_creation_instruction') ?></p>
            </div>

            <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 value="<?php echo $user_group['user_group_uuid'] ?>"><?php echo $user_group['user_group_name'] ?></option>
                            <?php }
                        } ?>
                        <!-- Options should be dynamically generated from the database -->
                    </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" placeholder="user@example.xxx" required autofocus 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" 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" 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"/>
                </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="inactive"><?php echo __('inactive') ?></option>
                        <option value="banned"><?php echo __('banned') ?></option>
                        <option value="pending" selected><?php echo __('pending') ?></option>
                    </select>
                </div>
            </div>
        </div>
        <?php $formBuilder->formFooter(); ?>
    </form>
<?php $formBuilder->endForm(); ?>