<?php
function template30($institutions, $branch, $students_array, $examination, $array, $result, $class_id, $section_id)
{
    // ✅ flatten + reindex (same safety as template29)
    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);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

    <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>

<?php
for ($i = 0; $i < count($students_array); $i += 2):

    $student1 = $students_array[$i] ?? null;
    $student2 = $students_array[$i+1] ?? null;
?>

<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']) ?>">
            <?php endif; ?>
        </div>
        <div class="school-name"><?= htmlspecialchars($institutions['institution_name']) ?></div>
        <div class="school-address"><?= htmlspecialchars($branch['address']) ?></div>
        <div class="school-meta"><?= htmlspecialchars($branch['branch_code']) ?></div>
    </div>

    <div class="report-title">Periodic Test – I</div>
    <div class="year">Academic Session 2025–2026</div>

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

    <table class="marks-table">
        <tr>
            <th>Subject</th><th>PT-1 (40)</th><th>Ass. (5)</th><th>N.B. (5)</th><th>Total (50)</th>
        </tr>
        <tr><td class="left">English</td><td><?= $m1[119] ?? '' ?></td><td><?= $m1[120] ?? '' ?></td><td><?= $m1[121] ?? '' ?></td><td><?= $m1[122] ?? '' ?></td></tr>
        <tr><td class="left">Hindi</td><td><?= $m1[127] ?? '' ?></td><td><?= $m1[128] ?? '' ?></td><td><?= $m1[129] ?? '' ?></td><td><?= $m1[130] ?? '' ?></td></tr>
        <tr><td class="left">Punjabi</td><td><?= $m1[135] ?? '' ?></td><td><?= $m1[136] ?? '' ?></td><td><?= $m1[137] ?? '' ?></td><td><?= $m1[138] ?? '' ?></td></tr>
        <tr><td class="left">Maths</td><td><?= $m1[143] ?? '' ?></td><td><?= $m1[144] ?? '' ?></td><td><?= $m1[145] ?? '' ?></td><td><?= $m1[146] ?? '' ?></td></tr>
        <tr><td class="left">EVS</td><td><?= $m1[151] ?? '' ?></td><td><?= $m1[152] ?? '' ?></td><td><?= $m1[153] ?? '' ?></td><td><?= $m1[154] ?? '' ?></td></tr>
        <tr>
            <th>G.Total</th>
            <th><?= $m1[159] ?? '' ?></th>
            <th><?= $m1[160] ?? '' ?></th>
            <th><?= $m1[161] ?? '' ?></th>
            <th><?= $m1[162] ?? '' ?></th>
        </tr>
    </table>

</div>
<?php endif; ?>

<!-- ================= STUDENT 2 ================= -->
<?php if ($student2): ?>
<?php $m2 = $student2['marksheet_data'] ?? []; ?>
<div class="student-col">

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

    <div class="report-title">Periodic Test – I</div>
    <div class="year">Academic Session 2025–2026</div>

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

    <table class="marks-table">
        <tr>
            <th>Subject</th><th>PT-1 (40)</th><th>Ass. (5)</th><th>N.B. (5)</th><th>Total (50)</th>
        </tr>
        <tr><td class="left">English</td><td><?= $m2[119] ?? '' ?></td><td><?= $m2[120] ?? '' ?></td><td><?= $m2[121] ?? '' ?></td><td><?= $m2[122] ?? '' ?></td></tr>
        <tr><td class="left">Hindi</td><td><?= $m2[127] ?? '' ?></td><td><?= $m2[128] ?? '' ?></td><td><?= $m2[129] ?? '' ?></td><td><?= $m2[130] ?? '' ?></td></tr>
        <tr><td class="left">Punjabi</td><td><?= $m2[135] ?? '' ?></td><td><?= $m2[136] ?? '' ?></td><td><?= $m2[137] ?? '' ?></td><td><?= $m2[138] ?? '' ?></td></tr>
        <tr><td class="left">Maths</td><td><?= $m2[143] ?? '' ?></td><td><?= $m2[144] ?? '' ?></td><td><?= $m2[145] ?? '' ?></td><td><?= $m2[146] ?? '' ?></td></tr>
        <tr><td class="left">EVS</td><td><?= $m2[151] ?? '' ?></td><td><?= $m2[152] ?? '' ?></td><td><?= $m2[153] ?? '' ?></td><td><?= $m2[154] ?? '' ?></td></tr>
        <tr>
            <th>G.Total</th>
            <th><?= $m2[159] ?? '' ?></th>
            <th><?= $m2[160] ?? '' ?></th>
            <th><?= $m2[161] ?? '' ?></th>
            <th><?= $m2[162] ?? '' ?></th>
        </tr>
    </table>

</div>
<?php endif; ?>

</div>

<?php endfor; ?>

</body>
</html>
<?php
}
?>
