<style id="table-print">
	.gm-report-wrap {
		font-family: Arial, sans-serif;
		font-size: 13px;
	}
	.gm-title {
		text-align: center;
		font-weight: bold;
		font-size: 17px;
		margin-bottom: 12px;
	}
	.gm-header-row {
		width: 100%;
		border-collapse: collapse;
		margin-bottom: 18px;
	}
	.gm-header-row td {
		padding: 3px 10px 3px 0;
		font-size: 13px;
		border: none !important;
	}
	.gm-table {
		width: 100%;
		border-collapse: collapse;
		margin-bottom: 10px;
	}
	.gm-table th {
		border: 1px solid #000;
		padding: 4px 6px;
		text-align: center;
		font-weight: bold;
		background: none !important;
		font-size: 13px;
	}
	.gm-table th:nth-child(1), .gm-table td:nth-child(1) { width: 6%; }
	.gm-table th:nth-child(2), .gm-table td:nth-child(2) { width: 12%; }
	.gm-table th:nth-child(3), .gm-table td:nth-child(3) { width: 28%; }
	.gm-table th:nth-child(4), .gm-table td:nth-child(4) { width: 18%; }
	.gm-table th:nth-child(5), .gm-table td:nth-child(5) { width: 18%; }
	.gm-table th:nth-child(6), .gm-table td:nth-child(6) { width: 18%; }
	.gm-table td {
		border: 1px solid #000;
		padding: 4px 6px;
		text-align: center;
		font-size: 13px;
		height: 20px;
	}
	.gm-table td.text-left { text-align: left; }
	.hidden { display: none !important; }
	.gm-print-header { display: none; }
	@media print {
		.gm-print-header { display: block; }
	}
</style>

{% if students|length > 0 %}
{% set firstStudent = students|first %}

