<?php

declare(strict_types=1);

use Fisharebest\Webtrees\Auth;
use Fisharebest\Webtrees\Contracts\UserInterface;
use Fisharebest\Webtrees\Http\RequestHandlers\ControlPanel;
use Fisharebest\Webtrees\Http\RequestHandlers\UserEditAction;
use Fisharebest\Webtrees\Http\RequestHandlers\UserListPage;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Registry;
use Fisharebest\Webtrees\Tree;
use Fisharebest\Webtrees\View;
use Illuminate\Support\Collection;

/**
 * @var array<string,string> $contact_methods
 * @var array<string,string> $languages
 * @var array<string,string> $roles
 * @var array<string,string> $theme_options
 * @var string               $title
 * @var Collection<int,Tree> $trees
 * @var UserInterface        $user
 */

?>

<?= view('components/breadcrumbs', ['links' => [
    route(ControlPanel::class) => I18N::translate('Control panel'),
    route(UserListPage::class) => I18N::translate('User administration'),
    $title,
]]) ?>

<h1><?= $title ?></h1>

<form method="post" action="<?= e(route(UserEditAction::class)) ?>" class="form-horizontal" autocomplete="off">
    <input type="hidden" name="user_id" value="<?= $user->id() ?>">

    <!-- REAL NAME -->
    <div class="row mb-3">
        <label class="col-sm-3 col-form-label" for="real_name">
            <?= I18N::translate('Real name') ?>
        </label>
        <div class="col-sm-9">
            <input class="form-control" type="text" id="real_name" name="real_name" required="required" maxlength="64" value="<?= e($user->realName()) ?>" dir="auto">
            <div class="form-text">
                <?= I18N::translate('This is your real name, as you would like it displayed on screen.') ?>
            </div>
        </div>
    </div>

    <!-- USER NAME -->
    <div class="row mb-3">
        <label class="col-sm-3 col-form-label" for="username">
            <?= I18N::translate('Username') ?>
        </label>
        <div class="col-sm-9">
            <input class="form-control" type="text" id="username" name="username" required="required" maxlength="32" value="<?= e($user->userName()) ?>" dir="auto">
            <div class="form-text">
                <?= I18N::translate('Usernames are case-insensitive and ignore accented letters, so that “chloe”, “chloë”, and “Chloe” are considered to be the same.') ?>
            </div>
        </div>
    </div>

    <!-- PASSWORD -->
    <div class="row mb-3">
        <label class="col-sm-3 col-form-label" for="password">
            <?= I18N::translate('Password') ?>
        </label>
        <div class="col-sm-9">
            <input class="form-control" type="password" id="password" name="password" pattern = ".{8,}" placeholder="<?= I18N::plural('Use at least %s character.', 'Use at least %s characters.', 8, I18N::number(8)) ?>" <?= $user->id() ? '' : 'required' ?> autocomplete="new-password" data-wt-show-password-text="<?= e(I18N::translate('show')) ?>" data-wt-show-password-title="<?= e(I18N::translate('Show password')) ?>" data-wt-hide-password-text="<?= e(I18N::translate('hide')) ?>" data-wt-hide-password-title="<?= e(I18N::translate('Hide password')) ?>">
            <div class="form-text">
                <?= I18N::translate('Passwords must be at least 8 characters long and are case-sensitive, so that “secret” is different from “SECRET”.') ?>
            </div>
        </div>
    </div>

    <!-- EMAIL ADDRESS -->
    <div class="row mb-3">
        <label class="col-sm-3 col-form-label" for="email">
            <?= I18N::translate('Email address') ?>
        </label>
        <div class="col-sm-9">
            <input class="form-control" type="email" id="email" name="email" required="required" maxlength="64" value="<?= e($user->email()) ?>">
            <div class="form-text">
                <?= I18N::translate('This email address will be used to send password reminders, website notifications, and messages from other family members who are registered on the website.') ?>
            </div>
        </div>
    </div>

    <!-- EMAIL VERIFIED -->
    <!-- ACCOUNT APPROVED -->
    <div class="row mb-3">
        <label class="col-sm-3 col-form-label" for="verified">
            <?= I18N::translate('Account approval and email verification') ?>
        </label>
        <div class="col-sm-9">
            <div class="form-check">
                <label>
                    <input type="checkbox" name="verified" value="1" <?= $user->getPreference(UserInterface::PREF_IS_EMAIL_VERIFIED) === '1' ? 'checked' : '' ?>>
                    <?= I18N::translate('Email verified') ?>
                </label>
                <label>
                    <input type="checkbox" name="approved" value="1" <?= $user->getPreference(UserInterface::PREF_IS_ACCOUNT_APPROVED) === '1' ? 'checked' : '' ?>>
                    <?= I18N::translate('Approved by administrator') ?>
                </label>
                <div class="form-text">
                    <?= I18N::translate('When a user registers for an account, an email is sent to their email address with a verification link. When they follow this link, we know the email address is correct, and the “email verified” option is selected automatically.') ?>
                </div>
                <div class="form-text">
                    <?= I18N::translate('If an administrator creates a user account, the verification email is not sent, and the email must be verified manually.') ?>
                </div>
                <div class="form-text">
                    <?= I18N::translate('You should not approve an account unless you know that the email address is correct.') ?>
                </div>
                <div class="form-text">
                    <?= I18N::translate('A user will not be able to sign in until both “email verified” and “approved by administrator” are selected.') ?>
                </div>
            </div>
        </div>
    </div>

    <!-- LANGUAGE -->
    <div class="row mb-3">
        <label class="col-sm-3 col-form-label" for="language">
            <?= /* I18N: A configuration setting */ I18N::translate('Language') ?>
        </label>
        <div class="col-sm-9">
            <?= view('components/select', ['name' => 'language', 'selected' => $user->getPreference(UserInterface::PREF_LANGUAGE, 'en-US'), 'options' => $languages]) ?>
        </div>
    </div>

    <!-- TIMEZONE -->
    <div class="row mb-3">
        <label class="col-sm-3 col-form-label" for="timezone">
            <?= /* I18N: A configuration setting */ I18N::translate('Time zone') ?>
        </label>
        <div class="col-sm-9">
            <?= view('components/select', ['name' => 'timezone', 'selected' => $user->getPreference(UserInterface::PREF_TIME_ZONE, 'UTC'), 'options' => array_combine(DateTimeZone::listIdentifiers(), DateTimeZone::listIdentifiers())]) ?>
            <div class="form-text">
                <?= I18N::translate('The time zone is required for date calculations, such as knowing today’s date.') ?>
            </div>
        </div>
    </div>

    <!-- AUTO ACCEPT -->
    <div class="row mb-3">
        <label class="col-sm-3 col-form-label" for="auto_accept">
            <?= I18N::translate('Changes') ?>
        </label>
        <div class="col-sm-9">
            <div class="form-check">
                <label>
                    <input type="checkbox" name="auto_accept" value="1" <?= $user->getPreference(UserInterface::PREF_AUTO_ACCEPT_EDITS) === '1' ? 'checked' : '' ?>>
                    <?= I18N::translate('Automatically accept changes made by this user') ?>
                </label>
                <div class="form-text">
                    <?= I18N::translate('Normally, any changes made to a family tree need to be reviewed by a moderator. This option allows a user to make changes without needing a moderator.') ?>
                </div>
            </div>
        </div>
    </div>

    <!-- VISIBLE ONLINE -->
    <div class="row mb-3">
        <label class="col-sm-3 col-form-label" for="visible-online">
            <?= /* I18N: A configuration setting */ I18N::translate('Visible online') ?>
        </label>
        <div class="col-sm-9">
            <div class="form-check">
                <label>
                    <input type="checkbox" id="visible-online" name="visible-online" value="1" <?= $user->getPreference(UserInterface::PREF_IS_VISIBLE_ONLINE) === '1' ? 'checked' : '' ?>>
                    <?= /* I18N: A configuration setting */ I18N::translate('Visible to other users when online') ?>
                </label>
                <div class="form-text">
                    <?= I18N::translate('You can choose whether to appear in the list of users who are currently signed-in.') ?>
                </div>
            </div>
        </div>
    </div>

    <!-- CONTACT METHOD -->
    <div class="row mb-3">
        <label class="col-sm-3 col-form-label" for="contact-method">
            <?= /* I18N: A configuration setting */ I18N::translate('Preferred contact method') ?>
        </label>
        <div class="col-sm-9">
            <?= view('components/select', ['id' => 'contact-method', 'name' => 'contact-method', 'selected' => $user->getPreference(UserInterface::PREF_CONTACT_METHOD), 'options' => $contact_methods]) ?>
            <div class="form-text">
                <?= /* I18N: Help text for the “Preferred contact method” configuration setting */
                I18N::translate('Site members can send each other messages. You can choose to how these messages are sent to you, or choose not receive them at all.') ?>
            </div>
        </div>
    </div>

    <!-- THEME -->
    <div class="row mb-3">
        <label class="col-sm-3 col-form-label" for="theme">
            <?= I18N::translate('Theme') ?>
        </label>
        <div class="col-sm-9">
            <?= view('components/select', ['name' => 'theme', 'selected' => $user->getPreference(UserInterface::PREF_THEME), 'options' => $theme_options]) ?>
        </div>
    </div>

    <!-- COMMENTS -->
    <div class="row mb-3">
        <label class="col-sm-3 col-form-label" for="comment">
            <?= I18N::translate('Administrator comments on user') ?>
        </label>
        <div class="col-sm-9">
            <textarea class="form-control" id="comment" name="comment" rows="4" dir="auto" maxlength="255"><?= e($user->getPreference(UserInterface::PREF_NEW_ACCOUNT_COMMENT)) ?></textarea>
        </div>
    </div>

    <!-- ADMINISTRATOR -->
    <div class="row mb-3">
        <label class="col-sm-3 col-form-label" for="admin">
        </label>
        <div class="col-sm-9">
            <div class="form-check">
                <label>
                    <input type="checkbox" id="admin" name="canadmin" value="1" <?= $user->getPreference(UserInterface::PREF_IS_ADMINISTRATOR) === '1' ? 'checked="checked"' : '' ?>  <?= $user->id() === Auth::id() ? 'disabled="disabled"' : '' ?>>
                    <?= I18N::translate('Administrator') ?>
                </label>
            </div>
        </div>
    </div>

    <h3><?= I18N::translate('Access to family trees') ?></h3>

    <p>
        <?= I18N::translate('A role is a set of access rights, which give permission to view data, change preferences, etc. Access rights are assigned to roles, and roles are granted to users. Each family tree can assign different access to each role, and users can have a different role in each family tree.') ?>
    </p>

    <div class="row">
        <div class="col-sm-4">
            <h4>
                <?= I18N::translate('Visitor') ?>
            </h4>
            <div class="form-text">
                <?= I18N::translate('Everybody has this role, including visitors to the website and search engines.') ?>
            </div>
            <h4>
                <?= I18N::translate('Member') ?>
            </h4>
            <div class="form-text">
                <?= I18N::translate('This role has all the permissions of the visitor role, plus any additional access granted by the family tree configuration.') ?>
            </div>
        </div>
        <div class="col-sm-4">
            <h4>
                <?= I18N::translate('Editor') ?>
            </h4>
            <div class="form-text">
                <?= I18N::translate('This role has all the permissions of the member role, plus permission to add/change/delete data. Any changes will need to be reviewed by a moderator, unless the user has the “automatically accept changes” option enabled.') ?>
            </div>
            <h4>
                <?= I18N::translate('Moderator') ?>
            </h4>
            <div class="form-text">
                <?= I18N::translate('This role has all the permissions of the editor role, plus permission to accept/reject changes made by other users.') ?>
            </div>
        </div>
        <div class="col-sm-4">
            <h4>
                <?= I18N::translate('Manager') ?>
            </h4>
            <div class="form-text">
                <?= I18N::translate('This role has all the permissions of the moderator role, plus any additional access granted by the family tree configuration, plus permission to change the settings/configuration of a family tree.') ?>
            </div>
            <h4>
                <?= I18N::translate('Administrator') ?>
            </h4>
            <div class="form-text">
                <?= I18N::translate('This role has all the permissions of the manager role in all family trees, plus permission to change the settings/configuration of the website, users, and modules.') ?>
            </div>
        </div>
    </div>

    <table class="table table-bordered table-sm">
        <thead>
            <tr>
                <th>
                    <?= I18N::translate('Family tree') ?>
                </th>
                <th>
                    <?= I18N::translate('Role') ?>
                </th>
                <th>
                    <?= I18N::translate('Individual record') ?>
                </th>
                <th>
                    <?= I18N::translate('Restrict to immediate family') ?>
                    <?= view('help/link', ['topic' => 'relationship-privacy']) ?>
                </th>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                </td>
                <td>
                    <div class="form-text">
                        <?= I18N::translate('Link this user to an individual in the family tree.') ?>
                    </div>
                </td>
                <td>
                </td>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($trees as $tree) : ?>
                <tr>
                    <td>
                        <?= e($tree->title()) ?>
                    </td>
                    <td>
                        <select class="form-select" name="canedit<?= $tree->id() ?>">
                            <?php foreach ($roles as $role => $description) : ?>
                                <option value="<?= $role ?>"
                                    <?= $role === $tree->getUserPreference($user, UserInterface::PREF_TREE_ROLE) ? 'selected' : '' ?>>
                                    <?= $description ?>
                                </option>
                            <?php endforeach ?>
                        </select>
                    </td>
                    <td>
                        <?= view('components/select-individual', ['name' => 'gedcomid' . $tree->id(), 'individual' => Registry::individualFactory()->make($tree->getUserPreference($user, 'gedcomid'), $tree), 'tree' => $tree]) ?>
                    </td>
                    <td>
                        <select class="form-select" name="RELATIONSHIP_PATH_LENGTH<?= $tree->id() ?>" id="RELATIONSHIP_PATH_LENGTH<?= $tree->id() ?>" class="relpath">
                            <?php for ($n = 0; $n <= 10; ++$n) : ?>
                                <option value="<?= $n ?>" <?= (int) $tree->getUserPreference($user, UserInterface::PREF_TREE_PATH_LENGTH) === $n ? 'selected' : '' ?>>
                                    <?= $n ?: I18N::translate('No') ?>
                                </option>
                            <?php endfor ?>
                        </select>
                    </td>
                </tr>
            <?php endforeach ?>
        </tbody>
    </table>

    <div class="row mb-3">
        <div class="offset-sm-3 col-sm-9">
            <button type="submit" class="btn btn-primary">
                <?= I18N::translate('save') ?>
            </button>
        </div>
    </div>

    <?= csrf_field() ?>
</form>

<?= view('modals/ajax') ?>

<?php View::push('javascript') ?>
<script>
    $(".relpath").change(function () {
        var fieldIDx = $(this).attr("id");
        var idNum    = fieldIDx.replace("RELATIONSHIP_PATH_LENGTH", "");
        var newIDx   = "gedcomid" + idNum;
        if ($("#" + newIDx).val() === "" && $("#".fieldIDx).val() !== "0") {
            alert("<?= I18N::translate('You must specify an individual record before you can restrict the user to their immediate family.') ?>");
            $(this).val("0");
        }
    });
</script>
<?php View::endpush() ?>
