<style>
    body {
        font-family: Arial, sans-serif;
        font-size: 12px;
    }
    .report-title {
        text-align: center;
        font-size: 16px;
        font-weight: bold;
        margin-bottom: 5px;
        text-transform: uppercase;
    }
    .exam-name {
        text-align: center;
        font-size: 13px;
        margin-bottom: 3px;
    }
    .chairman-name {
        text-align: center;
        font-size: 12px;
        margin-bottom: 10px;
    }
    table {
        width: 100%;
        border-collapse: collapse;
        margin-bottom: 15px;
    }
    th, td {
        border: 1px solid #333;
        padding: 5px 8px;
        font-size: 11px;
    }
    th {
        background-color: #f0f0f0;
        font-weight: bold;
        text-align: center;
        vertical-align: middle;
    }
    td {
        vertical-align: top;
    }
    td.center {
        text-align: center;
    }
    td.left {
        text-align: left;
    }
    .no-data {
        text-align: center;
        padding: 20px;
        font-style: italic;
    }
</style>

{% if collegeData.isHeaderEnabled %}
<div class="text-center" style="text-align:center; margin-bottom: 15px;">
    <img style="width: auto; height: 115px;" src="{{ collegeData.collegeHeader }}">
</div>
{% else %}
<div style="text-align:center; margin-top:10px;">
    <img id="college_logo" class="college_logo" src="{{ collegeData.collegeLogo }}" style="width:85px; height:115px;">
    <div align="center">
        <div style="font-size:15px; margin-top:10px;"><b>{{ collegeData.collegeName }}</b></div>
        <div><b>{{ collegeData.collegeAddress }}</b></div>
        <div>{{ collegeData.collegeAddress2 }}</div>
    </div>
</div>
{% endif %}

<div style="margin-top: 15px;"></div>

<div style="text-align:center; font-weight:bold; font-size:16px; text-transform:uppercase; margin-bottom:5px;">Valuation Status Report</div>
{% if reportData.examRegistrationName %}
<div style="text-align:center; font-weight:bold; font-size:13px; margin-bottom:3px;">Exam Registration: {{ reportData.examRegistrationName }}</div>
{% endif %}
{% if reportData.chairmanName %}
<div style="text-align:center; font-weight:bold; font-size:12px; margin-bottom:10px;">Chairman: {{ reportData.chairmanName }}</div>
{% endif %}

{% if reportData.subjects is not empty %}

{# Calculate total rows for rowspan #}
{% set totalRows = 0 %}
{% for subject in reportData.subjects %}
    {% if subject.additionalExaminers is not empty %}
        {% set totalRows = totalRows + subject.additionalExaminers|length %}
    {% else %}
        {% set totalRows = totalRows + 1 %}
    {% endif %}
{% endfor %}

<table>
    <thead>
        <tr>
            <th data-f-bold="true" data-a-h="center" style="width:16%; font-weight:bold;">Exam Registration</th>
            <th data-f-bold="true" data-a-h="center" style="width:8%; font-weight:bold;">Total Papers</th>
            <th data-f-bold="true" data-a-h="center" style="width:10%; font-weight:bold;">Department</th>
            <th data-f-bold="true" data-a-h="center" style="width:14%; font-weight:bold;">Course</th>
            <th data-f-bold="true" data-a-h="center" style="width:13%; font-weight:bold;">Additional Valuator</th>
            <th data-f-bold="true" data-a-h="center" style="width:8%; font-weight:bold;">Assigned Papers</th>
            <th data-f-bold="true" data-a-h="center" style="width:10%; font-weight:bold;">Valuation Start Date</th>
            <th data-f-bold="true" data-a-h="center" style="width:10%; font-weight:bold;">Valuation End Date</th>
            <th data-f-bold="true" data-a-h="center" style="width:6%; font-weight:bold;">Marks Entered</th>
            <th data-f-bold="true" data-a-h="center" style="width:6%; font-weight:bold;">Mark Submitted</th>
            <th data-f-bold="true" data-a-h="center" style="width:6%; font-weight:bold;">Valuation %</th>
        </tr>
    </thead>
    <tbody>
        {% set isFirstRow = true %}
        {% for subject in reportData.subjects %}
            {% if subject.additionalExaminers is not empty %}
                {% for examiner in subject.additionalExaminers %}
                    <tr>
                        {# Exam Registration: spans all rows, only on first row #}
                        {% if isFirstRow %}
                            <td rowspan="{{ totalRows }}" style="vertical-align:top; padding-top:8px;">
                                {{ reportData.examRegistrationName }}
                            </td>
                            <td class="center" rowspan="{{ totalRows }}" style="vertical-align:top; padding-top:8px;">
                                {{ reportData.totalPapers ?? 0 }}
                            </td>
                        {% endif %}
                        {# Department & Course: spans all examiners of this subject #}
                        {% if loop.first %}
                            <td class="center" rowspan="{{ subject.additionalExaminers|length }}" style="vertical-align:top; padding-top:8px;">
                                {{ subject.departmentName }}
                            </td>
                            <td class="left" rowspan="{{ subject.additionalExaminers|length }}" style="vertical-align:top; padding-top:8px;">
                                <div><strong>{{ subject.subjectCode }}</strong></div>
                                <div>{{ subject.subjectName }}</div>
                            </td>
                        {% endif %}
                        <td class="left">{{ examiner.staffName ?? '-' }}</td>
                        <td class="center">{{ examiner.assignedPaperCount ?? '' }}</td>
                        <td class="center">
                            {% if subject.valuationStartDate %}
                                {{ subject.valuationStartDate|date('d-m-Y') }}
                            {% else %}
                                -
                            {% endif %}
                        </td>
                        <td class="center">
                            {% if subject.valuationEndDate %}
                                {{ subject.valuationEndDate|date('d-m-Y') }}
                            {% else %}
                                -
                            {% endif %}
                        </td>
                        <td class="center">{{ examiner.marksEntered ?? 0 }}</td>
                        <td class="center">{{ examiner.marksSubmitted ?? 0 }}</td>
                        <td class="center">
                            {% if examiner.assignedPaperCount and examiner.assignedPaperCount > 0 %}
                                {{ ((examiner.marksSubmitted / examiner.assignedPaperCount) * 100)|number_format(2) }}%
                            {% endif %}
                        </td>
                    </tr>
                    {% set isFirstRow = false %}
                {% endfor %}
            {% else %}
                <tr>
                    {% if isFirstRow %}
                        <td rowspan="{{ totalRows }}" style="vertical-align:top; padding-top:8px;">
                            {{ reportData.examRegistrationName }}
                        </td>
                        <td class="center" rowspan="{{ totalRows }}" style="vertical-align:top; padding-top:8px;">
                            {{ reportData.totalPapers ?? 0 }}
                        </td>
                    {% endif %}
                    <td class="center" style="vertical-align:top; padding-top:8px;">{{ subject.departmentName }}</td>
                    <td class="left" style="vertical-align:top; padding-top:8px;">
                        <div><strong>{{ subject.subjectCode }}</strong></div>
                        <div>{{ subject.subjectName }}</div>
                    </td>
                    <td class="center" colspan="7">No examiners assigned</td>
                </tr>
                {% set isFirstRow = false %}
            {% endif %}
        {% endfor %}
    </tbody>
</table>
{% else %}
    <div class="no-data">No subjects found for this exam registration.</div>
{% endif %}

<div style="text-align:right; font-size:11px; margin-top:10px; padding-right:5px;">
    <em>Generated on: {{ currentDateTime }}</em>
</div>
