{# Hierarchical Export Template for KPI Student Lifecycle #}
{# Renders hierarchical data with proper indentation, styling, and subtotals #}

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: Arial, sans-serif;
            font-size: 11px;
        }

        .container {
            width: 100%;
        }

        /* Common Header (Institution Banner) */
        .common-header {
            margin-bottom: 10px;
            border-bottom: 1px solid #ccc;
            padding-bottom: 10px;
        }

        .common-header table {
            width: 100%;
            margin-bottom: 0;
        }

        .common-header td {
            text-align: center;
            padding: 3px 8px;
            border: none;
            font-size: 12px;
        }

        /* Header Section */
        .header {
            text-align: center;
            margin-bottom: 20px;
            border-bottom: 2px solid #333;
            padding-bottom: 10px;
        }

        .institution-name {
            font-size: 14px;
            font-weight: bold;
            margin-bottom: 5px;
        }

        .address {
            font-size: 11px;
            color: #666;
            margin-bottom: 10px;
        }

        .report-title {
            font-size: 12px;
            font-weight: bold;
            margin-bottom: 5px;
        }

        .report-context {
            font-size: 10px;
            color: #333;
            margin-bottom: 10px;
        }

        .report-summary {
            margin-bottom: 20px;
            width: 100%;
            border-collapse: collapse;
            font-size: 11px;
        }

        .report-summary tr {
            border-bottom: 1px solid #333;
        }

        .report-summary td {
            padding: 8px 12px;
            background-color: #ffffff;
            border: 1px solid #333;
            font-weight: bold;
            font-size: 11px;
            color: #000;
        }

        .summary-item {
            display: inline-block;
            margin-right: 25px;
            font-weight: bold;
        }

        /* Main Data Table Styles */
        .data-table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
            font-size: 11px;
        }

        .data-table th,
        .data-table td {
            border: 1px solid #333;
            padding: 7px 6px;
            vertical-align: middle;
        }

        .data-table th {
            background-color: #ffffff;
            color: #000;
            font-weight: bold;
            font-size: 10px;
            text-align: center;
            border: 1px solid #333;
        }

        /* Column Width Classes */
        .col-sno     { width: 5%; text-align: center; }
        .col-item    { width: 35%; text-align: left; padding-left: 10px; }
        .col-active  { width: 12%; text-align: center; }
        .col-dormant { width: 12%; text-align: center; }
        .col-failed  { width: 12%; text-align: center; }
        .col-discontinued { width: 12%; text-align: center; }
        .col-completed    { width: 12%; text-align: center; }
        .col-total   { width: 12%; text-align: center; }

        /* Row Types */
        tr.school-header td {
            background-color: #ffffff;
            font-weight: bold;
            color: #000;
            padding: 10px 8px;
            border: 1px solid #333;
        }

        tr.school-header td.col-item {
            padding-left: 10px;
        }

        /* Light gray background for main rows when admission years are present */
        tr.school-header-with-year td {
            background-color: #f0f0f0;
        }

        tr.year-row td {
            background-color: #ffffff;
            font-weight: 500;
            color: #000;
            border: 1px solid #333;
        }

        tr.year-row td.col-item {
            padding-left: 30px;
        }

        tr.program-row td {
            background-color: #ffffff;
            color: #000;
            border: 1px solid #333;
        }

        tr.program-row td.col-item {
            padding-left: 50px;
        }

        tr.year-subtotal td {
            background-color: #ffffff;
            font-weight: 600;
            border: 1px solid #333;
            color: #000;
        }

        tr.year-subtotal td.col-item {
            padding-left: 30px;
        }

        /* Footer */
        .footer {
            margin-top: 20px;
            padding-top: 10px;
            border-top: 1px solid #ccc;
            font-size: 10px;
            color: #666;
            text-align: right;
        }

        .generated-date {
            font-size: 10px;
            color: #999;
            margin-top: 10px;
        }
    </style>
