@extends('layouts/unauthorized')

@section('title')
	@parent
	Вход на сайт
@stop

@section('js')
	<script type="text/javascript">
		runAllForms();

		$(function() {
			// Validation
			$("#login-form").validate({
				// Rules for form validation
				rules : {
					email : {
						required : true,
						email : true
					},
					password : {
						required : true,
						minlength : 3
					}
				},

				// Messages for form validation
				messages : {
					email : {
						required : 'Необходимо указать электронную почту',
						email : 'Необходимо указать корректную электронную почту'
					},
					password : {
						required : 'Необходимо указать пароль',
						minlength : 'Пароль не должен быть короче 3 символов'
					}
				},

				// Ajax form submition
				submitHandler : function(form) {
					var l = Ladda.create($('#login-form button[type="submit"]').get()[0]);
					l.start();
					$(form).ajaxSubmit({
						error : function(data) {
							l.stop();
						},
						success : function(data) {
							if (data.status == 'success') {
								$.root_.addClass('animated fadeOutUp');
								setTimeout(function(){ window.location = '/login' }, 1000);
							}
							else {
								l.stop();
								$('#login-form label').removeClass('state-error').removeClass('state-success');
								$('em.invalid').remove();
								$.each(data.errorMessages, function(index, value){
									$('#login-form input[name="'+index+'"]').parent().addClass('state-error').after('<em for="'+index+'" class="invalid">'+value+'</em>');
								});
							}
						}
					});
				},

				// Do not change code below
				errorPlacement : function(error, element) {
					error.insertAfter(element.parent());
				}
			});
		});
	</script>
@stop

@section('content')
	{{ Form::open(array('action' => 'UserController@login', 'id' => 'login-form', 'class' => 'smart-form client-form')) }}
		<header>
			Вход на сайт
		</header>

		<fieldset>

			<section>
				<label class="label">E-mail</label>
				<label class="input {{ ($errors->has('email')) ? 'state-error' : '' }}"> <i class="icon-append fa fa-envelope"></i>
					{{ Form::email('email', isset($input['email']) ? $input['email'] : null) }}
					<b class="tooltip tooltip-top-right"><i class="fa fa-user txt-color-teal"></i> Введите адрес своей электронной почты</b></label>
					{{ ($errors->has('email') ?  '<em for="email" class="invalid">'.$errors->first('email').'</em>' : '') }}
			</section>

			<section>
				<label class="label">Пароль</label>
				<label class="input {{ ($errors->has('password')) ? 'state-error' : '' }}"> <i class="icon-append fa fa-lock"></i>
					{{ Form::password('password', null) }}
					<b class="tooltip tooltip-top-right"><i class="fa fa-lock txt-color-teal"></i> Введите свой пароль</b> </label>
				{{ ($errors->has('password') ?  '<em for="password" class="invalid">'.$errors->first('password').'</em>' : '') }}
				<div class="note">
					<a href="{{ action('RemindersController@getRemind') }}">Я забыл пароль</a>
				</div>
			</section>

			<section>
				<label class="checkbox">
					<input type="checkbox" name="noremember">
					<i></i>Не запоминать меня</label>
			</section>
		</fieldset>
		<footer>
			<button type="submit" class="btn btn-primary ladda-button" data-style="zoom-out">
				Войти
			</button>
		</footer>
	{{ Form::close() }}
@stop
