$(document).ready(function () {
    $(document).on('change', '[data-toggle="toggle"]', function (e) {
        $(this).parents().next('.hide').toggle();
    });

     $(".accordion-toggle").click(function () {

        //var clickedValue = $(this).find('td:first').next()
        var clickedValue = $(this).closest('tr').children('td:first').text()
       
        if (clickedValue == '+') {
        
            $(this).closest('tr').children('td:first').text('-')
        }

        if (clickedValue == '-') {
   
            $(this).closest('tr').children('td:first').text('+')
        }


    });

});

function loadData(fromdate, todate, category, searchby, inventory) {
    
    //let ica = JSON.parse(getInventoryCostAverage());

    //let icad = JSON.parse(getInventoryStockAdjustDetail());
 
    let icad = getICAD();
    let icadtemp = icad;

    let fromDate = fromdate
    let toDate = todate

    icad = icad.filter(e => e['stockdate'].substring(0, 10) >= fromDate && e['stockdate'].substring(0, 10) <= toDate);

    let categories = getCategories();
    let inventories = getInventories();
    let itemdata = [];
    let headerdata = [];

    inventories = inventories.filter((e) => e['Class'] == 0 && e['Barcode'].toLowerCase() != 'round');

    let item;
    categories.forEach(function (category) {
        inventories.forEach(function (inventory) {
            if (category['CategoryID'] == inventory['CategoryID']) {
                item = {
                    'inventoryid': inventory['InventoryID'],
                    'barcode': inventory['Barcode'].trim(),
                    'name': inventory['InventoryName'].trim(),
                    'description': inventory['Description'].trim(),
                    'category': category['CategoryName'],
                    'openning': 0,
                    'stockin': 0,
                    'stockout': 0,
                    'retailsales': 0,
                    'closingbalance': 0
                }
                itemdata.push(item);
            }
        })
    });

    //group by batch id
    var groupbybatchid = []
    icad.reduce(function (res, value) {
        //console.log(value)
        if (!res[value['batchid']]) {
            res[value['batchid']] = {
                inventoryid: value['inventoryid'],
                batchid: value['batchid'],
                unitcost: value['unitcost'],
                inqty: 0,
                outqty: 0
            };
            groupbybatchid.push(res[value['batchid']])
        }


        res[value['batchid']].inqty += value['inqty'];
        res[value['batchid']].outqty += value['outqty'];

        //if (res[value['batchid']].notes.toLowerCase() == 'adj out') {
        //    res[value['batchid']].outqty += value['outqty'];
        //} else if (res[value['batchid']].notes.toLowerCase() == 'rs') {
        //    res[value['batchid']].retail += value['retail'];
        //}
        return res;
    }, {});

    //search by category
    if (category.trim().toLowerCase() != 'select category') {
        itemdata = itemdata.filter((e) => e['category'].trim().toLowerCase() == category.toLowerCase());
    }

    //search by inventory
  
    if (inventory.trim().toLowerCase() != '') {
      
        if (searchby.trim().toLowerCase() == 'inventoryid') { itemdata = itemdata.filter((e) => e['inventoryid'] == inventory);}
        if (searchby.trim().toLowerCase() == 'barcode') { itemdata = itemdata.filter((e) => e['barcode'].trim().toLowerCase().indexOf(inventory) > -1); }
        if (searchby.trim().toLowerCase() == 'name') { itemdata = itemdata.filter((e) => e['name'].trim().toLowerCase().indexOf(inventory) > -1);}
        if (searchby.trim().toLowerCase() == 'description') { itemdata = itemdata.filter((e) => e['description'].trim().toLowerCase().indexOf(inventory) > -1);}
   
    }

    itemdata.forEach(function (item) {
        //get openning
        var openning = icadtemp.filter(e => e['stockdate'].substring(0, 10) < fromDate && e['inventoryid'] == item['inventoryid']);


        //grand total
        var icadbyinventoryid = icad.filter((e) => e['inventoryid'] == item['inventoryid']);

        var stockin = icadbyinventoryid.filter((e) => e['notes'].toLowerCase() == 'adj in' || e['notes'].toLowerCase() == 'rsr');
        var stockout = icadbyinventoryid.filter((e) => e['notes'].toLowerCase() == 'adj out');
        var retail = icadbyinventoryid.filter((e) => e['notes'].toLowerCase() == 'rs');
        let openningbalance = 0;
        let totalin = 0;
        let totalout = 0;
        let totalretail = 0;

        let openningstockin = 0;
        let openningstockout = 0
        openning.forEach(function (e) {
            openningstockin += e['inqty']
            openningstockout += e['outqty']
        })

        openningbalance = openningstockin - openningstockout

        stockin.forEach((el) => totalin += el['inqty']);
        stockout.forEach((el) => totalout += el['outqty']);
        retail.forEach((el) => totalretail += el['outqty']);
        closing = openningbalance + totalin - (totalout + totalretail)

        //by batch
        var getallbatchbyinventoryid = icad.filter((e) => e['inventoryid'] == item['inventoryid'])
        const allbatch = [...new Set(getallbatchbyinventoryid.map(item => item['batchid']))];

        let batches = []

        allbatch.forEach(function (e) {
            let unitcost = 0;
            let inqty = 0;
            let outqty = 0;
            let retail = 0;

            //let openningbatch = openning.filter((x) => x['batchid'] == e);
            //console.log(openningbatch)
            icadbyinventoryid.forEach(function (detail) {
                if (e == detail['batchid']) {
                    unitcost = detail['unitcost']
                    if (detail['notes'].toLowerCase() == 'adj in' || detail['notes'].toLowerCase() == 'rsr') {
                        inqty += parseFloat(detail['inqty'])
                    }

                    if (detail['notes'].toLowerCase() == 'adj out') {
                        outqty += parseFloat(detail['outqty'])
                    }

                    if (detail['notes'].toLowerCase() == 'rs') {
                        retail += parseFloat(detail['outqty'])
                    }


                }
            })

            let batch = {
                'batchid': e,
                'unitcost': unitcost,
                'inqty': inqty,
                'outqty': outqty,
                'retail': retail
            }

            batches.push(batch)
        })


        x = {
            'inventoryid': item['inventoryid'],
            'barcode': item['barcode'].trim(),
            'name': item['name'].trim(),
            'description': item['description'].trim(),
            'category': item['category'],
            'openning': openningbalance,
            'stockin': totalin,
            'stockout': totalout,
            'retail': totalretail,
            'closing': closing,
            'batch': batches
        }
        headerdata.push(x)


    });

    //fillTableCollapse(headerdata)
    TableExpandable(headerdata)
}

