
let antibiotic_results = []

var $copy = null;

$('#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){
                $copy = $('#footer').clone().attr('id', 'new-footer').css('display','block');
                $('#sample-result-print').empty()
                let res = response.data
                antibiotic_results = res.antibiotic_results
                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){
                        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')
                                        })
                                    }
                                });
                            }
                        })
                    }
                }

            }
        }
    });
})

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>' +
        '                                   <td style="text-align: center; font-weight: bold; color: #0000ff !important; text-decoration: underline">'+sample_type+'</td>' +
        '                               </tr>\n' +
'</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')
        tr.insertCell(0).innerHTML = '<b class="sourceserifpro-bold">' + sample_tests.test_name +' : </b>';
        tr.cells[0].style.width=150+'px'
        $('#' + closet_parent_id).append(tr);
        if(sample_tests.comment!=""){
            renderPrintTestItemCommentRow(sample_tests, level, closet_parent_id);
        }
        
        if (sample_tests.test_name == 'CONCLUSION') {
            let tr = document.createElement('TR');
            tr.setAttribute('valign', 'top');
            tr.insertCell(0).innerHTML = '';
            tr.cells[0].style.borderBottom="3px solid #555";
            tr.cells[0].setAttribute('id', 'signature-area')
            $('#' + closet_parent_id).append(tr);
        }
        
        $('#footer').hide();
        $('#signature-area').html('');
        $('#signature-area').html($copy);

    }
}

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="margin-bottom:20px !important; '+(sample_tests.test_name=='CONCLUSION' ? 'font-weight:bold;':'')+'">' + sample_tests.comment + '</div>' : '');
        $('#' + closet_parent_id).append(tr);
    }
}
