let BSValidator = {
    handleSubmit:(event)=>{
        // event.preventDefault();
        let send = true;

        let inputs = form.querySelectorAll('input');

        BSValidator.clearErrors();

        for(let i=0;i<inputs.length;i++) {
            let input = inputs[i];
            let check = BSValidator.checkInput(input);
            if(check !== true) {
                send = false;
                BSValidator.showError(input, check);
            }
        }

        if(send) {
            return true;
        }
    },
    checkInput:(input) => {
        let rules = input.getAttribute('data-rules');

        if(rules !== null) {
            rules = rules.split('|');
            for(let k in rules) {
                let rDetails = rules[k].split('=');
                switch(rDetails[0]) {
                    case 'required':
                        if(input.value == '') {
                            return 'Campo não pode ser vazio!';
                        }
                    break;
                    case 'min':
                        if(input.value.length < rDetails[1]) {
                            return 'Campo tem que ter pelo menos '+rDetails[1]+' caractes!';
                        }
                    break;
                    case 'email':
                        if(input.value != '') {
                            let regex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
                            if(!regex.test(input.value.toLowerCase())) {
                                return 'E-mail digitado não é válido!';
                            }
                        }
                    break;
                    case 'phonecel':
                        if(input.value != '') {
                            if(input.value.length < 15) {
                                return 'Telefone Celular incorreto!';
                            }
                        }                        
                    break;
                    case 'phonefix':
                        if(input.value != '') {
                            if(input.value.length < 14) {
                                return 'Telefone Fixo incorreto!';
                            }    
                        }                        
                    break;
                    case 'cpf':
                        if(input.value != '') {
                            if(input.value.length != 14) {
                                return 'CPF incorreto!';
                            }
                        }                        
                    break;
                    case 'cnpj':
                        if(input.value != '') {
                            if(input.value.length != 18) {
                                return 'CNPJ incorreto!';
                            }
                        }                        
                    break;
                }
            }
        }

        return true;
    },
    showError:(input, error) => {
        input.style.borderColor = '#FF0000';

        let errorElement = document.createElement('div');
        errorElement.classList.add('error');
        errorElement.innerHTML = error;

        input.parentElement.insertBefore(errorElement, input.ElementSibling);
    },
    clearErrors:() => {
        let inputs = form.querySelectorAll('input');
        for(let i=0;i<inputs.length;i++) {
            inputs[i].style = '';
        }

        let errorElements = document.querySelectorAll('.error');
        for(let i=0;i<errorElements.length;i++) {
            errorElements[i].remove();
        }
    }
};

let form = document.querySelector('#form_validator');
form.addEventListener('button[type="submit"], input[type="submit"]', BSValidator.handleSubmit);