function TableExpandable(headerdata){
    const tblbody = document.getElementById('tblbody')
    $.each(headerdata, function (key, item) {
        var tr = document.createElement('tr')
        tr.className = 'accordion-toggle'
        tr.setAttribute('data-toggle', 'collapse')
        tr.setAttribute('data-target', '#' + item['barcode'].trim().toLowerCase().trim().split(" ").join(""))

        var tdcollapseicon = document.createElement('td')
        tdcollapseicon.style='font-size:20px;font-weight:bold'
        var iicon = document.createElement('text')
        iicon.innerHTML = '+'
        
        var tdbarcode = document.createElement('td')
        tdbarcode.className = 'col-2 border-end p-1 align-content-center'
        var barcode = document.createElement('text')
        barcode.innerHTML = item['barcode'].trim();

        var tdname = document.createElement('td')
        tdname.className = 'col-2 border-end p-1 align-content-center'
        var name = document.createElement('text')
        name.innerHTML = item['name'].trim();

        var tddesc = document.createElement('td')
        tddesc.className = 'col-2 border-end p-1 align-content-center'
        var desc = document.createElement('text')
        desc.innerHTML = item['description'].trim();

        var tdcategory = document.createElement('td')
        tdcategory.className = 'col-2 border-end p-1 align-content-center text-start'
        var category = document.createElement('text')
        //category.innerHTML = itemCategory[0]['CategoryName'].trim();
        category.innerHTML = item['category'].trim();

        var tdopenning = document.createElement('td')
        tdopenning.className = 'col-1 border-end p-1 align-content-center  text-end'
        var openning = document.createTextNode(item['openning'].toFixed(2));

        var tdstockin = document.createElement('td')
        tdstockin.className = 'col-1 border-end p-1 align-content-center  text-end'
        var stockin = document.createTextNode(item['stockin'].toFixed(2));

        var tdstockout = document.createElement('td')
        tdstockout.className = 'col-1 border-end p-1 align-content-center  text-end'
        var stockout = document.createTextNode(item['stockout'].toFixed(2));

        var tdretail = document.createElement('td')
        tdretail.className = 'col-1 border-end p-1 align-content-center  text-end'
        var retail = document.createTextNode(item['retail'].toFixed(2));

        var tdclosing = document.createElement('td')
        tdclosing.className = 'col-1 border-end p-1 align-content-center  text-end'
        var closing = document.createTextNode(item['closing'].toFixed(2));


        tdcollapseicon.appendChild(iicon)   
        tdbarcode.appendChild(barcode)
        tdname.appendChild(name);
        tddesc.appendChild(desc);
        tdcategory.appendChild(category);
        tdopenning.appendChild(openning)
        tdstockin.appendChild(stockin)
        tdstockout.appendChild(stockout);
        tdretail.appendChild(retail);
        tdclosing.appendChild(closing);

        tr.appendChild(tdcollapseicon)
        tr.appendChild(tdbarcode)
        tr.appendChild(tdname);
        tr.appendChild(tddesc);
        tr.appendChild(tdcategory);
        tr.appendChild(tdopenning);
        tr.appendChild(tdstockin);
        tr.appendChild(tdstockout);
        tr.appendChild(tdretail);
        tr.appendChild(tdclosing);
        
        //batches
        var trheadbatch = document.createElement('tr')
        var tdheadbatch = document.createElement('td')
        tdheadbatch.colSpan = '12'
        tdheadbatch.className = 'hiddenRow'     

        var divchildbatch = document.createElement('div')
        divchildbatch.className = 'accordian-body collapse'
        divchildbatch.id = item['barcode'].toLowerCase().trim().split(" ").join("")

        var tablechildbatch = document.createElement('table')    
        tablechildbatch.className = 'table'

        var theadchildbatch = document.createElement('thead')

        var trchildheaderbatch = document.createElement('tr')
        trchildheaderbatch.className = 'bg-light'

        /*var thicon = document.createElement('th')
        thicon.style = 'width:10px'*/

        var thbatch = document.createElement('th')
        //thbatch.className = 'col-7'
        var batch = document.createElement('text')
        batch.innerHTML = 'Batch #'

        var thunitcost = document.createElement('th')
        //thunitcost.className = 'col-4'
        var unitcost = document.createElement('text')
        unitcost.innerHTML = 'Unit Cost'

        var thopenning = document.createElement('th')
        thopenning.className = 'col-1 text-nowrap'
        var openning = document.createElement('text')
        openning.innerHTML = 'Openning Balance'

        var thstockin = document.createElement('th')
        thstockin.className = 'col-1 text-nowrap'
        var stockin = document.createElement('text')
        stockin.innerHTML = 'Stock In'

        var thstockout = document.createElement('th')
        thstockout.className = 'col-1 text-nowrap'
        var stockout = document.createElement('text')
        stockout.innerHTML = 'Stock Out'
    
        var thretail = document.createElement('th')
        thretail.className = 'col-1 text-nowrap'
        var retail = document.createElement('text')
        retail.innerHTML = 'Retail Sales'

        var thclosing = document.createElement('th')
        thclosing.className = 'col-1 text-nowrap'
        var closing = document.createElement('text')
        closing.innerHTML = 'Closing Balance'

        trheadbatch.appendChild(tdheadbatch)
        tdheadbatch.appendChild(divchildbatch)
        divchildbatch.appendChild(tablechildbatch)
        tablechildbatch.appendChild(theadchildbatch)
        theadchildbatch.appendChild(trchildheaderbatch)

        //trchildheaderbatch.appendChild(thicon)
        trchildheaderbatch.appendChild(thbatch)
        trchildheaderbatch.appendChild(thunitcost)
        trchildheaderbatch.appendChild(thopenning)
        trchildheaderbatch.appendChild(thstockin)
        trchildheaderbatch.appendChild(thstockout)
        trchildheaderbatch.appendChild(thretail)
        trchildheaderbatch.appendChild(thclosing)
        
        thbatch.appendChild(batch)
        thunitcost.appendChild(unitcost)
        thopenning.appendChild(openning)
        thstockin.appendChild(stockin)
        thstockout.appendChild(stockout)
        thretail.appendChild(retail)
        thclosing.appendChild(closing)
        
        var tbodychildbatch = document.createElement('tbody')
        tablechildbatch.appendChild(tbodychildbatch)
        if(item['batch'].length == 0){
            theadchildbatch.className = 'd-none'
            tdcollapseicon.style = 'color:white'
        }
        $.each(item['batch'], function (key, item) {   
           
            var trchildbatchdata = document.createElement('tr')

            tbodychildbatch.appendChild(trchildbatchdata)
            
            var tdchildbatch = document.createElement('td')
            var batch = document.createElement('text')
            batch.innerHTML = item['batchid']
        
            var tdchildunitcost = document.createElement('td')
            var unitcost = document.createElement('text')
            unitcost.innerHTML = item['unitcost'].toFixed(2)

            var tdchildopenning = document.createElement('td')
 
            var openning = document.createElement('text')
            openning.innerHTML = '0.00'

            var tdchildstockin = document.createElement('td')
       
            var stockin = document.createElement('text')
            stockin.innerHTML = item['inqty'].toFixed(2)
            
            var tdchildstockout = document.createElement('td')
   
            var stockout = document.createElement('text')
            stockout.innerHTML = item['outqty'].toFixed(2)

            var tdchildretail = document.createElement('td')
   
            var retail =  document.createTextNode(item['retail'].toFixed(2));
            //retail.innerHTML = item['retail'].toFixed(2)

            var tdchildclosing = document.createElement('td')
         
            var closing = document.createElement('text')
            closing.innerHTML =  (parseFloat(item['inqty']) - parseFloat(item['outqty']) - parseFloat(item['retail'])).toFixed(2)

            tbodychildbatch.appendChild(tdchildbatch)
            tbodychildbatch.appendChild(tdchildunitcost)
            tbodychildbatch.appendChild(tdchildopenning)
            tbodychildbatch.appendChild(tdchildstockin)
            tbodychildbatch.appendChild(tdchildstockout)
            tbodychildbatch.appendChild(tdchildretail)
            tbodychildbatch.appendChild(tdchildclosing)

            tdchildbatch.appendChild(batch)
            tdchildunitcost.appendChild(unitcost)
            tdchildopenning.appendChild(openning)
            tdchildstockin.appendChild(stockin)
            tdchildstockout.appendChild(stockout)
            tdchildretail.appendChild(retail)
            tdchildclosing.appendChild(closing)
            
        
        })

        tblbody.appendChild(tr)
        tblbody.appendChild(trheadbatch)
    });
 

}

