$(document).ready(function () {

   $('.userpassword').click(function(){
        $('#password-modal').modal('toggle');
        $('#target_user_id').val($(this).data('id'));
        $('#returned_username').html('Имя пользователя: ' + $(this).data('username'));
        $('#get_password').prop('disabled', false);
   });

   $('#password-modal').on('hidden.bs.modal', function () {
        $('#returned_password').html();
        $('#password-input-redspan').hide();
        $('#selfpasswordinput').show();
        $('#returned_password').hide();
        $('#returned_username').hide();
        $('#new_pwd_form').hide();
        $('#change_password').hide();
    });


   $('#change_password').click(function(){
        $('#password-input-redspan').hide();
        var message = {
            id: $('#target_user_id').val(),
            password: $('#selfpassword').val(),
            new_password: $('#new_password').val()
        }
        $.ajax({
            type:'POST',
            url: '/change_userpassword',
            async: true,
            processData: false,  // tell jQuery not to process the data
            contentType: false,  // tell jQuery not to set contentType
            data: JSON.stringify(message),
            success: (data) => {
                if(data['password'])
                {
                    $('#selfpasswordinput').hide();
                    $('#returned_password').show();
                    $('#returned_username').show();
                    $('#new_pwd_form').show();
                    $('#change_password').show();
                    $('#returned_password').html('Пароль: ' + data['password']);
                    $('#get_password').prop('disabled', true);
                }
                else
                {
                    $('#password-input-redspan').show();
                    $('#password-input-redspan').html('Ошибка доступа, проверьте правильность ввода');
                }
            },
            error:function (jqXHR, exception) {

            }
        });
   });

   $('#get_password').click(function(){
        $('#password-input-redspan').hide();
        var message = {
            id: $('#target_user_id').val(),
            password: $('#selfpassword').val()
        }
        $.ajax({
            type:'POST',
            url: '/get_userpassword',
            async: true,
            processData: false,  // tell jQuery not to process the data
            contentType: false,  // tell jQuery not to set contentType
            data: JSON.stringify(message),
            success: (data) => {
                if(data['password'])
                {
                    $('#selfpasswordinput').hide();
                    $('#returned_password').show();
                    $('#returned_username').show();
                    $('#new_pwd_form').show();
                    $('#change_password').show();
                    $('#returned_password').html('Пароль: ' + data['password']);
                    $('#get_password').prop('disabled', true);
                }
                else
                {
                    $('#password-input-redspan').show();
                    $('#password-input-redspan').html('Ошибка доступа, проверьте правильность ввода');
                }
            },
            error:function (jqXHR, exception) {

            }
        });
   });

});
