<link rel="stylesheet" href="{{url(env("APP_URL").'storage/assets/vendors/mdi/css/materialdesignicons.min.css')}}">
<link rel="stylesheet" href="{{ url(env("APP_URL").'storage/assets/vendors/css/vendor.bundle.base.css') }}">
<link rel="stylesheet" href="{{ url(env("APP_URL").'storage/assets/css/vertical-layout-light/style.css') }}">

<div class=" A4-portrait">
    <div class="row">
        <div class="col-sm-12">
            <a href="{{url('customer/'.md5($labSettings->id))}}" class="btn btn-light btn-sm my-2 ml-2"> <i class="mdi mdi-chevron-left"></i> ត្រលប់ក្រោយ</a>
        </div>
    </div>
    <div class="psample-result active" id="presult-1">
        <title>Result</title>
        <meta name="viewport" content="width=device-width, initial-scale=.5, maximum-scale=12.0, minimum-scale=.25, user-scalable=yes">
        <link rel="stylesheet" href="{{url(env("APP_URL").'storage/assets/libs/lightgallery/lightgallery.css')}}" >
        <style>


            @font-face {
                font-family: 'KhmerOS_muollight';

                src: url('{{url(env("APP_URL").'storage/assets/fonts/KhmerOS_muollight.ttf')}}');
            }

            .KhmerOS {
                font-family: 'SourceSerifPro-Regular', 'KhmerOS', 'serif';
            }
            .KhmerMoulLight {
                font-family: 'SourceSerifPro-Regular', 'KhmerOS_muollight', 'serif';
            }
            .tacteng {
                font-family: Tacteng !important;
            }
            .sourceserifpro-bold {
                font-family: 'SourceSerifPro-Bold', 'serif';
            }
            /*body {
                background: rgb(204, 204, 204);
            }*/
            page[size="A4"] {
                background: white;
                width: 100%;
                display: block;
                margin: 10px auto;
                margin-left: 0.5cm !important;
                /*box-shadow: 0 0 0.5cm rgba(0,0,0,0.5);*/
                padding: 1cm;
                font-family: 'SourceSerifPro-Regular', 'Noto Sans Khmer', 'serif';
                font-size: 12pt;
                line-height: 23px;
                page-break-after: always;
            }

            page[size="A4"], th, td {
                -webkit-box-sizing: border-box;
                -moz-box-sizing: border-box;
                box-sizing: border-box;
            }

            page[size="A4"] table {
                border-spacing: 2px !important;
                border-collapse: separate !important;
            }

            table th, table td {
                text-align: left;
            }
            table td {
                font-weight: normal;
            }

            table.patient-info, table.department-sample-info  {
                border: 1px solid #7a7a7a;
                line-height: 18px;
                padding: 3px;
            }
            table.patient-info td, table.patient-info th {
                vertical-align: top;
            }
            table.department-sample-info {
                background-color: #ebebeb;
            }

            table.test-result {
                font-family: 'SourceSerifPro-Regular', 'serif';
            }
            table.test-result tr th {
                border-bottom: 1px solid black;
            }
            table.test-result tr th:not(:last-child), table.test-result tr td:not(:last-child) {
                padding-right: .2cm;
            }
            table.test-result tr:first-child + tr td {
                padding-top: 5px;
            }

            p.value-pair {
                position: relative;
                width: 100%;
                margin: 0;
            }

            p.value-pair:before {
                content: '';
                display: block;
                position: absolute;
                width: 100%;
                bottom: .25rem;
                height: 0;
                line-height: 0;
                border-bottom: 2px dotted #858585;
            }

            p.value-pair .name {
                background: white;
                z-index: 1;
                position: relative;
                padding-right: 5px;
            }

            p.value-pair .value {
                background: white;
                z-index: 1;
                right: 0;
                position: absolute;
                padding-left: 5px;
                font-weight: bold;
            }
            .out-of-range {
                font-family: 'SourceSerifPro-Bold', 'serif';
                font-weight: bold;
                font-size: 12pt;
            }

            .text-center {
                text-align: center;
            }
            .text-left {
                text-align: left;
            }
            .text-right {
                text-align: right;
            }
            .text-top {
                vertical-align: top;
            }
            .text-bottom {
                vertical-align: bottom;
            }
            .text-middle {
                vertical-align: middle;
            }
            .text-bold {
                font-weight: bold;
            }
            .text-no-wrap {
                white-space: nowrap;
            }
            table.test-result {
                line-height: 16px;
            }
            #footer {
                line-height: 17px;
            }
            #footer .laboratory-address td {
                border-top: 1px solid black;
                padding-top: 10px;
            }
            #footer .result-footer-signature td {
                vertical-align: top;
                padding-bottom: 0px;
            }

            @page {
                margin: 1cm 1cm 0;
            }

            @media print {
                @page {
                    margin: 1cm;
                    margin-left: 0.5cm !important;
                }

                body,
                page[size="A4"] {
                    margin: 0;
                    margin-left: 0.5cm !important;
                    padding: 0;
                    box-shadow: none;
                    background-color: transparent;
                }

                /*tbody .sample-result {
                    page-break-inside: avoid;

                }*/

                body .sample-result .sample-type{
                    page-break-inside: avoid;
                }


                #footer {
                    display: block;
                    position: fixed;
                    bottom: 0;
                    margin-top: 500px !important;
                    padding-bottom: 0px !important;
                }

                #footer .result-footer-signature td {
                    vertical-align: top;
                    padding-bottom: 0px;
                }

                /* table ._department-sample-info tr th:first-child, table ._department-sample-info tr td:first-child{
                     width:390px !important;
                 }*/

                table .tname{

                    width:360px !important;
                }


            }


            table .tname{

                width:360px !important;
            }

            /*table ._department-sample-info tr th:first-child, table ._department-sample-info tr td:first-child{
                width:390px !important;
            }*/

            #print_invoice_modal .table-inv th, .table-inv td {
                border: 1px solid black;
                border-collapse: collapse;
            }
        </style>


        <input type="hidden" name="verify" id="verify" value="">
        <input type="hidden" name="patient_sample_id" id="patient_sample_id" value="4353911">

        <page size="A4">
            <table border="0" width="100%">
                <thead>
                <tr>
                    <th style="width:100%" class="text-center">
                        @if(!empty($samplePhysician->logo))
                            <img width="100%" class="rounded-0" src="{{ url(env("APP_URL").'storage/images/physician/'.$samplePhysician->logo) }}"  />
                        @else
                            <table width="100%">
                                <tr>
                                    <td style="width:120px; vertical-align: top;" class="text-center">
                                        <img src="{{url(env("APP_URL").'storage/images/avatars/labs/'.$labSettings->logo)}}" alt="Logo" style="width: 120px;">
                                    </td>
                                    <td class="text-center">
                                        <p style="line-height: 13px !important; font-size: 15pt; font-weight: bold; color: #0000ff;" class="KhmerMoulLight py-1">{{$labSettings->name_kh}}</p>
                                        <p style="line-height: 13px !important; font-size: 13.5pt; font-weight: bold; color: #0000ff;" class="text-uppercase py-1">{{$labSettings->name_en}}</p>
                                    </td>
                                    <td style="width:120px" >&nbsp;</td>
                                </tr>
                            </table>
                        @endif
                    </th>
                </tr>
                <tr>
                    <td class="text-top text-center">
                        <div class="KhmerMoulLight_" style="font-size: 13.5pt; text-transform: uppercase; color: #0000ff; font-weight: bold;">{{__('sample.print_title')}}</div>
                    </td>
                </tr>
                <tr>
                    <th>
                        <table width="100%" border="0" class="patient-info">
                            <tbody>
                            <tr>
                                <td width="20%" style="font-size:17px !important; padding-top:7px; line-height:25px; color: #0000ff; font-weight:bold;">{{__('sample.table_patient_name')}}</td>
                                <td width="30%" style="font-size:17px !important; padding-top:7px;  line-height:25px;" align="left">: {{$sample->patient->name_en}}</td>
                                <td width="25%" style="padding-left:0.7cm; padding-top:7px; font-size:17px !important;  line-height:25px; color: #0000ff; font-weight:bold;">{{__('sample.form_sample_number')}}</td>
                                <td width="25%" style="font-size:17px !important; padding-top:7px;  line-height:25px;" align="left">: {{$sample->sample_number}}</td>
                            </tr>
                            <tr>
                                <td width="20%" style="font-size:17px !important;  line-height:25px; color: #0000ff; font-weight:bold;">{{__('sample.form_patient_age')}}</td>
                                <td width="30%" style="font-size:17px !important;  line-height:25px;" align="left">: {{\App\Helpers\Helpers::getAge($sample->patient->dob, $sample->admission_date)}} , <b style="color: #0000ff;">{{__('sample.form_patient_gender')}}</b> : {{\App\Helpers\Helpers::getGender($sample->patient->gender)}}</td>
                                <td width="25%" style="padding-left:0.7cm; font-size:17px !important;  line-height:25px; color: #0000ff; font-weight:bold;">{{__('sample.requested_date')}}</td>
                                <td width="25%" style="font-size:17px !important;  line-height:25px;" align="left">: {{date('d-M-Y H:i',strtotime($sample->collected_date))}}</td>
                            </tr>
                            <tr>
                                <td width="20%" style="font-size:17px !important; padding-bottom:7px;  line-height:25px; color: #0000ff; font-weight:bold;">{{__('sample.form_patient_address')}}</td>
                                <td width="30%" style="font-size:17px !important; padding-bottom:7px;  line-height:25px;" align="left">:  {{\App\Helpers\Helpers::getPatientAddress($sample->patient)}}</td>
                                <td width="25%" style="padding-left:0.7cm; padding-bottom:7px; font-size:17px !important; line-height:25px; color: #0000ff; font-weight:bold;">{{__('sample.physician_name')}}</td>
                                <td width="25%" style="font-size:17px !important; padding-bottom:7px;  line-height:25px;" align="left">: {{@$samplePhysician->name_en}}</td>
                            </tr>

                            </tbody>
                        </table>
                    </th>
                </tr>
                </thead>

                <tbody class="sample-result" id="sample-result-print" style="font-size: 12pt;">
                <!-- Comment -->
                </tbody>



                <!-- Note -->
            </table>
            <table id="footer" width="100%" border="0" style="margin-top:20px !important;">
                <tbody>
                <tr style="height:40px !important;">&nbsp;<br><td colspan="3"></td></tr>
                <tr>
                    <td class="text-center text-no-wrap" style="width: 7cm">
                        {{__('sample.last_test_date')}} :                 </td>
                    <td style="width: 7cm"></td>
                    <td class="text-center text-no-wrap" style="width: 7cm">
                        {{__('sample.report_date')}} : {{date('d-M-Y H:i')}} </td>
                </tr>
                <tr class="result-footer-signature">
                    <td class="text-center text-no-wrap" valign="bottom">
                        {{__('sample.verify_by')}}
                        <br>
                        <br>
                        @if(!empty($labSettings->verify_label))
                            <img id="signature-preview" style="width: 120px !important;" src="{{url(env("APP_URL").'storage/images/avatars/labs/'.$labSettings->verify_label)}}">
                        @else
                            <br>
                        @endif
                    </td>
                    <td class="text-center text-no-wrap"></td>
                    <td class="text-center text-no-wrap">
                        {{__('sample.lab_technician')}} <br>
                        @if(!empty(Auth::user()->signature))
                            <img id="signature-preview" style="width: 120px !important;" src="{{url(env("APP_URL").'storage/images/signature/'.Auth::user()->signature)}}">
                        @else
                            <br>
                        @endif
                        <p style="text-transform: capitalize;">{{$labSettings->technician_label}}</p>
                    </td>
                </tr>
{{--                <tr>--}}
{{--                    <td colspan="3" style="text-align:center; color: #0000ff; border-top:1px solid #000 !important; padding-top:6px;">--}}
{{--                        <p style="line-height: 10px !important;">{{ app()->getLocale()=='kh' ? $labSettings->address_kh : $labSettings->address_en}} </p>--}}
{{--                        <p style="line-height: 10px !important;">{{$labSettings->phone_number}} {{$labSettings->email}}</p>--}}
{{--                    </td>--}}
{{--                </tr>--}}
                </tbody></table>
        </page>
    </div>
