<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Consolidated Course Report</title>
    <style>
        * { box-sizing: border-box; }
        body { font-family: Arial, sans-serif; color: #111; font-size: 10px; margin: 0; }
        table { width: 100%; border-collapse: collapse; margin: 0 0 8px; }
        th, td { border: 1px solid #222; padding: 4px 5px; vertical-align: middle; }
        th { background: #f4f4f4; font-weight: bold; text-align: center; }
        thead { display: table-header-group; }
        tr, .avoid-break {
            page-break-inside: avoid !important;
            break-inside: avoid-page !important;
        }
        .tei-batch-table, .tei-subject-table { table-layout: fixed; }
        .tei-subject-row,
        .tei-subject-cell,
        .tei-subject-table,
        .tei-subject-table tbody {
            page-break-inside: avoid !important;
            break-inside: avoid-page !important;
        }
        .tei-subject-cell { border: 0; padding: 0; }
        .tei-subject-table { margin: 0; }
        .common-header { width: 100%; text-align: center; }
        .common-header table { width: 100%; margin: 0 auto 8px; text-align: center; }
        .common-header th, .common-header td { text-align: center !important; }
        .info-label { width: 22%; font-weight: normal; }
        .text-left { text-align: left; }
        .text-center { text-align: center; }
        .text-right { text-align: right; }
        .bold { font-weight: bold; }
        .section-title { font-size: 12px; text-align: center; background: #fff; }
        .question-title { background: #f5f5f5; font-weight: bold; }
        .grade { color: #e53935; font-weight: bold; }
        .spacer { height: 8px; border: 0; }
        .charts-section { page-break-before: always; }
        .chart-pair { page-break-inside: avoid; margin: 0 0 16px; }
        .chart-question { font-size: 11px; font-weight: bold; margin-bottom: 6px; }
        .chart-table td { width: 50%; border: 0; padding: 2px; text-align: center; }
        .chart-image { width: 100%; max-width: 360px; height: auto; }
        .generated-on { margin-top: 12px; text-align: right; color: #777; font-size: 9px; }
    </style>
</head>
<body>
    {# Institution branding is generated by the backend, not accepted from the frontend. #}
    {% if commonHeader %}
        <div class="common-header">{{ commonHeader|raw }}</div>
    {% endif %}

    {# Main scored-question table; all totals are precomputed in the frontend payload. #}
    <table>
        <tr><td class="info-label">Programme</td><td class="bold">{{ report.programme }}</td></tr>
        <tr><td class="info-label">Batch</td><td class="bold">{{ report.batch }}</td></tr>
        <tr><td class="info-label">Course Name</td><td class="bold">{{ report.course }}</td></tr>
        <tr><td class="info-label">Evaluation Name</td><td class="bold">{{ report.evaluationName }}</td></tr>
        <tr>
            <td class="info-label">Staff Name</td>
            <td class="bold">
                {{ report.staffName }}
                {% if report.staffCode %} ({{ report.staffCode }}){% endif %}
            </td>
        </tr>
    </table>

    <table>
        <thead>
            <tr>
                <th colspan="{{ answerOptions|length + 5 }}">{{ report.answerOptionsLegend }}</th>
            </tr>
            <tr>
                <th style="width:5%">Sl. No.</th>
                <th class="text-left" style="width:40%">Questions</th>
                {% for answer in answerOptions %}
                    <th>{{ answer.answerOptionName }}</th>
                {% endfor %}
                <th>Total Mark</th>
                <th>Total No</th>
                <th>Average Mark</th>
            </tr>
        </thead>
        <tbody>
            {% for question in questionRows %}
                <tr>
                    <td class="text-center">{{ loop.index }}</td>
                    <td class="text-left">{{ question.questionHtml|raw }}</td>
                    {% for count in question.answerCounts %}
                        <td class="text-center">{% if count != 0 %}{{ count }}{% endif %}</td>
                    {% endfor %}
                    <td class="text-center">{{ question.totalMark }}</td>
                    <td class="text-center">{{ question.totalNo }}</td>
                    <td class="text-center">{{ question.averageMark }}</td>
                </tr>
            {% endfor %}
            <tr>
                <td></td>
                <td class="text-center bold">Total</td>
                {% for answer in answerOptions %}<td></td>{% endfor %}
                <td class="text-center bold">{{ totals.totalMark }}</td>
                <td class="text-center bold">-</td>
                <td class="text-center bold">{{ totals.averageMark }}</td>
            </tr>
        </tbody>
    </table>

    {% if options.showFeedback %}
        {# Free-text answers grouped under their original non-MCQ question. #}
        <table>
            <thead><tr><th class="section-title">General Feedback</th></tr></thead>
            <tbody>
                {% if feedbackGroups is empty %}
                    <tr><td class="text-center bold">No feedback found</td></tr>
                {% else %}
                    {% for feedback in feedbackGroups %}
                        <tr>
                            <td class="question-title">
                                Q{{ loop.index }}. {{ feedback.questionHtml|raw }}
                                {% if feedback.isOptional %}<em> (Optional)</em>{% endif %}
                            </td>
                        </tr>
                        {% for response in feedback.responses %}
                            <tr><td class="text-left">{{ loop.index }}. {{ response.answerHtml|raw }}</td></tr>
                        {% endfor %}
                    {% endfor %}
                {% endif %}
            </tbody>
        </table>
    {% endif %}

    {% if options.showTEI %}
        {# Supports both course-specific TEI rows and the single faculty-evaluation TEI summary. #}
        <table>
            <thead><tr><th colspan="4" class="section-title">Individual Course Score(s)</th></tr></thead>
            <tbody>
                {% if tei.isCourseEvaluation %}
                    {% for subject in tei.subjects %}
                        <tr>
                            <td rowspan="{% if options.showOverallGrade %}3{% else %}2{% endif %}" class="text-center">{{ loop.index }}</td>
                            <td rowspan="{% if options.showOverallGrade %}3{% else %}2{% endif %}" class="text-left">{{ subject.course }}</td>
                            <td>Teaching Effective Index</td>
                            <td class="bold">{{ subject.tei }}</td>
                        </tr>
                        <tr><td>Teaching Effective Index Percentage</td><td class="bold">{{ subject.percentage }}</td></tr>
                        {% if options.showOverallGrade %}
                            <tr><td>Overall Grade</td><td class="grade">{{ subject.grade }}</td></tr>
                        {% endif %}
                    {% endfor %}
                    <tr><td colspan="4" class="text-right bold">Average Teaching Effective Index: {{ tei.averageTei }}</td></tr>
                    <tr><td colspan="4" class="text-right bold">Average Teaching Effective Index Percentage: {{ tei.averagePercentage }}</td></tr>
                    {% if tei.showWeightedAverage %}
                        <tr><td colspan="4" class="text-right bold">Weighted average rating on the scale of 10: {{ tei.weightedAverage }}</td></tr>
                    {% endif %}
                    {% if options.showOverallGrade %}
                        <tr><td colspan="4" class="text-right bold">Overall Grade: <span class="grade">{{ tei.overallGrade }}</span></td></tr>
                    {% endif %}
                {% else %}
                    <tr>
                        <td>Teaching Effective Index</td><td class="text-center bold">{{ tei.tei }}</td>
                        <td>Teaching Effective Index Percentage</td><td class="text-center bold">{{ tei.percentage }}</td>
                    </tr>
                    {% if options.showOverallGrade %}
                        <tr><td colspan="3" class="text-center">Overall Grade</td><td class="text-center grade">{{ tei.overallGrade }}</td></tr>
                    {% endif %}
                {% endif %}
            </tbody>
        </table>
    {% endif %}

    {% if options.showTeiBatchSplit and teiBatchSplit.subjects is not empty %}
        {#
            Each course is wrapped in one outer row so wkhtmltopdf moves the complete
            rowspan group to the next page instead of splitting its batch rows.
        #}
        <table class="tei-batch-table">
            <colgroup>
                <col style="width: 6%">
                <col style="width: 18%">
                <col style="width: 10%">
                <col style="width: 8%">
                <col style="width: 29%">
                <col style="width: 29%">
            </colgroup>
            <thead>
                <tr><th colspan="6" class="section-title">TEI Batch Split</th></tr>
                <tr>
                    <th>SL NO</th><th>Course</th><th>Batch</th><th>Student Attended</th>
                    <th>TEI Batchwise</th><th>Average TEI</th>
                </tr>
            </thead>
            <tbody>
                {% for subject in teiBatchSplit.subjects %}
                    <tr class="tei-subject-row">
                        <td colspan="6" class="tei-subject-cell">
                            <table class="tei-subject-table">
                                <colgroup>
                                    <col style="width: 6%">
                                    <col style="width: 18%">
                                    <col style="width: 10%">
                                    <col style="width: 8%">
                                    <col style="width: 29%">
                                    <col style="width: 29%">
                                </colgroup>
                                <tbody>
                                    {% for batch in subject.batches %}
                                        <tr>
                                            {% if loop.first %}
                                                <td rowspan="{{ subject.batches|length }}" class="text-center">{{ subject.slno }}</td>
                                                <td rowspan="{{ subject.batches|length }}" class="text-left">{{ subject.course }}</td>
                                            {% endif %}
                                            <td>{{ batch.batchName }}</td>
                                            <td class="text-center">
                                                {{ batch.attended }}{% if batch.totalStudents %}/{{ batch.totalStudents }}{% endif %}
                                            </td>
                                            <td>
                                                Teaching Effective Index: <b>{{ batch.tei }}</b><br>
                                                Teaching Effective Index Percentage: <b>{{ batch.percentage }}</b>
                                                {% if teiBatchSplit.showWeightedAverage %}<br>Weighted average rating on the scale of 10: <b>{{ batch.tei10 }}</b>{% endif %}
                                                {% if options.showOverallGrade %}<br>Overall Grade: <span class="grade">{{ batch.grade }}</span>{% endif %}
                                            </td>
                                            {% if loop.first %}
                                                <td rowspan="{{ subject.batches|length }}">
                                                    Teaching Effective Index: <b>{{ subject.average.tei }}</b><br>
                                                    Teaching Effective Index Percentage: <b>{{ subject.average.percentage }}</b>
                                                    {% if teiBatchSplit.showWeightedAverage %}<br>Weighted average rating on the scale of 10: <b>{{ subject.average.tei10 }}</b>{% endif %}
                                                    {% if options.showOverallGrade %}<br>Overall Grade: <span class="grade">{{ subject.average.grade }}</span>{% endif %}
                                                </td>
                                            {% endif %}
                                        </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </td>
                    </tr>
                {% endfor %}
                <tr><td colspan="6" class="text-center bold">Average Teaching Effective Index Percentage from all Courses: {{ teiBatchSplit.overallPercentage }}</td></tr>
                {% if teiBatchSplit.showWeightedAverage %}
                    <tr><td colspan="6" class="text-center bold">Weighted average rating on the scale of 10: {{ teiBatchSplit.overallTenScale }}</td></tr>
                {% endif %}
                {% if options.showOverallGrade %}
                    <tr><td colspan="6" class="text-center bold">Overall Grade: <span class="grade">{{ teiBatchSplit.overallGrade }}</span></td></tr>
                {% endif %}
            </tbody>
        </table>
    {% endif %}

    {% if options.showOverallGrade %}
        <table>
            <thead><tr><th colspan="6" class="section-title">Grade Scale</th></tr></thead>
            <tbody>
                <tr>
                    <th>Percentage</th><td class="text-center">90 - 100</td><td class="text-center">80 - 90</td>
                    <td class="text-center">60 - 80</td><td class="text-center">40 - 60</td><td class="text-center">LESS THAN 40</td>
                </tr>
                <tr>
                    <th>Grade</th><td class="text-center">Excellent</td><td class="text-center">Very good</td>
                    <td class="text-center">Good</td><td class="text-center">Satisfactory</td><td class="text-center">Not Satisfactory</td>
                </tr>
            </tbody>
        </table>
    {% endif %}

    {% if options.showGraphs and charts is not empty %}
        {# Chart.js canvases cannot be rendered by wkhtmltopdf, so PNG data URLs are embedded instead. #}
        <div class="charts-section">
            {% for chart in charts %}
                <div class="chart-pair">
                    <div class="chart-question">{{ loop.index }}. {{ chart.questionHtml|raw }}</div>
                    <table class="chart-table">
                        <tr>
                            <td>{% if chart.barImage %}<img class="chart-image" src="{{ chart.barImage }}" alt="Bar chart">{% endif %}</td>
                            <td>{% if chart.pieImage %}<img class="chart-image" src="{{ chart.pieImage }}" alt="Pie chart">{% endif %}</td>
                        </tr>
                    </table>
                </div>
            {% endfor %}
        </div>
    {% endif %}

    <div class="generated-on">Generated On: <strong>{{ report.generatedOn }}</strong></div>
</body>
</html>
