<?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->isSuperuser()) {
    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_permission', '<i class="fas fa-plus"></i>', '/accesscontrol/#permissions');

# Retrieve Information for the page
$system_modules_data = $GLOBALS['conn']->query("SELECT * FROM system_modules WHERE module_enabled = 1");
$system_modules = array();
while ($module = $system_modules_data->fetch_assoc()) {
    $system_modules[] = $module;
}


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

# Start page output
$pageNavbar->outPutNavbar();
$formBuilder->startForm();
?>
    <form id="FormValidation" method="POST" action="/api/v1/portal-management/permissions/">
        <input type="hidden" name="_return" value="/accesscontrol/#permission">
        <div class="card-body">

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

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

            <div class="form-group form-show-validation row">
                <label for="module_uuid" class="col-lg-3 col-md-3 col-sm-4 mt-sm-2"><?php echo __('module') ?></label>
                <div class="col-lg-9 col-md-12 col-sm-10">
                    <div class="select2-input">
                        <select id="module_uuid" name="module_uuid" class="form-control" required>
                            <option value=""></option>
                            <?php
                            foreach ($system_modules as $module) { ?>
                                <option value="<?php echo $module['module_uuid'] ?>"> <?php echo $module['module_name'] ?></option>
                            <?php } ?>
                        </select>
                    </div>
                </div>
            </div>

            <div class="form-group form-show-validation row">
                <label for="permission_description" class="col-lg-3 col-md-3 col-sm-4 mt-sm-2"><?php echo __('permission_description') ?></label>
                <div class="col-lg-9 col-md-12 col-sm-10">
                    <textarea type="text" class="form-control" id="permission_description" name="permission_description" placeholder="" required rows="5"></textarea>
                </div>
            </div>
        </div>
        <?php $formBuilder->formFooter(); ?>
    </form>
<?php $formBuilder->endForm(); ?>