<?php

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

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

# Includes Section
include_once($_SERVER['DOCUMENT_ROOT'] . '/bin/php/Classes/formBuilder.php');
include_once($_SERVER['DOCUMENT_ROOT'] . '/bin/php/Classes/pageNavbar.php');
require_once $_SERVER['DOCUMENT_ROOT'] . '/api/classes/API.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/api/classes/API_permissions.php';

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

# Page functions

# JS Scripts to load for this page
$jsScriptLoadData['form'][] = true;
$jsScriptLoadData['breadCrumbs'] = true;

# PageClasses Setup
$pageNavbar = new pageNavbar(true);
$formBuilder = new formBuilder('edit_permission', '<i class="fas fa-plus"></i>', '/accesscontrol/#permission');

# Retrieve Information for the page
$permission_uuid = $_GET['permission_edit'];
$_GET['permission_uuid'] = $permission_uuid;
$API_permissions = new API_permissions();
$_GET['builder'] = [1 => ['where' => [0 => 'permission_uuid', 1 => $permission_uuid]]];
$requiredFields = ['permission_uuid' => ['type' => 'uuid']];
$API_permissions->validateData($requiredFields);
$permission_data = $API_permissions->getPermission()[0];

# 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()) {
    array_push($system_modules, $module);
}

# Set breadcrumb data
array_push($GLOBALS['breadCrumbArray'], array('display' => __('permission'), 'href' => '/accesscontrol/#permissions'));
array_push($GLOBALS['breadCrumbArray'], array('display' => __('edit_permission'), 'href' => ''));

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

if ($permission_data) {
    $formBuilder->startForm(); ?>
    <form id="FormValidation" method="post" action="/api/v1/permissions/">
        <input type="hidden" name="_return" value="/accesscontrol/?permission_view=<?php echo $permission_uuid ?>">
        <input type="hidden" name="_method" value="PUT">
        <input type="hidden" name="permission_uuid" value="<?php echo $permission_uuid ?>">
        <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" value="<?php echo $permission_data['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" value="<?php echo $permission_data['permission_slugify'] ?>" placeholder="" disabled/>
                </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_uuid'] == $permission_data['module_uuid']) ? 'selected' : '' ?>> <?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"><?php echo $permission_data['permission_description'] ?></textarea>
                </div>
            </div>
        </div>
        <?php $formBuilder->formFooter(); ?>
    </form>
    <?php $formBuilder->endForm(); ?><?php } else {
    echo 'permission not found';
} ?>
