{% extends '@RxConnexion/Admin/global.html.twig' %}
{% block body %}
    <h5 class="report-header">{{ page_title }}</h5>
    <table class="report-data" width="100%">
        <thead class="header">
        <tr>
            <th>Referral Date</th>
            <th>Receive Date</th>
            <th>Patient</th>
            <th>Patient Service</th>
            <th>Company</th>
            <th>Branch</th>
            <th>Referring Affiliate</th>
            <th>Referring Physician</th>
            <th>Latest Encounter</th>
            <th>Latest Encounter Status</th>
            <th>Letter Type</th>
            <th>Letter Status</th>
            <th>Agent</th>
            <th>Invoice No.</th>
            <th>Billing Date</th>
            <th>Rate</th>
        </tr>
        </thead>
        <tbody>
        {% set totalCount = 0 %}
        {% for record in records.marketing %}
            {% set trC = loop.index > 1 and loop.index % 2 == 0 ? 'even' : 'odd' %}
            {% set totalCount = totalCount + 1 %}
            <tr class="{{ trC }}">
                <td>{{ record.referral_date }}</td>
                <td>{{ record.receive_date }}</td>
                <td><a href="{{ path('patient_detail', {patient_id : record.patientId|base64Encode}) }}" target="_blank">{{ record.patient }}</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.companyDesc }}</td>
                <td>{{ record.branch }}</td>
                <td>{{ record.referring_affiliate }}</td>
                <td>{{ record.referring_physician }}</td>
                <td>{{ record.chartTypeDesc }}</td>
                <td>{{ record.chartStatusDesc }}</td>
                <td>
                    {% if record.letterType is not null %}
                        <a href="{{ path('patient_letter_preview', {patientLetter_id : record.patient_letter_id|base64Encode, letter_type:record.letterType|default('null')}) }}" target="_blank">{{ record.letterType }}</a></td>
                    {% endif %}
                <td>{{ record.letterStatus }}</td>
                <td>{{ record.sales_agent }}</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>
                <td style="text-align: right;">{{ record.rate }}</td>
            </tr>
        {% endfor %}
    </table>
    <div>Total Records: {{ totalCount }}</div>
    <hr>
    <h5 class="report-header">Summary</h5>
    <table class="report-data" width="50%">
        <thead class="header">
        <tr>
            <th>Agent</th>
            <th>Total</th>
        </tr>
        </thead>
        <tbody>
        {% set totalCount = 0 %}
        {% for record in records.soldCount %}
            {% set trC = loop.index > 1 and loop.index % 2 == 0 ? 'even' : 'odd' %}
            {% set totalCount = totalCount + 1 %}
            <tr class="{{ trC }}">
                <td>{{ record.sales_agent }}</td>
                <td>{{ record.rate }}</td>
            </tr>
        {% endfor %}
    </table>
    {% include '@RxConnexion/Admin/js.html.twig' %}
    <script>
        //<![CDATA[
        jQ(document).ready(function(){
            report_preview.init();
        });
        //]]>
    </script>
{% endblock %}