$(function() {
    'use strict';
    $('#tlicence').change(changeLicence);
});

function changeLicence() {
    var licence = $(this).val();
    if (licence != "") {
        var array = [];
        switch (licence) {
            case "LFM":
                var fields = $('#tlicencefield');
                fields.find('label').remove();
                fields.find('input').remove();
                fields.find('div').remove();
                fields.find('table').remove();
                fields.find('fieldset').remove();

                var label = document.createElement('label');
                label.setAttribute("for", "accountNumber");
                label.textContent = "Número de cuenta";

                var input = document.createElement('input');
                input.setAttribute("type", "text");
                input.setAttribute("class", "form-control form-control-user");
                input.setAttribute("id", "accountNumber");
                input.setAttribute("name", "accountNumber");
                input.setAttribute("placeholder", "Ingresar el número de cuenta");
                input.setAttribute("required", "required");

                var invalid = document.createElement('div');
                invalid.setAttribute("class", "invalid-feedback");
                invalid.textContent = "Favor de ingresar el número de cuenta";

                fields.append(label);
                fields.append(input);
                fields.append(invalid);

                break;
            case "LAL":
                var fields = $('#tlicencefield');
                fields.find('label').remove();
                fields.find('input').remove();
                fields.find('div').remove();
                fields.find('table').remove();
                fields.find('fieldset').remove();
                var label = document.createElement('label');
                label.setAttribute("for", "accountNumber");
                label.textContent = "Número de cuenta";

                var fieldset = document.createElement('fieldset');

                var legend = document.createElement('legend');
                legend.textContent = "Cómo desea hacer su búsqueda";
                legend.setAttribute('class', 'legendSearching');

                var br = document.createElement('br');

                var radio = document.createElement('input');
                radio.setAttribute("type", "radio");
                radio.setAttribute('id', 'acountN');
                radio.setAttribute('name', 'typeSearch');
                radio.setAttribute('value', 'acountN');
                radio.setAttribute('checked', 'checked');

                var labelRadio = document.createElement('label');
                labelRadio.setAttribute('for', 'acountN');
                labelRadio.textContent = "Simple/Varias";

                var radio2 = document.createElement('input');
                radio2.setAttribute("type", "radio");
                radio2.setAttribute('id', 'range');
                radio2.setAttribute('name', 'typeSearch');
                radio2.setAttribute('value', 'range');

                var labelRadio2 = document.createElement('label');
                labelRadio2.setAttribute('for', 'range');
                labelRadio2.textContent = "Rango";

                var radio3 = document.createElement('input');
                radio3.setAttribute("type", "radio");
                radio3.setAttribute('id', 'conc');
                radio3.setAttribute('name', 'typeSearch');
                radio3.setAttribute('value', 'conc');

                var labelRadio3 = document.createElement('label');
                labelRadio3.setAttribute('for', 'conc');
                labelRadio3.textContent = "Concesionario";

                var input = document.createElement('input');
                input.setAttribute("type", "text");
                input.setAttribute("class", "form-control form-control-user");
                input.setAttribute("id", "accountNumber");
                input.setAttribute("name", "accountNumber");
                input.setAttribute("placeholder", "Ingresar el número de cuenta");
                input.setAttribute("required", "required");

                var labelMessage = document.createElement('label');
                labelMessage.setAttribute('class', 'labelMessage');
                labelMessage.textContent = "En caso de querer buscar varías, separarlas por una coma (,)";

                var invalid = document.createElement('div');
                invalid.setAttribute("class", "invalid-feedback");
                invalid.textContent = "Favor de ingresar el número de cuenta";

                fields.append(fieldset);
                fieldset.append(legend)
                fieldset.append(radio);
                fieldset.append(labelRadio);
                fieldset.append(radio2);
                fieldset.append(labelRadio2);
                fieldset.append(radio3);
                fieldset.append(labelRadio3);
                fields.append(input);
                fields.append(labelMessage);
                fields.append(invalid);

                $('input[type=radio][name=typeSearch]').change(byType);

                break;
            case "LPO":
                var fields = $('#tlicencefield');
                fields.find('label').remove();
                fields.find('input').remove();
                fields.find('div').remove();
                fields.find('table').remove();
                fields.find('fieldset').remove();

                var label = document.createElement('label');
                label.setAttribute("for", "accountNumber");
                label.textContent = "Número de recibo oficial";

                var input = document.createElement('input');
                input.setAttribute("type", "text");
                input.setAttribute("class", "form-control form-control-user");
                input.setAttribute("id", "accountNumber");
                input.setAttribute("name", "accountNumber");
                input.setAttribute("placeholder", "Ingresar el número de recibo oficial");
                input.setAttribute("required", "required");

                var invalid = document.createElement('div');
                invalid.setAttribute("class", "invalid-feedback");
                invalid.textContent = "Favor de ingresar el número de recibo oficial";

                fields.append(label);
                fields.append(input);
                fields.append(invalid);
                break;
            case "LAM":
                var fields = $('#tlicencefield');
                fields.find('label').remove();
                fields.find('input').remove();
                fields.find('div').remove();
                fields.find('table').remove();
                fields.find('fieldset').remove();

                var label = document.createElement('label');
                label.setAttribute("for", "accountNumber");
                label.textContent = "Número de cuenta";

                var input = document.createElement('input');
                input.setAttribute("type", "text");
                input.setAttribute("class", "form-control form-control-user");
                input.setAttribute("id", "accountNumber");
                input.setAttribute("name", "accountNumber");
                input.setAttribute("placeholder", "Ingresar el número de cuenta");
                input.setAttribute("required", "required");

                var invalid = document.createElement('div');
                invalid.setAttribute("class", "invalid-feedback");
                invalid.textContent = "Favor de ingresar el número de cuenta";

                fields.append(label);
                fields.append(input);
                fields.append(invalid);

                break;
            default:
                break;
        }

    }
}

