<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
    <h4 class="modal-title" id="reset2-label">Redefinir senha</h4>
</div>
<div class="modal-body">
    <div id="reset2-msg"></div>
    <form class="row" id="reset2-form">
        <input type="hidden" class="form-control" name="email" value="{{ @redefinir.email }}">
        <input type="hidden" class="form-control" name="codigo" value="{{ @redefinir.codigo }}">
        <div class="col-md-6">
            <label>Nova senha:</label>
            <input type="password" class="form-control" name="senha" required>
        </div>
        <div class="col-md-6">
            <label>Confirme a senha:</label>
            <input type="password" class="form-control" name="senha_confirmacao" required>
        </div>
    </form>
</div>
<div class="modal-footer">
    <button type="submit" class="btn btn-success" form="reset2-form" id="reset2-btn">Redefinir</button>
</div>

<script>
$('#reset2-form').submit(function(evt) {
    evt.preventDefault();
    
    var $frm = $(this);
    var $btn = $('#reset2-btn');
    var $msg = $('#reset2-msg');
    
    $btn.prop('disabled', true);
    $msg.removeAttr('class').text('');
    
    $.post('{{ "softsat_locacao_redefinir2"|alias }}', $frm.serialize(), function(resp) {
        
        $msg.text((resp.message + '').replace('senha_confirmacao', 'Confirme a senha')).attr('class', resp.error ? 'alert alert-danger' : 'alert alert-success');
        
        if(resp.error)
            return;
        
        history.replaceState(null, window.title, '{{ "softsat_locacao_index"|alias }}');

        $frm.css('display', 'none');
        $btn.css('display', 'none');
    
        $msg.append('<br>Clique <a href="#">aqui</a> para entrar.').find('a').click(function(evt) {
            evt.preventDefault();
            
            $('#reset2').modal('hide');
            $('#login').modal('show');
            
            return false;
        });
    
        $frm.find('input').val('');
    }, 'json').fail(function() {
        alert('Falha na requisição, contate o administrador');
    }).always(function() {
        $btn.prop('disabled', false);
    });
    
    return false;
});
</script>