const idTipoMatricula = document.getElementById("idTipoMatricula");
const preco = document.getElementById("preco");
const nota = document.getElementById("nota");

const btn = document.getElementById("btn-submit");
console.log("Cheguei!");

let numberRegExp = new RegExp('^[0-9-() ]+$');
let letterRegExp = new RegExp('^[a-zA-Z ]+$');
let biRegExp = new RegExp('^[a-zA-Z0-9]+$');
let emailRegExp = new RegExp('^[A-Za-z0-9._-]{3,}@[A-Za-z]{3,}[.]{1}[A-Za-z.]{2,6}$');
//document.querySelector("#idClasse").classList.add("is-invalidoooooo");

console.log('Pegar os smalls: ', document.querySelector('.form-group small'));

function precoWarning(small){
	preco.className = "form-control is-warning";
	small.style.visibility = "visible";
	small.style.color = "#ffc107";
	small.innerText = "Campo Preço é obrigatório!";
	btn.setAttribute('disabled', 'disabled');
}

function precoValid(small){
	preco.className = "form-control is-valid";
	btn.removeAttribute('disabled');
	small.style.visibility = "hidden";
}

preco.addEventListener("keyup", function(){
	let small = preco.parentElement.querySelectorAll("small")[1];
	if(preco.value === ''){
		precoWarning(small);
	} else {
		precoValid(small);
	}
});

function notaWarning(small){
	nota.className = "form-control";
	small.style.visibility = "hidden";
	btn.removeAttribute('disabled');
}

function notaValid(small){
	nota.className = "form-control is-valid";
	btn.removeAttribute('disabled');
	small.style.visibility = "hidden";
}

nota.addEventListener("keyup", function(){
	let small = nota.parentElement.querySelectorAll("small")[1];
	if(nota.value === ''){
		notaWarning(small);
	} else {
		notaValid(small);
	}
});

function idTipoMatriculaInvalid(small){
	idTipoMatricula.classList.add("select2-danger");
	idTipoMatricula.setAttribute('data-dropdown-css-class', 'select2-danger');
	small.style.visibility = "visible";
	small.style.color = "#dc3545";
	small.innerText = "Selecione um Tipo de Matrícula!";
	btn.setAttribute('disabled', 'disabled');
}

function idTipoMatriculaValid(small){
	idTipoMatricula.classList.remove("select2-danger");
	idTipoMatricula.classList.add("is-valid");
	idTipoMatricula.style.borderColor = "#ffc107";
	btn.removeAttribute('disabled');
	small.style.visibility = "hidden";
}

$('#idTipoMatricula').select2().on('change', function(){
    let small = idTipoMatricula.parentElement.querySelectorAll("small")[1];
    console.log($("#idTipoMatricula").val());
    if($("#idTipoMatricula").val() == 0){
		idTipoMatriculaInvalid(small);
    } else {
		idTipoMatriculaValid(small);
    }
});


btn.addEventListener("click", function(){
	let smallIdTipoMatricula = idTipoMatricula.parentElement.querySelectorAll("small")[1];
	let smallpreco = preco.parentElement.querySelectorAll("small")[1];
	let smallNota = nota.parentElement.querySelectorAll("small")[1];
	if($("#idTipoMatricula").val() == 0){
        idTipoMatriculaInvalid(smallIdTipoMatricula);
    }else {
		idTipoMatriculaValid(smallIdTipoMatricula);
	} 
	if(preco.value == ''){
        precoWarning(smallpreco);
    } else {
		precoValid(smallpreco);
	}
	if(nota.value == ''){
        notaWarning(smallNota);
    } else {
		notaValid(smallNota);
	}
});