{% extends '@RxConnexion/Admin/global.html.twig' %}
{% block body %}
    <style>
        .col-left{
            padding-left: 0!important;
            padding-right: 0!important;
        }
        .col-right{
            padding-right: 0!important;
        }
        .toggle-sub-table, .toggle-sub-table-all{
            cursor: pointer;
        }
        .sub-table{
            padding: 0px 5px 5px 5px;
        }
        #modal-small{
            height: 75vh;
        }
    </style>
    <h5 class="report-header" style="width: 275%;">{{ page_title }}</h5>
    <div class="row max-width-initial">
        <div class="small-12 columns col-left">
            <h5 class="report-header">Cycle Summary</h5>
            <table class="report-data" data-floathead="false" width="100%" style="margin-bottom: 20px;">
                <thead class="header">
                <tr>
                    <th>Branch</th>
                    <th>Referrals</th>
                    <th>Received Fax</th>
                    <th>Statues</th>
                    {% for cycleType in records.summary.cycleTypes %}
                        <th>{{ cycleType }}</th>
                    {% endfor %}
                </tr>
                </thead>
                <tbody>
                {% set totalReferral = 0 %}
                {% set totalFaxCount = 0 %}
                {% for branchName,cycle in records.summary.cycle %}
                    {% for status in cycle.statuses %}
                        <tr style="border : 1px solid grey !important;">
                            {% if loop.first %}
                                {% set totalReferral = totalReferral + cycle.referral %}
                                <td rowspan="{{ cycle.statuses|length }}">{{ branchName }}</td>
                                <td rowspan="{{ cycle.statuses|length }}" class="text-right">{{ cycle.referral }}</td>
                                <td rowspan="{{ cycle.statuses|length }}" class="text-right">{{ cycle.faxCount }}</td>
                            {% endif %}
                            <td>{{ status.status }}</td>
                            {% for cycleType in records.summary.cycleTypes %}
                                {% set count = '' %}
                                {% for typeName,typeCount in status.types %}
                                    {% if typeName == cycleType %}
                                        {% set count = typeCount %}
                                    {% endif %}
                                {% endfor %}
                                <td class="text-right">{{ count }}</td>
                            {% endfor %}
                        </tr>
                    {% endfor %}
                {% endfor %}
                <tr>
                    <td>Total:</td>
                    <td>{{ totalReferral }}</td>
                    <td></td>
                </tr>

                </tbody>
            </table>
        </div>
    </div>
    <table class="report-data" width="275%">
        <thead class="header">
        <tr>
            <th>Referral Date</th>
            <th>Received Date</th>
            <th>Type of Fax</th>
            <th>Received From</th>
            <th>Patient Name</th>
            <th>Primary Clinician</th>
            <th>Affiliation</th>
            <th>Physician</th>
            <th>Patient Services</th>
            <th>Previous Branch</th>
            <th>Branch</th>
            <th>Acknowledgement Letter Date</th>
            <th>Status</th>
            <th>HSAT Date</th>
            <th>Type</th>
            <th>Status</th>
            <th>HSAT Repeat Date</th>
            <th>Type</th>
            <th>Status</th>
            <th>HSAT Results Interpretation Date</th>
            <th>Type</th>
            <th>Status</th>
            <th>PFT Date</th>
            <th>Type</th>
            <th>Status</th>
            <th>Result</th>
            <th>Prescription Date</th>
            <th>Type</th>
            <th>Status</th>
            <th>Signed Prescription Date</th>
            <th>Latest Letter Sent</th>
            <th>CPAP Setup Date</th>
            <th>Type</th>
            <th>Status</th>
            <th>Invoice</th>
            <th>Endorsement Note(s)</th>
            <th>Patient Note(s)</th>
        </tr>
        </thead>
        <tbody>
        {% set totalCount = 0 %}
        {% for record in records.patientCycle %}
            {% set trC = loop.index > 1 and loop.index % 2 == 0 ? 'even' : 'odd' %}
            {% set totalCount = totalCount + 1 %}

            {% set receiveDateToHsat = record.receiveDate|date_modify("+ "~expectedNoOfDays.hsat|default('0')~" day")|date('Y-m-d') %}
            {% set receiveDateToHsatRepeat = record.receiveDate|date_modify("+ "~expectedNoOfDays.hsat_repeat|default('0')~" day")|date('Y-m-d') %}
            {% set receiveDateToHri = record.receiveDate|date_modify("+ "~expectedNoOfDays.hri|default('0')~" day")|date('Y-m-d') %}
            {% set receiveDateToPft = record.receiveDate|date_modify("+ "~expectedNoOfDays.pft|default('0')~" day")|date('Y-m-d') %}
            {% set receiveDateToPrescription = record.receiveDate|date_modify("+ "~expectedNoOfDays.prescription|default('0')~" day")|date('Y-m-d') %}
            {% set receiveDateToCpapSetup = record.receiveDate|date_modify("+ "~expectedNoOfDays.cpap_setup|default('0')~" day")|date('Y-m-d') %}
            {% set receiveDateToInvoice = record.receiveDate|date_modify("+ "~expectedNoOfDays.invoice|default('0')~" day")|date('Y-m-d') %}

            {% set hsatIsDue = false %}
            {% set hsatDueColor = false %}
            {% if record.hsatChartIdEncoded == '' or record.hsatChartIdEncoded == null %}
                {% if date('now'|date('Y-m-d')|date_modify("+3 day")|date('Y-m-d')) >= date(receiveDateToHsat)
                    or date('now'|date('Y-m-d')|date_modify("+2 day")|date('Y-m-d')) >= date(receiveDateToHsat)
                    or date('now'|date('Y-m-d')|date_modify("+1 day")|date('Y-m-d')) >= date(receiveDateToHsat)
                %}
                    {% set hsatIsDue = true %}
                {% endif %}

                {% if date('now'|date('Y-m-d')|date_modify("+3 day")|date('Y-m-d')) == date(receiveDateToHsat) %}
                    {% set hsatDueColor = '#feff4e' %}
                {% elseif date('now'|date('Y-m-d')|date_modify("+2 day")|date('Y-m-d')) == date(receiveDateToHsat) %}
                    {% set hsatDueColor = 'orange' %}
                {% elseif date('now'|date('Y-m-d')|date_modify("+1 day")|date('Y-m-d')) >= date(receiveDateToHsat) %}
                    {% set hsatDueColor = 'red' %}
                {% endif %}
            {% endif %}

            {% set hsatRepeatIsDue = false %}
            {% set hsatRepeatDueColor = '' %}
            {% if record.hrdChartIdEncoded == '' or record.hrdChartIdEncoded == null %}
                {% if date('now'|date('Y-m-d')|date_modify("+3 day")|date('Y-m-d')) >= date(receiveDateToHsatRepeat)
                    or date('now'|date('Y-m-d')|date_modify("+2 day")|date('Y-m-d')) >= date(receiveDateToHsatRepeat)
                    or date('now'|date('Y-m-d')|date_modify("+1 day")|date('Y-m-d')) >= date(receiveDateToHsatRepeat)
                %}
                    {% set hsatRepeatIsDue = true %}
                {% endif %}

                {% if date('now'|date('Y-m-d')|date_modify("+3 day")|date('Y-m-d')) == date(receiveDateToHsatRepeat) %}
                    {% set hsatRepeatDueColor = '#feff4e' %}
                {% elseif date('now'|date('Y-m-d')|date_modify("+2 day")|date('Y-m-d')) == date(receiveDateToHsatRepeat) %}
                    {% set hsatRepeatDueColor = 'orange' %}
                {% elseif date('now'|date('Y-m-d')|date_modify("+1 day")|date('Y-m-d')) >= date(receiveDateToHsatRepeat) %}
                    {% set hsatRepeatDueColor = 'red' %}
                {% endif %}
            {% endif %}

            {% set hriIsDue = false %}
            {% set hriDueColor = '' %}
            {% if record.hriChartIdEncoded == '' or record.hriChartIdEncoded == null %}
                {% if date('now'|date('Y-m-d')|date_modify("+3 day")|date('Y-m-d')) >= date(receiveDateToHri)
                    or date('now'|date('Y-m-d')|date_modify("+2 day")|date('Y-m-d')) >= date(receiveDateToHri)
                    or date('now'|date('Y-m-d')|date_modify("+1 day")|date('Y-m-d')) >= date(receiveDateToHri)
                %}
                    {% set hriIsDue = true %}
                {% endif %}

                {% if date('now'|date('Y-m-d')|date_modify("+3 day")|date('Y-m-d')) == date(receiveDateToHri) %}
                    {% set hriDueColor = '#feff4e' %}
                {% elseif date('now'|date('Y-m-d')|date_modify("+2 day")|date('Y-m-d')) == date(receiveDateToHri) %}
                    {% set hriDueColor = 'orange' %}
                {% elseif date('now'|date('Y-m-d')|date_modify("+1 day")|date('Y-m-d')) >= date(receiveDateToHri) %}
                    {% set hriDueColor = 'red' %}
                {% endif %}
            {% endif %}

            {% set pftIsDue = false %}
            {% set pftDueColor = '' %}
            {% if record.pftChartIdEncoded == '' or record.pftChartIdEncoded == null %}
                {% if date('now'|date('Y-m-d')|date_modify("+3 day")|date('Y-m-d')) >= date(receiveDateToPft)
                    or date('now'|date('Y-m-d')|date_modify("+2 day")|date('Y-m-d')) >= date(receiveDateToPft)
                    or date('now'|date('Y-m-d')|date_modify("+1 day")|date('Y-m-d')) >= date(receiveDateToPft)
                %}
                    {% set pftIsDue = true %}
                {% endif %}

                {% if date('now'|date('Y-m-d')|date_modify("+3 day")|date('Y-m-d')) == date(receiveDateToPft) %}
                    {% set pftDueColor = '#feff4e' %}
                {% elseif date('now'|date('Y-m-d')|date_modify("+2 day")|date('Y-m-d')) == date(receiveDateToPft) %}
                    {% set pftDueColor = 'orange' %}
                {% elseif date('now'|date('Y-m-d')|date_modify("+1 day")|date('Y-m-d')) >= date(receiveDateToPft) %}
                    {% set pftDueColor = 'red' %}
                {% endif %}
            {% endif %}

            {% set prescriptionIsDue = false %}
            {% set prescriptionDueColor = '' %}
            {% if record.pftChartIdEncoded == '' or record.pftChartIdEncoded == null %}
                {% if date('now'|date('Y-m-d')|date_modify("+3 day")|date('Y-m-d')) >= date(receiveDateToPrescription)
                    or date('now'|date('Y-m-d')|date_modify("+2 day")|date('Y-m-d')) >= date(receiveDateToPrescription)
                    or date('now'|date('Y-m-d')|date_modify("+1 day")|date('Y-m-d')) >= date(receiveDateToPrescription)
                %}
                    {% set prescriptionIsDue = true %}
                {% endif %}

                {% if date('now'|date('Y-m-d')|date_modify("+3 day")|date('Y-m-d')) == date(receiveDateToPrescription) %}
                    {% set prescriptionDueColor = '#feff4e' %}
                {% elseif date('now'|date('Y-m-d')|date_modify("+2 day")|date('Y-m-d')) == date(receiveDateToPrescription) %}
                    {% set prescriptionDueColor = 'orange' %}
                {% elseif date('now'|date('Y-m-d')|date_modify("+1 day")|date('Y-m-d')) >= date(receiveDateToPrescription) %}
                    {% set prescriptionDueColor = 'red' %}
                {% endif %}
            {% endif %}

            {% set cpapSetupIsDue = false %}
            {% set cpapSetupDueColor = '' %}
            {% if record.cpapSetupIdEncoded == '' or record.cpapSetupIdEncoded == null %}
                {% if date('now'|date('Y-m-d')|date_modify("+3 day")|date('Y-m-d')) >= date(receiveDateToCpapSetup)
                    or date('now'|date('Y-m-d')|date_modify("+2 day")|date('Y-m-d')) >= date(receiveDateToCpapSetup)
                    or date('now'|date('Y-m-d')|date_modify("+1 day")|date('Y-m-d')) >= date(receiveDateToCpapSetup)
                %}
                    {% set cpapSetupIsDue = true %}
                {% endif %}

                {% if date('now'|date('Y-m-d')|date_modify("+3 day")|date('Y-m-d')) == date(receiveDateToCpapSetup) %}
                    {% set cpapSetupDueColor = '#feff4e' %}
                {% elseif date('now'|date('Y-m-d')|date_modify("+2 day")|date('Y-m-d')) == date(receiveDateToCpapSetup) %}
                    {% set cpapSetupDueColor = 'orange' %}
                {% elseif date('now'|date('Y-m-d')|date_modify("+1 day")|date('Y-m-d')) >= date(receiveDateToCpapSetup) %}
                    {% set cpapSetupDueColor = 'red' %}
                {% endif %}
            {% endif %}

            {% set invoiceIsDue = false %}
            {% set invoiceDueColor = '' %}
            {% if record.invoice_no == '' or record.invoice_no == null %}
                {% if date('now'|date('Y-m-d')|date_modify("+3 day")|date('Y-m-d')) >= date(receiveDateToInvoice)
                    or date('now'|date('Y-m-d')|date_modify("+2 day")|date('Y-m-d')) >= date(receiveDateToInvoice)
                    or date('now'|date('Y-m-d')|date_modify("+1 day")|date('Y-m-d')) >= date(receiveDateToInvoice)
                %}
                    {% set invoiceIsDue = true %}
                {% endif %}

                {% if date('now'|date('Y-m-d')|date_modify("+3 day")|date('Y-m-d')) == date(receiveDateToInvoice) %}
                    {% set invoiceDueColor = '#feff4e' %}
                {% elseif date('now'|date('Y-m-d')|date_modify("+2 day")|date('Y-m-d')) == date(receiveDateToInvoice) %}
                    {% set invoiceDueColor = 'orange' %}
                {% elseif date('now'|date('Y-m-d')|date_modify("+1 day")|date('Y-m-d')) >= date(receiveDateToInvoice) %}
                    {% set invoiceDueColor = 'red' %}
                {% endif %}
            {% endif %}

            {% set ps_split = record.patientServices|split(',') %}

            <tr class="{{ trC }}">
                <td>{{ record.referralDate }}</td>
                <td>{{ record.receiveDate }}</td>
                <td>{{ record.fax_category }}</td>
                <td>
                    {% if record.faxId != '' %}
                        <div>
                            {{ record.receivedFax ? record.receivedFax : '8332624798' }} (<a href="{{ path('fax_view', {id:record.faxId|default('null')}) }}" target="_blank">View</a>)
                        </div>
                    {% endif %}
                </td>
                <td><a href="{{ path('patient_detail', {patient_id:record.id|base64Encode}) }}" target="_blank">{{ record.full_name }}</a></td>
                <td>{{ record.primaryClinician }}</td>
                <td>{{ record.referringHealthFacility }}</td>
                <td>{{ record.referringProvider }}</td>
                <td>
                    {% for ps in ps_split %}
                        {% set patientService = ps|split('|') %}
                        <div>{{ patientService[1]|default('') }}</div>
                    {% endfor %}
                </td>
                <td>{{ record.prevHealthFacility }}</td>
                <td>{{ record.healthFacility }}</td>
                <td>
                    {% if record.ackDate != 'null' and record.ackDate != '' %}
                        {{ record.ackDate }}(<a href="{{ path('patient_letter_preview', {patientLetter_id : record.ackId|default('null')|base64Encode, letter_type:record.ackLetterType|default('null')}) }}" target="_blank">Preview</a>)
                    {% endif %}
                </td>
                <td>{{ record.ackStatus }}</td>
                <td>
                    {{ record.hsatChartDate }}
                    {% if record.hsatChartIdEncoded == 'null' or record.hsatChartIdEncoded == '' %}
                        {% set isHsatService = false %}
                        {% for ps in ps_split %}
                            {% set patientService = ps|split('|') %}
                            {% if patientService[1]|default('') == 'Home Sleep Apnea Test (HSAT)' or patientService[0]|default('') == '' %}
                                {% set isHsatService = true %}
                            {% endif %}
                        {% endfor %}
                        {% if isHsatService == true %}
                            <strong style="color: {{ hsatDueColor }}">{{ hsatIsDue == true ? 'Due' : '' }}</strong>
                        {% endif %}
                    {% endif %}
                </td>
                <td>
                    {% if record.hsatChartIdEncoded != 'null' and record.hsatChartIdEncoded != '' %}
                        <a href="{{ path('patient_chart_form',{patient_id:record.id|base64Encode,action:'u',id:record.hsatChartIdEncoded}) }}" target="_blank">{{ record.hsatChartType }}</a>
                    {% endif %}
                </td>
                <td>{{ record.hsatChartStatus }}</td>
                <td>
                    {{ record.hrdChartDate }}
                    {% if record.hrdChartIdEncoded == 'null' or record.hrdChartIdEncoded == '' %}
                        {% if record.isHsatRepeatChecked == true %}
                            {% set isHsatService = false %}
                            {% for ps in ps_split %}
                                {% set patientService = ps|split('|') %}
                                {% if patientService[1]|default('') == 'Home Sleep Apnea Test (HSAT)' or patientService[0]|default('') == '' %}
                                    {% set isHsatService = true %}
                                {% endif %}
                            {% endfor %}
                            {% if isHsatService == true %}
                                <strong style="color: {{ hsatRepeatDueColor }}">{{ hsatRepeatIsDue == true ? 'Due' : '' }}</strong>
                            {% endif %}
                        {% endif %}
                    {% endif %}
                </td>
                <td>
                    {% if record.hrdChartIdEncoded != 'null' and record.hrdChartIdEncoded != '' %}
                        <a href="{{ path('patient_chart_form',{patient_id:record.id|base64Encode,action:'u',id:record.hrdChartIdEncoded}) }}" target="_blank">{{ record.hrdChartType }}</a>
                    {% endif %}
                </td>
                <td>{{ record.hrdChartStatus }}</td>
                <td>
                    {{ record.hriChartDate }}
                    {% if record.hriChartIdEncoded == 'null' or record.hriChartIdEncoded == '' %}
                        {% set isHsatService = false %}
                        {% for ps in ps_split %}
                            {% set patientService = ps|split('|') %}
                            {% if patientService[1]|default('') == 'Home Sleep Apnea Test (HSAT)' or patientService[0]|default('') == '' %}
                                {% set isHsatService = true %}
                            {% endif %}
                        {% endfor %}
                        {% if isHsatService == true %}
                            <strong style="color: {{ hriDueColor }}">{{ hriIsDue == true ? 'Due' : '' }}</strong>
                        {% endif %}
                    {% endif %}
                </td>
                <td>
                    {% if record.hriChartIdEncoded != 'null' and record.hriChartIdEncoded != '' %}
                        <a href="{{ path('patient_chart_form',{patient_id:record.id|base64Encode,action:'u',id:record.hriChartIdEncoded}) }}" target="_blank">{{ record.hriChartType }}</a>
                    {% endif %}
                </td>
                <td>{{ record.hriChartStatus }}</td>
                <td>
                    {{ record.pftChartDate }}
                    {% if record.pftChartIdEncoded == 'null' or record.pftChartIdEncoded == '' %}
                        {% set isHsatService = false %}
                        {% for ps in ps_split %}
                            {% set patientService = ps|split('|') %}
                            {% if patientService[1]|default('') == 'Full Pulmonary Function' %}
                                {% set isHsatService = true %}
                            {% endif %}
                        {% endfor %}
                        {% if isHsatService == true %}
                            <strong style="color: {{ pftDueColor }}">{{ pftIsDue == true ? 'Due' : '' }}</strong>
                        {% endif %}
                    {% endif %}
                </td>
                <td>
                    {% if record.pftChartIdEncoded != 'null' and record.pftChartIdEncoded != '' %}
                        <a href="{{ path('patient_chart_form',{patient_id:record.id|base64Encode,action:'u',id:record.pftChartIdEncoded}) }}" target="_blank">{{ record.pftChartType }}</a>
                    {% endif %}
                </td>
                <td>{{ record.pftChartStatus }}</td>
                <td>
                    {% set pf_split = record.patientFiles|split(',') %}
                    {% for pf in pf_split %}
                        {% set patientFiles = pf|split('|') %}
                        <div>
                            {{ patientFiles[1]|default('') }}
                            {% if patientFiles[0]|default('') != 'null' and patientFiles[0]|default('') != '' %}
                                (<a href="{{ path('patient_file_download', {patientFile_id:patientFiles[0]|base64Encode|default('null')}) }}">Download</a>)
                            {% endif %}
                        </div>
                    {% endfor %}
                </td>
                <td>
                    {{ record.preChartDate }}
                    {% if record.preChartIdEncoded == 'null' or record.preChartIdEncoded == '' %}
                        {% set isHsatService = false %}
                        {% for ps in ps_split %}
                            {% set patientService = ps|split('|') %}
                            {% if patientService[1]|default('') == 'Home Sleep Apnea Test (HSAT)' or patientService[0]|default('') == '' %}
                                {% set isHsatService = true %}
                            {% endif %}
                        {% endfor %}
                        {% if isHsatService == true %}
                            <strong style="color: {{ prescriptionDueColor }}">{{ prescriptionIsDue == true ? 'Due' : '' }}</strong>
                        {% endif %}

                    {% endif %}
                </td>
                <td>
                    {% if record.preChartIdEncoded != 'null' and record.preChartIdEncoded != '' %}
                        <a href="{{ path('patient_chart_form',{patient_id:record.id|base64Encode,action:'u',id:record.preChartIdEncoded}) }}" target="_blank">{{ record.preChartType }}</a>
                    {% endif %}
                </td>
                <td>{{ record.preChartStatus }}</td>
                <td>
                    {% if record.signedPrescriptionDate != 'null' and record.signedPrescriptionDate != ''  %}
                        {{ record.signedPrescriptionDate }} (<a href="{{ path('patient_chart_form', {patient_id:record.id|base64Encode,action:'u',id:record.signedPrescriptionId}) }}" target="_blank">Preview</a>)
                    {% endif %}
                </td>
                <td>
                    {% if record.latestLetterSent != 'null' and record.latestLetterSent != ''  %}
                        {{ record.latestLetterSent }} (<a href="{{ path('patient_letter_preview', {patientLetter_id:record.latestLetterSentIdEncoded,letter_type:record.latestLetterSent}) }}" target="_blank">Preview</a>)
                    {% endif %}
                </td>
                <td>
                    {{ record.cpapSetupDate }}
                    {% if record.cpapSetupIdEncoded == 'null' or record.cpapSetupIdEncoded == '' %}
                        {% set isHsatService = false %}
                        {% for ps in ps_split %}
                            {% set patientService = ps|split('|') %}
                            {% if patientService[1]|default('') == 'Home Sleep Apnea Test (HSAT)' or patientService[0]|default('') == '' %}
                                {% set isHsatService = true %}
                            {% endif %}
                        {% endfor %}
                        {% if isHsatService == true %}
                            <strong style="color: {{ cpapSetupDueColor }}">{{ cpapSetupIsDue == true ? 'Due' : '' }}</strong>
                        {% endif %}
                    {% endif %}
                </td>
                <td>
                    {% if record.cpapSetupIdEncoded != 'null' and record.cpapSetupIdEncoded != '' %}
                        <a href="{{ path('patient_chart_form',{patient_id:record.id|base64Encode,action:'u',id:record.cpapSetupIdEncoded}) }}" target="_blank">{{ record.cpapSetupType }}</a>
                    {% endif %}
                </td>
                <td>{{ record.cpapSetupStatus }}</td>
                <td>
                    {% if record.invoice_no != 'null' and record.invoice_no != '' %}
                        <a href="{{ path('patient_detail',{patient_id:record.id|base64Encode, tab:'b', invoiceNo:record.invoice_no}) }}" target="_blank">{{ record.invoice_no }}</a>
                    {% endif %}
                    {% if record.invoice_no == 'null' or record.invoice_no == '' %}
                        {% set isHsatService = false %}
                        {% for ps in ps_split %}
                            {% set patientService = ps|split('|') %}
                            {% if patientService[1]|default('') == 'Home Sleep Apnea Test (HSAT)' or patientService[0]|default('') == '' %}
                                {% set isHsatService = true %}
                            {% endif %}
                        {% endfor %}
                        {% if isHsatService == true %}
                            <strong style="color: {{ invoiceDueColor }}">{{ invoiceIsDue == true ? 'Due' : '' }}</strong>
                        {% endif %}
                    {% endif %}
                </td>
                <td>
                    {% if record.ticketExist == 'true' %}
                        <a class="href-note-form" data-patient-id="{{ record.id|base64Encode }}" href="javascript:void(0)" >View</a>
                    {% endif %}
                </td>
                <td>
                    {% if record.patientNoteExist == 'true' %}
                        <a class="href-patient-note-form" data-patient-id="{{ record.id|base64Encode }}" href="javascript:void(0)" >View</a>
                    {% endif %}
                </td>
            </tr>
        {% endfor %}
    </table>
    <div>Total Records: {{ totalCount }}</div>
    <div class="row max-width-initial">
        <div class="small-6 columns col-left">
            <h5 class="report-header">Booked Appointments</h5>
            <table class="report-data" data-floathead="false" width="100%" style="margin-bottom: 20px;">
                <thead class="header">
                <tr>
                    <th width="30%">Encounter Type</th>
                    <th width="30%">Count</th>
                    <th class="text-right">{#<i class="fa fa-sort"></i>#}</th>
                </tr>
                </thead>
                <tbody>
                <tr>
                    <td>
                        <div>HSAT</div>
                        <div class="sub-table" style="display: none;">
                            {% set totalHsatBooked = 0 %}
                            {% for key, desc in records.summary.booked.hsat %}
                                {% set totalHsatBooked = totalHsatBooked + desc %}
                                <div class="row">
                                    <div class="small-6 columns">{{ key }}</div>
                                    <div class="small-2 columns">:</div>
                                    <div class="small-2 columns">{{ desc }}</div>
                                    <div class="small-2 columns"></div>
                                </div>
                            {% endfor %}
                        </div>
                    </td>
                    <td class="toggle-sub-table"><strong>{{ totalHsatBooked }}</strong></td>
                    <td class="toggle-sub-table text-right"><i class="fa fa-caret-down"></i></td>
                </tr>
                <tr>
                    <td>
                        <div>HSAT Repeat</div>
                        <div class="sub-table" style="display: none;">
                            {% set totalHsatRepeatBooked = 0 %}
                            {% for key, desc in records.summary.booked.hsatRepeat %}
                                {% set totalHsatRepeatBooked = totalHsatRepeatBooked + desc %}
                                <div class="row">
                                    <div class="small-6 columns">{{ key }}</div>
                                    <div class="small-2 columns">:</div>
                                    <div class="small-2 columns">{{ desc }}</div>
                                    <div class="small-2 columns"></div>
                                </div>
                            {% endfor %}
                        </div>
                    </td>
                    <td class="toggle-sub-table"><strong>{{ totalHsatRepeatBooked }}</strong></td>
                    <td class="toggle-sub-table text-right"><i class="fa fa-caret-down"></i></td>
                </tr>
                <tr>
                    <td>
                        <div>HSAT Results Interpretation</div>
                        <div class="sub-table" style="display: none;">
                            {% set totalHriBooked = 0 %}
                            {% for key, desc in records.summary.booked.hri %}
                                {% set totalHriBooked = totalHriBooked + desc %}
                                <div class="row">
                                    <div class="small-6 columns">{{ key }}</div>
                                    <div class="small-2 columns">:</div>
                                    <div class="small-2 columns">{{ desc }}</div>
                                    <div class="small-2 columns"></div>
                                </div>
                            {% endfor %}
                        </div>
                    </td>
                    <td class="toggle-sub-table"><strong>{{ totalHriBooked }}</strong></td>
                    <td class="toggle-sub-table text-right"><i class="fa fa-caret-down"></i></td>
                </tr>
                <tr>
                    <td>
                        <div>PFT</div>
                        <div class="sub-table" style="display: none;">
                            {% set totalPftBooked = 0 %}
                            {% for key, desc in records.summary.booked.pft %}
                                {% set totalPftBooked = totalPftBooked + desc %}
                                <div class="row">
                                    <div class="small-6 columns">{{ key }}</div>
                                    <div class="small-2 columns">:</div>
                                    <div class="small-2 columns">{{ desc }}</div>
                                    <div class="small-2 columns"></div>
                                </div>
                            {% endfor %}
                        </div>
                    </td>
                    <td class="toggle-sub-table"><strong>{{ totalPftBooked }}</strong></td>
                    <td class="toggle-sub-table text-right"><i class="fa fa-caret-down"></i></td>
                </tr>
                <tr>
                    <td>
                        <div>Prescription</div>
                        <div class="sub-table" style="display: none;">
                            {% set totalPrescriptionBooked = 0 %}
                            {% for key, desc in records.summary.booked.prescription %}
                                {% set totalPrescriptionBooked = totalPrescriptionBooked + desc %}
                                <div class="row">
                                    <div class="small-6 columns">{{ key }}</div>
                                    <div class="small-2 columns">:</div>
                                    <div class="small-2 columns">{{ desc }}</div>
                                    <div class="small-2 columns"></div>
                                </div>
                            {% endfor %}
                        </div>
                    </td>
                    <td class="toggle-sub-table"><strong>{{ totalPrescriptionBooked }}</strong></td>
                    <td class="toggle-sub-table text-right"><i class="fa fa-caret-down"></i></td>
                </tr>
                <tr>
                    <td>
                        <div>CPAP Setup</div>
                        <div class="sub-table" style="display: none;">
                            {% set totalCpapSetupBooked = 0 %}
                            {% for key, desc in records.summary.booked.cpapSetup %}
                                {% set totalCpapSetupBooked = totalCpapSetupBooked + desc %}
                                <div class="row">
                                    <div class="small-6 columns">{{ key }}</div>
                                    <div class="small-2 columns">:</div>
                                    <div class="small-2 columns">{{ desc }}</div>
                                    <div class="small-2 columns"></div>
                                </div>
                            {% endfor %}
                        </div>
                    </td>
                    <td class="toggle-sub-table"><strong>{{ totalCpapSetupBooked }}</strong></td>
                    <td class="toggle-sub-table text-right"><i class="fa fa-caret-down"></i></td>
                </tr>
                <tr>
                    <td>
                        <div>Invoice</div>
                        <div class="sub-table" style="display: none;">
                            {% set totalInvoiceBooked = 0 %}
                            {% for key, desc in records.summary.booked.invoice %}
                                {% set totalInvoiceBooked = totalInvoiceBooked + desc %}
                                <div class="row">
                                    <div class="small-6 columns">{{ key|default('Count') }}</div>
                                    <div class="small-2 columns">:</div>
                                    <div class="small-2 columns">{{ desc }}</div>
                                    <div class="small-2 columns"></div>
                                </div>
                            {% endfor %}
                        </div>
                    </td>
                    <td class="toggle-sub-table"><strong>{{ totalInvoiceBooked }}</strong></td>
                    <td class="toggle-sub-table text-right"><i class="fa fa-caret-down"></i></td>
                </tr>
                </tbody>
            </table>
        </div>
        <div class="small-6 columns col-right">
            <h5 class="report-header">Unbooked Appointments</h5>
            <table class="report-data" data-floathead="false" width="100%" style="margin-bottom: 20px;">
                <thead class="header">
                <tr>
                    <th width="30%">Encounter Type</th>
                    <th width="30%">Count</th>
                    <th class="text-right">{#<i class="fa fa-sort"></i>#}</th>
                </tr>
                </thead>
                <tbody>
                <tr>
                    <td>
                        <div>HSAT</div>
                        <div class="sub-table" style="display: none;">
                            {% set totalHsatUnbooked = 0 %}
                            {% for key, desc in records.summary.unbooked.hsat %}
                                {% set totalHsatUnbooked = totalHsatUnbooked + desc %}
                                <div class="row">
                                    <div class="small-6 columns">{{ key|default('Count') }}</div>
                                    <div class="small-2 columns">:</div>
                                    <div class="small-2 columns">{{ desc }}</div>
                                    <div class="small-2 columns"></div>
                                </div>
                            {% endfor %}
                        </div>
                    </td>
                    <td class="toggle-sub-table"><strong>{{ totalHsatUnbooked }}</strong></td>
                    <td class="toggle-sub-table text-right"><i class="fa fa-caret-down"></i></td>
                </tr>
                <tr>
                    <td>
                        <div>HSAT Repeat</div>
                        <div class="sub-table" style="display: none;">
                            {% set totalHsatRepeatUnbooked = 0 %}
                            {% for key, desc in records.summary.unbooked.hsatRepeat %}
                                {% set totalHsatRepeatUnbooked = totalHsatRepeatUnbooked + desc %}
                                <div class="row">
                                    <div class="small-6 columns">{{ key|default('Count') }}</div>
                                    <div class="small-2 columns">:</div>
                                    <div class="small-2 columns">{{ desc }}</div>
                                    <div class="small-2 columns"></div>
                                </div>
                            {% endfor %}
                        </div>
                    </td>
                    <td class="toggle-sub-table"><strong>{{ totalHsatRepeatUnbooked }}</strong></td>
                    <td class="toggle-sub-table text-right"><i class="fa fa-caret-down"></i></td>
                </tr>
                <tr>
                    <td>
                        <div>HSAT Results Interpretation</div>
                        <div class="sub-table" style="display: none;">
                            {% set totalHriUnbooked = 0 %}
                            {% for key, desc in records.summary.unbooked.hri %}
                                {% set totalHriUnbooked = totalHriUnbooked + desc %}
                                <div class="row">
                                    <div class="small-6 columns">{{ key|default('Count') }}</div>
                                    <div class="small-2 columns">:</div>
                                    <div class="small-2 columns">{{ desc }}</div>
                                    <div class="small-2 columns"></div>
                                </div>
                            {% endfor %}
                        </div>
                    </td>
                    <td class="toggle-sub-table"><strong>{{ totalHriUnbooked }}</strong></td>
                    <td class="toggle-sub-table text-right"><i class="fa fa-caret-down"></i></td>
                </tr>
                <tr>
                    <td>
                        <div>PFT</div>
                        <div class="sub-table" style="display: none;">
                            {% set totalPftUnbooked = 0 %}
                            {% for key, desc in records.summary.unbooked.pft %}
                                {% set totalPftUnbooked = totalPftUnbooked + desc %}
                                <div class="row">
                                    <div class="small-6 columns">{{ key|default('Count') }}</div>
                                    <div class="small-2 columns">:</div>
                                    <div class="small-2 columns">{{ desc }}</div>
                                    <div class="small-2 columns"></div>
                                </div>
                            {% endfor %}
                        </div>
                    </td>
                    <td class="toggle-sub-table"><strong>{{ totalPftUnbooked }}</strong></td>
                    <td class="toggle-sub-table text-right"><i class="fa fa-caret-down"></i></td>
                </tr>
                <tr>
                    <td>
                        <div>Prescription</div>
                        <div class="sub-table" style="display: none;">
                            {% set totalPrescriptionUnbooked = 0 %}
                            {% for key, desc in records.summary.unbooked.prescription %}
                                {% set totalPrescriptionUnbooked = totalPrescriptionUnbooked + desc %}
                                <div class="row">
                                    <div class="small-6 columns">{{ key|default('Count') }}</div>
                                    <div class="small-2 columns">:</div>
                                    <div class="small-2 columns">{{ desc }}</div>
                                    <div class="small-2 columns"></div>
                                </div>
                            {% endfor %}
                        </div>
                    </td>
                    <td class="toggle-sub-table"><strong>{{ totalPrescriptionUnbooked }}</strong></td>
                    <td class="toggle-sub-table text-right"><i class="fa fa-caret-down"></i></td>
                </tr>
                <tr>
                    <td>
                        <div>CPAP Setup</div>
                        <div class="sub-table" style="display: none;">
                            {% set totalCpapSetupUnbooked = 0 %}
                            {% for key, desc in records.summary.unbooked.cpapSetup %}
                                {% set totalCpapSetupUnbooked = totalCpapSetupUnbooked + desc %}
                                <div class="row">
                                    <div class="small-6 columns">{{ key|default('Count') }}</div>
                                    <div class="small-2 columns">:</div>
                                    <div class="small-2 columns">{{ desc }}</div>
                                    <div class="small-2 columns"></div>
                                </div>
                            {% endfor %}
                        </div>
                    </td>
                    <td class="toggle-sub-table"><strong>{{ totalCpapSetupUnbooked }}</strong></td>
                    <td class="toggle-sub-table text-right"><i class="fa fa-caret-down"></i></td>
                </tr>
                <tr>
                    <td>
                        <div>Invoice</div>
                        <div class="sub-table" style="display: none;">
                            {% set totalInvoiceUnbooked = 0 %}
                            {% for key, desc in records.summary.unbooked.invoice %}
                                {% set totalInvoiceUnbooked = totalInvoiceUnbooked + desc %}
                                <div class="row">
                                    <div class="small-6 columns">{{ key|default('Count') }}</div>
                                    <div class="small-2 columns">:</div>
                                    <div class="small-2 columns">{{ desc }}</div>
                                    <div class="small-2 columns"></div>
                                </div>
                            {% endfor %}
                        </div>
                    </td>
                    <td class="toggle-sub-table"><strong>{{ totalInvoiceUnbooked }}</strong></td>
                    <td class="toggle-sub-table text-right"><i class="fa fa-caret-down"></i></td>
                </tr>
                </tbody>
            </table>
        </div>
    </div>
    <div class="reveal" id="modal-small" data-reveal>
        <div id="modal-content-small"></div>
        <button class="close-button" data-close aria-label="Close modal" type="button">
            <span aria-hidden="true">&times;</span>
        </button>
    </div>
    {% include '@RxConnexion/Admin/js.html.twig' %}
    <script>
        //<![CDATA[
        jQ(document).ready(function(){
            report_preview.settings.view_noteUrl = '{{ path('report_view_note') }}';
            report_preview.settings.view_patient_noteUrl = '{{ path('report_view_patient_note') }}';
            report_preview.init();

            let tgl = 0;
            jQ(".toggle-sub-table").click(function (e) {
                let subTable = jQ(this).parent().find('.sub-table');
                subTable.slideToggle();
                if(tgl === 0){
                    tgl = 1;
                    jQ('html,body').animate({
                        scrollTop: jQ(window).scrollTop() + 5
                    });
                } else if(tgl === 1){
                    tgl = 0;
                    jQ('html,body').animate({
                        scrollTop: jQ(window).scrollTop() - 5
                    });
                }
            });

            jQ(".toggle-sub-table-all").click(function (e) {
                jQ('.sub-table').slideToggle();
            })
        });
        //]]>
    </script>
{% endblock %}