<?php

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/pageNavbar.php";
require_once "{$_SERVER['DOCUMENT_ROOT']}/bin/php/Classes/formBuilder.php";
# Page functions

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

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

# 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();
$user_data = false;
while ($user_group = $user_groups_data->fetch_assoc()) {
    $user_groups[] = $user_group;
    $last_weight = $user_group['user_group_weight'];
}

$user_uuid = $_SESSION['user']['user_uuid'];
$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) {
    $user_data = $result->fetch_assoc();
}

# Set breadcrumb data
$_SESSION['breadCrumbArray'][] = ['display' => __('user_profile'), 'href' => '/userprofile/'];
$_SESSION['breadCrumbArray'][] = ['display' => __('edit'), 'href' => ''];

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

if ($user_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="user_uuid" value="<?php echo $user_uuid; ?>"/>
        <input type="hidden" name="user_profile_change" value="true"/>
        <div class="card-body">

            <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 $user_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 $user_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 $user_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 $user_data['user_phone_number'] ?>"/>
                </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">
                        <?php foreach (scandir($_SERVER['DOCUMENT_ROOT'] . '/bin/locales/') as $file) {
                            if (pathinfo($file, PATHINFO_EXTENSION) === 'php') {
                                $language = str_replace('.php', '', $file); ?>
                                <option <?php echo(($user_data['user_pref_language'] == $language) ? 'selected' : '') ?> value="<?php echo $language ?>"><?php echo __($language) ?></option>
                            <?php }
                        } ?>
                    </select>
                </div>
            </div>
        </div>

        <?php $formBuilder->formFooter(); ?>
    </form>
    <?php $formBuilder->endForm(); ?><?php } else { ?>
    <p>no admin with this uuid found.</p>
<?php } ?>