const prazoPagamento = document.getElementById("prazoPagamento");
const cliente = document.getElementById("cliente");
const nomeCliente = document.getElementById("nomeCliente");
const nifCliente = document.getElementById("nifCliente");
const entregueSpan = document.getElementById("entregueSpan");
const totalSpan = document.getElementById("totalSpan");
const totalEntregue = document.getElementById("totalEntregue");
/*const nomeCompleto = document.getElementById("nomeCompleto");
const cartaConducao = document.getElementById("cartaConducao");
const bi = document.getElementById("bi");

const telefone = document.getElementById("telefone");
const telefoneSos = document.getElementById("telefoneSos");
const email = document.getElementById("email");

const grupoSanguineo = document.getElementById("grupoSanguineo");
const dataNasc = document.getElementById("dataNasc");
const endereco = document.getElementById("endereco");*/

const slidePage = document.querySelector(".slidePage");
const firstNextBtn = document.querySelector(".nextBtn");
const prevBtnSec = document.querySelector(".prev-1");
const nextBtnSec = document.querySelector(".next-1");
const prevBtnThird = document.querySelector(".prev-2");
const nextBtnThird = document.querySelector(".next-2");
const prevBtnFourth = document.querySelector(".prev-3");
const submitBtn = document.querySelector(".submit");
const progressText = document.querySelectorAll(".step p");
const progressCheck = document.querySelectorAll(".step .check");
const bullet = document.querySelectorAll(".step .bullet");

const footerPagamento = document.getElementById("footerPagamento");

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}$');

let max = 4;
let current = 1;

submitBtn.addEventListener("click", function (e) {
    e.preventDefault();
    let bool = true;
    let tabela = document.getElementById("tabelaArtigo");
    for (i = 1; i < tabela.rows.length; i++) {
        let qtdInput = tabela.rows[i].querySelector(".qtd");
        let qtdValue = parseFloat(qtdInput.value.replace(",", "."));
        if (isNaN(qtdValue) || qtdValue <= 0) {
            bool = false;
            break;
        }
    }
    if (bool) {
        entregueSpan.className = "";
        entregueSpan.innerText = "0,00 Kzs";
        totalSpan.className = "";
        totalSpan.innerText = "0,00 Kzs";
        totalEntregue.className = "";
        totalEntregue.innerText = "ENTREGUE";
        createDocumento();
    }
});

function prazoPagamentoInvalid(small) {
    prazoPagamento.classList.add("select2-danger");
    prazoPagamento.setAttribute('data-dropdown-css-class', 'select2-danger');
    small.style.visibility = "visible";
    small.style.color = "#dc3545";
    small.innerText = "Selecione um Prazo de Pagamento!";
    firstNextBtn.setAttribute('disabled', 'disabled');
}

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

$('#prazoPagamento').select2().on('change', function () {
    let small = prazoPagamento.parentElement.querySelectorAll("small")[1];
    if ($("#prazoPagamento").val() == 0) {
        prazoPagamentoInvalid(small);
    } else {
        prazoPagamentoValid(small);
    }
});

function resetForm() {
    slidePage.style.marginLeft = "0%";
    current = 1;
}