function fillTableCollapse(data) {

    const table = document.getElementById('tblheader')
    $.each(data, function (key, item) {
        const tbodyheader = document.createElement('tbody');
        tbodyheader.id = item['barcode'].trim() + "parent"
        table.appendChild(tbodyheader)

        var tr = document.createElement('tr')
        var tdbarcode = document.createElement('td')
        tdbarcode.className = 'col-2 border-start border-end p-1 align-content-center'
        var labelbarcode = document.createElement('label')

        labelbarcode.innerText = item['barcode'].trim()
        //labelbarcode.className = "d-block"
        labelbarcode.style = "display:block"

        var barcode = document.createElement('input')
        barcode.id = item['barcode'].trim()
        barcode.type = 'checkbox'
        barcode.name = item['barcode'].trim()
        barcode.id = item['barcode'].trim()

        labelbarcode.setAttribute("for", item['barcode'].trim());
        barcode.setAttribute('data-toggle', 'toggle')

        var tdname = document.createElement('td')
        tdname.className = 'col-2 border-end p-1 align-content-center'
        var name = document.createElement('text')
        name.innerHTML = item['name'].trim();

        var tddesc = document.createElement('td')
        tddesc.className = 'col-2 border-end p-1 align-content-center'
        var desc = document.createElement('text')
        desc.innerHTML = item['description'].trim();

        var tdcategory = document.createElement('td')
        tdcategory.className = 'col-2 border-end p-1 align-content-center text-start'
        var category = document.createElement('text')
        //category.innerHTML = itemCategory[0]['CategoryName'].trim();
        category.innerHTML = item['category'].trim();

        var tdopenning = document.createElement('td')
        tdopenning.className = 'col-1 border-end p-1 align-content-center  text-end'
        var openning = document.createTextNode(item['openning'].toFixed(2));

        var tdstockin = document.createElement('td')
        tdstockin.className = 'col-1 border-end p-1 align-content-center  text-end'
        var stockin = document.createTextNode(item['stockin'].toFixed(2));

        var tdstockout = document.createElement('td')
        tdstockout.className = 'col-1 border-end p-1 align-content-center  text-end'
        var stockout = document.createTextNode(item['stockout'].toFixed(2));

        var tdretail = document.createElement('td')
        tdretail.className = 'col-1 border-end p-1 align-content-center  text-end'
        var retail = document.createTextNode(item['retail'].toFixed(2));

        var tdclosing = document.createElement('td')
        tdclosing.className = 'col-1 border-end p-1 align-content-center  text-end'
        var closing = document.createTextNode(item['closing'].toFixed(2));

        tdbarcode.appendChild(labelbarcode)
        tdbarcode.appendChild(barcode)
        tdname.appendChild(name);
        tddesc.appendChild(desc);
        tdcategory.appendChild(category);
        tdopenning.appendChild(openning)
        tdstockin.appendChild(stockin)
        tdstockout.appendChild(stockout);
        tdretail.appendChild(retail);
        tdclosing.appendChild(closing);

        tr.appendChild(tdbarcode)
        tr.appendChild(tdname);
        tr.appendChild(tddesc);
        tr.appendChild(tdcategory);
        tr.appendChild(tdopenning);
        tr.appendChild(tdstockin);
        tr.appendChild(tdstockout);
        tr.appendChild(tdretail);
        tr.appendChild(tdclosing);
        tbodyheader.appendChild(tr)


        const tbodydetail = document.createElement('tbody');
        tbodydetail.id = item['barcode'].trim() + "child"
        tbodydetail.className = 'hide'

        var thead = document.createElement('thead')
        var trhead = document.createElement('tr')
        var thcol1 = document.createElement('th')
        var thcol2 = document.createElement('th')
        var thbatch = document.createElement('th')
        var thcost = document.createElement('th')
        var thopenning = document.createElement('th')
        var thinqty = document.createElement('th')
        var thoutqty = document.createElement('th')
        var thretail = document.createElement('th')
        var thclosing = document.createElement('th')

        var batch = document.createElement('text')
        batch.innerHTML = 'Batch ID'

        var cost = document.createElement('text')
        cost.innerHTML = 'Unit Cost'

        var openning = document.createElement('text')
        openning.innerHTML = 'Openning Balance'

        var inqty = document.createElement('text')
        inqty.innerHTML = 'Stock In'

        var outqty = document.createElement('text')
        outqty.innerHTML = 'Stock Out'

        var retail = document.createElement('text')
        retail.innerHTML = 'Retail Sales'

        var closing = document.createElement('text')
        closing.innerHTML = 'Closing Balance'

        //thbatch.appendChild(batch)
        //thcost.appendChild(cost)
        //thopenning.appendChild(openning)
        //thinqty.appendChild(inqty)
        //thoutqty.append(outqty)
        //thretail.appendChild(retail)
        //thclosing.appendChild(closing)

        //trhead.appendChild(thcol1)
        //trhead.appendChild(thcol2)
        //trhead.appendChild(thbatch)
        //trhead.appendChild(thcost)
        //trhead.appendChild(thopenning)
        //trhead.appendChild(thinqty)
        //trhead.appendChild(thoutqty)
        //trhead.appendChild(thretail)
        //trhead.appendChild(thclosing)

        //thead.appendChild(trhead)
        //table.appendChild(thead)
        table.appendChild(tbodydetail)

        $.each(item['batch'], function (key, item) {
            //console.log(item)



            var tr = document.createElement('tr')
            var td1 = document.createElement('td')
            var td2 = document.createElement('td')
            var tdbatch = document.createElement('td')

            var tdunicost = document.createElement('td')
            tdunicost.className = 'col-1 border-end p-1 align-content-center  text-end'

            var tdopenning = document.createElement('td')
            tdopenning.className = 'col-1 border-end p-1 align-content-center  text-end'

            var tdstockin = document.createElement('td')
            tdstockin.className = 'col-1 border-end p-1 align-content-center  text-end'

            var tdstockout = document.createElement('td')
            tdstockout.className = 'col-1 border-end p-1 align-content-center  text-end'

            var tdretail = document.createElement('td')
            tdretail.className = 'col-1 border-end p-1 align-content-center  text-end'

            var tdclosing = document.createElement('td')
            tdclosing.className = 'col-1 border-end p-1 align-content-center  text-end'

            var batch = document.createElement('text')
            batch.innerHTML = item['batchid'].trim();

            var unitcost = document.createElement('text')
            unitcost.innerHTML = item['unitcost'].toFixed(2)

            var openning = document.createElement('text')
            openning.innerHTML = '0.00'

            var stockin = document.createElement('text')
            stockin.innerHTML = item['inqty'].toFixed(2)

            var stockout = document.createElement('text')
            stockout.innerHTML = item['outqty'].toFixed(2)

            var retail = document.createElement('text')
            retail.innerHTML = item['retail'].toFixed(2)

            var closing = document.createElement('text')
            closing.innerHTML = (parseFloat(item['inqty']) - parseFloat(item['outqty']) - parseFloat(item['retail'])).toFixed(2)

            tdbatch.appendChild(batch)
            tdunicost.appendChild(unitcost)
            tdopenning.append(openning)
            tdstockin.appendChild(stockin)
            tdstockout.appendChild(stockout)
            tdretail.appendChild(retail)
            tdclosing.appendChild(closing)

            tr.appendChild(td1)
            tr.appendChild(td2)
            tr.appendChild(tdbatch)
            tr.appendChild(tdunicost)
            tr.appendChild(tdopenning)
            tr.appendChild(tdstockin)
            tr.appendChild(tdstockout)
            tr.appendChild(tdretail)
            tr.appendChild(tdclosing)
            tbodydetail.appendChild(tr)
        });
    });

}


