{% 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;
        }
        table.report-data tr:hover:NOT(:first-child) {
            background: #5ac3f9 !important;
        }
        table.report-data tr:nth-child(even) {
            background: #bcbcbc !important;
        }
    </style>
    <h5 class="report-header" style="width: 375%;">{{ page_title }}
        {% if (date_from == 'null' or date_to == 'null') and (date_to == 'null' or date_to == 'null') %}
            {{ '( Received Date from '~receive_date_from~' - '~receive_date_to~' )' }}
        {% endif %}
    </h5>
    <div class="row max-width-initial">
        <div class="small-12 columns col-left">
            <h5 class="report-header">HSAT Cycle Summary <a href="{{ path('report_link_patient_cycle_report_redirect_preview', {
                    date_from:date_from|default('null'),
                    date_to:date_to|default('null'),
                    receive_date_from:receive_date_from|default('null'),
                    receive_date_to:receive_date_to|default('null'),
                    company:company|default('null'),
                    branch:'null',
                    referring_health_facility:referring_health_facility|default('null'),
                    referring_provider:referring_provider|default('null'),
                    chartType:'null',
                    chartStatus:'null',
                    service:'null',
                    primary_provider:'null',
                    fileType:'null',
                    is_due:is_due|default('null'),
                }) }}" style="float: right; color: #fff">Reset</a></h5>
            <table class="report-data" data-floathead="false" width="100%" style="margin-bottom: 20px;">
                <thead class="header">
                <tr>
                    <th>Branch</th>
                    <th class="text-right">Referrals</th>
                    <th class="text-right">Received Fax</th>
                    <th class="text-right">HSAT Referral</th>
                    <th>Statues</th>
                    {% for cycleType in records.summary.cycleTypes %}
                        <th class="text-right">{{ cycleType }}</th>
                    {% endfor %}
                </tr>
                </thead>
                <tbody>
                {% set totalReferral = 0 %}
                {% set totalFaxCount = 0 %}
                {% set totalHsatReferral = 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 %}
                                {% set totalFaxCount = totalFaxCount + cycle.faxCount %}
                                {% set totalHsatReferral = totalHsatReferral + cycle.hsatReferrals %}
                                <td rowspan="{{ cycle.statuses|length }}">{{ branchName }}</td>
                                <td rowspan="{{ cycle.statuses|length }}" class="text-right">
                                    <a href="{{ path('report_link_patient_cycle_report_redirect_preview', {
                                        date_from:date_from|default('null'),
                                        date_to:date_to|default('null'),
                                        receive_date_from:receive_date_from|default('null'),
                                        receive_date_to:receive_date_to|default('null'),
                                        company:company|default('null'),
                                        branch:cycle.branchId|default('null'),
                                        referring_health_facility:referring_health_facility|default('null'),
                                        referring_provider:referring_provider|default('null'),
                                        chartType:'null',
                                        chartStatus:'null',
                                        service:1|default('null'),
                                        primary_provider:primary_provider|default('null'),
                                        fileType:fileType|default('null'),
                                        is_due:is_due|default('null'),
                                    }) }}">{{ cycle.referral }}</a>
                                </td>
                                <td rowspan="{{ cycle.statuses|length }}" class="text-right">
                                    <a href="{{ path('report_link_patient_cycle_report_redirect_preview', {
                                        date_from:date_from|default('null'),
                                        date_to:date_to|default('null'),
                                        receive_date_from:receive_date_from|default('null'),
                                        receive_date_to:receive_date_to|default('null'),
                                        company:company|default('null'),
                                        branch:cycle.branchId|default('null'),
                                        referring_health_facility:referring_health_facility|default('null'),
                                        referring_provider:referring_provider|default('null'),
                                        chartType:'Fax Count',
                                        chartStatus:'Fax Count',
                                        service:1|default('null'),
                                        primary_provider:primary_provider|default('null'),
                                        fileType:fileType|default('null'),
                                        is_due:is_due|default('null'),
                                    }) }}">{{ cycle.faxCount }}</a>
                                </td>
                                <td rowspan="{{ cycle.statuses|length }}" class="text-right">{{ cycle.hsatReferrals }}</td>
                            {% endif %}
                            <td>{{ status.status }}</td>
                            {% for cycleType in records.summary.cycleTypes %}
                                {% set count = '' %}
                                {% set chartType = '' %}
                                {% set chartStatus = '' %}
                                {% for typeName,typeCount in status.types %}
                                    {% if typeName == cycleType %}
                                        {% set count = typeCount %}
                                        {% set chartType = typeName %}
                                        {% set chartStatus = typeName %}
                                    {% endif %}
                                {% endfor %}
                                <td class="text-right">
                                   <a href="{{ path('report_link_patient_cycle_report_redirect_preview', {
                                                date_from:date_from|default('null'),
                                                date_to:date_to|default('null'),
                                                receive_date_from:receive_date_from|default('null'),
                                                receive_date_to:receive_date_to|default('null'),
                                                company:company|default('null'),
                                                branch:cycle.branchId|default('null'),
                                                referring_health_facility:referring_health_facility|default('null'),
                                                referring_provider:referring_provider|default('null'),
                                                chartType:cycleType|default('null'),
                                                chartStatus:status.status|default('null'),
                                                service:1|default('null'),
                                                primary_provider:primary_provider|default('null'),
                                                fileType:fileType|default('null'),
                                                is_due:is_due|default('null'),
                                            }) }}">{{ count }}</a>
    
                                </td>
                            {% endfor %}
                        </tr>
                    {% endfor %}
                {% endfor %}
                <tr>
                    <td>Total:</td>
                    <td class="text-right">{{ totalReferral }}</td>
                    <td class="text-right">{{ totalFaxCount }}</td>
                    <td class="text-right">{{ totalHsatReferral }}</td>
                    <td></td>
                </tr>

                </tbody>
            </table>
        </div>
    </div>
    <div class="row max-width-initial">
        <div class="small-6 columns col-left">
            <h5 class="report-header">Services</h5>
            <table class="report-data" data-floathead="false" width="100%" style="margin-bottom: 20px;">
                <thead class="header">
                <tr>
                    <th>Service</th>
                    {% for key,desc in records.summary.service_branch %}
                        {% set hf_split = key|split('-') %}
                        <th class="text-right">{{ hf_split[1] }}</th>
                    {% endfor %}
                    <th class="text-right">Count</th>
                </tr>
                </thead>
                <tbody>
                {% set serviceGrandTotal = 0 %}
                {% for key, desc in records.summary.service %}
                    {% set service_split = key|split('-') %}
                    <tr style="border : 1px solid grey !important;">
                        <td>{{ service_split[1] }}</td>
                        {% set totalServices = 0 %}
                        {% for key2,desc2 in records.summary.service_branch %}
                            {% set count = 0 %}
                            {% set branch = null %}
                            {% for name,serviceCount in desc %}
                                {% set name_split = name|split('-') %}
                                {% set hf_split = key2|split('-') %}
                                {% if name_split[0] == hf_split[0] %}
                                    {% set totalServices = totalServices + serviceCount %}
                                    {% set branch = name_split[0] %}
                                    {% set count = serviceCount %}
                                {% endif %}
                            {% endfor %}
                            <td class="text-right">
                                <a href="{{ count > 0 ? path('report_link_patient_cycle_report_redirect_preview', {
                                    date_from:date_from|default('null'),
                                    date_to:date_to|default('null'),
                                    receive_date_from:receive_date_from|default('null'),
                                    receive_date_to:receive_date_to|default('null'),
                                    company:company|default('null'),
                                    branch:branch|default('null'),
                                    referring_health_facility:referring_health_facility|default('null'),
                                    referring_provider:referring_provider|default('null'),
                                    chartType:chart_type|default('null'),
                                    chartStatus:chart_status|default('null'),
                                    service:service_split[0],
                                    primary_provider:primary_provider|default('null'),
                                    fileType:fileType|default('null'),
                                    is_due:is_due|default('null'),
                                }) : 'javascript:void(0)' }}">{{ count }}</a>
                                </td>
                        {% endfor %}
                        <td class="text-right">{{ totalServices }}</td>
                    </tr>
                    {% set serviceGrandTotal = serviceGrandTotal + totalServices %}
                {% endfor %}

                <tr>
                    <td><strong>Total:</strong></td>
                    {% for key,desc in records.summary.service_branch %}
                        <td></td>
                    {% endfor %}
                    <td class="text-right"><strong>{{ serviceGrandTotal }}</strong></td>
                </tr>
                </tbody>
            </table>
        </div>
        <div class="small-6 columns col-right">
            <h5 class="report-header">Prescription</h5>
            <table class="report-data" data-floathead="false" width="100%" style="margin-bottom: 20px;">
                <thead class="header">
                <tr>
                    <th>Branch</th>
                    <th>Statues</th>
                    {% for prescriptionChartTypes in records.summary.prescriptionChartType %}
                        <th class="text-right">{{ prescriptionChartTypes }}</th>
                    {% endfor %}
                </tr>
                </thead>
                <tbody>
                {% for prescriptionBranch,prescription in records.summary.prescription %}
                    {% if prescription.statuses  is defined %}
                        {% for status in prescription.statuses %}
                            <tr style="border : 1px solid grey !important;">
                                {% if loop.first %}
                                    <td rowspan="{{ prescription.statuses|length }}">{{ prescriptionBranch }}</td>
                                {% endif %}
                                <td>{{ status.status }}</td>
                                {% for prescriptionChartTypes in records.summary.prescriptionChartType %}
                                    {% set count = '' %}
                                    {% set chartType = '' %}
                                    {% set chartStatus = '' %}
                                    {% for typeName,typeCount in status.types %}
                                        {% if typeName == prescriptionChartTypes %}
                                            {% set count = typeCount %}
                                            {% set chartType = typeName %}
                                            {% set chartStatus = typeName %}
                                        {% endif %}
                                    {% endfor %}
                                    <td class="text-right"><a href="{{ path('report_link_patient_cycle_report_redirect_preview', {
                                            date_from:date_from|default('null'),
                                            date_to:date_to|default('null'),
                                            receive_date_from:receive_date_from|default('null'),
                                            receive_date_to:receive_date_to|default('null'),
                                            company:company|default('null'),
                                            branch:cycle.branchId|default('null'),
                                            referring_health_facility:referring_health_facility|default('null'),
                                            referring_provider:referring_provider|default('null'),
                                            chartType:chart_type|default('null'),
                                            chartStatus:status.status|default('null'),
                                            service:service|default('null'),
                                            primary_provider:primary_provider|default('null'),
                                            fileType:fileType|default(''),
                                            is_due:is_due|default('null'),
                                        }) }}">{{ count }}</a></td>
                                {% endfor %}
                            </tr>
                        {% endfor %}
                    {% endif %}

                {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
    <div class="row max-width-initial">
        <div class="small-6 columns col-left">
            <h5 class="report-header">Result</h5>
            <table class="report-data" data-floathead="false" width="100%" style="margin-bottom: 20px;">
                <thead class="header">
                <tr>
                    <th>Result</th>
                    <th class="text-right">Count</th>
                </tr>
                </thead>
                <tbody>
                {% set totalPatientFile = 0 %}
                {% for key, desc in records.summary.results %}
                    {% set patient_file_split = key|split('-') %}
                    {% set totalPatientFile = totalPatientFile + desc %}
                    <tr style="border : 1px solid grey !important;">
                        <td>{{ patient_file_split[1]}}</td>
                        <td class="text-right"><a href="{{ path('report_link_patient_cycle_report_redirect_preview', {
                                date_from:date_from|default('null'),
                                date_to:date_to|default('null'),
                                receive_date_from:receive_date_from|default('null'),
                                receive_date_to:receive_date_to|default('null'),
                                company:company|default('null'),
                                branch:branch|default('null'),
                                referring_health_facility:referring_health_facility|default('null'),
                                referring_provider:referring_provider|default('null'),
                                chartType:chart_type|default('null'),
                                chartStatus:chart_status|default('null'),
                                service:service|default('null'),
                                primary_provider:primary_provider|default('null'),
                                fileType:patient_file_split[0],
                                is_due:is_due|default('null'),
                            }) }}">{{ desc }}</a></td>
                    </tr>
                {% endfor %}
                <tr>
                    <td><strong>Total:</strong></td>
                    <td class="text-right"><strong>{{ totalPatientFile }}</strong></td>
                </tr>
                </tbody>
            </table>
        </div>
        <div class="small-6 columns col-right">
            <h5 class="report-header">Purchase Summary</h5>
            <table class="report-data" data-floathead="false" width="100%" style="margin-bottom: 20px;">
                <thead class="header">
                <tr>
                    <th>Branch</th>
                    <th>Statues</th>
                    <th>Count</th>
                </tr>
                </thead>
                <tbody>
                {% for purchaseBranch,purchase in records.summary.purchase %}
                    {% if purchase.statuses is defined %}
                        {% for status, count in purchase.statuses %}
                            <tr style="border : 1px solid grey !important;">
                                {% if loop.first %}
                                    <td rowspan="{{ purchase.statuses|length }}">{{ purchaseBranch }}</td>
                                {% endif %}
                                <td>{{ status }}</td>
                                <td class="text-right">{{ count }}</td>
                            </tr>
                        {% endfor %}
                    {% endif %}

                {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
    <table class="report-data" width="375%">
        <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>Endorsement Note(s)</th>
            <th>Patient Note(s)</th>
            <th>Acknowledgement Letter Date</th>
            <th>Status</th>
            <th>Pre-HSAT Date</th>
            <th>Type</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>Serial Number</th>
            <th>Status</th>
            {#<th>6 Month Follow up Date</th>#}
            {#<th>Type</th>#}
            {#<th>Status</th>#}
            {#<th>Next 6 Month Follow up</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 receiveDateToIpc = record.receiveDate|date_modify("+ "~expectedNoOfDays.ipc|default('0')~" day")|date('Y-m-d') %}
            {% 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 receiveDateToSixMoFF = record.receiveDate|date_modify("+ "~expectedNoOfDays.six_mo_ff|default('0')~" day")|date('Y-m-d') %}
            {% set receiveDateToInvoice = record.receiveDate|date_modify("+ "~expectedNoOfDays.invoice|default('0')~" day")|date('Y-m-d') %}

            {% set ipcIsDue = false %}
            {% set ipcDueColor = false %}
            {% if record.ipcChartIdEncoded == '' or record.ipcChartIdEncoded == null %}
                {% if date('now'|date('Y-m-d')|date_modify("+3 day")|date('Y-m-d')) >= date(receiveDateToIpc)
                    or date('now'|date('Y-m-d')|date_modify("+2 day")|date('Y-m-d')) >= date(receiveDateToIpc)
                    or date('now'|date('Y-m-d')|date_modify("+1 day")|date('Y-m-d')) >= date(receiveDateToIpc)
                %}
                    {% set ipcIsDue = true %}
                {% endif %}

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

            {% 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 = '#b3b300' %}
                {% 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)
                    or record.isHsatRepeat == true
                %}
                    {% set hsatRepeatIsDue = true %}
                {% endif %}

                {% if date('now'|date('Y-m-d')|date_modify("+3 day")|date('Y-m-d')) == date(receiveDateToHsatRepeat) %}
                    {% set hsatRepeatDueColor = '#b3b300' %}
                {% 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 = '#b3b300' %}
                {% 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 = '#b3b300' %}
                {% 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 = '#b3b300' %}
                {% 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 = '#b3b300' %}
                {% 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 sixMoFFIsDue = false %}
            {% set sixMoFFDueColor = '' %}
            {% if record.sixMoFFIdEncoded == '' or record.sixMoFFIdEncoded == null %}
                {% if date('now'|date('Y-m-d')|date_modify("+3 day")|date('Y-m-d')) >= date(receiveDateToSixMoFF)
                    or date('now'|date('Y-m-d')|date_modify("+2 day")|date('Y-m-d')) >= date(receiveDateToSixMoFF)
                    or date('now'|date('Y-m-d')|date_modify("+1 day")|date('Y-m-d')) >= date(receiveDateToSixMoFF)
                %}
                    {% set sixMoFFIsDue = true %}
                {% endif %}

                {% if date('now'|date('Y-m-d')|date_modify("+3 day")|date('Y-m-d')) == date(receiveDateToSixMoFF) %}
                    {% set sixMoFFDueColor = '#b3b300' %}
                {% elseif date('now'|date('Y-m-d')|date_modify("+2 day")|date('Y-m-d')) == date(receiveDateToSixMoFF) %}
                    {% set sixMoFFDueColor = 'orange' %}
                {% elseif date('now'|date('Y-m-d')|date_modify("+1 day")|date('Y-m-d')) >= date(receiveDateToSixMoFF) %}
                    {% set sixMoFFDueColor = '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 = '#b3b300' %}
                {% 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 nameColor = '' %}
            {% if hsatIsDue == true
                or ipcIsDue == true
                or hsatRepeatIsDue == true
                or hriIsDue == true
                or pftIsDue == true
                or prescriptionIsDue == true
                or cpapSetupIsDue == true
                or invoiceIsDue == true %}
                {% set nameColor = 'red' %}
            {% endif %}

            {% set ps_split = record.patientServices|split('@@') %}
            {% set pbi_split = record.patientBillingInventory|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}) }}" style="color:{{ nameColor }}" 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.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>
                <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.ipcChartDate }}
                    {% if record.ipcChartIdEncoded == 'null' or record.ipcChartIdEncoded == '' %}
                        {% set isIpcService = false %}
                        {% for ps in ps_split %}
                            {% set patientService = ps|split('|') %}
                            {% if patientService[1]|default('') == 'Home Sleep Apnea Test (HSAT)' or patientService[0]|default('') == '' %}
                                {% set isIpcService = true %}
                            {% endif %}
                        {% endfor %}
                        {% if isIpcService == true %}
                            <strong style="color: {{ ipcDueColor }}">{{ ipcIsDue == true ? 'Due' : '' }}</strong>
                        {% endif %}
                    {% endif %}
                </td>
                <td>
                    {% if record.ipcChartIdEncoded != 'null' and record.ipcChartIdEncoded != '' %}
                        <a href="{{ path('patient_chart_form',{patient_id:record.id|base64Encode,action:'u',id:record.ipcChartIdEncoded}) }}" target="_blank">{{ record.ipcChartType }}</a>
                    {% endif %}
                </td>
                <td>{{ record.ipcChartStatus }}</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[2]|default('') }}
                            {% if patientFiles[1]|default('') != 'null' and patientFiles[1]|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>
                    {% for pbi in pbi_split %}
                        {% set patientBillingInventory = pbi|split('|') %}
                        <div>{{ patientBillingInventory[1]|default('') }}</div>
                    {% endfor %}
                </td>
                <td>
                    {% for pbi in pbi_split %}
                        {% set patientBillingInventory = pbi|split('|') %}
                        <div>{{ patientBillingInventory[2]|default('') }}</div>
                    {% endfor %}
                </td>
                {#<td>#}
                    {#{{ record.sixMoFFDate }}#}
                    {#{% if record.sixMoFFIdEncoded == 'null' or record.sixMoFFIdEncoded == '' %}#}
                        {#{% 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: {{ sixMoFFDueColor }}">{{ sixMoFFIsDue == true ? 'Due' : '' }}</strong>#}
                        {#{% endif %}#}
                    {#{% endif %}#}
                {#</td>#}
                {#<td>#}
                    {#{% if record.sixMoFFIdEncoded != 'null' and record.sixMoFFIdEncoded != '' %}#}
                        {#<a href="{{ path('patient_chart_form',{patient_id:record.id|base64Encode,action:'u',id:record.sixMoFFIdEncoded}) }}" target="_blank">{{ record.sixMoFFType }}</a>#}
                    {#{% endif %}#}
                {#</td>#}
                {#<td>{{ record.sixMoFFStatus }}</td>#}
                {#<td>{{ record.nextSixMonthsFF }}</td>#}
            </tr>
        {% endfor %}
    </table>
    <div>Total Records: {{ totalCount }}</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 %}