<div class="gm-report-wrap">

	<div class="gm-print-header">
		{% if additionalDetails.collegeData.isHeaderEnabled %}
			<div style="text-align: center; margin-bottom: 8px;">
				<img src="{{ additionalDetails.collegeData.collegeHeader }}" style="max-width: 100%; height: auto;">
			</div>
		{% else %}
			{% if additionalDetails.collegeData.collageLogo %}
			<div style="text-align: center; margin-bottom: 10px;">
				<img src="{{ additionalDetails.collegeData.collageLogo }}" style="width: 100px; height: auto;">
			</div>
			{% endif %}
			<div style="text-align: center; margin-bottom: 10px;">
				<div style="font-size: 18px; font-weight: bold; line-height: 1.4;">{{ additionalDetails.collegeData.collegeName ?? '' }}{% if additionalDetails.collegeData.universityName %} ({{ additionalDetails.collegeData.universityName }}){% endif %}</div>
				<div style="font-size: 14px; margin-top: 2px;">{{ additionalDetails.collegeData.collegeAddress ?? '' }}</div>
			</div>
		{% endif %}
	</div>

	<div class="gm-title">GRACE MARK REPORT - COURSE WISE</div>

	{#
		Build subject summary grouped by academicPaperSubjectId across ALL batches.
		- passedBefore : subjectResultStatusWithOutGraceMark == '0' (externalMarkObtainedInExamIsFailed = 0)
		- passedAfter  : already passed (passedBefore) OR (graceMark > 0 AND grade after grace is not 'F' / not empty)
		- maxGraceMark : highest graceMark seen for the subject
	#}
	{% set subjectMap = {} %}
	{% set batchNames = [] %}
	{% for student in students %}
		{% if student.batchName and student.batchName not in batchNames %}
			{% set batchNames = batchNames|merge([student.batchName]) %}
		{% endif %}
		{% for academicTerm in student.academicTerms %}
			{% for subject in academicTerm.subjects %}
				{% for examReg in subject.examRegistrations %}
					{% set key = subject.id %}

					{% if subjectMap[key] is not defined %}
						{% set subjectMap = subjectMap|merge({(key): {
							"code": subject.code,
							"name": subject.name,
							"passedBefore": 0,
							"passedAfter": 0,
							"maxGraceMark": 0
						}}) %}
					{% endif %}

					{% set row = subjectMap[key] %}

					{# passedBefore: isFailed == '0' means student passed before grace #}
					{% set passedBeforeFlag = (examReg.subjectResultStatusWithOutGraceMark == '0' or examReg.subjectResultStatusWithOutGraceMark == 0) %}

					{# passedAfter: already passed, or got a grace mark that resulted in a non-F grade #}
					{% set gradeAfterGrace = examReg.subjectExtrenalGradeWithGraceMark ?? '' %}
					{% set gotGraceMark = examReg.graceMark is not null and examReg.graceMark > 0 %}
					{% set passedAfterFlag = passedBeforeFlag or (gotGraceMark and gradeAfterGrace is not empty and gradeAfterGrace != 'F') %}

					{% set newPassedBefore = passedBeforeFlag ? row.passedBefore + 1 : row.passedBefore %}
					{% set newPassedAfter  = passedAfterFlag  ? row.passedAfter  + 1 : row.passedAfter %}
					{% set grace = examReg.graceMark|default(0) %}
					{% set newMax = grace > row.maxGraceMark ? grace : row.maxGraceMark %}

					{% set subjectMap = subjectMap|merge({(key): {
						"code": row.code,
						"name": row.name,
						"passedBefore": newPassedBefore,
						"passedAfter": newPassedAfter,
						"maxGraceMark": newMax
					}}) %}
				{% endfor %}
			{% endfor %}
		{% endfor %}
	{% endfor %}

	<table class="gm-table">
		<thead>
			<tr class="hidden">
				<th data-a-h="center" data-f-bold="true" colspan="6">{{ additionalDetails.collegeData.collegeName ?? '' }}{% if additionalDetails.collegeData.universityName %} ({{ additionalDetails.collegeData.universityName }}){% endif %}</th>
			</tr>
			<tr class="hidden">
				<th data-a-h="center" data-f-bold="true" colspan="6">{{ additionalDetails.collegeData.collegeAddress ?? '' }}</th>
			</tr>
			<tr class="hidden">
				<th data-a-h="center" data-f-bold="true" colspan="6">GRACE MARK REPORT</th>
			</tr>
			<tr class="hidden">
				<th data-a-h="left" data-f-bold="true" colspan="6">Examination: {{ firstStudent.examRegistrationName }}</th>
			</tr>
			<tr class="hidden">
				<th data-a-h="left" data-f-bold="true" colspan="6">Batch: {{ batchNames|join(', ') }}</th>
			</tr>
            <tr>
            <tr>
            <th colspan="6" style="border: none !important; padding: 8px 0 4px 0; font-size: 13px; text-align: left;">
                    <span style="display: inline-block; width: 110px;">
                        <strong>Examination</strong>
                    </span>
                    : {{ firstStudent.examRegistrationName }}
                </th>
            </tr>

            <tr>
                <th colspan="6" style="border: none !important; padding: 4px 0 8px 0; font-size: 13px; text-align: left;">
                    <span style="display: inline-block; width: 110px;">
                        <strong>Batch</strong>
                    </span>
                    : {{ batchNames|join(', ') }}
                </th>
            </tr>
			<tr>
				<th data-a-h="center" data-f-bold="true">Sl. No</th>
				<th data-a-h="center" data-f-bold="true">Course Code</th>
				<th data-a-h="center" data-f-bold="true">Course</th>
				<th data-a-h="center" data-f-bold="true">No. of Passed Students Before Gracing</th>
				<th data-a-h="center" data-f-bold="true">No. of Students Passed After Gracing</th>
				<th data-a-h="center" data-f-bold="true">Max Grace Mark</th>
			</tr>
		</thead>
		<tbody>
			{% set slNo = 1 %}
			{% for key, row in subjectMap %}
				<tr>
					<td>{{ slNo }}</td>
					<td>{{ row.code }}</td>
					<td class="text-left">{{ row.name }}</td>
					<td>{{ row.passedBefore }}</td>
					<td>{{ row.passedAfter }}</td>
					<td>{{ row.maxGraceMark }}</td>
				</tr>
				{% set slNo = slNo + 1 %}
			{% endfor %}
		</tbody>
	</table>

	<div class="gm-print-header" style="text-align: right; font-size: 12px; margin-top: 20px;">
		Generated on: {{ "now"|date("d-m-Y H:i:s") }}
	</div>

</div>
{% endif %}
