{#
 # ---------------------------------------------------------------------
 #
 # GLPI - Gestionnaire Libre de Parc Informatique
 #
 # http://glpi-project.org
 #
 # @copyright 2015-2025 Teclib' and contributors.
 # @licence   https://www.gnu.org/licenses/gpl-3.0.html
 #
 # ---------------------------------------------------------------------
 #
 # LICENSE
 #
 # This file is part of GLPI.
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
 # the Free Software Foundation, either version 3 of the License, or
 # (at your option) any later version.
 #
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.
 #
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
 #
 # ---------------------------------------------------------------------
 #}

{% set field = field|default('password') %}
{% set list_view = list_view|default(false) %}

{% if config('use_password_security') %}
    <{{ list_view ? 'ul' : 'span' }} class="password-security-checks">
       <{{ list_view ? 'li' : 'span' }} id="password_min_length" class="text-danger">{{ __('Minimum length: %s')|format(config('password_min_length')) }}</{{ list_view ? 'li' : 'span' }}>
       {% set checks = {
           password_need_number: __('Digit'),
           password_need_letter: __('Lowercase'),
           password_need_caps: __('Uppercase'),
           password_need_symbol: __('Symbol')
       } %}
        {% if config('password_need_number') or config('password_need_letter') or config('password_need_caps') or config('password_need_symbol') %}
            {% set check_fields %}
                {% if not list_view %}
                    <span>, </span>
                {% endif %}
                {% for name, label in checks|filter((v, k) => config(k)) %}
                    <{{ list_view ? 'li' : 'span' }} id="{{ name }}" class="text-danger">{{ label }}</{{ list_view ? 'li' : 'span' }}>
                {% if not loop.last and not list_view %}
                    <span>, </span>
                {% endif %}
                {% endfor %}
            {% endset %}
            {% if list_view %}
                <li>
            {% endif %}
            <{{ list_view ? 'ul' : 'span' }} >{{ __('%1$s: %2$s')|format(__('Password must contain'), check_fields|raw)|raw }}</{{ list_view ? 'ul' : 'span' }}>
            {% if list_view %}
                </li>
            {% endif %}
        {% endif %}
    </{{ list_view ? 'ul' : 'span' }}>

    <script>
        function passwordCheck() {
            const pwd = $('#{{ field }}').val();
            const meet_min_length = pwd.length >= {{ config('password_min_length') }};
            const meet_need_number = {{ config('password_need_number') }} ? !!pwd.match(/[0-9]/g) : true;
            const meet_need_letter = {{ config('password_need_letter') }} ? !!pwd.match(/[a-z]/g) : true;
            const meet_need_caps = {{ config('password_need_caps') }} ? !!pwd.match(/[A-Z]/g) : true;
            const meet_need_symbol = {{ config('password_need_symbol') }} ? !!pwd.match(/[^a-zA-Z0-9]/g) : true;

            $('#password_min_length').toggleClass('text-danger', !meet_min_length).toggleClass('text-success', meet_min_length);
            $('#password_need_number').toggleClass('text-danger', !meet_need_number).toggleClass('text-success', meet_need_number);
            $('#password_need_letter').toggleClass('text-danger', !meet_need_letter).toggleClass('text-success', meet_need_letter);
            $('#password_need_caps').toggleClass('text-danger', !meet_need_caps).toggleClass('text-success', meet_need_caps);
            $('#password_need_symbol').toggleClass('text-danger', !meet_need_symbol).toggleClass('text-success', meet_need_symbol);
        }
    </script>
{% endif %}
