<?php
function template30($institutions, $branch, $students_array, $examination, $array, $result, $class_id, $section_id)
{
    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 = array_values($flat);
    }
?>
<!DOCTYPE html>
<html>
<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{
    min-height: 78px;           /* fixed height */
    padding-top: 12px !important;
  }

  /* 🎯 perfectly center logo vertically */
  .school-logo{
    top: 50% !important;
    transform: translateY(-50%);
  }
}

        /* ================= 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 – III</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-3 (40)</th><th>Ass. (5)</th><th>N.B. (5)</th><th>Total (50)</th>
</tr>

<tr><td class="left">English</td><td><?= $m1[1] ?></td><td><?= $m1[2] ?></td><td><?= $m1[3] ?></td><td><?= $m1[4] ?></td></tr>
<tr><td class="left">Hindi</td><td><?= $m1[11] ?></td><td><?= $m1[12] ?></td><td><?= $m1[13] ?></td><td><?= $m1[14] ?></td></tr>
<tr><td class="left">Punjabi</td><td><?= $m1[21] ?></td><td><?= $m1[22] ?></td><td><?= $m1[23] ?></td><td><?= $m1[24] ?></td></tr>
<tr><td class="left">Maths</td><td><?= $m1[31] ?></td><td><?= $m1[32] ?></td><td><?= $m1[33] ?></td><td><?= $m1[34] ?></td></tr>
<tr><td class="left">EVS</td><td><?= $m1[41] ?></td><td><?= $m1[42] ?></td><td><?= $m1[43] ?></td><td><?= $m1[44] ?></td></tr>
<tr><td class="left">Drawing</td><td><?= $m1[51] ?></td><td><?= $m1[52] ?></td><td><?= $m1[53] ?></td><td><?= $m1[54] ?></td></tr>
<tr><td class="left">Computer</td><td><?= $m1[61] ?></td><td><?= $m1[62] ?></td><td><?= $m1[63] ?></td><td><?= $m1[64] ?></td></tr>
<tr><td class="left">GK</td><td><?= $m1[71] ?></td><td><?= $m1[72] ?></td><td><?= $m1[73] ?></td><td><?= $m1[74] ?></td></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 – III</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-3 (40)</th><th>Ass. (5)</th><th>N.B. (5)</th><th>Total (50)</th>
</tr>

<tr><td class="left">English</td><td><?= $m2[1] ?></td><td><?= $m2[2] ?></td><td><?= $m2[3] ?></td><td><?= $m2[4] ?></td></tr>
<tr><td class="left">Hindi</td><td><?= $m2[11] ?></td><td><?= $m2[12] ?></td><td><?= $m2[13] ?></td><td><?= $m2[14] ?></td></tr>
<tr><td class="left">Punjabi</td><td><?= $m2[21] ?></td><td><?= $m2[22] ?></td><td><?= $m2[23] ?></td><td><?= $m2[24] ?></td></tr>
<tr><td class="left">Maths</td><td><?= $m2[31] ?></td><td><?= $m2[32] ?></td><td><?= $m2[33] ?></td><td><?= $m2[34] ?></td></tr>
<tr><td class="left">EVS</td><td><?= $m2[41] ?></td><td><?= $m2[42] ?></td><td><?= $m2[43] ?></td><td><?= $m2[44] ?></td></tr>
<tr><td class="left">Drawing</td><td><?= $m2[51] ?></td><td><?= $m2[52] ?></td><td><?= $m2[53] ?></td><td><?= $m2[54] ?></td></tr>
<tr><td class="left">Computer</td><td><?= $m2[61] ?></td><td><?= $m2[62] ?></td><td><?= $m2[63] ?></td><td><?= $m2[64] ?></td></tr>
<tr><td class="left">GK</td><td><?= $m2[71] ?></td><td><?= $m2[72] ?></td><td><?= $m2[73] ?></td><td><?= $m2[74] ?></td></tr>

</table>

</div>
<?php endif; ?>

</div>
<?php endfor; ?>

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