{% extends '@RxConnexion/Admin/global.html.twig' %}
{% block body %}
    {% set totalReferrals = 0 %}
    {% for record in records %}
        {% set totalReferrals = totalReferrals + 1 %}
    {% endfor %}
    <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;
        }
    </style>
    <h5 class="report-header" style="width: 200%;">{{ page_title }} (Referral Date: {{ dateFrom != 'null' ? dateFrom : 'N/A' }} - {{ dateTo != 'null' ? dateTo : 'N/A' }})</h5>
    <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><a href="{{ path('report_link_patient_progress_report_redirect_preview', { date_from:dateFrom, date_to:dateTo, receive_date_from:receiveDateFrom, receive_date_to:receiveDateTo, branch:'null', clinician:'null',booked:'null',unbooked:'null' }) }}"><strong>All</strong></a></td>
                </tr>
                <tr>
                    <td>
                        <a href="{{ path('report_link_patient_progress_report_redirect_preview', { date_from:dateFrom, date_to:dateTo, receive_date_from:receiveDateFrom, receive_date_to:receiveDateTo, branch:'null', clinician:'null',booked:'HSAT',unbooked:'null' }) }}">HSAT</a>
                        <div class="sub-table" style="display: none;">
                            {% for key, desc in records.summary.hsat %}
                                <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>{{ summary.booked.hsatCount }}</strong></td>
                    <td class="toggle-sub-table text-right"><i class="fa fa-caret-down"></i></td>
                    {#<td><strong>{{ summary.hsatPerc }}</strong></td>#}
                </tr>
                <tr>
                    <td>
                        <a href="{{ path('report_link_patient_progress_report_redirect_preview', { date_from:dateFrom, date_to:dateTo, receive_date_from:receiveDateFrom, receive_date_to:receiveDateTo, branch:'null', clinician:'null',booked:'HSAT (Mobile)',unbooked:'null' }) }}">HSAT (Mobile)</a>
                        <div class="sub-table" style="display: none;">
                            {% for key, desc in records.summary.hsatMobile %}
                                <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>{{ summary.booked.hsatMobileCount }}</strong></td>
                    <td class="toggle-sub-table text-right"><i class="fa fa-caret-down"></i></td>
                    {#<td><strong>{{ summary.hsatPerc }}</strong></td>#}
                </tr>
                <tr>
                    <td>
                        <a href="{{ path('report_link_patient_progress_report_redirect_preview', { date_from:dateFrom, date_to:dateTo, receive_date_from:receiveDateFrom, receive_date_to:receiveDateTo, branch:'null', clinician:'null',booked:'HSAT Results Interpretation',unbooked:'null' }) }}">HSAT Results Interpretation</a>
                        <div class="sub-table" style="display: none;">
                            {% for key, desc in records.summary.hri %}
                                <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>{{ summary.booked.hsatResultInterCount }}</strong></td>
                    <td class="toggle-sub-table text-right"><i class="fa fa-caret-down"></i></td>
                    {#<td><strong>{{ summary.hsatResultInterPerc }}</strong></td>#}
                </tr>
                <tr>
                    <td>
                        <a href="{{ path('report_link_patient_progress_report_redirect_preview', { date_from:dateFrom, date_to:dateTo, receive_date_from:receiveDateFrom, receive_date_to:receiveDateTo, branch:'null', clinician:'null',booked:'CPAP Setup',unbooked:'null' }) }}">CPAP Setup</a>
                        <div class="sub-table" style="display: none;">
                            {% for key, desc in records.summary.cpapSetup %}
                                <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>{{ summary.booked.cpapSetupCount }}</strong></td>
                    <td class="toggle-sub-table text-right"><i class="fa fa-caret-down"></i></td>
                    {#<td><strong>{{ summary.cpapSetupPerc }}</strong></td>#}
                </tr>
                <tr>
                    <td>
                        <a href="{{ path('report_link_patient_progress_report_redirect_preview', { date_from:dateFrom, date_to:dateTo, receive_date_from:receiveDateFrom, receive_date_to:receiveDateTo, branch:'null', clinician:'null',booked:'CPAP Eval',unbooked:'null' }) }}">CPAP Eval</a>
                        <div class="sub-table" style="display: none;">
                            {% for key, desc in records.summary.cpapEval %}
                                <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>{{ summary.booked.cpapEvalCount }}</strong></td>
                    <td class="toggle-sub-table text-right"><i class="fa fa-caret-down"></i></td>
                    {#<td><strong>{{ summary.cpapEvalPerc }}</strong></td>#}
                </tr>
                <tr>
                    <td>
                        <a href="{{ path('report_link_patient_progress_report_redirect_preview', { date_from:dateFrom, date_to:dateTo, receive_date_from:receiveDateFrom, receive_date_to:receiveDateTo, branch:'null', clinician:'null',booked:'CPAP End Eval',unbooked:'null' }) }}">CPAP End Eval</a>
                        <div class="sub-table" style="display: none;">
                            {% for key, desc in records.summary.cpapEndEval %}
                                <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>{{ summary.booked.cpapEndEvalCount }}</strong></td>
                    <td class="toggle-sub-table text-right"><i class="fa fa-caret-down"></i></td>
                    {#<td><strong>{{ summary.cpapEndEvalPerc }}</strong></td>#}
                </tr>
                <tr>
                    <td>
                        <a href="{{ path('report_link_patient_progress_report_redirect_preview', { date_from:dateFrom, date_to:dateTo, receive_date_from:receiveDateFrom, receive_date_to:receiveDateTo, branch:'null', clinician:'null',booked:'CPAP Setup Sold',unbooked:'null' }) }}">CPAP Setup Sold</a>
                        <div class="sub-table" style="display: none;">
                            {% for key, desc in records.summary.cpapSetupSold %}
                                <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>{{ summary.booked.cpapSetupSoldCount }}</strong></td>
                    <td class="toggle-sub-table text-right"><i class="fa fa-caret-down"></i></td>
                    {#<td><strong>{{ summary.cpapSetupSoldPerc }}</strong></td>#}
                </tr>
                <tr>
                    <td>
                        <a href="{{ path('report_link_patient_progress_report_redirect_preview', { date_from:dateFrom, date_to:dateTo, receive_date_from:receiveDateFrom, receive_date_to:receiveDateTo, branch:'null', clinician:'null',booked:'Invoices',unbooked:'null' }) }}">Invoices</a>
                    </td>
                    <td><strong>{{ summary.booked.invoicesCount }}</strong></td>
                    <td></td>
                    {#<td><strong>{{ summary.invoicesPerc }}</strong></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>Count</th>
                </tr>
                </thead>
                <tbody>
                <tr>
                    <td><a href="{{ path('report_link_patient_progress_report_redirect_preview', { date_from:dateFrom, date_to:dateTo, receive_date_from:receiveDateFrom, receive_date_to:receiveDateTo, branch:'null', clinician:'null',booked:'null',unbooked:'null' }) }}"><strong>All</strong></a></td>
                </tr>
                <tr>
                    <td><a href="{{ path('report_link_patient_progress_report_redirect_preview', { date_from:dateFrom, date_to:dateTo, receive_date_from:receiveDateFrom, receive_date_to:receiveDateTo, branch:'null', clinician:'null',booked:'null',unbooked:'HSAT' }) }}">HSAT</a></td>
                    <td><strong>{{ summary.unbooked.hsatCount|default(0) }}</strong></td>
                </tr>
                <tr>
                    <td><a href="{{ path('report_link_patient_progress_report_redirect_preview', { date_from:dateFrom, date_to:dateTo, receive_date_from:receiveDateFrom, receive_date_to:receiveDateTo, branch:'null', clinician:'null',booked:'null',unbooked:'HSAT (Mobile)' }) }}">HSAT (Mobile)</a></td>
                    <td><strong>{{ summary.unbooked.hsatMobileCount|default(0) }}</strong></td>
                </tr>
                <tr>
                    <td><a href="{{ path('report_link_patient_progress_report_redirect_preview', { date_from:dateFrom, date_to:dateTo, receive_date_from:receiveDateFrom, receive_date_to:receiveDateTo, branch:'null', clinician:'null',booked:'null',unbooked:'HSAT Results Interpretation' }) }}">HSAT Results Interpretation</a></td>
                    <td><strong>{{ summary.unbooked.hsatResultInterCount|default(0) }}</strong></td>
                </tr>
                <tr>
                    <td><a href="{{ path('report_link_patient_progress_report_redirect_preview', { date_from:dateFrom, date_to:dateTo, receive_date_from:receiveDateFrom, receive_date_to:receiveDateTo, branch:'null', clinician:'null',booked:'null',unbooked:'CPAP Setup' }) }}">CPAP Setup</a></td>
                    <td><strong>{{ summary.unbooked.cpapSetupCount|default(0) }}</strong></td>
                </tr>
                <tr>
                    <td><a href="{{ path('report_link_patient_progress_report_redirect_preview', { date_from:dateFrom, date_to:dateTo, receive_date_from:receiveDateFrom, receive_date_to:receiveDateTo, receive_date_from:receiveDateFrom, receive_date_to:receiveDateTo, branch:'null', clinician:'null',booked:'null',unbooked:'CPAP Eval' }) }}">CPAP Eval</a></td>
                    <td><strong>{{ summary.unbooked.cpapEvalCount|default(0) }}</strong></td>
                </tr>
                <tr>
                    <td><a href="{{ path('report_link_patient_progress_report_redirect_preview', { date_from:dateFrom, date_to:dateTo, receive_date_from:receiveDateFrom, receive_date_to:receiveDateTo, branch:'null', clinician:'null',booked:'null',unbooked:'CPAP End Eval' }) }}">CPAP End Eval</a></td>
                    <td><strong>{{ summary.unbooked.cpapEndEvalCount|default(0) }}</strong></td>
                </tr>
                <tr>
                    <td><a href="{{ path('report_link_patient_progress_report_redirect_preview', { date_from:dateFrom, date_to:dateTo, receive_date_from:receiveDateFrom, receive_date_to:receiveDateTo, branch:'null', clinician:'null',booked:'null',unbooked:'CPAP Setup Sold' }) }}">CPAP Setup Sold</a></td>
                    <td><strong>{{ summary.unbooked.cpapSetupSoldCount|default(0) }}</strong></td>
                </tr>
                <tr>
                    <td><a href="{{ path('report_link_patient_progress_report_redirect_preview', { date_from:dateFrom, date_to:dateTo, receive_date_from:receiveDateFrom, receive_date_to:receiveDateTo, branch:'null', clinician:'null',booked:'null',unbooked:'Invoices' }) }}">Invoices</a></td>
                    <td><strong>{{ summary.unbooked.invoicesCount|default(0) }}</strong></td>
                </tr>
                </tbody>
            </table>
        </div>
    </div>
    <div class="row max-width-initial">
        <div class="small-6 columns col-left">
            <h5 class="report-header" >Total Number of Referrals by Branch</h5>
            <table class="report-data" data-floathead="false" width="100%" style="margin-bottom: 20px;">
                <thead class="header">
                <tr>
                    <th width="30%">Branch</th>
                    <th>Count</th>
                </tr>
                </thead>
                <tbody>
                {% set branchCount = 0 %}
                {% for branchSummary in summary.branch %}
                    {% set branchCount = branchCount + branchSummary.branchCount %}
                {% endfor %}

                <tr style="border-bottom: 1px solid;">
                    <td><a href="{{ path('report_link_patient_progress_report_redirect_preview', { date_from:dateFrom, date_to:dateTo, receive_date_from:receiveDateFrom, receive_date_to:receiveDateTo, branch:'null', clinician:'null', booked:'null',unbooked:'null'}) }}"><strong>All</strong></a></td>
                    <td><strong>{{ branchCount }}</strong></td>
                </tr>
                {% for branchSummary in summary.branch %}
                    <tr>
                        <td><a href="{{ path('report_link_patient_progress_report_redirect_preview', { date_from:dateFrom, date_to:dateTo, receive_date_from:receiveDateFrom, receive_date_to:receiveDateTo, branch:branchSummary.health_facility_id, clinician:'null',booked:'null',unbooked:'null'}) }}">{{ branchSummary.branchDesc }}</a></td>
                        <td><strong>{{ branchSummary.branchCount }}</strong></td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
        <div class="small-6 columns col-right">
            <h5 class="report-header" style="">Total Number of HSATs Booked per Clinician</h5>
            <table class="report-data" data-floathead="false" width="100%" style="margin-bottom: 20px;">
                <thead class="header">
                <tr>
                    <th width="30%">Clinician</th>
                    <th>Count</th>
                </tr>
                </thead>
                <tbody>
                {% set clinicianCount = 0 %}
                {% for clinicianSummary in summary.clinician %}
                    {% set clinicianCount = clinicianCount + clinicianSummary.primaryProviderCount %}
                {% endfor %}
                <tr style="border-bottom: 1px solid;">
                    <td><a href="{{ path('report_link_patient_progress_report_redirect_preview', { date_from:dateFrom, date_to:dateTo, receive_date_from:receiveDateFrom, receive_date_to:receiveDateTo, branch:'null', clinician:'null',booked:'null',unbooked:'null'}) }}"><strong>All</strong></a></td>
                    <td><strong>{{ clinicianCount }}</strong></td>
                </tr>
                {% for clinicianSummary in summary.clinician %}
                    <tr>
                        <td><a href="{{ path('report_link_patient_progress_report_redirect_preview', { date_from:dateFrom, date_to:dateTo, receive_date_from:receiveDateFrom, receive_date_to:receiveDateTo, branch:'null', clinician:clinicianSummary.provider_id,booked:'null',unbooked:'null'}) }}">{{ clinicianSummary.primaryProvider }}</a></td>
                        <td><strong>{{ clinicianSummary.primaryProviderCount }}</strong></td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
    <div style="width: 100%; margin-bottom: 20px; padding: 5px;">
        <span style="float: left;">Total Referral(s): <strong>{{ records.count.referralCount }}</strong></span>
    </div>

    {% set filterDesc = '' %}
    {% if booked != 'null' and booked != '' %}
        {% set filterDesc = 'Booked: '~booked %}
    {% endif %}

    {% if unbooked != 'null' and unbooked != '' %}
        {% set filterDesc = 'Unbooked: '~unbooked %}
    {% endif %}

    {% if branchDesc != 'null' and branchDesc != '' %}
        {% set filterDesc = 'Branch: '~branchDesc %}
    {% endif %}

    {% if providerDesc != 'null' and providerDesc != '' %}
        {% set filterDesc = 'Primary Clinician: '~providerDesc %}
    {% endif %}

    {% if filterDesc != '' and filterDesc != null and filterDesc != 'null' %}
        <div style="width: 100%;">
            <h5 class="report-header" style="width: 200%;">{{ 'Patient Progress Report - '~filterDesc  }}</h5>
        </div>
    {% endif %}
    <table class="report-data" width="250%">
        <thead class="header">
        <tr id="header-id">
            <th>Patient ID</th>
            <th>Referral Date</th>
            <th>Received Date</th>
            <th>Patient</th>
            <th>Patient Services</th>
            <th>Previous Branch</th>
            <th>Branch</th>
            <th>Primary Clinician</th>
            {#<th>Date</th>#}
            <th>Specialist Referral</th>
            <th>HSAT Status</th>
            <th>HSAT Interval</th>
            <th>HSAT Date</th>
            <!--NEW-->
            <th>HSAT(Mobile) Status</th>
            <th>HSAT(Mobile) Date</th>

            <th>HSAT(Repeat) Status</th>
            <th>HSAT(Repeat) Date</th>

            <th>HSAT Results Interpretation Status</th>
            <th>HSAT Results Interpretation Interval</th>
            <th>HSAT Results Interpretation Date</th>
            <!--NEW-->
            <th>HSAT Results Interpretation (Telehealth) Status</th>
            <th>HSAT Results Interpretation (Telehealth) Date</th>

            <th>CPAP Setup Status</th>
            <th>CPAP Setup Interval</th>
            <th>CPAP Setup Date</th>
            <!--NEW-->
            <th>CPAP Setup (Mobile) Status</th>
            <th>CPAP Setup (Mobile) Date</th>

            <th>CPAP Setup Sold Status</th>
            <th>CPAP Setup Sold Interval</th>
            <th>CPAP Setup Sold Date</th>
            <th>CPAP Eval</th>
            <th>CPAP Eval Interval</th>
            <th>CPAP Eval Date</th>
            <th>CPAP End Eval</th>
            <th>CPAP End Eval Interval</th>
            <th>CPAP End Eval Date</th>
            <!--NEW-->
            <th>6 Months Follow Up (Mobile) Setup Status</th>
            <th>6 Months Follow Up( Mobile) Date</th>

            <th>Invoice No</th>
            <th>Billing Date</th>
        </tr>
        </thead>
        <tbody>
        {% set totalCount = 0 %}
        {% set threeDayCtr = 0 %}
        {% set twoDayCtr = 0 %}
        {% set oneDayCtr = 0 %}
        {% set hasHSAT = false %}
        {% for record in records.patientProgress %}
            {% set trC = loop.index > 1 and loop.index % 2 == 0 ? 'even' : 'odd' %}
            {% set totalCount = totalCount + 1 %}

            {#Date + exp no. of day#}
            {% set refferalToHsat = record.referralDate|date_modify("+ "~hsatExpNoOfDays|default('0')~" day")|date('Y-m-d') %}
            {% set refferalToHsatMobile = record.referralDate|date_modify("+ "~hsatMobileExpNoOfDays|default('0')~" day")|date('Y-m-d') %}
            {% set hsatInterpretation = record.referralDate|date_modify("+ "~hsatResultInterpretationNoOfDays|default('0')~" day")|date('Y-m-d') %}
            {% set cpapSetup = record.referralDate|date_modify("+ "~cpapSetupNoOfDays|default('0')~" day")|date('Y-m-d') %}
            {% set cpapSetupSold = record.referralDate|date_modify("+ "~cpapSetupSoldNoOfDays|default('0')~" day")|date('Y-m-d') %}
            {% set cpapEval = record.referralDate|date_modify("+ "~cpapEvalNoOfDays|default('0')~" day")|date('Y-m-d') %}
            {% set cpapEndEval = record.referralDate|date_modify("+ "~cpapEndEvalNoOfDays|default('0')~" day")|date('Y-m-d') %}

            {% set color = '' %}
            {% set hsatStringDue = '' %}
            {% set hsatMobileStringDue = '' %}
            {% set hriStringDue = '' %}
            {% set csStringDue = '' %}
            {% set cssStringDue = '' %}
            {% set cpapEvalStringDue = '' %}
            {% set cpapEndEvalStringDue = '' %}
            {% set invoiceStringDue = '' %}

            {% set hsatDueColor = '' %}
            {% set hsatMobileDueColor = '' %}
            {% set hriDueColor = '' %}
            {% set csDueColor = '' %}
            {% set cssDueColor = '' %}
            {% set cpapEvalDueColor = '' %}
            {% set cpapEndEvalDueColor = '' %}
            {% set invoiceDueColor = '' %}

            {% set hsatStatusColor = '' %}
            {% set hsatMobileStatusColor = '' %}
            {% set hriStatusColor = '' %}
            {% set csStatusColor = '' %}
            {% set cssStatusColor = '' %}
            {% set cpapEvalStatusColor = '' %}
            {% set cpapEndEvalStatusColor = '' %}

            {% set hsatStringMedical = '' %}
            {% set hsatMobileStringMedical = '' %}
            {% set hriStringMedical = '' %}
            {% set csStringMedical = '' %}
            {% set cssStringMedical = '' %}
            {% set cpapEvalStringMedical = '' %}
            {% set cpapEndEvalStringMedical = '' %}
            {% set invoiceStringMedical = '' %}

            {% if record.hsatChartDate == '' %}
                {#{% if 'now'|date("m/d/Y")|date_modify("+3 day")|date("m/d/Y") == refferalToHsat %}#}
                    {#{% set threeDayCtr = threeDayCtr + 1 %}#}
                    {#{% set color = '#b4c200' %}#}
                {#{% elseif 'now'|date("m/d/Y")|date_modify("+2 day")|date("m/d/Y") == refferalToHsat %}#}
                    {#{% set twoDayCtr = twoDayCtr + 1 %}#}
                    {#{% set color = 'orange' %}#}
                {% if record.hsatChartDate is null %}
                    {% set color = 'red' %}
                {% endif %}
            {% endif %}

            {% if record.ctHsatMobile == '' %}
                {#{% if 'now'|date("m/d/Y")|date_modify("+3 day")|date("m/d/Y") == refferalToHsat %}#}
                {#{% set threeDayCtr = threeDayCtr + 1 %}#}
                {#{% set color = '#b4c200' %}#}
                {#{% elseif 'now'|date("m/d/Y")|date_modify("+2 day")|date("m/d/Y") == refferalToHsat %}#}
                {#{% set twoDayCtr = twoDayCtr + 1 %}#}
                {#{% set color = 'orange' %}#}
                {% if record.ctHsatMobile is null %}
                    {% set color = 'red' %}
                {% endif %}
            {% endif %}

            {% if 'Medical Supplies' in record.branchDesc == true %}
                {% set hsatStringMedical = 'Medical' %}
                {% set hsatMobileStringMedical = 'Medical' %}
                {% set hriStringMedical = 'Medical' %}
                {% set csStringMedical = 'Medical' %}
                {% set cssStringMedical = 'Medical' %}
                {% set cpapEvalStringMedical = 'Medical' %}
                {% set cpapEndEvalStringMedical = 'Medical' %}
                {% set invoiceStringMedical = 'Medical' %}
            {% endif %}

            {#{% set ps_split = record.patientServices|split(',') %}#}
            {#{% for ps in ps_split %}#}
                {#{% set patientService = ps|split('|') %}#}
                 {#{% if patientService[1]|default('') == 'Home Sleep Apnea Test (HSAT)' %}#}
                    {#{% set hasHSAT = true %}#}
                {#{% endif %}#}
            {#{% endfor %}#}

            {% if record.hsatChartDate == '' %}
                {% if (date('now'|date('Y-m-d')|date_modify("+3 day")|date('Y-m-d')) >= date(refferalToHsat)
                    or date('now'|date('Y-m-d')|date_modify("+2 day")|date('Y-m-d')) >= date(refferalToHsat)
                    or date('now'|date('Y-m-d')|date_modify("+1 day")|date('Y-m-d')) >= date(refferalToHsat))
                %}
                    {% set hsatStringDue = 'Due' %}
                {% endif %}

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

            {% if record.pcHsatMobileDate == '' %}
                {% if date('now'|date('Y-m-d')|date_modify("+3 day")|date('Y-m-d')) >= date(refferalToHsatMobile)
                    or date('now'|date('Y-m-d')|date_modify("+2 day")|date('Y-m-d')) >= date(refferalToHsatMobile)
                    or date('now'|date('Y-m-d')|date_modify("+1 day")|date('Y-m-d')) >= date(refferalToHsatMobile)
                %}
                    {% set hsatMobileStringDue = 'Due' %}
                {% endif %}

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

            {% if record.hsatResultInterChartDate == '' %}
                {% if date('now'|date('Y-m-d')|date_modify("+3 day")|date('Y-m-d')) >= date(hsatInterpretation)
                    or date('now'|date('Y-m-d')|date_modify("+2 day")|date('Y-m-d')) >= date(hsatInterpretation)
                    or date('now'|date('Y-m-d')|date_modify("+1 day")|date('Y-m-d')) >= date(hsatInterpretation)
                %}
                    {% set hriStringDue = 'Due' %}
                {% endif %}

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

            {% if record.cpapSetupChartDate == '' %}
                {% if date('now'|date('Y-m-d')|date_modify("+3 day")|date('Y-m-d')) >= date(cpapSetup)
                    or date('now'|date('Y-m-d')|date_modify("+2 day")|date('Y-m-d')) >= date(cpapSetup)
                    or date('now'|date('Y-m-d')|date_modify("+1 day")|date('Y-m-d')) >= date(cpapSetup)
                %}
                    {% set csStringDue = 'Due' %}
                {% endif %}

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

            {% if record.cpapSetupSold == '' %}
                {% if date('now'|date('Y-m-d')|date_modify("+3 day")|date('Y-m-d')) >= date(cpapSetupSold)
                    or date('now'|date('Y-m-d')|date_modify("+2 day")|date('Y-m-d')) >= date(cpapSetupSold)
                    or date('now'|date('Y-m-d')|date_modify("+1 day")|date('Y-m-d')) >= date(cpapSetupSold)
                %}
                    {% set cssStringDue = 'Due' %}
                {% endif %}

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

            {% if record.cpapEvalChartDate == '' %}
                {% if date('now'|date('Y-m-d')|date_modify("+3 day")|date('Y-m-d')) >= date(cpapEval)
                    or date('now'|date('Y-m-d')|date_modify("+2 day")|date('Y-m-d')) >= date(cpapEval)
                    or date('now'|date('Y-m-d')|date_modify("+1 day")|date('Y-m-d')) >= date(cpapEval)
                %}
                    {% set cpapEvalStringDue = 'Due' %}
                {% endif %}

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

            {% if record.cpapEndEvalChartDate == '' %}
                {% if date('now'|date('Y-m-d')|date_modify("+3 day")|date('Y-m-d')) >= date(cpapEndEval)
                    or date('now'|date('Y-m-d')|date_modify("+2 day")|date('Y-m-d')) >= date(cpapEndEval)
                    or date('now'|date('Y-m-d')|date_modify("+1 day")|date('Y-m-d')) >= date(cpapEndEval)
                %}
                    {% set cpapEndEvalStringDue = 'Due' %}
                {% endif %}

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

            {% if record.billingDate == '' %}
                {% if date('now'|date('Y-m-d')|date_modify("+3 day")|date('Y-m-d')) >= date(cpapSetupSold)
                    or date('now'|date('Y-m-d')|date_modify("+2 day")|date('Y-m-d')) >= date(cpapSetupSold)
                    or date('now'|date('Y-m-d')|date_modify("+1 day")|date('Y-m-d')) >= date(cpapSetupSold)
                %}
                    {% set invoiceStringDue = 'Due' %}
                {% endif %}

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


            {% if (record.hsatStatusDesc == 'No Show' or record.hsatStatusDesc == 'Cancelled') %}
                {% set hsatStatusColor = 'red' %}
            {% endif %}
            {% if (record.csHsatMobile == 'No Show' or record.csHsatMobile == 'Cancelled') %}
                {% set hsatMobileStatusColor = 'red' %}
            {% endif %}
            {% if (record.hsatResultInterStatusDesc == 'No Show' or record.hsatResultInterStatusDesc == 'Cancelled') %}
                {% set hriStatusColor = 'red' %}
            {% endif %}
            {% if (record.cpapSetupStatusDesc == 'No Show' or record.cpapSetupStatusDesc == 'Cancelled') %}
                {% set csStatusColor = 'red' %}
            {% endif %}
            {% if (record.cpapSetupSoldStatusDesc == 'No Show' or record.cpapSetupSoldStatusDesc == 'Cancelled') %}
                {% set cssStatusColor = 'red' %}
            {% endif %}
            {% if (record.cpapEvalStatusDesc == 'No Show' or record.cpapEvalStatusDesc == 'Cancelled') %}
                {% set cpapEvalStatusColor = 'red' %}
            {% endif %}
            {% if (record.cpapEndEvalChartDate == 'No Show' or record.cpapEndEvalChartDate == 'Cancelled') %}
                {% set cpapEndEvalStatusColor = 'red' %}
            {% endif %}



            <tr class="{{ trC }}">
                <td>{{ record.patientId }}</td>
                <td>{{ record.referralDate }}</td>
                <td>{{ record.receivedDate }}</td>
                <td><a href="{{ path('patient_detail', {patient_id : record.patientId|base64Encode}) }}" target="_blank" style="  color: {{ color }}">{{ record.patientName }}</a></td>
                <td>
                    {% set ps_split = record.patientServices|split(',') %}
                    {% for ps in ps_split %}
                        {% set patientService = ps|split('|') %}
                        <div>{{ patientService[1]|default('') }}</div>

                    {% endfor %}
                </td>
                <td>{{ record.prevBranchDesc }}</td>
                <td>{{ record.branchDesc }}</td>
                <td>{{ record.primaryProvider }}</td>
                {#<td>{{ record.specialistStartDate }}</td>#}
                <td>{{ record.specialistFullName }}</td>
                <td style="color: {{ hsatStatusColor }};">{{ record.hsatStatusDesc }}</td>
                <td style="text-align: right;">{{ record.hsatTimeDiff }}</td>
                <td>{{ record.hsatChartDate }} <strong style="color: {{ hsatDueColor }}">{{ hsatStringMedical ? hsatStringMedical : record.hsatStatusDesc != 'Booked' ? hsatStringDue : 'Booked' }}</strong></td>

                <td>{{ record.csHsatMobile }}</td>
                <td>{{ record.pcHsatMobileDate }} <strong style="color: {{ hsatMobileDueColor }}">{{ hsatMobileStringMedical ? hsatMobileStringMedical : record.csHsatMobile != 'Booked' ? hsatMobileStringDue : 'Booked' }}</strong></td>

                <td>{{ record.csHsatRepeat }}</td>
                <td>{{ record.pcHsatRepeatDate }}</td>

                <td style="color: {{ hriStatusColor }};">{{ record.hsatResultInterStatusDesc }}</td>
                <td style="text-align: right;">{{ record.hsatInterTimeDiff }}</td>
                <td>{{ record.hsatResultInterChartDate }} <strong style="color: {{ hriDueColor }}">{{ hriStringMedical ? hriStringMedical : record.hsatResultInterStatusDesc != 'Booked' ? hriStringDue : 'Booked' }}</strong></td>

                <td>{{ record.csHriRepeat }}</td>
                <td>{{ record.pcHriRepeatDate }}</td>

                <td style="color: {{ csStatusColor }};">{{ record.cpapSetupStatusDesc }}</td>
                <td style="text-align: right;">{{ record.cpapSetupTimeDiff }}</td>
                <td>{{ record.cpapSetupChartDate }}  <strong style="color: {{ csDueColor }}">{{ csStringMedical ? csStringMedical : record.cpapSetupStatusDesc != 'Booked' ? csStringDue : 'Booked' }}</strong></td>

                <td>{{ record.csCpapTele }}</td>
                <td>{{ record.pcCpapTeleDate }}</td>

                <td style="color: {{ cssStatusColor }};">{{ record.cpapSetupSoldStatusDesc }}</td>
                <td style="text-align: right;">{{ record.cpapSetupSoldTimeDiff }}</td>
                <td>{{ record.cpapSetupSold }} <strong style="color: {{ cssDueColor }}">{{ cssStringMedical ? cssStringMedical : record.cpapSetupSoldStatusDesc != 'Booked' ? cssStringDue : 'Booked' }}</strong></td>
                <td style="color: {{ cpapEvalStatusColor }};">{{ record.cpapEvalStatusDesc }}</td>
                <td style="text-align: right;">{{ record.cpapEvalTimeDiff }}</td>
                <td>{{ record.cpapEvalChartDate }} <strong style="color: {{ cpapEvalDueColor }}">{{ cpapEvalStringMedical ? cpapEvalStringMedical : record.cpapEvalStatusDesc != 'Booked' ? cpapEvalStringDue : 'Booked' }}</strong></td>
                <td style="color: {{ cssStatusColor }};">{{ record.cpapEndEvalStatusDesc }}</td>
                <td style="text-align: right;">{{ record.cpapEndEvalTimeDiff }}</td>
                <td>{{ record.cpapEndEvalChartDate }} <strong style="color: {{ cpapEndEvalDueColor }}">{{ cpapEndEvalStringMedical ? cpapEndEvalStringMedical : record.cpapEndEvalStatusDesc != 'Booked' ? cpapEndEvalStringDue : 'Booked' }}</strong></td>

                <td>{{ record.csSixMoFfTele }}</td>
                <td>{{ record.pcSixMoFfTeleDate }}</td>

                <td><a href="{{ path('patient_detail', {patient_id : record.patientId|base64Encode, tab: 'b'}) }}" target="_blank">{{ record.invoice_no }}</a></td>
                <td>{{ record.billingDate }} <strong style="color: {{ invoiceDueColor }}">{{ invoiceStringDue }}</strong></td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
    <div>Total Records: {{ totalCount }}</div>

    {#<h5 class="report-header" style="margin-top: 5%;">Encounter Due</h5>#}
    {#<table class="report-data" width="100%" style="margin-bottom: 5%;">#}
        {#<thead class="header">#}
        {#<tr>#}
            {#<th width="30%">Phase</th>#}
            {#<th>Count</th>#}
        {#</tr>#}
        {#</thead>#}
        {#<tbody>#}
        {#<tr>#}
            {#<td>Three Days Before Due</td>#}
            {#<td><strong>{{ threeDayCtr }}</strong></td>#}
        {#</tr>#}
        {#<tr>#}
            {#<td>Two Days Before Due</td>#}
            {#<td><strong>{{ twoDayCtr }}</strong></td>#}
        {#</tr>#}
        {#<tr>#}
            {#<td>One Day Before and After Due</td>#}
            {#<td><strong>{{ oneDayCtr }}</strong></td>#}
        {#</tr>#}
        {#</tbody>#}
    {#</table>#}
    {% include '@RxConnexion/Admin/js.html.twig' %}
    <script>
        //<![CDATA[
        jQ(document).ready(function(){
            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 %}