$(document).ready(function () {
    $("#gen_pass").click(function () {
        $("#password").val(makePassword(8));
        return false;
    });

    function makePassword(length) {
        var result = '';
        var characters_g = 'AEIOUYaeiouy';
        var characters_s = 'BCDFGHJKLMNPQRSTVWXZbcdfghjklmnpqrstvwxz';
        var characters_g_Length = characters_g.length;
        var characters_s_Length = characters_s.length;
        for (var i = 0; i < length; i++) {
            if (i % 2 == 0) {
                result += characters_g.charAt(Math.floor(Math.random() * characters_g_Length));
            } else {
                result += characters_s.charAt(Math.floor(Math.random() * characters_s_Length));
            }
        }
        return result;
    }

    $('#add_user').on('click', function(){
        $('#username').css('background-color', '#fff');
        $('#password').css('background-color', '#fff');
        $('#full_name').css('background-color', '#fff');
        clinic_manager = $('#clinic_manager').prop("checked");
        examiner = $('#examiner').prop("checked");
        doctor = $('#doctor').prop("checked");
        directions = $('#directions').prop("checked");
        prof_doctor = $('#prof_doctor').prop("checked");
        username = $('#username').val();
        password = $('#password').val();
        notification_email = $('#notification_email').val();
        full_name = $('#full_name').val();
        false_form = false;
        if(!(directions))
        {
            directions = false;
        }

        if(!(prof_doctor))
        {
            prof_doctor = false;
        }


        if(!(full_name))
        {
            $('#full_name').css('background-color', '#f55');
            full_name = true;
        }

        if(!(username))
        {
            $('#username').css('background-color', '#f55');
            false_form = true;
        }
         if(!(password))
        {
            $('#password').css('background-color', '#f55');
            false_form = true;
        }

        if(false_form==false)
        {
            var message = {
                clinic_manager: clinic_manager,
                examiner: examiner,
                username:username,
                password:password,
                full_name:full_name,
                directions:directions,
                doctor:doctor,
                prof_doctor:prof_doctor,
                notification_email:notification_email
            }
            $.each($('.area_check'), function(e) {
                if($(this).prop('checked') == true)
                    message['area'] = $(this).data('area_id');
            });

            $.ajax({
                type:'POST',
                url: '/ca_add_user',
                async: !1,
                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['success'] == true)
                    {
                        $('#username').val('');
                        $('#password').val('');
                        $('#success').slideDown(500);
                        $('#success').delay(2000).slideUp();
                    }
                    else
                    {
                        $('#exception').slideDown(500);
                        $('#exception').delay(2000).slideUp();
                        $('#username').css('background-color', '#f55');
                        if(data.hasOwnProperty('message'))
                        {
                            show_error(data.message,0);
                        }
                    }
                },
                error:function (jqXHR, exception) {
                    if(data.hasOwnProperty('message'))
                    {
                        show_error('Ошибка при добавлении пользователя',0);
                    }
                }
            });
        }
        else
        {
            $('#exception').slideDown(500);
            $('#exception').delay(2000).slideUp();
        }
    });

    $('#edit_user').on('click', function(){
        $('#username').css('background-color', '#fff');
        $('#password').css('background-color', '#fff');
        $('#full_name').css('background-color', '#fff');
        manager_role = $('#manager_role').prop("checked");
        admin_role = $('#admin_role').prop("checked");
        examiner = $('#examiner').prop("checked");
        doctor = $('#doctor').prop("checked");
        directions = $('#directions').prop("checked");
        prof_doctor = $('#prof_doctor').prop("checked");
        password = $('#password').val();
        full_name = $('#full_name').val();
        notification_email = $('#notification_email').val();
        user_id = $('#user_id').val();
        false_form = false;
        if(!directions)
            directions = false
        if(!(prof_doctor))
        {
            prof_doctor = false;
        }

        if(false_form==false)
        {
            var message = {
                password:password,
                full_name:full_name,
                examiner:examiner,
                manager:manager_role,
                admin: admin_role,
                user_id:user_id,
                directions:directions,
                doctor:doctor,
                prof_doctor:prof_doctor,
                notification_email:notification_email
            }
            console.log(message)
            $.each($('.area_check'), function(e) {
                if($(this).prop('checked') == true)
                    message['area'] = $(this).data('area_id');
            });

            $.ajax({
                type:'POST',
                url: '/ca_update_user',
                async: !1,
                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['success'] == true)
                    {
                        $('#username').val('');
                        $('#password').val('');
                        $('#success').slideDown(500);
                        $('#success').delay(2000).slideUp();
                    }
                    else
                    {
                        $('#exception').slideDown(500);
                        $('#exception').delay(2000).slideUp();
                        $('#username').css('background-color', '#f55');
                    }
                }
            });
        }
        else
        {
            $('#exception').slideDown(500);
            $('#exception').delay(2000).slideUp();
        }
    });

    $('#recover_user').click(function(){

        user_id = $('#user_id').val()
        var message = {
            user_id: user_id,
        }
        $.ajax({
            type:'POST',
            url: '/ca_recover_user',
            async: !1,
            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['success'] == true)
                {
                    $('#recover_user').hide();
                    $('#delete_user').show();
                }
                else
                {
                    console.log('Saving error')
                    if(data['self_delete'] == true)
                        $('#exception').html('Ошибка, действие не применимо к своему пользователю');
                    else
                        $('#exception').html('Ошибка, не удалось обновить информацию');
                    $('#exception').slideDown(500);
                    $('#exception').delay(2000).slideUp();
                }
            }
        });

    });

    $('#delete_user').click(function(){
        user_id = $('#user_id').val()
        var message = {
            user_id: user_id,
        }
        $.ajax({
            type:'POST',
            url: '/ca_delete_user',
            async: !1,
            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['success'] == true)
                {
                    $('#recover_user').show();
                    $('#delete_user').hide();
                }
                else
                {

                    console.log('Saving error')
                    if(data['self_delete'] == true)
                        $('#exception').html('Ошибка, действие не применимо к своему пользователю');
                    else
                        $('#exception').html('Ошибка, не удалось обновить информацию');
                    $('#exception').slideDown(500);
                    $('#exception').delay(2000).slideUp();
                }
            }
        });
    });


});
