{% extends request.user.is_admin|yesno:"base.html,base-ui.html" %}
{% load widget_tweaks %}
{% load i18n %}{% block body %}
    <div class="d-flex flex-column-fluid">
        <!--begin::Container-->
        <div class="container-fluid">
            <div class="row">
                <div class="col-md-12">
                    <!--begin::Card-->
                    <div class="card card-custom">
                        <div class="card-header">
                            <h3 class="card-title">
                                {% trans "Change Password" %}
                            </h3>
                        </div>
                        <!--begin::Form-->
                        <form class="form" id="form">
                            <div class="card-body">
                                <div class="row">
                                    {% if not dont_ask_password %}
                                        <div class="col-md-4">
                                            <div class="form-group">
                                                <label class="col-form-label">{% trans "Current Password" %}</label>
                                                <input required type="password" name="old_password" id="id_old_password"
                                                       class="form-control">
                                            </div>
                                        </div>
                                    {% else %}
                                        <input type="hidden" class="d-none" name="token_id" value="{{ token_id }}">
                                        <input type="hidden" class="d-none" name="hash_code" value="{{ hash_code }}">
                                    {% endif %}
                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <label class="col-form-label">{% trans "New Password" %}</label>
                                            <input required type="password" name="new_password" id="id_new_password"
                                                   class="form-control">
                                        </div>
                                    </div>
                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <label class="col-form-label">{% trans "Repeat Password" %}</label>
                                            <input required type="password" name="repeat_password"
                                                   id="id_repeat_password" class="form-control">
                                        </div>
                                    </div>

                                </div>
                            </div>
                            <div class="card-footer">
                                <a href="{{ back }}" class="btn mx-2 btn-secondary">{% trans "Back" %}</a>
                                <button class="btn btn-primary">{% trans "Submit" %}</button>
                            </div>
                        </form>
                        <!--end::Form-->
                    </div>
                    <!--end::Card-->
                </div>
            </div>
        </div>
        <!--end::Container-->
    </div>


{% endblock %}
{% block script %}
    <script>
        const $new_password = document.getElementById('id_new_password')
        const $repeat_password = document.getElementById('id_repeat_password')
        $('#form').submit(function (e) {
            e.preventDefault()
            const button = $(this).find('button');
                if ($new_password.value === $repeat_password.value) {
                    ApiAjax({
                        success_url: '/',
                        url: {% if not dont_ask_password %}CHANGE_PASSWORD_API_URL{% else %}
                            FORGOT_PASSWORD_CHANGE_URL{% endif %},
                        form: '#form',
                        method: 'PUT',
                        {% if dont_ask_password %}
                            token: false,
                        {% endif %}
                        button: button,
                        success_message: '{% trans "Password changed successfully" %}',
                    })
                } else {
                    Swal.fire({
                        text: '{% trans "new Password and repeat password must be equal!" %}',
                        icon: "error",
                        confirmButtonText: '{% trans "Got it" %}',
                        customClass: {
                            confirmButton: "btn font-weight-bold btn-secondary"
                        }
                    }).then(function () {
                        KTUtil.scrollTop();
                    });
                }
        });
    </script>
{% endblock %}
