$(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_clinic').on('click', function () {
        $('#clinic_name').css('background-color', '#fff');
        $('#username').css('background-color', '#fff');
        $('#password').css('background-color', '#fff');
        $('#full_name').css('background-color', '#fff');

        demo = $("#create_demo").prop("checked");


        clinic_name = $('#clinic_name').val();
        username = $('#username').val();
        
        password = $('#password').val();
        full_name = $('#full_name').val();
        email = $('#email').val();
        profarea = $('#profarea').val();

        send_directions = $('#send_directions').prop("checked");
        accept_direction = $('#accept_direction').prop("checked");
        partner_clinic = $('#partner_clinic').prop("checked");
        add_pirogov = $('#add_pirogov').prop("checked");
        balance = $('#rec_balance').val();
        false_form = false
        if (!(clinic_name)) {
            $('#clinic_name').css('background-color', '#f55');
            false_form = true;
        }
        if (!(username)) {
            $('#username').css('background-color', '#f55');
            false_form = true;
        }
        if (!(password)) {
            $('#password').css('background-color', '#f55');
            false_form = true;
        }
        if (!(full_name)) {
            $('#full_name').css('background-color', '#f55');
            full_name = true;
        }
        if (false_form == false) {
            var message = {
                demo: demo,
                clinic_name: clinic_name,
                username: username,
                password: password,
                full_name: full_name,
                profarea: profarea,
                email: email,
                send_directions: send_directions,
                accept_direction: accept_direction,
                partner_clinic: partner_clinic,
                add_pirogov: add_pirogov,
                balance: balance
            }
            $.ajax({
                type: 'POST',
                url: '/su_add_clinic',
                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) {
                        $('#clinic_name').val('');
                        $('#username').val('');
                        $('#password').val('');
                        $('#full_name').val('');
                        $('#success').slideDown(500);
                        $('#success').delay(2000).slideUp();
                    } else {
                        //$('#username').css('background-color', '#f55');
                        $('#exception').slideDown(500);
                        $('#exception').delay(2000).slideUp();
                    }
                }
            });
        }
    });

    $('#su_add_user').on('click', function () {
        $('#username').css('background-color', '#fff');
        $('#password').css('background-color', '#fff');
        $('#full_name').css('background-color', '#fff');
        username = $('#username').val();
        password = $('#password').val();
        full_name = $('#full_name').val();
        super_admin = $('#super_admin').prop("checked");
        false_form = false

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

        if (false_form == false) {
            var message = {
                username: username,
                password: password,
                full_name: full_name,
                super_admin: super_admin
            }
            $.ajax({
                type: 'POST',
                url: '/su_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) {
                        $('#clinic_name').val('');
                        $('#username').val('');
                        $('#password').val('');
                        $('#full_name').val('');
                        $('#success').slideDown(500);
                        $('#success').delay(2000).slideUp();
                    } else {
                        $('#username').css('background-color', '#f55');
                        $('#exception').slideDown(500);
                        $('#exception').delay(2000).slideUp();
                    }
                }
            });
        }
    });

    $('#su_add_clinic_user').on('click', function () {
        $('#username').css('background-color', '#fff');
        $('#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();
        clinic_id = $('#clinic_id').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,
                clinic_id:clinic_id
            }
            $.each($('.area_check'), function(e) {
                if($(this).prop('checked') == true)
                    message['area'] = $(this).data('area_id');
            });

            $.ajax({
                type:'POST',
                url: '/su_add_clinic_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();
        }
    });
    $("#create_demo").on('click', function () {

        let send_directions = $("#send_directions");
        let accept_direction = $("#accept_direction");
        let partner_clinic = $("#partner_clinic");

        if ($("#create_demo")[0].checked == true) {
            partner_clinic[0].checked = false
            send_directions[0].checked = false
            accept_direction[0].checked = false

            partner_clinic[0].disabled = true
            send_directions[0].disabled = true
            accept_direction[0].disabled = true
            $('#remaining').attr("style", "display: block")
        } else {
            partner_clinic[0].disabled = false
            send_directions[0].disabled = false
            accept_direction[0].disabled = false
            $('#remaining').attr("style", "display: none")
        }




    })

});