    {% 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;
        }
    </style>
    <h3 class="report-header"><strong>{{ page_title }}</strong> From: {{ dateFrom }} To: {{ dateTo }}</h3>

    {% for record in records %}
        <div>
            <h4 style="background-color: #4ccfdd"><strong style="color: #0a0a0a;">{{ record.description }}</strong></h4>
        </div>
        <table>
            <thead>
                <tr>
                    <th style="background-color: #b2b2b2"><span style="color: #000000">Activity/Services</span></th>
                    {% for provider in record.providers %}
                        <th style="background-color: #b2b2b2"><span style="color: #000000">{{ provider.name }}</span></th>
                    {% endfor %}
                    <th style="background-color: #b2b2b2"><span style="color: #000000">Total</span></th>
                </tr>
            </thead>
            <tbody>

            {% set subTotal = 0 %}
            {% for chartType in record.chartTypes %}

                {% set total_activity = 0 %}
                {% set percentage = 0 %}
                {% set count = null %}

                {% for pv in record.providers %}
                    {% for prr in chartType.providers if prr.id == pv.id %}
                        {% set count = count + prr.count %}
                    {% endfor %}
                {% endfor %}

                <tr>
                    <td style="background-color: #b2b2b2"><span style="color: #000000">{{ chartType.description }}</span></td>
                    {% for provider in record.providers %}
                        {% set hasTD = false %}
                        {% for pr in chartType.providers if pr.id == provider.id %}
                            {% set percentage = ((pr.count/count)|round(2))  *100  %}
                            {% set hasTD = true %}
                            {% set total_activity = total_activity + pr.count %}
                            <td><a href="{{ path('report_encounter_preview', {date_from:dateFrom, date_to:dateTo, company_id:'null', branch_id:record.id, provider_id:provider.id, patient_id:'null', encountertype_id:chartType.id, encounter_status:'Completed', user_id:'null', status:'null' }) }}" target="_blank">{{ pr.count }}</a> ({{ percentage|round~'%' }})</td>
                            {% set subTotal = subTotal + pr.count %}
                        {% endfor %}
                        {% if not hasTD %}
                            <td>0</td>
                        {% endif %}
                    {% endfor %}
                    <td><strong>{{ total_activity }}</strong></td>
                </tr>
            {% endfor %}
            <tr style="border-top: 1px solid #aaaaaa;">
                <td><b>Total:</b></td>
                {% for provider in record.providers  %}
                    {% set totalPercentage = null %}
                    {% set total_prov = 0 %}
                    {% for chartType in record.chartTypes  %}
                        {% for pr in chartType.providers if pr.id == provider.id  %}
                            {% set total_prov = total_prov + pr.count %}
                            {% set totalPercentage = (total_prov / subTotal) * 100 %}
                        {% endfor %}
                    {% endfor %}
                    <td><strong>{{ total_prov }} ({{ totalPercentage|round~'%' }})</strong></td>
                {% endfor %}
                <td><strong style="font-size: 15px">{{ subTotal }}</strong></td>
            </tr>
            </tbody>
        </table>

    {% endfor %}

    <div>
        <h4 class="report-header"><strong>Branch Summary</strong></h4>
        <table>
            <thead>
            <tr>
                <th style="background-color: #b2b2b2"><span style="color: #000000">Activity/Services</span></th>
                {% for record in summaryHf %}
                    {% for hf in record.healthFacility %}
                        <th style="background-color: #b2b2b2"><span style="color: #000000">{{ hf.name }}</span></th>
                    {% endfor %}
                {% endfor %}
                <th style="background-color: #b2b2b2"><span style="color: #000000">Total Actual Number</span></th>
            </tr>
            </thead>
            <tbody>

            {% set subTotal = 0 %}
            {% for record in summaryHf %}
                {% for chartType in record.chartTypes %}

                    {% set total_activity = 0 %}
                    {% set percentage = 0 %}
                    {% set count = null %}

                    {% for hf in record.healthFacility %}
                        {% set hasTD = false %}
                        {% for ctHf in chartType.healthFacility if hf.id == ctHf.healthFacility_id %}
                            {% set count = count + ctHf.totalCount %}
                        {% endfor %}
                    {% endfor %}

                    <tr>
                        <td style="background-color: #b2b2b2"><span style="color: #000000">{{ chartType.description }}</span></td>
                        {% for hf in record.healthFacility %}
                            {% set hasTD = false %}
                            {% for ctHf in chartType.healthFacility if hf.id == ctHf.healthFacility_id %}
                                {% set percentage = ((ctHf.totalCount/count)|round(2))  *100  %}
                                {% set hasTD = true %}
                                {% set total_activity = ctHf.totalCount %}
                                <td><strong>{{ ctHf.totalCount }}</strong> ({{ percentage~' %' }})</td>
                                {% set subTotal = subTotal + ctHf.totalCount %}
                            {% endfor %}
                            {% if not hasTD %}
                                <td>0</td>
                            {% endif %}
                        {% endfor %}
                        <td><strong>{{ total_activity }}</strong></td>
                    </tr>
                {% endfor %}
                <tr style="border-top: 1px solid #aaaaaa;">
                    <td><b>Total:</b></td>
                    {% for healthFacility in record.healthFacility  %}
                        {% set totalPercentage = null %}
                        {% set total_hf = 0 %}
                        {% for chartType in record.chartTypes  %}
                            {% for ctHf in chartType.healthFacility if healthFacility.id == ctHf.healthFacility_id %}

                                {% set hasTD = true %}
                                {% set total_hf = total_hf + ctHf.totalCount %}
                                {% set totalPercentage = (total_hf / subTotal) * 100 %}
                            {% endfor %}
                        {% endfor %}
                        <td><strong>{{ total_hf }} ({{ totalPercentage|round~'%' }})</strong></td>
                    {% endfor %}
                    <td><strong style="font-size: 15px">{{ subTotal }}</strong></td>
                </tr>
            {% endfor %}
            </tbody>
        </table>
    </div>

    <div style="width: 100%">
        <div style="width: 49%">
            <h4 class="report-header"><strong>Clinician Summary</strong></h4>
            {% for sumClinician in summary %}
                <ul>
                    <li class="toggle-sub-table"><strong>{{ sumClinician.provider }} </strong><i class="fa fa-caret-down"></i></li>
                {% for ct in sumClinician.chart_types %}
                    <ul class="sub-table" style="display: none;">
                        <div style="margin-left: 10px;">{{ ct.patient_chart_count.chartType }} : <strong>{{ ct.patient_chart_count.chart_status_count }}</strong></div>
                    </ul>
                {% endfor %}
                </ul>
            {% endfor %}
        </div>
    </div>

    {% 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 %}