[% USE raw %]
[% USE Koha %]
[% USE Categories %]
[% USE AdditionalContents %]
[% PROCESS 'i18n.inc' %]
[% PROCESS 'html_helpers.inc' %]
[% INCLUDE 'doc-head-open.inc' %]
<title
    >[% FILTER collapse %]
        [% t("Reset your password") | html %]
        &rsaquo; [% PROCESS 'library_name_title' %]
    [% END %]</title
>
[% INCLUDE 'doc-head-close.inc' %]
[% BLOCK cssinclude %]
[% END %]
</head>
[% INCLUDE 'bodytag.inc' bodyid='opac-login-page' bodyclass='scrollto' %]
[% INCLUDE 'masthead.inc' %]

<main class="main">
    [% WRAPPER breadcrumbs %]
        [% WRAPPER breadcrumb_item bc_active= 1 %]
            <span>Reset your password</span>
        [% END %]
    [% END #/ WRAPPER breadcrumbs %]

    <div class="container-fluid">
        <div class="row justify-content-center">
            <div class="col-md-10 col-lg-6">
                <div id="opac-auth" class="maincontent">
                    [% IF Koha.Preference( 'opacuserlogin' ) && Koha.Preference('EnableExpiredPasswordReset') %]
                        [% IF ( error ) %]
                            <div class="alert alert-warning">
                                <h2>There was a problem with your submission</h2>
                                <p>
                                    [% SWITCH error %]
                                    [% CASE  'no_change' %]
                                        <span>New password must not be the same as old password.</span>
                                    [% CASE  'passwords_mismatch' %]
                                        <span>Passwords do not match. Please re-type your new password.</span>
                                    [% CASE 'password_too_short' %]
                                        <span>Password must be at least [% minPasswordLength | html %] characters long.</span>
                                    [% CASE 'password_too_weak' %]
                                        <span>Password must contain at least one digit, one lowercase and one uppercase.</span>
                                    [% CASE 'password_has_whitespaces' %]
                                        <span>Password must not contain leading or trailing whitespaces.</span>
                                    [% CASE 'invalid_credentials' %]
                                        <span
                                            >You entered an incorrect username or password. Please try again! But note that passwords are case
                                            sensitive[% IF Koha.Preference('FailedLoginAttempts') %]and that your account will be locked out after a fixed number of failed login attempts[% END %]. Please contact a library staff member if
                                            you continue to have problems.</span
                                        >
                                    [% CASE 'no_expire' %]
                                        <span>Please log-in to account to update your password.</span><br />
                                        <a href="/cgi-bin/koha/opac-user.pl" class="nav-link login-link loginModal-trigger"
                                            ><i class="fa fa-user fa-icon-black fa-fw" aria-hidden="true"></i> <span class="userlabel">Log in to your account</span></a
                                        >.
                                    [% CASE 'account_locked' %]
                                        This account has been locked!
                                        [% IF Categories.can_any_reset_password && Koha.Preference('OpacBaseURL') %]
                                            <a href="[% Koha.Preference('OpacBaseURL') | url %]/cgi-bin/koha/opac-password-recovery.pl">You must reset your password via e-mail</a>.
                                        [% ELSE %]
                                            <span>You must contact the library for assistance.</span>
                                        [% END %]
                                    [% CASE %]
                                        <span>An unknown error occurred. Please try again or contact the library for assistance.</span>
                                    [% END %]
                                </p>
                            </div>
                        [% END %]
                        [% IF ( password_updated ) %]
                            <div class="alert dialog-alert">
                                <h2>Your password has successfully been updated</h2>
                                <a href="/cgi-bin/koha/opac-main.pl" class="btn btn-primary">Go to OPAC</a>
                                [% IF ( Koha.Preference('staffClientBaseURL') && staff_access ) %]
                                    <a href="[% Koha.Preference('staffClientBaseURL') | url %]" class="btn btn-primary">Go to staff interface</a>
                                [% END %]
                            </div>
                        [% ELSE %]
                            <form action="/cgi-bin/koha/opac-reset-password.pl" name="mainform" id="mainform" method="post" autocomplete="off">
                                [% INCLUDE 'csrf-token.inc' %]
                                <legend class="sr-only">Reset your password</legend>

                                <fieldset class="brief">
                                    <div class="form-group">
                                        [% PROCESS login_label for="userid" %]
                                        <input class="form-control" autocomplete="off" type="text" size="25" id="userid" name="userid" />
                                    </div>
                                    <div class="form-group">
                                        <label for="currentpassword">Current password:</label>
                                        <input class="form-control" autocomplete="current-password" type="password" size="25" id="currentpassword" name="currentpassword" />
                                    </div>
                                    <div class="form-group">
                                        <label for="newpassword">New password:</label>
                                        <input class="form-control" autocomplete="new-password" type="password" size="25" id="newpassword" name="newpassword" />
                                    </div>
                                    <div class="form-group">
                                        <label for="confirmpassword">Confirm new password:</label>
                                        <input class="form-control" autocomplete="new-password" type="password" size="25" id="confirmpassword" name="confirmpassword" />
                                    </div>
                                    <fieldset class="action">
                                        <input type="submit" value="Update password" class="btn btn-primary" />
                                    </fieldset>
                                    <input type="hidden" name="op" value="cud-update" />
                                </fieldset>
                                [% IF Koha.Preference('OpacPasswordChange') && Categories.can_any_reset_password %]
                                    <div id="resetpassword">
                                        <a href="/cgi-bin/koha/opac-password-recovery.pl">Forgot your password?</a>
                                    </div>
                                [% END %]
                            </form>
                        [% END # /IF Error_messages %]
                    [% ELSE %]
                        <h1>Resetting your password has not been enabled by the library.</h1>
                        [% IF Koha.Preference('OpacPasswordChange') && Categories.can_any_reset_password %]
                            <div id="resetpassword">
                                <a href="/cgi-bin/koha/opac-password-recovery.pl">Forgot your password?</a>
                            </div>
                        [% ELSE %]
                            <p>You must contact the library to reset your password</p>
                        [% END %]
                    [% END # / IF opacuserlogin %]
                </div>
                <!-- /.opac-auth -->
            </div>
            <!-- /.col-md-10 col-lg-6 -->
        </div>
        <!-- /.row -->
    </div>
    <!-- /.container-fluid -->
</main>
<!-- /.main -->

[% INCLUDE 'opac-bottom.inc' %]
[% BLOCK jsinclude %]
    [% Asset.js("lib/jquery/plugins/jquery.validate.min.js") | $raw %]
    <script>
        jQuery.validator.addMethod(
            "password_no_spaces",
            function (value, element) {
                return this.optional(element) || (!value.match(/^\s/) && !value.match(/\s$/));
            },
            _("Password contains leading and/or trailing spaces")
        );
        jQuery.validator.addMethod(
            "password_match",
            function (value, element) {
                var new_password_node = $("input[name='newpassword']:first");
                return value == $(new_password_node).val();
            },
            _("Please enter the same password as above")
        );

        $(document).ready(function () {
            $("#mainform").validate({
                rules: {
                    currentpassword: {
                        required: true,
                    },
                    newpassword: {
                        required: true,
                        password_no_spaces: true,
                    },
                    confirmpassword: {
                        required: true,
                        password_match: true,
                    },
                },
            });
        });
    </script>
[% END %]
