<?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-control-user-groups', 'RW', true)) {
    echo 'error 401 unauthorized';
    exit;
}

# Page functions

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

# PageClasses Setup
$pageNavbar = new pageNavbar(true);
$formBuilder = new formBuilder('add_user_groups', '<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 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'];
}

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

# Start page output
$pageNavbar->outPutNavbar();
$formBuilder->startForm();
?>
    <form id="FormValidation" enctype="multipart/form-data" method="post" action="/api/v1/portal-management/user-groups/">
        <div class="card-body">
            <div class="form-group form-show-validation row">
                <label for="user_group_name" class="col-lg-3 col-md-3 col-sm-4 mt-sm-2"><?php echo __('user_group_name') ?></label>
                <div class="col-lg-9 col-md-12 col-sm-10">
                    <input type="text" class="form-control" id="user_group_name" name="user_group_name" placeholder="" data-slugify="user_group_slugify" required/>
                </div>
            </div>

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

            <div class="form-group form-show-validation row">
                <label for="user_group_type" class="col-lg-3 col-md-3 col-sm-4 mt-sm-2"><?php echo __('user_group_type') ?></label>
                <div class="col-lg-9 col-md-12 col-sm-10">
                    <div class="select2-input">
                        <select id="user_group_type" name="user_group_type" class="form-control" required>
                            <option value="user" selected><?php echo __('users') ?></option>
                            <option value="admin"><?php echo __('administrators') ?></option>
                        </select>
                    </div>
                </div>
            </div>

            <div class="form-group form-show-validation row">
                <label for="user_group_slugify" class="col-lg-3 col-md-3 col-sm-4 mt-sm-2"><?php echo __('weight') ?></label>
                <div class="col-lg-9 col-md-12 col-sm-10">
                    <div class="mb-3">
                        <label>
                            <input type="number" class="form-control" name="user_group_weight" min="1" max="900" step="1" value="<?php echo $last_weight + 10 ?>">
                        </label>
                    </div>
                </div>
            </div>
        </div>
        <?php $formBuilder->formFooter(); ?>
    </form>
<?php $formBuilder->endForm(); ?>