<?php
function template1($institutions, $branch, $students_array, $examination, $array, $academic_session, $class_name, $section_name)
{
    global $conn_rjllearning_specific;
?>

<!DOCTYPE html>
<html>
    
<!--<head>-->
<!--  <meta charset="utf-8">-->
<!--  <meta name="viewport" content="width=device-width, initial-scale=1.0">-->
<!--  <title>Template 1</title>-->
<!--  <link href="<?php echo 'https://rejoicebilldesk.in/rjllearning_development/resources/marksheet_template/template1/custom_stylesheets/4/4_temp1.css?key=' . time(); ?>" type="text/css" rel="stylesheet">-->
<!--</head>-->

<body>
    <div class="container_marksheet_template1">
    <div class="content_container_template1">
        <div class="header_template1 flexbox-row-center-start">
          <div class="logo_div_img_template1">
            <img src="<?php echo $institutions['logo']; ?>">
          </div>
          <div class="header_institution">
            <h1><?php echo htmlspecialchars($institutions['institution_name']); ?></h1>
            <div class="header_details">
                <p><?php echo htmlspecialchars($branch['affiliation_code'] !== null ? $branch['affiliation_code'] : ''); ?></p>
                <p>
                    <?php echo htmlspecialchars($branch['address']);
                    if ($branch['pincode'] != "" || $branch['city'] != "" || $branch['state_name'] != "" || $branch['country_name'] != "") {
                        echo ", " . $branch['city'] . ", " . $branch['state_name'] . ", " . $branch['country_name'] . " - " . $branch['pincode'];
                    }
                    ?>
                </p>
                <p><?php echo htmlspecialchars($branch['contact_number']);
                  if ($branch['email'] != "") {
                    echo ", &nbsp;" . strtolower(htmlspecialchars($branch['email']));
                  }
                ?></p>
            </div>
          </div>
        </div>

        <hr >
        <div class="heading_report_template1">Progress Report</div>
        <div class="aligncenter">Academic Session <?php echo htmlspecialchars($academic_session); ?></div>

        <!-- Student Info -->
        <table class="student_info_table_template1 transparent_table">
            <tr>
                <td><strong>Name :</strong></td>
                <td><?php echo htmlspecialchars($students_array[0]['name']); ?></td>
                <td><strong>Admission Number :</strong></td>
                <td><?php echo htmlspecialchars($students_array[0]['admission_number']); ?></td>
            </tr>
            <tr>
                <td><strong>Class :</strong></td>
                <td><?php echo htmlspecialchars($class_name); ?></td>
                <td><strong>Section :</strong></td>
                <td><?php echo htmlspecialchars($section_name); ?></td>
            </tr>
            <tr>
                <td><strong>Father's Name:</strong></td>
                <td><?php echo htmlspecialchars($students_array[0]['fathername']); ?></td>
                <td><strong>Mother's Name:</strong></td>
                <td><?php echo htmlspecialchars($students_array[0]['mothername']); ?></td>
            </tr>
        </table>

        <!-- Marks Table -->
        <table class="table1_template1 fullstretch">
            <thead>
                <tr>
                    <th>SUBJECT</th>
                    <th>TH (40)</th>
                    <th>M.O.</th>
                    <th>PR (40)</th>
                    <th>M.O.</th>
                    <th>TOTAL</th>
                    <th>GRADE</th>
            
                </tr>
            </thead>
            <tbody>
            <?php
            $base = 1;
            for($i=1;$i<=7;$i++) {
                $th_val = isset($array[$base+7]) && $array[$base+7] !== '' ? (int)$array[$base+7] : 0;
                $pr_val = isset($array[$base+14]) && $array[$base+14] !== '' ? (int)$array[$base+14] : 0;

                $total = $th_val + $pr_val;

                // Simple grading logic
                if ($total >= 70) {
                    $grade = "A";
                } elseif ($total >= 60) {
                    $grade = "B";
                } elseif ($total >= 50) {
                    $grade = "C";
                } elseif ($total >= 40) {
                    $grade = "D";
                } else {
                    $grade = "F";
                }
                ?>
                <tr>
                    <th><?php echo htmlspecialchars($array[$base] ?? ''); ?></th>
                    <td>40</td>
                    <td><?php echo htmlspecialchars($th_val); ?></td>
                    <td>40</td>
                    <td><?php echo htmlspecialchars($pr_val); ?></td>
                    <td><?php echo $total; ?></td>
                    <td><?php echo $grade; ?></td>
                </tr>
            <?php $base++; } ?>
            </tbody>
        </table>


        <!-- Attendance & Remarks -->
        <div class="margintop35 flex-row-info">
            <p><strong>Attendance:</strong> <?php echo htmlspecialchars($array[23]??'-'); ?></p>
            <p><strong>Remarks:</strong> <?php echo htmlspecialchars($array[24]??'-'); ?></p>
        </div>

        <!-- Congratulation & New Session (in one styled div) -->
        <div class="promotion-container margintop20">
            <?php
            $promotionText = '';
            if (!empty($array[25]) && $array[25] != 'na') {
                $className = '';
                $stmt = $conn_rjllearning_specific->prepare(
                    "SELECT classname FROM class_info WHERE id = :id LIMIT 1"
                );
                $stmt->bindParam(':id', $array[25], PDO::PARAM_INT);
                $stmt->execute();
                $classRow = $stmt->fetch(PDO::FETCH_ASSOC);

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

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

                if(!empty($className)){
                    $promotionText = "Congratulations! "
                        . $salutation . " "
                        . ucwords(strtolower($students_array[0]['name']))
                        . " has been promoted to Class "
                        . htmlspecialchars($className);
                } else {
                    $promotionText = "Congratulations! "
                        . $salutation . " "
                        . ucwords(strtolower($students_array[0]['name']))
                        . " has been promoted to next class.";
                }
            }

            $sessionText = '';
            if (!empty($array[26])) {
                $formattedDate = date("d-m-Y", strtotime($array[26]));
                $sessionText = "New session begins from : <span style='color:#000; font-weight:bold;'>" . htmlspecialchars($formattedDate) . "</span>";
            }

            if ($promotionText || $sessionText) {
                echo "<p class='congratulate_message' style='margin:5px 0; font-weight:bold; color:#000;'>{$promotionText}</p>";
                echo "<p class='new_session' style='margin:5px 20px; color:#000;'>{$sessionText}</p>";
            }
            ?>
        </div>
    
        <!-- Signatures -->
        <div class="flexbox-row-spacearound-start margintop50">
            <div class="signature_div">Class Incharge</div>
            <div class="signature_div">Principal / Headmistress</div>
            <div class="signature_div">Parent</div>
        </div>

        <!-- Disclaimer -->
        <p class="margintop20 fontsize11">
            * In case of any discrepancies in marksheet, please contact school administration.
        </p>

    </div>
</div>
</body>

</html>

<?php
}
?>