function fillTable(headerdata) {
    const tblBody = document.getElementById('tblbodyinventory')
    const tbldetailBody = document.getElementById('tblbodyinventorydetail')
    $(".paginationjs").remove();
    $("#inventory-list").pagination({
        dataSource: headerdata,
        pageSize: 50,
        pageNumber: 1,
        afterRender: function () {
            document.getElementById('tblbodyinventory').innerHTML = '';
            //$(".paginationjs")

            if ($("#inventory-list div.paginationjs").length == 1) {
                $("#inventory-list div.paginationjs").insertBefore("#inventory-list")
            }
        },
        callback: function (data, pagination) {
            //sessionStorage.setItem('currentpage', pagination['pageNumber']);

            $("#inventory-list").empty();
            $.each(data, function (key, item) {

                const tr = document.createElement("tr");

                var tdbarcode = document.createElement("td");
                tdbarcode.className = 'col-2 border-start border-end p-1 align-content-center'
                tdbarcode.id = item['barcode'].trim()
                //var barcode = document.createElement('text')
                //barcode = document.createTextNode(item['barcode'].trim());

                var barcodelabel = document.createElement('label')
                //barcodelabel.for = item['barcode'].trim()
                barcodelabel.innerText = item['barcode'].trim()
                barcodelabel.className = "d-block "

                var barcode = document.createElement('input')
                barcode.id = item['barcode'].trim()
                barcode.type = 'checkbox'
                barcode.name = item['barcode'].trim()

                barcodelabel.setAttribute("for", item['barcode'].trim());
                barcode.setAttribute('data-toggle', 'toggle')

                var tdname = document.createElement('td')
                tdname.className = 'col-2 border-end p-1 align-content-center'
                var name = document.createElement('text')
                name.innerHTML = item['name'].trim();

                var tddesc = document.createElement('td')
                tddesc.className = 'col-2 border-end p-1 align-content-center'
                var desc = document.createElement('text')
                desc.innerHTML = item['description'].trim();

                var tdcategory = document.createElement('td')
                tdcategory.className = 'col-2 border-end p-1 align-content-center text-start'
                var category = document.createElement('text')
                //category.innerHTML = itemCategory[0]['CategoryName'].trim();
                category.innerHTML = item['category'].trim();

                var tdopenning = document.createElement('td')
                tdopenning.className = 'col-1 border-end p-1 align-content-center  text-end'
                var openning = document.createTextNode(item['openning'].toFixed(2));

                var tdstockin = document.createElement('td')
                tdstockin.className = 'col-1 border-end p-1 align-content-center  text-end'
                var stockin = document.createTextNode(item['stockin'].toFixed(2));

                var tdstockout = document.createElement('td')
                tdstockout.className = 'col-1 border-end p-1 align-content-center  text-end'
                var stockout = document.createTextNode(item['stockout'].toFixed(2));

                var tdretail = document.createElement('td')
                tdretail.className = 'col-1 border-end p-1 align-content-center  text-end'
                var retail = document.createTextNode(item['retail'].toFixed(2));

                var tdclosing = document.createElement('td')
                tdclosing.className = 'col-1 border-end p-1 align-content-center  text-end'
                var closing = document.createTextNode(item['closing'].toFixed(2));

                tdbarcode.appendChild(barcodelabel)

                tdbarcode.appendChild(barcode);
                tdname.appendChild(name);
                tddesc.appendChild(desc);
                tdcategory.appendChild(category);
                tdopenning.appendChild(openning)
                tdstockin.appendChild(stockin)
                tdstockout.appendChild(stockout);
                tdretail.appendChild(retail);
                tdclosing.appendChild(closing);

                tr.appendChild(tdbarcode);
                tr.appendChild(tdname);
                tr.appendChild(tddesc);
                tr.appendChild(tdcategory);
                tr.appendChild(tdopenning);
                tr.appendChild(tdstockin);
                tr.appendChild(tdstockout);
                tr.appendChild(tdretail);
                tr.appendChild(tdclosing);
                //tr.appendChild(tdqoh)
                //tr.appendChild(tdaction)
                //tdbarcode.setAttribute('data-toggle', 'toggle')
                tblBody.appendChild(tr);


                //let inputbarcode = document.createElement('input')
                //inputbarcode.type = 'checkbox'
                //inputbarcode.id = barcode
                //document.getElementById('a01').appendChild(inputbarcode)
                //console.log(tdbarcode)
                const trchild = document.createElement("tr");
                const tdtest = document.createElement("td");
                tdtest.className = 'col-2 border-start border-end p-1 align-content-center'
                var test = document.createTextNode('sample');

                tdtest.appendChild(test);
                trchild.appendChild(tdtest);
                tbldetailBody.appendChild(trchild);
            });
        },
    });
}
