$(function () {
    $('.parent').change(function () {
        var $subs = $(this).parent().parent().children(".sub").children();
        $subs.children().remove().end();
        var $output = '';
        $liste = subsdata[$(this).val()];
        $sort = Object.entries($liste).sort((a, b) => (a[1] > b[1]) ? 1 : (b[1] > a[1]) ? -1 : 0);
        $.each($sort, function (key, value) {
            $output += '<option value="' + value[0] + '">' + value[1] + '</option>';
        });
        $subs.append($output);
    });
    $('#multi').change(function () {
        if ($(this).is(":checked")) {
            $('.single').addClass('d-none');
            $('.multi').removeClass('d-none');
            return;
        }
        $('.single').removeClass('d-none');
        $('.multi').addClass('d-none');
        //'unchecked' event code
    }).change();
    $('.dt-amount, .dt-subamount').change(function () {
        var temp = convertInput($(this).val());
        $(this).val(convertOutput(temp));
        if ($('#multi').is(":checked")) {
            recalcOpenAmount();
        }
    }).change();

});

function logData(event) {
    var data = document.getElementById("billfile");
    console.log(data.value);

}

function addLine() {
    $('#linecont > div').each(function () {
        if ($(this).hasClass("d-none")) {
            $(this).removeClass("d-none");
            return false;
        }
    });
}

function removeBillFile(e) {
    e.classList.add("d-none");
    document.getElementById("billfile").classList.remove("d-none");

    const billFile = document.getElementById("billfileRef");
    const form = document.getElementById('newbill');
    var DOB = document.createElement("input");
    DOB.setAttribute("name", "removeBillFile");
    DOB.setAttribute("value", billFile.innerText);
    DOB.setAttribute("type", "hidden");
    form.appendChild(DOB);
    
    
    billFile.innerText = "REMOVED FILE: "+billFile.innerText;
    billFile.classList.add("text-decoration-line-through");
    billFile.classList.add("text-danger");
    billFile.removeAttribute("href"); 
}

function convertInput(number) {
    var zahl = number.replace(/\,/g, ".");
    var czahl = eval(zahl);
    return czahl;
}

function convertOutput(number) {
    if (typeof number !== 'undefined') {
        var string = number.toFixed(2);
        return string.replace(/\,/g, ".");
    }
    else
        return "";
}

function recalcOpenAmount() {
    var summe = 0.0;
    $('.dt-subamount').each(function () {
        b = $(this).val();
        b = convertInput(b);
        if (typeof b !== 'undefined') {
            b = $(this).hasClass("text-danger") ? b * (-1) : b;
            summe = summe + b;
        }
        //       summe = summe + convertInput($(this).val());
    });

    $('#openamount').val(Math.round(($('#total_in').val() - $('#total_out').val() - summe) * 100) / 100);
}

// $('form').submit(function () {
//     alert("disable elements");
//     $( ".d-none" ).prop( "disabled", true );
//     return true;
// });
