{% extends "@UVDeskCore//Templates//layout.html.twig" %}

{% block title %}Reset Password{% endblock %}

{% block templateCSS %}
<style>
    #loginForm h1 {
        font-size: 28px;
        color: #6F6F6F;
        font-weight: 600;
        margin: 0px 0px 10px 0px;
    }

    .forgot-password-cta {
        position: absolute;
        font-size: 15px !important;
        right: 0px;
        top: 0px;
    }
</style>
{% endblock %}

{% block pageWrapper %}
    <div class="uv-large-box-plank">
		<div class="uv-large-box-rt">
			<div class="uv-center-box uv-text-center">
                <form action="" method="post" id="resetPasswordForm">
                    <div class="uv-adjacent-center">
                        <h1>Reset Password</h1>
                        <p>Enter your new password below to update your login credentials.</p>
                        
                        <div class="uv-adjacent-form">
                            <div class="uv-adjacent-element-block">
                                <label>Password</label>
                                <div class="uv-max-field">
                                    <input class="uv-field" type="password" name="password">
                                </div>
                            </div>

                            <div class="uv-adjacent-element-block">
                                <label>Confirm Password</label>
                                <div class="uv-max-field">
                                    <input class="uv-field" type="password" name="confirmPassword">
                                </div>
                            </div>

                            <button class="uv-btn">{{ 'Save Password'|trans }}</button>
                        </div>
                    </div>
                </div>
            </form>
		</div>

		<div class="uv-large-box-lt">
			<div class="uv-center-box uv-text-center">
				<a href="https://www.uvdesk.com">
                    <img alt="UVdesk" title="UVdesk" src="{{ asset('bundles/uvdeskcore/images/uvdesk-logo-symbol.svg') }}">
				</a>
            </div>
		</div>
	</div>
{% endblock %}

{% block footer %}
    {{ parent() }}
    <script type="text/javascript">
		$(function () {
            _.extend(Backbone.Validation.callbacks, {
                valid : function (view, attr, selector) {
                    var $el = view.$('[name="' + attr + '"]');
                    $el.removeClass('uv-field-error');
                    $el.parents('.uv-adjacent-element-block').find('.uv-field-message').remove();
                },
                invalid : function (view, attr, error, selector) {
                    var $el = view.$('[name="' + attr + '"]');
                    $el.addClass('uv-field-error');
                    $el.parents('.uv-adjacent-element-block').find('.uv-field-message').remove();
                    $el.parents('.uv-adjacent-element-block').append("<span class='uv-field-message'>" + error + "</span>");
                }
            });

			var ResetPasswordModel = Backbone.Model.extend({
                validation: {
                    'password': [{
                        required: true,
                        msg: '{{ "This field is mandatory"|trans }}'
                    },{
                        minLength: 8,
                        msg: '{{ "Password must contains 8 Characters"|trans }}'
                    }],
                    'confirmPassword': [{
                        required: true,
                        msg: '{{"This field is mandatory"|trans }}'
                    },{
                        equalTo: 'password',
                        msg: '{{ "The passwords does not match"|trans }}'
                    }]
                }
			});

			var ResetPasswordForm = Backbone.View.extend({
                events: {
                    'blur input': 'formChanegd',
                    'click .uv-btn': 'submit'
                },
                initialize: function () {
                    Backbone.Validation.bind(this);

					{% if error.messageKey is defined %}
                        app.appView.renderResponseAlert({'alertClass': 'danger', 'alertMessage': "{{ error.messageKey }}"})
                    {% endif %}
                },
                formChanegd: function(e) {
			    	this.model.set(Backbone.$(e.currentTarget).attr('name'), Backbone.$(e.currentTarget).val())
			    	this.model.isValid([Backbone.$(e.currentTarget).attr('name')])
			    },
				submit: function (e) {
					e.preventDefault();

					var data = this.$el.serializeObject();
                    this.model.set(data);
					if(this.model.isValid(true)){
						this.$el.submit();
					}
				}
			});

		    var view = new ResetPasswordForm({
		        el: '#resetPasswordForm',
		        model: new ResetPasswordModel()
		    });
		});
	</script>
{% endblock %}