<?php
function template22($institutions, $branch, $students_array, $examination, $array, $result, $class_id, $section_id = null)
{
    $marksData = [];
    foreach ($array as $index => $value) {
        $marksData[$index + 1] = $value;
    }
    global  $conn_rjllearning_specific;
    // Ensure all required indexes exist
    for ($i = 1; $i <= 30; $i++) {
        if (!isset($marksData[$i])) {
            $marksData[$i] = '';
        }
    }
?>

    <!DOCTYPE html>
    <html>

    <!--  <head>-->
    <!--    <meta charset="utf-8" />-->
    <!--    <meta name="viewport" content="width=device-width, initial-scale=1.0" />-->
    <!--    <title>Template 22</title>-->
    <!--    <link href="style_template22.css" type="text/css" rel="stylesheet" />-->
    <!--<link href="" type="text/css" rel="stylesheet">-->
    <!--</head>-->
    </head>

    <body>

        <div class="container_marksheet_template1">
            <div class="border_template1">
                <div class="content_container_template1">

                    <!-- Header -->
                    <div class="header_template1">
                        <!--<div class="logo_div_img_template1">-->
                        <!--<img src="<?php echo $institutions['logo']; ?>" alt="School Logo">-->
                        <!--    <img src="../../assets/gvis_logo/gvis_logo.svg" alt="School Logo">-->
                        <!--</div>-->
                        <div class="logo_div_img_template1">
                            <img src="<?php echo $institutions['logo']; ?>" alt="School Logo">
                        </div>
                        <div class="header_institution">
                            <span class="heading_template1"><?php echo $institutions['institution_name']; ?></span><br>
                            <span class="heading_code_info_template1"><?php echo htmlspecialchars($branch['affiliation_code'] !== null ? $branch['affiliation_code'] : ''); ?></span><br>
                            <span class="heading_address_template1"><?php echo $branch['address'];
                                                                    if ($branch['pincode'] != "" || $branch['city'] != "" || $branch['state_name'] != "" || $branch['country_name'] != "") {
                                                                        echo ", " . $branch['city'] . ", " . $branch['state_name'] . ", " . $branch['country_name'] . " - " . $branch['pincode'];
                                                                    }
                                                                    ?></span><br>
                            <span class="contact_template1"><?php echo $branch['contact_number'];
                                                            if ($branch['email'] != "") {
                                                                echo ", " . strtolower($branch['email']);
                                                            }
                                                            ?></span>
                        </div>
                    </div>

                    <!-- Report Title -->
                    <div class="header_info_container">
                        <div class="heading_report_template1">Holistic Progress Card</div>
                        <div class="year_template1">Academic Session <?php echo $result; ?></div>
                    </div>

                    <!-- Student Info -->
                    <div class="student-info-container">
                        <div class="student_info_template1">
                            <table class="student_info_table_template1">
                                <tr>
                                    <td>Name</td>
                                    <td><?php echo $students_array[0]['name']; ?></td>
                                </tr>
                                <tr>
                                    <td>Admission Number</td>
                                    <td><?php echo $students_array[0]['admission_number']; ?></td>
                                </tr>
                                <tr>
                                    <td>Father's Name</td>
                                    <td><?php echo $students_array[0]['fathername']; ?></td>
                                </tr>
                                <tr>
                                    <td>Mother's Name</td>
                                    <td><?php echo $students_array[0]['mothername']; ?></td>
                                </tr>
                            </table>
                        </div>
                        <div class="student_info_template1">
                            <table class="student_info_table_template1">
                                <tr>
                                    <td>Class - Section</td>
                                    <td>
                                        <?php echo $class_id; ?> - <?php echo $section_id; ?>
                                    </td>
                                </tr>
                                <!-- <tr>
                                <td>Section</td>
                                <td><?php echo $section_id; ?></td>
                            </tr> -->
                                <tr>
                                    <td>D.O.B.</td>
                                    <td><?php echo !empty($students_array[0]['dob']) ? date("d-m-Y", strtotime($students_array[0]['dob'])) : ""; ?></td>
                                </tr>
                                <tr>
                                    <td>Height</td>
                                    <td><?php echo ($marksData[1]); ?></td>
                                </tr>
                                <tr>
                                    <td>Weight</td>
                                    <td><?php echo ($marksData[2]); ?></td>
                                </tr>
                            </table>
                        </div>
                    </div>

                    <!-- Assessment Tables -->
                    <div class="assessment-container">
                        <div>
                            <table class="assessment-table">
                                <tr>
                                    <th colspan="2">ASSESSMENTS</th>
                                </tr>

                                <tr class="subject-header">
                                    <td colspan="2">ENGLISH</td>
                                </tr>
                                <tr>
                                    <td>Can write neatly</td>
                                    <td><?php echo ($marksData[3]); ?></td>
                                </tr>
                                <tr>
                                    <td>Can narrate stories effectively</td>
                                    <td><?php echo ($marksData[4]); ?></td>
                                </tr>
                                <tr>
                                    <td>Can recite poems with expressions</td>
                                    <td><?php echo ($marksData[5]); ?></td>
                                </tr>
                                <tr>
                                    <td>Can recognize and read properly</td>
                                    <td><?php echo ($marksData[6]); ?></td>
                                </tr>


                                <tr class="subject-header">
                                    <td colspan="2">MATHEMATICS</td>
                                </tr>
                                <tr>
                                    <td>Can identify the numbers correctly</td>
                                    <td><?php echo ($marksData[7]); ?></td>
                                </tr>
                                <tr>
                                    <td>Can grasp the combination of numbers</td>
                                    <td><?php echo ($marksData[8]); ?></td>
                                </tr>
                                <tr>
                                    <td>Can count and write in sequence</td>
                                    <td><?php echo ($marksData[9]); ?></td>
                                </tr>

                                <tr class="subject-header">
                                    <td colspan="2">PUNJABI</td>
                                </tr>
                                <tr>
                                    <td>Can express the word</td>
                                    <td><?php echo ($marksData[10]); ?></td>
                                </tr>
                                <tr>
                                    <td>Can write properly</td>
                                    <td><?php echo ($marksData[11]); ?></td>
                                </tr>

                                <tr class="subject-header">
                                    <td colspan="2">E.V.S. / GENERAL KNOWLEDGE</td>
                                </tr>
                                <tr>
                                    <td>Can identify</td>
                                    <td><?php echo ($marksData[12]); ?></td>
                                </tr>
                                <tr>
                                    <td>Can write properly</td>
                                    <td><?php echo ($marksData[13]); ?></td>
                                </tr>
                                <tr>
                                    <td>Can express / understand the word / pictures</td>
                                    <td><?php echo ($marksData[14]); ?></td>
                                </tr>


                                <?php if (!empty($marksData[15]) || !empty($marksData[16])) { ?>
                                    <tr class="subject-header">
                                        <td colspan="2">ART & CRAFT</td>
                                    </tr>
                                    <tr>
                                        <td>Enjoys drawing, colouring & pasting</td>
                                        <td><?php echo ($marksData[15]); ?></td>
                                    </tr>
                                    <tr>
                                        <td>Follows instructions carefully</td>
                                        <td><?php echo ($marksData[16]); ?></td>
                                    </tr>
                                <?php } ?>

                                <!--<?php if (!empty($marksData[41])) { ?>-->
                                <!--<tr class="subject-header">-->
                                <!--    <td colspan="2">Rhymes</td>-->
                                <!--</tr>-->
                                <!--<tr>-->
                                <!--    <td>Grade</td>-->
                                <!--    <td><?php echo ($marksData[41]); ?></td>-->
                                <!--</tr>-->
                                <!--<?php } ?>-->

                                <!--<?php if (!empty($marksData[43])) { ?>-->
                                <!--<tr class="subject-header">-->
                                <!--    <td colspan="2">Story</td>-->
                                <!--</tr>-->
                                <!--<tr>-->
                                <!--    <td>Grade</td>-->
                                <!--    <td><?php echo ($marksData[43]); ?></td>-->
                                <!--</tr>-->
                                <!--<?php } ?>-->

                                <!--<?php if (!empty($marksData[45])) { ?>-->
                                <!--<tr class="subject-header">-->
                                <!--    <td colspan="2">Conversation</td>-->
                                <!--</tr>-->
                                <!--<tr>-->
                                <!--    <td>Grade</td>-->
                                <!--    <td><?php echo ($marksData[45]); ?></td>-->
                                <!--</tr>-->
                                <!--<?php } ?>-->

                                <tr class="subject-header">
                                    <td colspan="1">ATTENDANCE: </td>
                                    <td><?php echo ($marksData[17]); ?></td>
                                </tr>
                                <tr class="subject-header">
                                    <td colspan="2">GENERAL REMARKS: <br>
                                        <span style="color: #000000;">
                                            <?php echo ($marksData[30]); ?>
                                        </span>    
                                    </td>
                                </tr>
                            </table>
                        </div>

                        <div>
                            <table class="assessment-table">
                                <tr>
                                    <th colspan="2">COMPETENCIES</th>
                                </tr>
                                <tr class="subject-header">
                                    <td colspan="2">PHYSICAL DEVELOPMENT</td>
                                </tr>

                                <tr class="section-header">
                                    <td colspan="2">PART-I (HEALTHY & SAFE HABITS)</td>
                                </tr>
                                <tr>
                                    <td>Shows a liking for and understanding of nutritious food and does not waste food</td>
                                    <td><?php echo ($marksData[18]); ?></td>
                                </tr>
                                <tr>
                                    <td>Practices basic self- care and hygiene</td>
                                    <td><?php echo ($marksData[19]); ?></td>
                                </tr>

                                <tr class="section-header">
                                    <td colspan="2">PART-II (SENSORIAL PERCEPTIONS)</td>
                                </tr>
                                <tr>
                                    <td>Differentiates between shapes, colours and their shades</td>
                                    <td><?php echo ($marksData[20]); ?></td>
                                </tr>
                                <tr>
                                    <td>Differentiates multiple smells and tastes</td>
                                    <td><?php echo ($marksData[21]); ?></td>
                                </tr>
                                <tr>
                                    <td>Develops discriminations in the sense of touch</td>
                                    <td><?php echo ($marksData[22]); ?></td>
                                </tr>

                                <tr class="section-header">
                                    <td colspan="2">PART-III (PHYSICAL FITNESS)</td>
                                </tr>
                                <tr>
                                    <td>Shows balance, coordination and flexibility in various physical activities</td>
                                    <td><?php echo ($marksData[23]); ?></td>
                                </tr>
                                <tr>
                                    <td>Shows precision and control in working with their hands and fingers</td>
                                    <td><?php echo ($marksData[24]); ?></td>
                                </tr>
                                <tr>
                                    <td>Shows strength and endurance in carrying, walking and running</td>
                                    <td><?php echo ($marksData[25]); ?></td>
                                </tr>

                                <tr class="subject-header">
                                    <td colspan="2">SOCIO-EMOTIONAL & ETHICAL DEVELOPMENT</td>
                                </tr>

                                <tr class="section-header">
                                    <td colspan="2">PART-I (EMOTIONAL INTELLIGENCE)</td>
                                </tr>
                                <tr>
                                    <td>Recognises 'self' as an individual belong to a family and community</td>
                                    <td><?php echo ($marksData[26]); ?></td>
                                </tr>
                                <tr>
                                    <td>Recognises different emotions and makes deliberate efforts to regulate them</td>
                                    <td><?php echo ($marksData[27]); ?></td>
                                </tr>
                                <tr>
                                    <td>Interacts comfortably with other children and adults</td>
                                    <td><?php echo ($marksData[28]); ?></td>
                                </tr>
                                <tr>
                                    <td>Shows kindness and helpfulness to others</td>
                                    <td><?php echo ($marksData[29]); ?></td>
                                </tr>
                            </table>
                        </div>
                    </div>

                    <!-- Promotion Message -->
                    <?php
                    if ((!empty($marksData[31]) && strtolower($marksData[31]) !== 'na') || !empty($marksData[32])) {
                    ?>
                        <div class="promotion-container">
                            <div class="congratulate_message">
                                <?php
                                if (!empty($marksData[31])) {
                                    // Fetch class_name from DB using the class ID
                                    $className = '';
                                    $stmt = $conn_rjllearning_specific->prepare(
                                        "SELECT classname FROM class_info WHERE id = :id LIMIT 1"
                                    );
                                    $stmt->bindParam(':id', $marksData[31], PDO::PARAM_INT);
                                    $stmt->execute();
                                    $classRow = $stmt->fetch(PDO::FETCH_ASSOC);

                                    if ($classRow && !empty($classRow['classname'])) {
                                        $className = $classRow['classname'];
                                    }

                                    // Map gender to salutation
                                    $gender = strtoupper(trim($students_array[0]['gender'] ?? ''));
                                    $salutation = ($gender === 'M') ? 'Mr.' : (($gender === 'F') ? 'Miss.' : '');

                                    // Display student promotion message with class name
                                    if (!empty($className)) {
                                        echo "Congratulations! "
                                            . $salutation . " "
                                            . ucwords(strtolower($students_array[0]['name']))
                                            . " has been promoted to Class "
                                            . htmlspecialchars($className);
                                    }
                                }
                                ?>
                            </div>

                            <div class="new_session">
                                <?php
                                if (!empty($marksData[32])) {
                                    // Format the date (d-m-Y)
                                    $formattedDate = date("d-m-Y", strtotime($marksData[32]));
                                    echo "New session begins from : " . htmlspecialchars($formattedDate);
                                }
                                ?>
                            </div>
                        </div>
                    <?php
                    }
                    ?>


                    <!-- Signatures -->
                    <div class="signature-container">
                        <div class="signature-box">
                            <div class="signature-line"></div>
                            <div class="signature-title">Class Incharge</div>
                        </div>
                        <div class="signature-box">
                            <div class="signature-line"></div>
                            <div class="signature-title">Principal / Headmistress</div>
                        </div>
                        <div class="signature-box">
                            <div class="signature-line"></div>
                            <div class="signature-title">Parent</div>
                        </div>
                    </div>

                    <!-- Footer Note -->
                    <div class="footer-note">
                        * Incase of any discrepancies in marksheet, please contact school administration.
                    </div>
                </div>
            </div>
        </div>

    </body>

    </html>

<?php
}
?>