</div>

@include('layout.common_script')

<script>


    var patient_base_url = "{{url('patient')}}";


    var get_refs_base_url = "{{url('shared')}}";
    var sample_base_url = "{{url('sample')}}"
    var is_auto_generate_sample_number = 0;
    let sample_id = {{$sample->id}};
    let invoice_id = "{{($sample->invoice) ? $sample->invoice->id : 0}}";
    let invoice_code = "{{($sample->invoice) ? $sample->invoice->invoice_code : 0}}";

    let testSamples = [];

    let antibiotic_results = []



    //$('#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
                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')
                                        })
                                    }
                                });
                            }
                        })
                    }
                }

            } else{
                antibiotic_results = []
            }
        }
    });
    //})

    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: 52% !important; color: #0000ff;"><div style="width:380px !important; float:left; height:10px; ">&nbsp;</div></th>\n' +
            '                                    <th class="sourceserifpro-bold" style="width: 15%; color: #0000ff;">Result</th>\n' +
            '                                    <th class="sourceserifpro-bold" style="width: 5%; color: #0000ff;"></th>\n' +
            '                                    <th class="sourceserifpro-bold" style="width: 10%; color: #0000ff;">Units</th>\n' +
            '                                    <th class="sourceserifpro-bold" style="width: 18%; color: #0000ff; text-align: center">Reference Range</th>\n' +
            '                                </tr></thead>\n' +
            '                                <tbody id="test-dep-'+sample_tests.sample_type_id+'"></tbody>'+
            '                                </thead>' +
            '                            </table>'
        return tableData;
    }

    function manipulateSubHeading(setSelectorId) {
        let subTableHeading = '<table width="100%" border="0" class="_test-result">\n' +
            '                                <thead><tr>\n' +
            '                                    <th class="sourceserifpro-bold" style="/*width: 361px !important;*/ color: #0000ff;"></th>\n' +
            '                                    <th class="sourceserifpro-bold" style="width: 2cm; color: #0000ff;">Result</th>\n' +
            '                                    <th class="sourceserifpro-bold" style="width: 0.5cm; color: #0000ff;"></th>\n' +
            '                                    <th class="sourceserifpro-bold" style="width: 1.5cm; color: #0000ff;">Units</th>\n' +
            '                                    <th class="sourceserifpro-bold" style="width: 3.5cm; color: #0000ff; text-align: center">Reference Range</th>\n' +
            '                                </tr>\n' +
            '                                <tbody id='+setSelectorId+'></tbody>'+
            '                                </thead>' +
            '</table>'
        return subTableHeading
    }

    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 ? 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);
        //var subtr = document.createElement('TR');
        //subtr.insertCell(0).innerHTML = manipulateSubHeading('test-dep-'+sample_tests.sample_type_id);
        //$('#'+wrapperId).append(subtr);
    }

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

            let IsAbnormal = computeRefRange(sample_tests);
            let tr = document.createElement('TR');
            tr.setAttribute('valign', 'top')
            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>') + ''; // + (sample_tests.description!="" ? '<p class="pl-4 pt-2">'+sample_tests.description+'</p>' : '');
            //tr.cells[0].classList.add('tname')
            if(["2","3"].includes(sample_tests.field_type)){
                tr.insertCell(1).innerHTML = (sample_tests.field_type == 0 ? '' : ': ') + manipulatePrintOrganismResult(sample_tests) // (sample_tests.field_type!=0 ? (sample_tests.test_result.length<1 ? 'Pending' : sample_tests.test_result) : '');
                tr.cells[1].colSpan=4
            }else{
                tr.insertCell(1).innerHTML = '<span class="name" style="font-family: \'Times New Roman\', Times, serif;">'+ (sample_tests.field_type == 0 ? '' : ': ') + manipulatePrintOrganismResult(sample_tests) +'</span>'; // (sample_tests.field_type!=0 ? (sample_tests.test_result.length<1 ? 'Pending' : sample_tests.test_result) : '');
                tr.insertCell(2).innerHTML = (IsAbnormal !="" ? '<b style="margin-right: 20px">'+IsAbnormal+'</b>' : '');
                tr.insertCell(3).innerHTML = sample_tests.unit_sign;
                tr.insertCell(4).innerHTML = '<span class="name" style="font-family: \'Times New Roman\', Times, serif;">'+ sample_tests.ref_range +'</span>';
                tr.cells[4].style.textAlign = 'center'
            }

            tr.cells[0].style.paddingLeft = (5 * level * 5) + 'px'
            tr.cells[0].style.width="380px";

            if(IsAbnormal!=''){
                tr.cells[1].style.fontWeight = 'bold'
            }
            if (sample_tests.field_type == 0) {
                tr.cells[0].classList.add('pl-' + (level * 2))
            }
            $('#' + closet_parent_id).append(tr);
            if(sample_tests.description!=""){
                renderPrintTestItemDescriptionRow(sample_tests, level, closet_parent_id);
            }

        }
    }

    function renderPrintTestItemDescriptionRow(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.description!=="" ? '<p class="pl-4 pt-2">'+sample_tests.description+'</p>' : '');
            tr.cells[0].style.paddingLeft = (5 * level * 5) + 'px'
            tr.cells[0].colSpan=5
            if (sample_tests.field_type == 0) {
                tr.cells[0].classList.add('pl-' + (level * 2))
            }
            $('#' + closet_parent_id).append(tr);
        }
    }

    function computeRefRange(sample_tests) {
        if(sample_tests.min_ref_val=='' || sample_tests.max_ref_val=='' || sample_tests.test_result=='' || sample_tests.ref_range == '') return ''
        else if([">",">=","<","<=","-", "Neg <"].includes(sample_tests.sign)){
            if(parseFloat(sample_tests.test_result) > parseFloat(sample_tests.max_ref_val)) return 'H';
            if(parseFloat(sample_tests.test_result) < parseFloat(sample_tests.min_ref_val)) return 'L';
            else return ''
        } else return ''
    }


    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 ? 'Pending' : sample_tests.org_results.map(function (value) {
                let anti_text =''
                antibiotic_results.map(function (anti) {
                    if(anti.organism_id == value.organism_id && anti.test_organism_id == value.test_organism_id){
                        anti_text+= '<p class="value-pair" style="width: 90% !important; position: relative;"><span class="name" style="padding-left: 10px; '+(anti.sensitive=='S' ? 'font-weight:bold':'')+'"> - '+anti.name_en+'</span> <span class="name" style="float: right; '+(anti.sensitive=='S' ? 'font-weight:bold':'')+'">'+anti.sensitive+'</span></p>';
                    }
                });
                return ((value.is_bold==1) ? '<b>'+ value.organism_name+'</b>' : value.organism_name) +' '+ (value.quantity_name !=null ? value.quantity_name : '') + anti_text;
            }).join(' '))
        } else if(sample_tests.field_type == 1){
            return (sample_tests.test_result.length<1 ? 'Pending' : sample_tests.test_result)
        }
        else{
            return (sample_tests.test_result.length<1 ? 'Pending' : sample_tests.test_result)
        }
    }

</script>


