



$('#print_preview_modal').on('show.bs.modal', function (event) {
    $.ajax({
        url: get_refs_base_url + '/test-items/' + sample_id,
        type: "GET",
        dataType: "json",
        success: function (response) {
            if(response.success){
                $('#sample-result-print').empty()
                let res = response.data



                for (var i in res.sample_tests){
                    //let heading = {"sample": res.sample, "department": res.sample_tests[i].department_name}
                    let samples_list = res.sample_tests[i].samples;
                    for(var j in samples_list){
                        var tests = samples_list[j].tests

                        tests.map(item => {

                            var checkBox = (item.org_results.length > 0 ? item.org_results[0].organism_name : 'No')
                            console.log(checkBox, item)
                            $('#sample-result-print_ #tchk-'+item.test_id).html(checkBox=='Yes' ? '&#9745;' :'&#9744;')
                        })

                        //var sampleTest = list_to_tree(tests)
                        //console.log('sampleTest ',tests)
                        /*renderPrintTestItemDepartmentRow(samples_list[j], heading,'sample-result-print')
                        var tests = samples_list[j].tests
                        var sampleTest = list_to_tree(tests)
                        sampleTest.map(parent => {
                            renderPrintTestItemRow(parent, '0' ,'test-dep-'+samples_list[j].sample_type_id,' sample-result-print')
                            if(parent.children.length){
                                var children = parent.children
                                children.map(function (child) {
                                    renderPrintTestItemRow(child, '1' ,'test-dep-'+samples_list[j].sample_type_id,' sample-result-print')
                                    if(child.children.length){
                                        var grandchildren = child.children
                                        grandchildren.map(function (grandchild) {
                                            renderPrintTestItemRow(grandchild, '1' ,'test-dep-'+samples_list[j].sample_type_id,' sample-result-print')

                                            if(grandchild.children.length){
                                                var childOfGrandChild = grandchild.children
                                                childOfGrandChild.map(function (childGrandChild) {
                                                    renderPrintTestItemRow(childGrandChild, '1' ,'test-dep-'+samples_list[j].sample_type_id,' sample-result-print')
                                                });
                                            }

                                        })
                                    }
                                });
                            }


                        })*/
                    }
                }

            }
        }
    });
})

function manipulateTableHeading(department, sample_type, sample_number, requester, collected_date, received_date, sample_tests) {
    let tableData = '<table width="100%" border="0" class="_department-sample-info"><thead>\n' +
        '                                <tr style="background: #eee;">' +
        '                                   <td colspan="5" style="text-align: center; font-weight: bold; color: #0000ff !important;">'+sample_type+'</td>' +
        '                               </tr>\n' +
        '<tr>\n' +
        '                                    <th class="sourceserifpro-bold" style="width: 20% !important; color: #0000ff;"><div style="width:150px !important; float:left; height:10px; ">&nbsp;</div></th>\n' +
        '                                    <th class="sourceserifpro-bold" style="width: 80%; color: #0000ff;"></th>\n' +
        '                                </tr></thead>\n' +
        '                                <tbody id="test-dep-'+sample_tests.sample_type_id+'"></tbody>'+
        '                                </thead>' +
        '                            </table>'
    return tableData;
}

function renderPrintTestItemDepartmentRow(sample_tests, department_heading, wrapperId)
{
    let tableData = manipulateTableHeading(department_heading.department, sample_tests.sample_type_name, department_heading.sample.sample_number, department_heading.sample.requested_by.name_en, department_heading.sample.collected_date, department_heading.sample.received_date, sample_tests)
    var tr = document.createElement('TR');
    tr.setAttribute('class', 'sample-type');
    tr.insertCell(0).innerHTML = tableData;
    $('#'+wrapperId).append(tr);
}

function renderPrintTestItemRow(sample_tests, level, closet_parent_id)
{
    if(sample_tests.is_show == 1) {
        let tr = document.createElement('TR');
        tr.setAttribute('valign', 'top')
        if(sample_tests.field_type==0){
            tr.insertCell(0).innerHTML = '' + (sample_tests.field_type == 0 ? '<b class="sourceserifpro-bold">' + sample_tests.test_name + '</b>' : '<p class="value-pair"><span style="font-size:13pt !important;" class="name">' + sample_tests.test_name + '</span></p>') + '';
            tr.cells[0].colSpan=2
        }
        else{
            tr.insertCell(0).innerHTML = ''
            tr.insertCell(1).innerHTML = manipulatePrintOrganismResult(sample_tests) +' '+sample_tests.test_name;
        }
        tr.cells[0].style.paddingLeft = (5 * level * 2.5) + 'px'
        if (sample_tests.field_type == 0) {
            tr.cells[0].classList.add('pl-' + (level * 2))
        }
        $('#' + closet_parent_id).append(tr);
    }
}

function renderPrintTestItemCommentRow(sample_tests, level, closet_parent_id)
{
    if(sample_tests.is_show == 1) {

        let tr = document.createElement('TR');
        tr.setAttribute('valign', 'top');
        tr.insertCell(0).innerHTML = (sample_tests.comment!=="" ? '<div style="padding-left: 15px; float:left; font-weight:bold;">Comment: </div><div style="padding-left:85px;  margin-top: 0px;  margin-left:10px;">'+sample_tests.comment+'</div>' : '');
        tr.cells[0].style.paddingLeft = (5 * 0) + 'px'
        tr.cells[0].colSpan=5
        if (sample_tests.field_type == 0) {
            tr.cells[0].classList.add('pl-' + (0 * 2))
        }
        $('#' + closet_parent_id).append(tr);
    }
}




function manipulatePrintOrganismResult(sample_tests) {

    if(sample_tests.field_type == 0){
        return ''
    } else if(["2","3"].includes(sample_tests.field_type)) {
        return (sample_tests.org_results.length < 1 ? ': &#9744;' : sample_tests.org_results.map(function (value) {
            return ': ' + (value.organism_name == 'Yes' ? '&#9745;' : '&#9744;')
        }).join(' '))
    }
    else{
        return ': &#9744;'
    }
}
