$(document).ready(function() {
    $('#next').on('click', function() {
        false_form = false
        if (!($('#fio').val())) {
            $('#fio').css('border-color', '#f55');
            false_form = true
        } else {
            false_form = false
        }
        if (!($('#telephone').val())) {
            $('#telephone').css('border-color', '#f55');
            false_form = true
        } else {
            false_form = false
        }
        
        if (!($('#email').val())) {
            $('#email').css('border-color', '#f55');
            false_form = true
        } else {
            false_form = false
        }
        if (!($('#city').val())) {
            $('#city').css('border-color', '#f55');
            false_form = true
        } else {
            false_form = false
        }


        if (false_form == false) {
            let data = {
                fio: $('#fio').val(),
                telephone: $('#telephone').val(),
                email: $('#email').val(),
                city: $('#city').val()
            }
        
            $.ajax({
                type: "POST",
                data: JSON.stringify(data),
                async: !1,
                processData: false,  // tell jQuery not to process the data
                contentType: false,  // tell jQuery not to set contentType
                url: '/check_one_form',
                success: (data) => {
                    if (data['email_uniqe'] && data['phone_uniqe']) {
                        let one_form = document.getElementById('one_form')
                        one_form.setAttribute('style', 'display: none')
                        let tho_form = document.getElementById('tho_form')
                        tho_form.setAttribute('style', 'display: block')
                        tho_form.setAttribute('data-id_user', data['id_user'])
                        tho_form.setAttribute('data-id_clinic', data['id_clinic'])
                        tho_form.setAttribute('data-id_profarea', 0)
                    } else {
                        if (!data['email_uniqe']) {
                            $('#email').css('border-color', '#f55');
                            $('#email_row').children("p").remove();
                            var smaul = $('<p class="col-12 text-danger">Пользователь с таким E-mail уже существует<p>').appendTo($('#email_row'));
                            smaul.slideDown(500);
                        }
                        if (!data['phone_uniqe']) {
                            $('#telephone').css('border-color', '#f55');
                            $('#phone_row').children("p").remove();
                            var smaul = $('<p class="col-12 text-danger">Пользователь с таким номером телефона уже существует<p>').appendTo($('#phone_row'));
                            smaul.slideDown(500);
                            
                        }
                    }
                    
                }           
            })
        } else {
            $('#exception').html('Не все поля заполнены');
            $('#exception').slideDown(500);
            $('#exception').delay(2000).slideUp();
        }   
    })
    $('.profarea').on('click', function() {
        let cards = document.getElementsByClassName('profarea')
        for (let i=0; i < cards.length; i++) {
            cards[i].setAttribute('style', 'background-color: none !important; color: black !important')
            //cards[ind].setAttribute('style', 'background-color: none !important; color: black !important')
        }
        $(this).attr('style', 'background-color: #6e3288 !important; color: #fff !important')
        let tho_form = document.getElementById('tho_form')
        tho_form.setAttribute('data-id_profarea', $(this).attr('data-area_id'))
    })

    $('#registration').on('click', function() {
        let false_form = false
        if (!($('#login').val())) {
            $('#login').css('border-color', '#f55');
            false_form = true
        } else {
            false_form = false
        }
        if (!($('#clinic').val())) {
            $('#clinic').css('border-color', '#f55');
            false_form = true
        } else {
            false_form = false
        }

        if ($('#tho_form').attr('data-id_profarea') == 0) {
            $('#profareas').attr('style', 'border-color: #f55 !important')
            false_form = true
        } else {
            false_form = false
        }


        if (false_form == 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;
            }

            let data = {
                id_user: $('#tho_form').attr('data-id_user'),
                id_clinic: $('#tho_form').attr('data-id_clinic'),
                id_profarea: $('#tho_form').attr('data-id_profarea'), 
                login: $('#login').val(),
                clinic: $('#clinic').val(), 
                password: makePassword(8)
            }

            $.ajax({
                type: "POST",
                data: JSON.stringify(data),
                async: !1,
                processData: false,  // tell jQuery not to process the data
                contentType: false,  // tell jQuery not to set contentType
                url: '/register_user_clinic',
                success: (data) => {
                    if (data['clinic_uniqe']) {
                        $('#success').html(data['message']);
                        $('#success').slideDown(500);
                        $('#success').delay(2000).slideUp();
                        $('#final_email').text($('#final_email').text() + $('#email').val());
                        $('#tho_form').hide();
                        $('#final_form').show();
                    } else {
                        var smaul = $('<p class="col-12 text-danger">' + data['message'] + '<p>').appendTo($('#clinic_row'));
                        smaul.slideDown(500);
                    }

                    
                }
            })
        } else {
            $('#exception2').html('Не все поля заполнены');
            $('#exception2').slideDown(500);
            $('#exception2').delay(2000).slideUp();
        }
    })
})