</head>
<body>
    {# Pre-calculate colspan for all header tables - ensures consistent, clean HTML #}
    {% set dataTableColspan = showDetails ? 8 : 6 %}

    <div class="container">
        {# Common Header (Institution Banner) #}
        {% if commonHeader %}
            <div class="common-header">
                <table style="width: 100%; border-collapse: collapse;">
                    <tbody>
                        <!-- Replace empty colspan with correct total columns based on showDetails -->
                        {{ commonHeader|replace({"colspan=''": "colspan='" ~ dataTableColspan ~ "' style='width: 100%;'"})|raw }}
                    </tbody>
                </table>
            </div>
        {% endif %}

        {# Report Title as Table (for Excel export compatibility) #}
        <table style="width: 100%; margin-bottom: 20px; border-collapse: collapse;">
            <tbody>
                <tr>
                    <td colspan="{{ dataTableColspan }}" style="text-align: center; padding: 12px 8px; font-size: 12px; font-weight: bold; border: 1px solid #333;">
                        {% if listType == 'SCHOOL' %}
                            Schools Student Info Report
                        {% elseif listType == 'DEPARTMENT' %}
                            Departments Wise Student Info Report
                        {% elseif listType == 'PROGRAM' %}
                            Programs Wise Student Info Report
                        {% elseif listType == 'BATCH' %}
                            Batch/Section Wise Student Info Report
                        {% elseif listType == 'STUDENT' %}
                            Students Information Report
                        {% else %}
                            Student Info Report
                        {% endif %}
                    </td>
                </tr>
            </tbody>
        </table>

        {# Filter Information #}
        {% if kpiHeader and (kpiHeader.schoolName or kpiHeader.deptName or kpiHeader.programName or kpiHeader.termName or kpiHeader.batchName) %}
            <table class="report-summary" style="width: 100%;">
                <tr>
                    <td colspan="{{ dataTableColspan }}" style="padding: 8px 12px;">
                        {% if kpiHeader.schoolName %}
                            <strong>School:</strong> {{ kpiHeader.schoolName }}
                        {% endif %}
                        {% if kpiHeader.deptName %}
                            | <strong>Department:</strong> {{ kpiHeader.deptName }}
                        {% endif %}
                        {% if kpiHeader.programName %}
                            | <strong>Program:</strong> {{ kpiHeader.programName }}
                        {% endif %}
                        {% if kpiHeader.termName %}
                            | <strong>Term:</strong> {{ kpiHeader.termName }}
                        {% endif %}
                        {% if kpiHeader.batchName %}
                            | <strong>Batch/Section:</strong> {{ kpiHeader.batchName }}
                        {% endif %}
                    </td>
                </tr>
            </table>
        {% endif %}

        {# Main Data Table #}
        <table class="data-table">
            <thead>
                <tr>
                    <th class="col-sno">Si.No</th>
                    <th class="col-item">
                        {% if listType == 'SCHOOL' %}
                            School
                        {% elseif listType == 'DEPARTMENT' %}
                            Department
                        {% elseif listType == 'PROGRAM' %}
                            Program
                        {% elseif listType == 'BATCH' %}
                            Batch/Section
                        {% elseif listType == 'STUDENT' %}
                            Student
                        {% else %}
                            Item
                        {% endif %}
                    </th>
                    <th class="col-active">Active</th>
                    <th class="col-dormant">Dormant</th>
                    <th class="col-failed">Failed</th>
                    {% if showDetails %}
                        <th class="col-discontinued">Discontinued</th>
                        <th class="col-completed">Completed</th>
                    {% endif %}
                    <th class="col-total">Total</th>
                </tr>
            </thead>
            <tbody>
                {% for item in studentListData %}
                    {# School/Department Header Row #}
                    <tr class="school-header{% if showAdmissionYear %} school-header-with-year{% endif %}">
                        <td class="col-sno">{{ loop.index }}</td>
                        <td class="col-item">{{ item.name|default('') }}</td>
                        <td class="col-active">{{ (item.totals.active_student|default(0))|number_format(0, '.', ',') }}</td>
                        <td class="col-dormant">{{ (item.totals.dormant_student|default(0))|number_format(0, '.', ',') }}</td>
                        <td class="col-failed">{{ (item.totals.failed_student|default(0))|number_format(0, '.', ',') }}</td>
                        {% if showDetails %}
                            <td class="col-discontinued">{{ (item.totals.discontinue_student|default(0))|number_format(0, '.', ',') }}</td>
                            <td class="col-completed">{{ (item.totals.completed_student|default(0))|number_format(0, '.', ',') }}</td>
                        {% endif %}
                        <td class="col-total">{{ (item.totals.total_student_count|default(0))|number_format(0, '.', ',') }}</td>
                    </tr>

                    {% if showAdmissionYear and item.admission_years %}
                        {% for yearData in item.admission_years %}
                            {% if not showProgramLevel %}
                                {# Year-only view: show year rows #}
                                <tr class="year-row">
                                    <td class="col-sno"></td>
                                    <td class="col-item">Admission Year: {{ yearData.admission_year == 9999 ? 'Unknown' : yearData.admission_year }}</td>
                                    <td class="col-active">{{ (yearData.active_student|default(0))|number_format(0, '.', ',') }}</td>
                                    <td class="col-dormant">{{ (yearData.dormant_student|default(0))|number_format(0, '.', ',') }}</td>
                                    <td class="col-failed">{{ (yearData.failed_student|default(0))|number_format(0, '.', ',') }}</td>
                                    {% if showDetails %}
                                        <td class="col-discontinued">{{ (yearData.discontinue_student|default(0))|number_format(0, '.', ',') }}</td>
                                        <td class="col-completed">{{ (yearData.completed_student|default(0))|number_format(0, '.', ',') }}</td>
                                    {% endif %}
                                    <td class="col-total">{{ (yearData.total_student_count|default(0))|number_format(0, '.', ',') }}</td>
                                </tr>
                            {% else %}
                                {# Full hierarchy view: show year + programs #}
                                <tr class="year-row">
                                    <td class="col-sno"></td>
                                    <td class="col-item"><strong>Admission Year: {{ yearData.admission_year == 9999 ? 'Unknown' : yearData.admission_year }}</strong></td>
                                    <td class="col-active">{{ (yearData.active_student|default(0))|number_format(0, '.', ',') }}</td>
                                    <td class="col-dormant">{{ (yearData.dormant_student|default(0))|number_format(0, '.', ',') }}</td>
                                    <td class="col-failed">{{ (yearData.failed_student|default(0))|number_format(0, '.', ',') }}</td>
                                    {% if showDetails %}
                                        <td class="col-discontinued">{{ (yearData.discontinue_student|default(0))|number_format(0, '.', ',') }}</td>
                                        <td class="col-completed">{{ (yearData.completed_student|default(0))|number_format(0, '.', ',') }}</td>
                                    {% endif %}
                                    <td class="col-total">{{ (yearData.total_student_count|default(0))|number_format(0, '.', ',') }}</td>
                                </tr>

                                {# Program Level Rows #}
                                {% if yearData.programs %}
                                    {% for progData in yearData.programs %}
                                        <tr class="program-row">
                                            <td class="col-sno"></td>
                                            <td class="col-item">Program Level: {{ progData.level|default('') }}</td>
                                            <td class="col-active">{{ (progData.active_student|default(0))|number_format(0, '.', ',') }}</td>
                                            <td class="col-dormant">{{ (progData.dormant_student|default(0))|number_format(0, '.', ',') }}</td>
                                            <td class="col-failed">{{ (progData.failed_student|default(0))|number_format(0, '.', ',') }}</td>
                                            {% if showDetails %}
                                                <td class="col-discontinued">{{ (progData.discontinue_student|default(0))|number_format(0, '.', ',') }}</td>
                                                <td class="col-completed">{{ (progData.completed_student|default(0))|number_format(0, '.', ',') }}</td>
                                            {% endif %}
                                            <td class="col-total">{{ (progData.total_student_count|default(0))|number_format(0, '.', ',') }}</td>
                                        </tr>
                                    {% endfor %}
                                {% endif %}
                            {% endif %}
                        {% endfor %}
                    {% endif %}
                {% endfor %}
            </tbody>
        </table>

        {# Footer #}
        <div class="footer">
            <div class="generated-date">Generated on: {{ 'now'|date('d/m/Y H:i A') }}</div>
        </div>
    </div>
</body>
</html>
