<?php
function template28($institutions, $branch, $students_array, $examination, $array, $analytics, $class_id, $section_id)
{
    $stream = $array[1] ?? "default";
?>
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Template 28 - Output</title>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
     <!--<link href="style_template28.css" type="text/css" rel="stylesheet">-->
<link href="/rjllearning_development/resources/marksheet_template/template28/custom_stylesheet/4/4_temp28.css" rel="stylesheet" type="text/css">
<!--<link href="custom_stylesheet/4/4_temp28.css" type="text/css" rel="stylesheet">-->
        <style>
        #success_div{color:green;margin-top:10px;}
        #error_div{color:red;margin-top:10px;}
    </style>
</head>
<body>
<div class="container_marksheet_template">
    <div class="content_container">
        <!-- Header -->
        <div class="fullstretch header_template">
            <div class="logo_div">
                <img src="<?php echo $institutions['logo']; ?>" alt="School Logo">
            </div>
            <div class="header_institution">
                <span class="heading_template"><?php echo $institutions['institution_name']; ?></span><br>
                <span class="heading_code_info"><?php echo $branch['branch_code']; ?></span><br>
                <span class="heading_address"><?php echo $branch['address']; ?><?php if($branch['pincode']!=""){echo " - ".$branch['pincode'];} ?></span><br>
                <span class="contact_info"><?php echo $branch['contact_number']; ?><?php if($branch['email']!=""){echo ", ".strtolower($branch['email']);} ?></span>
            </div>
        </div>

        <!-- Report Title -->
        <div class="fullstretch report_header text-center">
            <h2 class="heading_report">Progress Report</h2>
            <div class="academic_session">Academic Session <?php echo $analytics['session'] ?? ''; ?></div>
        </div>

        <!-- Student Info -->
        <div class="student_info_container">
            <div class="student_info_item"><label>Name :</label> <span class="student_data"><?php echo $students_array[0]['name']; ?></span></div>
            <div class="student_info_item"><label>Admission Number :</label> <span class="student_data"><?php echo $students_array[0]['admission_number']; ?></span></div>
            <div class="student_info_item"><label>Class :</label> <span class="student_data"><?php echo $class_id; ?></span></div>
            <div class="student_info_item"><label>Section :</label> <span class="student_data"><?php echo $section_id; ?></span></div>
            <div class="student_info_item"><label>Father's Name :</label> <span class="student_data"><?php echo $students_array[0]['fathername']; ?></span></div>
            <div class="student_info_item"><label>Mother's Name :</label> <span class="student_data"><?php echo $students_array[0]['mothername']; ?></span></div>
        </div>

        <!-- Stream Info -->
        <div class="stream_info">
            <label>Stream :</label> <span><?php echo ucfirst($stream); ?></span>
        </div>

        <!-- Subjects Table -->
        <table class="subjects-table">
            <thead>
                <tr>
                    <th>Subject</th>
                    <th>Type</th>
                    <th>Max Marks</th>
                    <th>Marks Obtained</th>
                </tr>
            </thead>
            <tbody>
                <?php
                $totalObtained = 0;
                $subjectsCount = 0;
                for ($i=2; $i<=32; $i+=3){
                    $subName = trim($array[$i] ?? '');
                    $subType = $array[$i+1] ?? '';
                    $subMarks = $array[$i+2] ?? '';

                    if($subName != ''){
                        $subjectsCount++;
                        $marksDisplay = ($subMarks === "0" || $subMarks === "00" || $subMarks === "") ? "-" : $subMarks;
                        if(is_numeric($subMarks)) $totalObtained += (int)$subMarks;
                        $typeText = ($subType=='M')?"Mandatory":(($subType=='O')?"Optional":"Additional");

                        echo "<tr>
                            <td>$subName</td>
                            <td>$typeText</td>
                            <td>20</td>
                            <td>$marksDisplay</td>
                        </tr>";
                    }
                }
                $maxTotal = $subjectsCount*20;
                ?>
                <tr class="total-row">
                    <td colspan="2"><strong>Total</strong></td>
                    <td><?php echo $maxTotal; ?></td>
                    <td><?php echo $totalObtained; ?></td>
                </tr>
                <tr class="total-row">
                    <td colspan="2"><strong>Percentage</strong></td>
                    <td colspan="2"><?php echo ($maxTotal>0)?round(($totalObtained/$maxTotal)*100,2)."%":"-"; ?></td>
                </tr>
                <?php if(!empty($analytics['grade'])){ ?>
                <tr class="total-row">
                    <td colspan="2"><strong>Grade</strong></td>
                    <td colspan="2"><?php echo $analytics['grade']; ?></td>
                </tr>
                <?php } ?>
            </tbody>
        </table>

        <!-- Attendance & Remarks -->
        <div class="summary-container">
            <div class="summary-item-inline"><label><strong>Attendance :</strong></label> <span><?php echo $array[34] ?? "-"; ?></span></div>
            <div class="summary-item-inline"><label><strong>Remarks :</strong></label> <span><?php echo $array[35] ?? "-"; ?></span></div>
        </div>

        <!-- Signatures -->
        <div class="signature_container">
            <div class="signature_div"><div class="signature-line"></div><span>Class Incharge</span></div>
            <div class="signature_div"><div class="signature-line"></div><span>Principal / Headmistress</span></div>
            <div class="signature_div"><div class="signature-line"></div><span>Parent</span></div>
        </div>

        <!-- Date -->
        <p class="date">Date: <?php echo date('d-m-Y'); ?></p>

        <!-- Disclaimer -->
        <div class="disclaimer">* In case of any discrepancies in marksheet, please contact school administration.</div>
    </div>
</div>
</body>
</html>
<?php
}
?>
