<!--
 ~ Copyright 2022 youngmonkeys.org
 ~ 
 ~ Licensed under the ezyplatform, Version 1.0.0 (the "License");
 ~ you may not use this file except in compliance with the License.
 ~ You may obtain a copy of the License at
 ~ 
 ~     https://youngmonkeys.org/licenses/ezyplatform-1.0.0.txt
 ~ 
 ~ Unless required by applicable law or agreed to in writing, software
 ~ distributed under the License is distributed on an "AS IS" BASIS,
 ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 ~ See the License for the specific language governing permissions and
 ~ limitations under the License.
-->

<!DOCTYPE html>
<html
        xmlns="http://www.w3.org/1999/xhtml"
        xmlns:th="http://www.thymeleaf.org"
        xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
        layout:decorate="~{ezyadmin}">

<body>
<div layout:fragment="content" class="row">
    <div class="col-md-10">
        <form id="addAdminForm" onsubmit="return false;">
            <div class="form-group row">
                <label for="username" class="col-sm-3 col-form-label">[[#{username}]] (<span class="text-danger">*</span>)</label>
                <div class="col-lg-7 col-md-6">
                    <input type="text" name="username" id="username" class="form-control" th:placeholder="#{username}">
                    <span>[[#{username_input_guide}]]</span>
                </div>
                <label id="usernameErrorLabel" class="col-lg-2 col-md-3 col-form-label text-danger d-none" for="username">
                    <i class="far fa-times-circle"></i> <span id="usernameError">[[#{invalid}]]</span>
                </label>
            </div>
            <div class="form-group row">
                <label for="email" class="col-sm-3 col-form-label">[[#{email}]] (<span class="text-danger">*</span>)</label>
                <div class="col-lg-7 col-md-6">
                    <input type="email" name="email" id="email" class="form-control" th:placeholder="#{email}">
                </div>
                <label id="emailErrorLabel" class="col-lg-2 col-md-3 col-form-label text-danger d-none" for="email">
                    <i class="far fa-times-circle"></i> <span id="emailError">[[#{invalid}]]</span>
                </label>
            </div>
            <div class="form-group row">
                <label for="displayName" class="col-sm-3 col-form-label">[[#{display_name}]]</label>
                <div class="col-lg-7 col-md-6">
                    <input type="text" name="displayName" id="displayName"
                           class="form-control" th:placeholder="#{display_name}">
                </div>
                <label id="displayNameErrorLabel" class="col-lg-2 col-md-3 col-form-label text-danger d-none" for="displayName">
                    <i class="far fa-times-circle"></i> <span id="displayNameError">[[#{invalid}]]</span>
                </label>
            </div>
            <div class="form-group row">
                <label for="phone" class="col-sm-3 col-form-label">[[#{phone}]]</label>
                <div class="col-lg-7 col-md-6">
                    <input type="tel" name="phone" id="phone"
                           class="form-control" th:placeholder="#{phone}">
                </div>
                <label id="phoneErrorLabel" class="col-lg-2 col-md-3 col-form-label text-danger d-none" for="email">
                    <i class="far fa-times-circle"></i> <span id="phoneError">[[#{invalid}]]</span>
                </label>
            </div>
            <div class="form-group row">
                <label for="jobTitle" class="col-sm-3 col-form-label">[[#{job_title}]]</label>
                <div class="col-lg-7 col-md-6">
                    <input type="text" name="jobTitle" id="jobTitle"
                           class="form-control" th:placeholder="#{job_title}">
                </div>
                <label id="jobTitleErrorLabel" class="col-lg-2 col-md-3 col-form-label text-danger d-none" for="jobTitle">
                    <i class="far fa-times-circle"></i> <span id="jobTitleError">[[#{invalid}]]</span>
                </label>
            </div>
            <div class="form-group row">
                <label for="website" class="col-sm-3 col-form-label">[[#{website}]]</label>
                <div class="col-lg-7 col-md-6">
                    <input type="text" name="website" id="website"
                           class="form-control" th:placeholder="#{website}">
                </div>
                <label id="websiteErrorLabel" class="col-lg-2 col-md-3 col-form-label text-danger d-none" for="website">
                    <i class="far fa-times-circle"></i> <span id="websiteError">[[#{invalid}]]</span>
                </label>
            </div>
            <div class="form-group row">
                <label for="username" class="col-sm-3 col-form-label">[[#{password}]] (<span class="text-danger">*</span>)</label>
                <div class="col-lg-7 col-md-6">
                    <div class="d-flex align-items-center">
                        <input type="password" name="password" id="password" class="form-control" th:placeholder="#{password}">
                        <button type="button" class="btn btn-secondary" id="showPasswordButton" tabindex="-1"
                                onclick="ezyadmin.togglePasswordVisible('password', 'showPasswordButton', 'hidePasswordButton')">
                            <i class="fas fa-eye"></i>
                        </button>
                        <button type="button" class="btn btn-secondary d-none" id="hidePasswordButton" tabindex="-1"
                                onclick="ezyadmin.togglePasswordVisible('password', 'showPasswordButton', 'hidePasswordButton')">
                            <i class="fas fa-eye-slash"></i>
                        </button>
                    </div>
                </div>
                <label id="passwordErrorLabel" class="col-lg-2 col-md-3 col-form-label text-danger d-none" for="password">
                    <i class="far fa-times-circle"></i> <span id="passwordError">[[#{invalid}]]</span>
                </label>
            </div>
            <div class="form-group row">
                <label class="col-sm-3 col-form-label">
                </label>
                <div class="col-sm-9">
                    <div class="d-flex align-items-center">
                        <button type="button" id="randomPasswordButton" class="btn btn-outline-primary pt-1 pb-1"
                                onclick="ezyadmin.onRandomPasswordClick();">
                            [[#{random}]]
                        </button>
                        <span class="ml-2" id="generatedPassword"></span>
                    </div>
                </div>
            </div>
            <div class="form-group row">
                <label for="username" class="col-sm-3 col-form-label">
                    [[#{retype_password}]] (<span class="text-danger">*</span>)
                </label>
                <div class="col-lg-7 col-md-6">
                    <div class="d-flex align-items-center">
                        <input type="password" id="retypePassword" name="retypePassword" class="form-control"
                               th:placeholder="#{retype_password}">
                        <button type="button" class="btn btn-secondary" id="showRetypePasswordButton" tabindex="-1"
                                onclick="ezyadmin.togglePasswordVisible('retypePassword', 'showRetypePasswordButton', 'hideRetypePasswordButton')">
                            <i class="fas fa-eye"></i>
                        </button>
                        <button type="button" class="btn btn-secondary d-none" id="hideRetypePasswordButton" tabindex="-1"
                                onclick="ezyadmin.togglePasswordVisible('retypePassword', 'showRetypePasswordButton', 'hideRetypePasswordButton')">
                            <i class="fas fa-eye-slash"></i>
                        </button>
                    </div>
                </div>
                <label id="retypePasswordErrorLabel" class="col-lg-2 col-md-3 col-form-label text-danger d-none" for="retypePassword">
                    <i class="far fa-times-circle"></i> [[#{not_match}]]
                </label>
            </div>
            <div class="form-group row">
                <label for="description" class="col-sm-3 col-form-label">[[#{description}]]</label>
                <div class="col-lg-7 col-md-6">
                    <textarea name="description" id="description"
                              class="form-control" th:placeholder="#{description}"></textarea>
                </div>
                <label id="descriptionErrorLabel" class="col-lg-2 col-md-3 col-form-label text-danger d-none" for="description">
                    <i class="far fa-times-circle"></i> <span id="descriptionError">[[#{invalid}]]</span>
                </label>
            </div>
            <div class="form-group row">
                <label for="roleIds" class="col-sm-3 col-form-label">[[#{roles}]] (<span class="text-danger">*</span>)</label>
                <div class="col-sm-4">
                    <select name="roleIds" id="roleIds" multiple="multiple" class="form-control" size="12">
                        <option th:each="role,iterator : ${roles}"
                                th:value="${role.id}" th:text="${(iterator.index + 1) + '. ' + role.displayName}">
                        </option>
                    </select>
                </div>
                <div class="col-sm-3">
                    <button type="button" class="btn btn-primary btn-block" data-toggle="modal" data-target="#add-new-role-modal"
                            th:if="${adminRoles.isAccessible('/api/v1/admin/role-names/add', 'POST')}">
                        [[#{add_new_role}]]
                    </button>
                </div>
                <label id="roleIdsErrorLabel" class="col-lg-2 col-md-3 col-form-label text-danger d-none" for="roleIds">
                    <i class="far fa-times-circle"></i> [[#{required}]]
                </label>
            </div>
            <div class="row">
                <div class="col-md-3"></div>
                <div class="col-md-4 col-sm-6">
                    <button type="button" class="btn btn-primary btn-block" onclick="ezyadmin.addNewAdmin();">
                        [[#{submit}]]
                    </button>
                </div>
                <div class="col-md-5"></div>
                <!-- /.col -->
            </div>
        </form>
    </div>
    <div class="col-md-2"></div>
</div>
<th:block layout:fragment="modals">
    <th:block th:replace="~{modals/add-new-role :: content}" />
</th:block>
<script layout:fragment="scripts">
// ========== random password =============
ezyadmin.onRandomPasswordClick = function() {
	var password = ezyadmin.randomPassword();
	$('#generatedPassword').text(password);
	$('#password').val(password);
	$('#retypePassword').val(password);
}
// ========== add admin =============
ezyadmin.addNewAdmin = function() {
    ezyadmin.addOrEditAdmin('add');
}

// ==================== add new role ============
ezyadmin.addNewRoleApi = {
    url: '/api/v1/admin/role-names/add'
};
</script>
<th:block layout:fragment="post-scripts">
    <th:block th:replace="~{admins/scripts :: add-edit}" />
    <th:block th:replace="~{modals/add-new-role :: scripts}" />
</th:block>
</body>
</html>
