{% extends '@RxConnexion/Admin/global.html.twig' %}
{% block body %}
    {% set totalReferrals = 0 %}
    {% for record in records %}
        {% set totalReferrals = totalReferrals + 1 %}
    {% endfor %}

    <h5 class="report-header" style="width: 200%;">{{ page_title }} (Referral Date: {{ dateFrom|default('') }} - {{ dateTo|default('') }})</h5>

    <div style="width: 100%; height: auto;">
        <div style="width: 50%; float: left; height: 100%">
            <h5 class="report-header" style="">Booked Appointments</h5>
            <table class="report-data" width="100%" style="margin-bottom: 20px;">
                <thead class="header">
                <tr>
                    <th width="30%">Encounter Type</th>
                    <th>Count</th>
                </tr>
                </thead>
                <tbody>
                <tr>
                    <td>
                        <ul class="vertical menu accordion-menu" data-accordion-menu data-multi-open="false">
                            <li>
                                <a href="javascript:void(0);" style="color: #000000;">HSAT</a>
                                <ul>
                                    {% for key, desc in records.summary.hsat %}
                                        <li>
                                            <div>{{ key }} : <div style="float: right; margin-right: 5px;">{{ desc }}</div></div>
                                        </li>
                                    {% endfor %}
                                </ul>
                            </li>
                        </ul>
                    </td>
                    <td><strong>{{ summary.booked.hsatCount }}</strong></td>
                    {#<td><strong>{{ summary.hsatPerc }}</strong></td>#}
                </tr>
                {#<tr>#}
                {#<td>Oximetry</td>#}
                {#<td><strong>{{ summary.booked.oximetryCount }}</strong></td>#}
                {#<td><strong>{{ summary.oximetryPerc }}</strong></td>#}
                {#</tr>#}
                <tr>
                    <td>
                        <ul class="vertical menu accordion-menu" data-accordion-menu data-multi-open="false" style="margin: 0;">
                            <li>
                                <a href="javascript:void(0);" style="color: #000000;">HSAT Results Interpretation</a>
                                <ul>
                                    {% for key, desc in records.summary.hri %}
                                        <li>
                                            <div>{{ key }} : <div style="float: right; margin-right: 5px;">{{ desc }}</div></div>
                                        </li>
                                    {% endfor %}
                                </ul>
                            </li>
                        </ul>
                    </td>
                    <td><strong>{{ summary.booked.hsatResultInterCount }}</strong></td>
                    {#<td><strong>{{ summary.hsatResultInterPerc }}</strong></td>#}
                </tr>
                <tr>
                    <td>
                        <ul class="vertical menu accordion-menu" data-accordion-menu data-multi-open="false">
                            <li>
                                <a href="javascript:void(0);" style="color: #000000;">CPAP Setup</a>
                                <ul>
                                    {% for key, desc in records.summary.cpapSetup %}
                                        <li>
                                            <div>{{ key }} : <div style="float: right; margin-right: 5px;">{{ desc }}</div></div>
                                        </li>
                                    {% endfor %}
                                </ul>
                            </li>
                        </ul>
                    </td>
                    <td><strong>{{ summary.booked.cpapSetupCount }}</strong></td>
                    {#<td><strong>{{ summary.cpapSetupPerc }}</strong></td>#}
                </tr>
                <tr>
                    <td>
                        <ul class="vertical menu accordion-menu" data-accordion-menu data-multi-open="false">
                            <li>
                                <a href="javascript:void(0);" style="color: #000000;">CPAP Eval</a>
                                <ul>
                                    {% for key, desc in records.summary.cpapEval %}
                                        <li>
                                            <div>{{ key }} : <div style="float: right; margin-right: 5px;">{{ desc }}</div></div>
                                        </li>
                                    {% endfor %}
                                </ul>
                            </li>
                        </ul>
                    </td>
                    <td><strong>{{ summary.booked.cpapEvalCount }}</strong></td>
                    {#<td><strong>{{ summary.cpapEvalPerc }}</strong></td>#}
                </tr>
                <tr>
                    <td>
                        <ul class="vertical menu accordion-menu" data-accordion-menu data-multi-open="false">
                            <li>
                                <a href="javascript:void(0);" style="color: #000000;">CPAP End Eval</a>
                                <ul>
                                    {% for key, desc in records.summary.cpapEndEval %}
                                        <li>
                                            <div>{{ key }} : <div style="float: right; margin-right: 5px;">{{ desc }}</div></div>
                                        </li>
                                    {% endfor %}
                                </ul>
                            </li>
                        </ul>
                    </td>
                    <td><strong>{{ summary.booked.cpapEndEvalCount }}</strong></td>
                    {#<td><strong>{{ summary.cpapEndEvalPerc }}</strong></td>#}
                </tr>
                <tr>
                    <td>
                        <ul class="vertical menu accordion-menu" data-accordion-menu data-multi-open="false">
                            <li>
                                <a href="javascript:void(0);" style="color: #000000;">CPAP Setup Sold</a>
                                <ul>
                                    {% for key, desc in records.summary.cpapSetupSold %}
                                        <li>
                                            <div>{{ key }} : <div style="float: right; margin-right: 5px;">{{ desc }}</div></div>
                                        </li>
                                    {% endfor %}
                                </ul>
                            </li>
                        </ul>
                    </td>
                    <td><strong>{{ summary.booked.cpapSetupSoldCount }}</strong></td>
                    {#<td><strong>{{ summary.cpapSetupSoldPerc }}</strong></td>#}
                </tr>
                <tr>
                    <td>
                        <ul class="vertical menu accordion-menu" data-accordion-menu data-multi-open="false">
                            <li>
                                <a href="javascript:void(0);" style="color: #000000;">Invoices</a>
                            </li>
                        </ul>
                    </td>
                    <td><strong>{{ summary.booked.invoicesCount }}</strong></td>
                    {#<td><strong>{{ summary.invoicesPerc }}</strong></td>#}
                </tr>
                </tbody>
            </table>
        </div>
        <div style="width: 49%; margin-left: 6px; float: right; height: 300px;">
            <h5 class="report-header" >Unbooked Appointments</h5>
            <table class="report-data" width="100%" style="margin-bottom: 20px;">
                <thead class="header">
                <tr>
                    <th width="30%">Encounter Type</th>
                    <th>Count</th>
                </tr>
                </thead>
                <tbody>
                <tr>
                    <td>HSAT</td>
                    <td><strong>{{ summary.unbooked.hsatCount|default(0) }}</strong></td>
                </tr>
                {#<tr>#}
                {#<td>Oximetry</td>#}
                {#<td><strong>{{ summary.unbooked.oximetryCount|default(0) }}</strong></td>#}
                {#</tr>#}
                <tr>
                    <td>HSAT Results Interpretation</td>
                    <td><strong>{{ summary.unbooked.hsatResultInterCount|default(0) }}</strong></td>
                </tr>
                <tr>
                    <td>CPAP Setup</td>
                    <td><strong>{{ summary.unbooked.cpapSetupCount|default(0) }}</strong></td>
                </tr>
                <tr>
                    <td>CPAP Eval</td>
                    <td><strong>{{ summary.unbooked.cpapEvalCount|default(0) }}</strong></td>
                </tr>
                <tr>
                    <td>CPAP End Eval</td>
                    <td><strong>{{ summary.unbooked.cpapEndEvalCount|default(0) }}</strong></td>
                </tr>
                <tr>
                    <td>CPAP Setup Sold</td>
                    <td><strong>{{ summary.unbooked.cpapSetupSoldCount|default(0) }}</strong></td>
                </tr>
                <tr>
                    <td>Invoices</td>
                    <td><strong>{{ summary.unbooked.invoicesCount|default(0) }}</strong></td>
                </tr>
                </tbody>
            </table>
        </div>
    </div>
    <div style="width: 100%; height: auto;">
        <div style="width: 50%; float: right;">
            {#<h5 class="report-header" style="">Overdue Encounters</h5>#}
            {#<table class="report-data" width="100%" style="margin-bottom: 20px;">#}
            {#<thead class="header">#}
            {#<tr>#}
            {#<th width="30%">Encounter Type</th>#}
            {#<th>Count</th>#}
            {#</tr>#}
            {#</thead>#}
            {#<tbody>#}
            {#<tr>#}
            {#<td>HSAT</td>#}
            {#<td><strong>{{ summary.overdue.hsatCount }}</strong></td>#}
            {#</tr>#}
            {#<tr>#}
            {#<td>Oximetry</td>#}
            {#<td><strong>{{ summary.overdue.oximetryCount }}</strong></td>#}
            {#</tr>#}
            {#<tr>#}
            {#<td>HSAT Results Interpretation</td>#}
            {#<td><strong>{{ summary.overdue.hsatResultInterCount }}</strong></td>#}
            {#</tr>#}
            {#<tr>#}
            {#<td>CPAP Setup</td>#}
            {#<td><strong>{{ summary.overdue.cpapSetupCount }}</strong></td>#}
            {#</tr>#}
            {#<tr>#}
            {#<td>CPAP Eval</td>#}
            {#<td><strong>{{ summary.overdue.cpapEvalCount }}</strong></td>#}

            {#</tr>#}
            {#<tr>#}
            {#<td>CPAP End Eval</td>#}
            {#<td><strong>{{ summary.overdue.cpapEndEvalCount }}</strong></td>#}
            {#</tr>#}
            {#<tr>#}
            {#<td>CPAP Setup Sold</td>#}
            {#<td><strong>{{ summary.overdue.cpapSetupSoldCount }}</strong></td>#}
            {#</tr>#}
            {#</tbody>#}
            {#<tbody>#}
            {#{% for key, overDue in records.summary.overdue %}#}
            {#<tr>#}
            {#<td>{{ key }}</td>#}
            {#<td><strong>{{ overDue }}</strong></td>#}
            {#</tr>#}
            {#{% endfor %}#}
            {#</tbody>#}
            {#</table>#}
            <h5 class="report-header" >Total Number of Referrals by Branch</h5>
            <table class="report-data" width="100%" style="margin-bottom: 20px;">
                <thead class="header">
                <tr>
                    <th width="30%">Branch</th>
                    <th>Count</th>
                </tr>
                </thead>
                <tbody>
                {% for branchSummary in summary.branch %}
                    <tr>
                        <td>{{ branchSummary.branchDesc }}</td>
                        <td><strong>{{ branchSummary.branchCount }}</strong></td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
        <div style="width: 49%; float: left;">
            <h5 class="report-header" style="">Total Number of HSATs Booked per Clinician</h5>
            <table class="report-data" width="100%" style="margin-bottom: 20px;">
                <thead class="header">
                <tr>
                    <th width="30%">Clinician</th>
                    <th>Count</th>
                </tr>
                </thead>
                <tbody>
                {% for clinicianSummary in summary.clinician %}
                    <tr>
                        <td>{{ clinicianSummary.primaryProvider }}</td>
                        <td><strong>{{ clinicianSummary.primaryProviderCount }}</strong></td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
    <div style="width: 100%; height: auto;">
        {#<div style="width: 49%; float: left;">#}
        {#<h5 class="report-header" style="">Overdue Encounters</h5>#}
        {#<table class="report-data" width="100%" style="margin-bottom: 20px;">#}
        {#<thead class="header">#}
        {#<tr>#}
        {#<th width="30%">Encounter Type</th>#}
        {#<th>Count</th>#}
        {#</tr>#}
        {#</thead>#}
        {#<tbody>#}
        {#<tr>#}
        {#<td>HSAT</td>#}
        {#<td><strong>{{ summary.overdue.hsatCount }}</strong></td>#}
        {#</tr>#}
        {#<tr>#}
        {#<td>Oximetry</td>#}
        {#<td><strong>{{ summary.overdue.oximetryCount }}</strong></td>#}
        {#</tr>#}
        {#<tr>#}
        {#<td>HSAT Results Interpretation</td>#}
        {#<td><strong>{{ summary.overdue.hsatResultInterCount }}</strong></td>#}
        {#</tr>#}
        {#<tr>#}
        {#<td>CPAP Setup</td>#}
        {#<td><strong>{{ summary.overdue.cpapSetupCount }}</strong></td>#}
        {#</tr>#}
        {#<tr>#}
        {#<td>CPAP Eval</td>#}
        {#<td><strong>{{ summary.overdue.cpapEvalCount }}</strong></td>#}

        {#</tr>#}
        {#<tr>#}
        {#<td>CPAP End Eval</td>#}
        {#<td><strong>{{ summary.overdue.cpapEndEvalCount }}</strong></td>#}
        {#</tr>#}
        {#<tr>#}
        {#<td>CPAP Setup Sold</td>#}
        {#<td><strong>{{ summary.overdue.cpapSetupSoldCount }}</strong></td>#}
        {#</tr>#}
        {#</tbody>#}
        {#<tbody>#}
        {#{% for key, overDue in records.summary.overdue %}#}
        {#<tr>#}
        {#<td>{{ key }}</td>#}
        {#<td><strong>{{ overDue }}</strong></td>#}
        {#</tr>#}
        {#{% endfor %}#}
        {#</tbody>#}
        {#</table>#}
        {#</div>#}
        <div style="width: 50%; float: left;">
            {#<h5 class="report-header" style="">Total Number of Referrals by Clinician</h5>#}
            {#<table class="report-data" width="100%" style="margin-bottom: 20px;">#}
            {#<thead class="header">#}
            {#<tr>#}
            {#<th width="30%">Clinician</th>#}
            {#<th>Count</th>#}
            {#</tr>#}
            {#</thead>#}
            {#<tbody>#}
            {#{% for clinicianSummary in summary.clinician %}#}
            {#<tr>#}
            {#<td>{{ clinicianSummary.primaryProvider }}</td>#}
            {#<td><strong>{{ clinicianSummary.primaryProviderCount }}</strong></td>#}
            {#</tr>#}
            {#{% endfor %}#}
            {#</tbody>#}
            {#</table>#}
        </div>
    </div>
    <div style="width: 100%; float: left;">
        <div style="margin-bottom: 20px; padding: 5px;">Total Referral(s): <strong>{{ records.count.referralCount }}</strong></div>
    </div>
    <table class="report-data" width="200%">
        <thead class="header">
        <tr id="header-id">
            <th>Patient ID</th>
            <th>Referral Date</th>
            <th>Received Date</th>
            <th>Patient</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>
            <th>HSAT Results Interpretation Status</th>
            <th>HSAT Results Interpretation Interval</th>
            <th>HSAT Results Interpretation Date</th>
            <th>CPAP Setup Status</th>
            <th>CPAP Setup Interval</th>
            <th>CPAP Setup 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>
            <th>Invoice No</th>
            <th>Billing Date</th>
        </tr>
        </thead>
        <tbody>
        {% set totalCount = 0 %}
        {% set threeDayCtr = 0 %}
        {% set twoDayCtr = 0 %}
        {% set oneDayCtr = 0 %}
        {% 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("m/d/Y") %}
            {% set hsatToHsatInterpretation = record.hsatResultInterChartDate|date_modify("+ "~hsatResultInterpretationNoOfDays|default('0')~" day")|date("m/d/Y") %}
            {% set cpapSetup = record.cpapSetupChartDate|date_modify("+ "~cpapSetupNoOfDays|default('0')~" day")|date("m/d/Y") %}
            {% set cpapEval = record.cpapEvalChartDate|date_modify("+ "~cpapEvalNoOfDays|default('0')~" day")|date("m/d/Y") %}
            {% set cpapEndEval = record.cpapEndEvalChartDate|date_modify("+ "~cpapEndEvalNoOfDays|default('0')~" day")|date("m/d/Y") %}

            {% set color = '#228B22' %}

            {% 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' %}
                {% elseif 'now'|date("m/d/Y")|date_modify("+1 day")|date("m/d/Y") >= refferalToHsat %}
                    {% set oneDayCtr = oneDayCtr + 1 %}
                    {% set color = 'red' %}
                {% endif %}
            {% endif %}

            {% if record.hsatResultInterChartDate != '' %}
                {% if 'now'|date("m/d/Y")|date_modify("+3 day")|date("m/d/Y") == hsatToHsatInterpretation %}
                    {% set threeDayCtr = threeDayCtr + 1 %}
                    {% set color = '#b4c200' %}
                {% elseif 'now'|date("m/d/Y")|date_modify("+2 day")|date("m/d/Y") == hsatToHsatInterpretation %}
                    {% set twoDayCtr = twoDayCtr + 1 %}
                    {% set color = 'orange' %}
                {% elseif 'now'|date("m/d/Y")|date_modify("+1 day")|date("m/d/Y") >= hsatToHsatInterpretation %}
                    {% set oneDayCtr = oneDayCtr + 1 %}
                    {% set color = 'red' %}
                {% endif %}
            {% endif %}

            {% if record.cpapSetupChartDate != '' %}
                {% if 'now'|date("m/d/Y")|date_modify("+3 day")|date("m/d/Y") == cpapSetup %}
                    {% set threeDayCtr = threeDayCtr + 1 %}
                    {% set color = '#b4c200' %}
                {% elseif 'now'|date("m/d/Y")|date_modify("+2 day")|date("m/d/Y") == cpapSetup %}
                    {% set twoDayCtr = twoDayCtr + 1 %}
                    {% set color = 'orange' %}
                {% elseif 'now'|date("m/d/Y")|date_modify("+1 day")|date("m/d/Y") >= cpapSetup %}
                    {% set oneDayCtr = oneDayCtr + 1 %}
                    {% set color = 'red' %}
                {% endif %}
            {% endif %}

            {% if record.cpapEvalChartDate != '' %}
                {% if 'now'|date("m/d/Y")|date_modify("+3 day")|date("m/d/Y") == cpapEval %}
                    {% set threeDayCtr = threeDayCtr + 1 %}
                    {% set color = '#b4c200' %}
                {% elseif 'now'|date("m/d/Y")|date_modify("+2 day")|date("m/d/Y") == cpapEval %}
                    {% set twoDayCtr = twoDayCtr + 1 %}
                    {% set color = 'orange' %}
                {% elseif 'now'|date("m/d/Y")|date_modify("+1 day")|date("m/d/Y") >= cpapEval %}
                    {% set oneDayCtr = oneDayCtr + 1 %}
                    {% set color = 'red' %}
                {% endif %}
            {% endif %}

            {% if record.cpapEndEvalChartDate != '' %}
                {% if 'now'|date("m/d/Y")|date_modify("+3 day")|date("m/d/Y") == cpapEndEval %}
                    {% set threeDayCtr = threeDayCtr + 1 %}
                    {% set color = '#b4c200' %}
                {% elseif 'now'|date("m/d/Y")|date_modify("+2 day")|date("m/d/Y") == cpapEndEval %}
                    {% set twoDayCtr = twoDayCtr + 1 %}
                    {% set color = 'orange' %}
                {% elseif 'now'|date("m/d/Y")|date_modify("+1 day")|date("m/d/Y") >= cpapEndEval %}
                    {% set oneDayCtr = oneDayCtr + 1 %}
                    {% set color = 'red' %}
                {% endif %}
            {% 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>{{ record.branchDesc }}</td>
                <td>{{ record.primaryProvider }}</td>
                <td>{{ record.specialistStartDate }}</td>
                <td>{{ record.specialistFullName }}</td>
                <td>{{ record.hsatStatusDesc }}</td>
                <td style="text-align: right;">{{ record.hsatTimeDiff }}</td>
                <td>{{ record.hsatChartDate }}</td>
                <td>{{ record.hsatResultInterStatusDesc }}</td>
                <td style="text-align: right;">{{ record.hsatInterTimeDiff }}</td>
                <td>{{ record.hsatResultInterChartDate }}</td>
                <td>{{ record.cpapSetupStatusDesc }}</td>
                <td style="text-align: right;">{{ record.cpapSetupTimeDiff }}</td>
                <td>{{ record.cpapSetupChartDate }}</td>
                <td>{{ record.cpapSetupSoldStatusDesc }}</td>
                <td style="text-align: right;">{{ record.cpapSetupSoldTimeDiff }}</td>
                <td>{{ record.cpapSetupSold }}</td>
                <td>{{ record.cpapEvalStatusDesc }}</td>
                <td style="text-align: right;">{{ record.cpapEvalTimeDiff }}</td>
                <td>{{ record.cpapEvalChartDate }}</td>
                <td>{{ record.cpapEndEvalStatusDesc }}</td>
                <td style="text-align: right;">{{ record.cpapEndEvalTimeDiff }}</td>
                <td>{{ record.cpapEndEvalChartDate }}</td>
                <td><a href="{{ path('patient_detail', {patient_id : record.patientId|base64Encode, tab: 'b'}) }}" target="_blank">{{ record.invoice_no }}</a></td>
                <td>{{ record.billingDate }}</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();
        });
        //]]>
    </script>
{% endblock %}