function byType() {
    var fields = $('#tlicencefield');
    fields.find('input[type=text]').remove();
    fields.find('label[class=labelMessage]').remove();
    fields.find('div[id=tableRange]').remove();
    var type = $(this).val();
    switch (type) {
        case "acountN":
            var input = document.createElement('input');
            input.setAttribute("type", "text");
            input.setAttribute("class", "form-control form-control-user");
            input.setAttribute("id", "accountNumber");
            input.setAttribute("name", "accountNumber");
            input.setAttribute("placeholder", "Ingresar el número de cuenta");
            input.setAttribute("required", "required");

            var labelMessage = document.createElement('label');
            labelMessage.setAttribute('class', 'labelMessage');
            labelMessage.textContent = "En caso de querer buscar varías, separarlas por una coma (,)";

            var invalid = document.createElement('div');
            invalid.setAttribute("class", "invalid-feedback");
            invalid.textContent = "Favor de ingresar el número de cuenta";

            fields.append(input);
            fields.append(labelMessage);
            fields.append(invalid);
            break;
        case "range":
            var table = document.createElement('table');
            table.setAttribute("class", "tableAL");
            var tr = document.createElement('tr');
            var tr2 = document.createElement('tr');

            var td = document.createElement('td');
            td.setAttribute('class', 'tdAL');

            var td2 = document.createElement('td');
            td2.setAttribute('class', 'tdAL');

            var td3 = document.createElement('td');
            var td4 = document.createElement('td');

            var div = document.createElement('div');
            div.setAttribute('id', 'tableRange');

            var labelD = document.createElement('label');
            labelD.setAttribute("for", "accountNumberD");
            labelD.textContent = "Desde:";

            var labelH = document.createElement('label');
            labelH.setAttribute("for", "accountNumberH");
            labelH.textContent = "Hasta:";

            var inputD = document.createElement('input');
            inputD.setAttribute("type", "text");
            inputD.setAttribute("class", "form-control form-control-user");
            inputD.setAttribute("id", "accountNumberD");
            inputD.setAttribute("name", "accountNumberD");
            inputD.setAttribute("placeholder", "Ingresar el número de recibo oficial");
            inputD.setAttribute("required", "required");

            var inputH = document.createElement('input');
            inputH.setAttribute("type", "text");
            inputH.setAttribute("class", "form-control form-control-user");
            inputH.setAttribute("id", "accountNumberH");
            inputH.setAttribute("name", "accountNumberH");
            inputH.setAttribute("placeholder", "Ingresar el número de recibo oficial");
            inputH.setAttribute("required", "required");

            fields.append(div);
            div.append(table);
            table.append(tr);
            tr.append(td);
            td.append(labelD);
            tr.append(td2);
            td2.append(labelH);
            table.append(tr2);
            tr2.append(td3);
            td3.append(inputD);
            tr2.append(td4);
            td4.append(inputH);
            break;
        case "conc":
            var input = document.createElement('input');
            input.setAttribute("type", "text");
            input.setAttribute("class", "form-control form-control-user");
            input.setAttribute("id", "conc");
            input.setAttribute("name", "conc");
            input.setAttribute("placeholder", "Ingresar el nombre del concesionario.");
            input.setAttribute("required", "required");

            var invalid = document.createElement('div');
            invalid.setAttribute("class", "invalid-feedback");
            invalid.textContent = "Favor de ingresar el nombre del concesionario.";

            fields.append(input);
            fields.append(labelMessage);
            fields.append(invalid);
            break;
        default:
            break;
    }
}