<?php
function template34($institutions, $branch, $students_array, $examination, $array, $result, $class_id, $section_id)
{
    
        require_once $GLOBALS['base_path_to_learning'] . '/resources/marksheet_template/template34/function.php';

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Holistic Progress Report</title>

<style>
/* ✅ PRINT SETTINGS - A4 LANDSCAPE */
@page{
  size: A4 landscape;
  margin: 6mm;
}

*{
  margin:0;
  padding:0;
  box-sizing:border-box;
}

/* ✅ SCREEN VIEW */
html, body{
  width: 100%;
  min-height: 100vh;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  background: #f2f2f8;
  padding: 20px;
}

/* ✅ REPORT CONTAINER */
.onepage-report{
  max-width: 1200px;
  margin: 0 auto 20px auto;
  background: #fff;
  border: 4px solid #c4161c;
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 10px 40px rgba(0,0,0,0.15);
  overflow: visible;
}

/* ✅ SCHOOL HEADER */
.school-header{
  text-align:center;
  padding-bottom: 10px;
  border-bottom: 3px double #c4161c;
  margin-bottom: 10px;
}

.school-logo img{
  width: 140px;
  height: 70px;
  object-fit:contain;
}

.school-name{
  font-size: 34px;
  font-weight: 900;
  color:#2c2f83;
  margin-top:4px;
}

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

.school-meta{
  font-size: 12px;
  margin-top:2px;
  color:#444;
}

/* ✅ REPORT TITLE */
.report-title{
  text-align:center;
  font-size: 26px;
  font-weight: 900;
  margin-top:8px;
  letter-spacing:1px;
  color:#111;
}

.year{
  text-align:center;
  font-size: 28px;
  font-weight: 900;
  color:#c4161c;
  margin:6px 0 12px 0;
}

/* ✅ STUDENT INFO TABLE */
.info-table{
  width: 100%;
  margin: 0 auto 10px auto;
  border-collapse: collapse;
}

.info-table td{
  border:none !important;
  padding: 5px 6px;
  font-size: 13px;
}

.info-table td:first-child{
  width: 20%;
  font-weight: 700;
  color:#2c2f83;
}

.line{
  display:inline-block;
  width: 70%;
  min-height: 16px;
  border-bottom: 2px dotted #888;
}

/* ✅ SECTION TITLE */
.page-title{
  font-weight:bold;
  font-size:14px;
  margin: 8px 0 6px 0;
  color:#2c2f83;
  padding-bottom: 3px;
  border-bottom: 2px solid #c4161c;
}

.sub-head{
  font-weight:bold;
  margin: 8px 0 5px;
  color: #2c2f83;
  font-size: 13px;
}

/* ✅ TABLE COMMON */
table{
  width:100%;
  border-collapse:collapse;
  font-size:10.5px;
}

th, td{
  border:1px solid #000;
  padding: 3px 4px;
  text-align:center;
  vertical-align:middle;
}

th{
  background:#b4b4d6;
  font-weight:700;
  font-size:10px;
}

.left{
  text-align:left;
  padding-left:7px;
  font-weight:600;
}

.term-head th{
  background:#2c2f83 !important;
  color:#fff !important;
}

/* ✅ SMALL BOX HEIGHT */
.blank-box{
  height:26px;
}

.remarks-line{
  border-bottom:1px dotted #444;
  min-height:18px;
  margin-top:3px;
  font-size:11px;
  padding:2px 0;
}

/* ✅ PRINT STYLES - EACH STUDENT ONE PAGE */
@media print{
  html, body{
    width: 297mm !important;
    height: 210mm !important;
    margin: 0 !important;
    padding: 0 !important;
    background:#fff !important;
  }

  body{
    padding:0 !important;
  }

  .onepage-report{
    width:100% !important;
    height:100% !important;
    margin:0 !important;
    padding:3mm !important;
    border-radius:0 !important;
    box-shadow:none !important;
    page-break-after: always !important;
    break-after: page !important;
  }

  .onepage-report:last-child{
    page-break-after: auto !important;
    break-after: auto !important;
  }

  .school-header{
    margin-bottom:3px;
    padding-bottom:3px;
  }

  .school-name{ font-size:28px; }
  .report-title{ font-size:20px; margin-top:4px; }
  .year{ font-size:22px; margin:4px 0 6px; }

  .info-table td{
    padding:2px 4px;
    font-size:11px;
  }

  .page-title{
    font-size:12px;
    margin:5px 0 4px;
  }

  table{
    font-size:9px;
  }

  th, td{
    padding:2px 3px;
  }

  .blank-box{ height:20px; }
  .remarks-line{ min-height:14px; font-size:10px; }

  *{
    page-break-inside: avoid !important;
    break-inside: avoid !important;
  }
}
</style>

</head>

<body>

<?php foreach($students_array as $stu){ ?>

<div class="onepage-report">

    <!-- ✅ SCHOOL HEADER -->
    <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>
    <?php $m = $stu['marksheet_data'] ?? []; ?>
    <!-- ✅ REPORT TITLE -->
    <div class="report-title">HOLISTIC PROGRESS REPORT</div>
    <div class="year">2025-2026</div>

    <!-- ✅ STUDENT INFO -->
    <table class="info-table">
        <tr><td>Name</td><td>: <span class="line"><?= $stu['name'] ?? '' ?></span></td></tr>
        <tr><td>Class & Sec.</td><td>: <span class="line"><?= $class_id; ?> <?= $section_id; ?></span></td></tr>
        <tr><td>PEN No.</td><td>: <span class="line"><?= $stu['admission_number'] ?? '' ?></span></td></tr>
        <tr><td>Admission No.</td><td>: <span class="line"><?= $stu['admission_number'] ?? '' ?></span></td></tr>
        <tr><td>Date of Birth</td><td>: <span class="line"><?= $stu['dob'] ?? '' ?></span></td></tr>
        <tr><td>Father's Name</td><td>: <span class="line"><?= $stu['fathername'] ?? '' ?></span></td></tr>
        <tr><td>Mother's Name</td><td>: <span class="line"><?= $stu['mothername'] ?? '' ?></span></td></tr>
    </table>

    <!-- ✅ MARKS TABLE -->
    <div class="page-title">ACADEMIC PERFORMANCE: SCHOLASTIC AREAS</div>

    <table class="term-head">
                    <thead>
                        <tr>
                            <th colspan="4" class="ms29-term-head">TERM - I (100 MARKS)</th>
                        </tr>
                        <tr>
                            <th colspan="2">Subject</th>
                            <th>Periodic -1</th>
                            <th>Periodic -2</th>
                        </tr>
                    </thead>

                    <tbody>

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

    <!-- ✅ Hindi -->
    <?php
    $hindi_oral_p3    = $m[43] ?? '';
    $hindi_oral_p4    = $m[44] ?? '';
    $hindi_written_p3 = $m[45] ?? '';
    $hindi_written_p4 = $m[46] ?? '';
    
    if ($hindi_oral_p3 !== '' || $hindi_oral_p4 !== '' || $hindi_written_p3 !== '' || $hindi_written_p4 !== ''):
    ?>
    <tr>
        <td rowspan="2">Hindi</td>
        <td>Oral</td>
        <td><?= is_numeric($hindi_oral_p3) ? gradeOutOf50($hindi_oral_p3) : '-' ?></td>
        <td><?= is_numeric($hindi_oral_p4) ? gradeOutOf50($hindi_oral_p4) : '-' ?></td>
    </tr>
    <tr>
        <td>Written</td>
        <td><?= is_numeric($hindi_written_p3) ? gradeOutOf50($hindi_written_p3) : '-' ?></td>
        <td><?= is_numeric($hindi_written_p4) ? gradeOutOf50($hindi_written_p4) : '-' ?></td>
    </tr>
    <?php endif; ?>

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

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

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

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

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

</tbody>

                </table>

    <!-- ✅ EXTRA TABLES ADDED -->
    <div class="sub-head">CO-SCHOLASTICS AREAS</div>

    <table class="health">
        <tr>
            <th colspan="4" class="left">HEALTH STATUS</th>
        </tr>
        <tr>
            <td class="left">Height</td>
            <td><?= $m[59] ?? '' ?></td>
            <td class="left">Weight</td>
            <td><?= $m[60] ?? '' ?></td>
        </tr>
        <tr>
            <td class="left">Blood Group</td>
            <td colspan="3"><?= $m[61] ?? '' ?></td>
        </tr>
    </table>

    <table class="attendance" style="margin-top:8px;">
        <tr>
            <th class="left">ATTENDENCE</th>
            <th>TERM I</th>
        </tr>
        <tr>
            <td class="left">No of Working Days</td>
            <td><?= $m[62] ?? '' ?></td>
        </tr>
        <tr>
            <td class="left">Days Attended</td>
            <td><?= $m[65] ?? '' ?></td>
        </tr>
        <tr>
            <td class="left">Attendance %</td>
            <td><?= $m[68] ?? '' ?></td>
        </tr>
    </table>

    <table style="margin-top:8px;">
        <tr>
            <th class="left">Competition</th>
        </tr>
        <tr>
            <td class="blank-box"><?= $m[71] ?? '' ?></td>
        </tr>
        <tr>
            <td class="blank-box"><?= $m[72] ?? '' ?></td>
        </tr>
    </table>

    <div style="margin-top:8px; font-size:12px;"><b>Class Teacher's Remarks:</b></div>
    <div class="remarks-line"><?= $m[73] ?? '' ?></div>

</div>

<?php } ?>

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