<?php
function template32($institutions, $branch, $students_array, $examination, $array, $result, $class_id, $section_id)
{
    
    require_once $GLOBALS['base_path_to_learning'] . '/resources/marksheet_template/template32/function.php';
    // ✅ flatten + reindex safe
    if (!empty($students_array) && isset($students_array[0][0]) && is_array($students_array[0][0])) {
        $flat = [];
        foreach ($students_array as $group) {
            foreach ($group as $st) {
                $flat[] = $st;
            }
        }
        $students_array = $flat;
    }
    $students_array = array_values($students_array);

    $student1 = $students_array[0] ?? null;
    $student2 = $students_array[1] ?? null;
?>
    <!DOCTYPE html>
    <html lang="en">

    <head>
        <meta charset="UTF-8">
        <title>Template 29</title>

        <style>
            * {
                box-sizing: border-box;
                font-family: Arial, Helvetica, sans-serif;
            }

            body {
                margin: 0;
                padding: 16px;
                background: #f3f5f9;
                color: #111;
            }

            /* ===========================
           ✅ NORMAL SCREEN VIEW
           =========================== */
            .print-page {
                width: 100%;
                max-width: 1100px;
                margin: 10px auto;
                background: #fff;
                border: 2px solid #111;
                border-radius: 12px;

                display: grid;
                grid-template-columns: 1fr;
                gap: 12px;
                padding: 12px;
                position: relative;
            }

            /* ✅ border bottom */
            .print-page {
                border-bottom: 6px solid #111;
            }

            .student-col {
                border: 2px solid #111;
                border-radius: 12px;
                padding: 12px;
                background: #fff;
                min-height: 160mm;
            }

            /* ✅ Hide middle line in screen mode */
            .print-page::after {
                display: none;
            }

            /* ===========================
           ✅ PRINT MODE ONLY
           =========================== */
            @page {
                size: A4 landscape;
                margin: 0mm;
            }

            @media print {

                html,
                body {
                    margin: 0 !important;
                    padding: 0 !important;
                    background: #fff;
                }

                .print-page {
                    border: none !important;
                    margin: 0 !important;
                    padding: 0 !important;
                    display: flex;
                    gap: 4mm;
                    page-break-after: always;
                }

                .student-col,
                .empty-col {
                    width: 50%;
                    border: 2px solid #111;
                    border-radius: 12px;
                    padding: 6px;
                    box-sizing: border-box;
                }

                .empty-col {
                    border: none !important;
                    padding: 0 !important;
                    background: transparent !important;
                }
            }






            /* ================= SCHOOL HEADER ================= */
            .school-header {
                text-align: center;
                padding: 10px 10px 14px 10px;
                border: 2px solid #111;
                border-radius: 12px;
                position: relative;
            }

            .school-logo {
                position: absolute;
                left: 10px;
                top: 10px;
                width: 55px;
                height: 55px;
                border-radius: 10px;
                overflow: hidden;
                border: 1px solid #ccc;
                background: #fff;
                display: flex;
                align-items: center;
                justify-content: center;
            }

            .school-logo img {
                width: 100%;
                height: 100%;
                object-fit: contain;
            }

            .school-name {
                font-size: 16px;
                font-weight: 900;
                text-transform: uppercase;
                margin-bottom: 4px;
            }

            .school-address {
                font-size: 12px;
                color: #333;
                margin-bottom: 3px;
            }

            .school-meta {
                font-size: 11px;
                color: #444;
            }

            /* Photo */
            .photo-box {
                width: 90px;
                height: 105px;
                border: 2px solid #111;
                border-radius: 10px;
                margin: 12px auto 8px auto;
                overflow: hidden;
                background: #fafafa;
                display: flex;
                align-items: center;
                justify-content: center;
                font-size: 12px;
                font-weight: 600;
                color: #666;
            }

            .student-photo {
                width: 100%;
                height: 100%;
                object-fit: cover;
            }

            .report-title {
                text-align: center;
                font-size: 13px;
                font-weight: 900;
                padding: 6px 0;
                border-top: 2px solid #111;
                border-bottom: 2px solid #111;
                margin-top: 6px;
            }

            .year {
                text-align: center;
                font-size: 12px;
                font-weight: 700;
                margin-top: 5px;
                margin-bottom: 8px;
            }

            .info-table {
                width: 100%;
                border-collapse: collapse;
                font-size: 12px;
                margin-top: 8px;
            }

            .info-table td {
                padding: 6px;
                border-bottom: 1px dashed #aaa;
            }

            .info-table td:first-child {
                font-weight: 800;
                width: 42%;
            }

            .page-title {
                font-size: 12px;
                font-weight: 900;
                text-align: center;
                padding: 7px;
                border: 2px solid #111;
                border-radius: 10px;
                margin: 12px 0 8px 0;
                background: #f7f7f7;
            }

            .marks-table {
                width: 100%;
                border-collapse: collapse;
                font-size: 12px;
            }

            .marks-table th,
            .marks-table td {
                border: 1px solid #000;
                padding: 6px 4px;
                text-align: center;
            }

            .marks-table th {
                background: #111;
                color: #fff;
                font-weight: 900;
            }

            .marks-table td.left {
                text-align: left;
                font-weight: 800;
                background: #fafafa;
            }
        </style>
    </head>

    <body>

        <div class="print-page">

            <!-- ✅ STUDENT 1 -->
            <?php if ($student1): ?>
                <?php $m1 = $student1['marksheet_data'] ?? []; ?>
                <div class="student-col">

                    <div class="school-header">
                        <div class="school-logo">
                            <?php if (!empty($institutions['logo'])): ?>
                                <img src="<?= htmlspecialchars($institutions['logo']) ?>" alt="School Logo">
                            <?php endif; ?>
                        </div>

                        <div class="school-name"><?= htmlspecialchars($institutions['institution_name']) ?></div>

                        <div class="school-address">
                            <?= htmlspecialchars($branch['address']) ?>
                            <?php if (!empty($branch['pincode'])): ?> - <?= htmlspecialchars($branch['pincode']) ?><?php endif; ?>
                        </div>

                        <div class="school-meta">
                            <?= htmlspecialchars($branch['branch_code']) ?>
                            <?php if (!empty($branch['contact_number'])): ?>, <?= htmlspecialchars($branch['contact_number']) ?><?php endif; ?>
                            <?php if (!empty($branch['email'])): ?>, <?= strtolower(htmlspecialchars($branch['email'])) ?><?php endif; ?>
                        </div>
                    </div>

                    <div class="photo-box">
                        <?php if (!empty($student1['student_image']['filepath'])): ?>
                            <img src="<?= htmlspecialchars($student1['student_image']['filepath']) ?>" class="student-photo">
                        <?php else: ?>
                            No Photo
                        <?php endif; ?>
                    </div>

                    <div class="report-title">ACADEMIC PERFORMANCE: SCHOLASTIC AREAS</div>
                    <div class="year">2025-2026</div>

                    <table class="info-table">
                        <tr>
                            <td>Name</td>
                            <td>: <?= htmlspecialchars($student1['name'] ?? '') ?></td>
                        </tr>
                        <tr>
                            <td>Class & Sec.</td>
                            <td>: <?= htmlspecialchars($class_id) ?> <?= htmlspecialchars($section_id) ?></td>
                        </tr>
                        <tr>
                            <td>Admission No.</td>
                            <td>: <?= htmlspecialchars($student1['admission_number'] ?? '') ?></td>
                        </tr>
                        <tr>
                            <td>Date of Birth</td>
                            <td>: <?= htmlspecialchars($student1['dob'] ?? '') ?></td>
                        </tr>
                        <tr>
                            <td>Father’s Name</td>
                            <td>: <?= htmlspecialchars($student1['fathername'] ?? '') ?></td>
                        </tr>
                        <tr>
                            <td>Mother’s Name</td>
                            <td>: <?= htmlspecialchars($student1['mothername'] ?? '') ?></td>
                        </tr>
                    </table>

                    <div class="page-title">ACADEMIC PERFORMANCE: SCHOLASTIC AREAS</div>

                    <table class="marks-table">
                        <thead>
                            <tr>
                                <th colspan="3" class="ms29-term-head">TERM - I</th>
                            </tr>
                            <tr>
                                <th>Subject</th>
                                <th>Type</th>
                                <th>Periodic -1</th>
                            </tr>
                        </thead>

                        <tbody>

    <!-- ✅ English -->
    <tr>
        <td rowspan="2">English</td>
        <td>Oral</td>
        <td><?= is_numeric($m1[39] ?? null) ? gradeOutOf30($m1[39]) : '-' ?></td>
    </tr>
    <tr>
        <td>Written</td>
        <td><?= is_numeric($m1[41] ?? null) ? gradeOutOf50($m1[41]) : '-' ?></td>
    </tr>

    <!-- ✅ Hindi -->
    <?php
    $hindi_oral_p3    = $m1[43] ?? '';
    $hindi_written_p3 = $m1[45] ?? '';

    if ($hindi_oral_p3 !== '' || $hindi_written_p3 !== ''):
    ?>
        <tr>
            <td rowspan="2">Hindi</td>
            <td>Oral</td>
            <td><?= is_numeric($hindi_oral_p3) ? gradeOutOf30($hindi_oral_p3) : '-' ?></td>
        </tr>
        <tr>
            <td>Written</td>
            <td><?= is_numeric($hindi_written_p3) ? gradeOutOf50($hindi_written_p3) : '-' ?></td>
        </tr>
    <?php endif; ?>

    <!-- ✅ Maths -->
    <tr>
        <td rowspan="2">Maths</td>
        <td>Oral</td>
        <td><?= is_numeric($m1[47] ?? null) ? gradeOutOf30($m1[47]) : '-' ?></td>
    </tr>
    <tr>
        <td>Written</td>
        <td><?= is_numeric($m1[49] ?? null) ? gradeOutOf50($m1[49]) : '-' ?></td>
    </tr>

    <!-- ✅ EVS (Oral) -->
    <?php
    $evs_p3 = $m1[51] ?? '';
    if ($evs_p3 !== ''):
    ?>
        <tr>
            <td colspan="2">EVS (Oral)</td>
            <td><?= is_numeric($evs_p3) ? gradeOutOf30($evs_p3) : '-' ?></td>
        </tr>
    <?php endif; ?>

    <!-- ✅ GK & Rhymes -->
    <tr>
        <td colspan="2">GK & Rhymes</td>
        <td><?= is_numeric($m1[53] ?? null) ? gradeOutOf30($m1[53]) : '-' ?></td>
    </tr>

    <!-- ✅ Drawing -->
    <tr>
        <td colspan="2">Drawing</td>
        <td><?= is_numeric($m1[55] ?? null) ? gradeOutOf30($m1[55]) : '-' ?></td>
    </tr>

    <!-- ✅ Overall Grade (UNCHANGED) -->
    <tr>
        <td colspan="2">Overall Grade</td>
        <td><?= $m1[57] ?? '' ?></td>
    </tr>

</tbody>

                    </table>


                </div>
            <?php endif; ?>

            <!-- ✅ STUDENT 2 OR EMPTY HALF -->
            <?php if ($student2): ?>
                <?php $m2 = $student2['marksheet_data'] ?? []; ?>
                <div class="student-col">
                    <!-- COPY SAME BLOCK FOR STUDENT2 -->
                    <div class="school-header">
                        <div class="school-logo">
                            <?php if (!empty($institutions['logo'])): ?>
                                <img src="<?= htmlspecialchars($institutions['logo']) ?>" alt="School Logo">
                            <?php endif; ?>
                        </div>

                        <div class="school-name"><?= htmlspecialchars($institutions['institution_name']) ?></div>
                        <div class="school-address">
                            <?= htmlspecialchars($branch['address']) ?>
                            <?php if (!empty($branch['pincode'])): ?> - <?= htmlspecialchars($branch['pincode']) ?><?php endif; ?>
                        </div>

                        <div class="school-meta">
                            <?= htmlspecialchars($branch['branch_code']) ?>
                            <?php if (!empty($branch['contact_number'])): ?>, <?= htmlspecialchars($branch['contact_number']) ?><?php endif; ?>
                            <?php if (!empty($branch['email'])): ?>, <?= strtolower(htmlspecialchars($branch['email'])) ?><?php endif; ?>
                        </div>
                    </div>

                    <div class="photo-box">
                        <?php if (!empty($student2['student_image']['filepath'])): ?>
                            <img src="<?= htmlspecialchars($student2['student_image']['filepath']) ?>" class="student-photo">
                        <?php else: ?>
                            No Photo
                        <?php endif; ?>
                    </div>

                    <div class="report-title">HOLISTIC PROGRESS REPORT</div>
                    <div class="year">2025-2026</div>

                    <table class="info-table">
                        <tr>
                            <td>Name</td>
                            <td>: <?= htmlspecialchars($student2['name'] ?? '') ?></td>
                        </tr>
                        <tr>
                            <td>Class & Sec.</td>
                            <td>: <?= htmlspecialchars($class_id) ?> <?= htmlspecialchars($section_id) ?></td>
                        </tr>
                        <tr>
                            <td>Admission No.</td>
                            <td>: <?= htmlspecialchars($student2['admission_number'] ?? '') ?></td>
                        </tr>
                        <tr>
                            <td>Date of Birth</td>
                            <td>: <?= htmlspecialchars($student2['dob'] ?? '') ?></td>
                        </tr>
                        <tr>
                            <td>Father’s Name</td>
                            <td>: <?= htmlspecialchars($student2['fathername'] ?? '') ?></td>
                        </tr>
                        <tr>
                            <td>Mother’s Name</td>
                            <td>: <?= htmlspecialchars($student2['mothername'] ?? '') ?></td>
                        </tr>
                    </table>

                    <div class="page-title">ACADEMIC PERFORMANCE: SCHOLASTIC AREAS</div>

                    <table class="marks-table">
                        <thead>
                            <tr>
                                <th colspan="3" class="ms29-term-head">TERM - I</th>
                            </tr>
                            <tr>
                                <th>Subject</th>
                                <th>Type</th>
                                <th>Periodic -1</th>
                            </tr>
                        </thead>

                        <tbody>

    <!-- ✅ English -->
    <tr>
        <td rowspan="2">English</td>
        <td>Oral</td>
        <td><?= is_numeric($m2[39] ?? null) ? gradeOutOf30($m2[39]) : '-' ?></td>
    </tr>
    <tr>
        <td>Written</td>
        <td><?= is_numeric($m2[41] ?? null) ? gradeOutOf50($m2[41]) : '-' ?></td>
    </tr>

    <!-- ✅ Hindi -->
    <?php
    $hindi_oral_p3    = $m2[43] ?? '';
    $hindi_written_p3 = $m2[45] ?? '';

    if ($hindi_oral_p3 !== '' || $hindi_written_p3 !== ''):
    ?>
        <tr>
            <td rowspan="2">Hindi</td>
            <td>Oral</td>
            <td><?= is_numeric($hindi_oral_p3) ? gradeOutOf30($hindi_oral_p3) : '-' ?></td>
        </tr>
        <tr>
            <td>Written</td>
            <td><?= is_numeric($hindi_written_p3) ? gradeOutOf50($hindi_written_p3) : '-' ?></td>
        </tr>
    <?php endif; ?>

    <!-- ✅ Maths -->
    <tr>
        <td rowspan="2">Maths</td>
        <td>Oral</td>
        <td><?= is_numeric($m2[47] ?? null) ? gradeOutOf30($m2[47]) : '-' ?></td>
    </tr>
    <tr>
        <td>Written</td>
        <td><?= is_numeric($m2[49] ?? null) ? gradeOutOf50($m2[49]) : '-' ?></td>
    </tr>

    <!-- ✅ EVS (Oral) -->
    <?php
    $evs_p3 = $m2[51] ?? '';
    if ($evs_p3 !== ''):
    ?>
        <tr>
            <td colspan="2">EVS (Oral)</td>
            <td><?= is_numeric($evs_p3) ? gradeOutOf30($evs_p3) : '-' ?></td>
        </tr>
    <?php endif; ?>

    <!-- ✅ GK & Rhymes -->
    <tr>
        <td colspan="2">GK & Rhymes</td>
        <td><?= is_numeric($m2[53] ?? null) ? gradeOutOf30($m2[53]) : '-' ?></td>
    </tr>

    <!-- ✅ Drawing -->
    <tr>
        <td colspan="2">Drawing</td>
        <td><?= is_numeric($m2[55] ?? null) ? gradeOutOf30($m2[55]) : '-' ?></td>
    </tr>

    <!-- ✅ Overall Grade (UNCHANGED) -->
    <tr>
        <td colspan="2">Overall Grade</td>
        <td><?= $m2[57] ?? '' ?></td>
    </tr>

</tbody>

                    </table>


                </div>
            <?php else: ?>
                <!-- ✅ EMPTY RIGHT SIDE (if odd student count) -->
                <div class="empty-col"></div>
            <?php endif; ?>

        </div>

    </body>

    </html>
<?php
}
?>