<?php
ini_set('display_errors', 1);

// Include necessary files
require_once '../../base_path_include.php';
require_once $base_path_to_learning_resources . "includes/connect.php";
require_once $base_path_to_learning_resources . 'lib/function.php';
require_once $base_path_to_learning_resources . 'lib/validate_api_signature.php';
require_once $base_path_to_learning_resources . 'lib/validate_function.php';
require_once $base_path_to_learning . 'user_management/authenticate.php';
require_once $base_path_to_learning . 'admin_api/academic_session/functions/validate_faculty.php';
require_once $base_path_to_learning . 'admin_api/academic_session/functions/functions.php';
require_once $base_path_to_learning . 'functions/common_functions.php';
require_once $base_path_to_learning_resources . 'includes/specific_db_config.php';
require_once $base_path_to_learning . "vendor/autoload.php";
require_once $base_path_to_learning_resources . 'lib/bucket_functions/bucket_functions.php';
require_once $base_path_to_learning_resources . 'includes/aws_bucket_config.php';
require_once $base_path_to_learning_resources . 'marksheet_template/template29/function.php';
// Get input data
$postdata = $_GET;

// Validate required fields
if (
    !isset($postdata['institution_id']) || !checkIfIssetAndNotEmpty($postdata['institution_id']) ||
    !isset($postdata['student_id']) || !checkIfIssetAndNotEmpty($postdata['student_id']) ||
    !isset($postdata['marksheet_id']) || !checkIfIssetAndNotEmpty($postdata['marksheet_id']) ||
    !isset($postdata['api_signature']) || !checkIfIssetAndNotEmpty($postdata['api_signature']) ||
    !isset($postdata['jwt_token']) || !checkIfIssetAndNotEmpty($postdata['jwt_token'])
) {
    errorreturn("All required fields are missing.");
}

validateApiSignature($postdata['api_signature']);

// Authenticate JWT token
$jwttoken = $postdata['jwt_token'];
$jwt_validate = authenticate_jwt($jwttoken);

// Extract user details
$userid = $jwt_validate->data->id;
$workspace_id = $jwt_validate->data->workspace_id;
$username = $jwt_validate->data->username;
$role = $jwt_validate->data->role;

// Validate user access
// Validate user access and get specific database name
$specific_db_name = validateUserAccess($conn_rjllearning, $userid, $role, $workspace_id, ["superadmin", "admin", "principal", "teacher"], null);
if (!$specific_db_name) {
    errorreturn('Access denied.');
} else {
    $conn_rjllearning_specific = dynamicDbConnection($specific_db_name);
}
$api_signature = $postdata['api_signature'];
$institution_id = $postdata['institution_id'];
$student_id = $postdata['student_id'];
$marksheet_id = $postdata['marksheet_id'];


$ExecuteSqlTemplate = $conn_rjllearning_specific->prepare("SELECT * FROM students WHERE institution_id = ? and id=?");
$ExecuteSqlTemplate->execute([$institution_id, $student_id]);
$Resultstudents = $ExecuteSqlTemplate->fetch(PDO::FETCH_ASSOC);

if (!$Resultstudents) {
    errorreturn("student does not exist.");
}

$branch_id = $Resultstudents['branch_id'];

$class_id = $Resultstudents['class_id'];

$section_id = $Resultstudents['section_id'];

$class_info = getClass($conn_rjllearning_specific, $institution_id, $class_id);
// Prepare the SQL statement

$section_info = getSection($conn_rjllearning_specific, $institution_id, $section_id);
$SqlTemplate = "SELECT * FROM academic_session WHERE institution_id = ? AND active_academic_session = ?";
$StmtTemplate = $conn_rjllearning_specific->prepare($SqlTemplate);
$StmtTemplate->execute([$institution_id, 1]);
$result = $StmtTemplate->fetchAll(PDO::FETCH_ASSOC); // Make sure to fetch associative array

if (count($result) < 1) {
    errorreturn("Academic session not found. Contact admin.");
}

// Fetch institution information
$institutions = getInstitution($conn_rjllearning, $institution_id);

// Define additional data fields
$additional_data = ['branch_code', 'address', 'pincode', 'contact_number', 'email'];

// Fetch branch information
$branch = getBranchInfo($conn_rjllearning_specific, $institution_id, $branch_id, $additional_data);

// Marksheet
$ExecuteSqlTemplate = $conn_rjllearning_specific->prepare("SELECT examination_name,template_id,academic_session, custom_stylesheet_id FROM marksheet WHERE institution_id = ? and id=?");
$ExecuteSqlTemplate->execute([$institution_id, $marksheet_id]);
$ResultTemplates = $ExecuteSqlTemplate->fetch(PDO::FETCH_ASSOC);

if (!$ResultTemplates) {
    errorreturn("examination_name does not exist.");
}

$examination = $ResultTemplates['examination_name'];
$template_id = $ResultTemplates['template_id'];
$academic_session = $ResultTemplates['academic_session'];


$ExecuteSqlTemplate = $conn_rjllearning->prepare("SELECT cols, template_name FROM template_info WHERE id = ? ");
$ExecuteSqlTemplate->execute([$template_id]);
$ResultTemplate = $ExecuteSqlTemplate->fetch(PDO::FETCH_ASSOC);

if (!$ResultTemplate) {
    errorreturn("Marksheet Template does not exist.");
}
$cols = $ResultTemplate['cols'];
$template_name = $ResultTemplate['template_name'];

// 🔹 Academic session dates (already fetched)
$session_start = $result[0]['startdate'];
$session_end   = $result[0]['enddate'];

// 🔹 Fetch full day-wise attendance ONCE
$stmtAtt = $conn_rjllearning_specific->prepare("
    SELECT date, value
    FROM student_attendance_daywise
    WHERE institution_id = ?
      AND student_id = ?
      AND date BETWEEN ? AND ?
");
$stmtAtt->execute([
    $institution_id,
    $student_id,
    $session_start,
    $session_end
]);

$attendance_rows = $stmtAtt->fetchAll(PDO::FETCH_ASSOC);

// Get marksheet data
$stmt = $conn_rjllearning_specific->prepare("SELECT data FROM student_marksheet_data WHERE student_id = ? AND marksheet_id = ? AND institution_id = ?");
$stmt->execute([$student_id, $marksheet_id, $institution_id]);
$fetchmarksheet_data = $stmt->fetch(PDO::FETCH_ASSOC);

$array = [];


if ($fetchmarksheet_data && !empty($fetchmarksheet_data['data'])) {

    $decoded = json_decode($fetchmarksheet_data['data'], true);

    if (is_array($decoded)) {

        // ðŸ”‘ FIX: Reindex 1-based â†’ 0-based
        $decoded = array_values($decoded);

        if (count($decoded) === $cols) {

            // PERFECT MATCH â†’ USE DIRECTLY
            for ($i = 0; $i < $cols; $i++) {
                $array[$i] = $decoded[$i];
            }
        } else {

            // MISMATCH â†’ RESET SAFELY
            for ($i = 0; $i < $cols; $i++) {
                $array[$i] = null;
            }
        }
    } else {

        // INVALID JSON
        for ($i = 0; $i < $cols; $i++) {
            $array[$i] = null;
        }
    }
} else {

    // NO DATA â†’ FRESH ARRAY
    for ($i = 0; $i < $cols; $i++) {
        $array[$i] = null;
    }
}




?>

<script>
    const ACADEMIC_SESSION = {
        start: "<?= $session_start ?>",
        end: "<?= $session_end ?>"
    };

    const STUDENT_ATTENDANCE = <?= json_encode($attendance_rows, JSON_HEX_TAG) ?>;
</script>

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MARKS UPLOAD</title>

    <script src="https://code.jquery.com/jquery-3.7.1.min.js"
        integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo="
        crossorigin="anonymous"></script>

    <link href="<?php echo 'https://rejoicebilldesk.in/rjllearning_development/resources/marksheet_template/template29/style_template29.css?key=' . time(); ?>"
        type="text/css" rel="stylesheet">
</head>

<body>

    <div class="ms29-container">
        <div class="ms29-border">
            <div class="ms29-content">

                <!-- HEADER -->
                <div class="ms29-header-wrapper" align="center">

                    <div class="ms29-logo-box">
                        <img class="ms29-logo-img" src="<?php echo $institutions['logo']; ?>">
                    </div>

                    <div class="ms29-institution-info">
                        <span class="ms29-institution-name">
                            <?php echo $institutions['institution_name']; ?>
                        </span><br>

                        <span class="ms29-branch-code">
                            <?php echo $branch['branch_code']; ?>
                        </span>
                        <?php if ($branch['branch_code'] != ""): ?><br><?php endif; ?>

                        <span class="ms29-branch-address">
                            <?php
                            echo $branch['address'];
                            if ($branch['pincode'] != "") echo " - " . $branch['pincode'];
                            ?>
                        </span><br>

                        <span class="ms29-branch-contact">
                            <?php
                            echo $branch['contact_number'];
                            if ($branch['email'] != "") echo ", " . strtolower($branch['email']);
                            ?>
                        </span>
                    </div>

                </div>

                <!-- REPORT TITLE -->
                <div class="ms29-report-header" align="center">
                    <div class="ms29-report-title">Progress Report</div>
                    <div class="ms29-academic-year">
                        Academic Session <?php echo $academic_session; ?>
                    </div>
                </div>

                <!-- STUDENT INFO -->
                <div class="ms29-student-wrapper" align="center">

                    <div class="ms29-student-box">
                        <table class="ms29-student-table">
                            <tr>
                                <td>Name</td>
                                <td><?php echo $Resultstudents['name']; ?></td>
                            </tr>
                            <tr>
                                <td>Class</td>
                                <td><?php echo $class_info['class_name']; ?></td>
                            </tr>
                            <tr>
                                <td>Father's Name</td>
                                <td><?php echo $Resultstudents['fathername']; ?></td>
                            </tr>
                        </table>
                    </div>

                    <div class="ms29-student-box">
                        <table class="ms29-student-table">
                            <tr>
                                <td>Admission Number</td>
                                <td><?php echo $Resultstudents['admission_number']; ?></td>
                            </tr>
                            <tr>
                                <td>Section</td>
                                <td><?php echo $section_info['section_name']; ?></td>
                            </tr>
                            <tr>
                                <td>Mother's Name</td>
                                <td><?php echo $Resultstudents['mothername']; ?></td>
                            </tr>
                        </table>
                    </div>

                </div>



                <!-- Marks Upload Form -->
                <form id="upload_marksheet_form" method="post">
                    <input type="hidden" name="marksheet_id" value="<?php echo $marksheet_id; ?>">
                    <input type="hidden" name="institution_id" value="<?php echo $institution_id; ?>">
                    <input type="hidden" name="student_id" value="<?php echo $student_id; ?>">
                    <input type="hidden" name="jwt_token" value="<?php echo $postdata['jwt_token']; ?>">
                    <input type="hidden" name="api_signature" value="<?php echo $postdata['api_signature']; ?>">


                    <div class="ms29-term-wrapper accordion">

                        <!-- ================= TERM II ================= -->
                        <div class="accordion-item">
                            <button type="button" class="accordion-header">
                                📕 TERM – II (Final)
                            </button>

                            <div class="accordion-body">

                                <div class="ms29-right-panel term-2">
                                    <div class="ms29-panel-header"></div>

                                    <div class="ms29-section-title">
                                        ACADEMIC PERFORMANCE: SCHOLASTIC AREAS
                                    </div>

                                    <div class="ms29-panel-content">

                                        <!-- ================= TERM II MARKS ================= -->


                                        <table class="ms29-marks-table">
                                            <thead>
                                                <tr>
                                                    <th colspan="7" class="ms29-term-head">TERM - II (100 MARKS)</th>
                                                </tr>
                                                <tr>
                                                    <th>Subject</th>
                                                    <th style="text-align:center">PT-3<br>40</th>
                                                    <th style="text-align:center">Ass.<br>5</th>
                                                    <th style="text-align:center">N.B.<br>5</th>
                                                    <th style="text-align:center">PT-4<br>40</th>
                                                    <th style="text-align:center">Ass.<br>5</th>
                                                    <th style="text-align:center">N.B.<br>5</th>
                                                </tr>
                                            </thead>

                                            <tbody>

                                                <!-- ================= ROW 1 : 0–9 ================= -->
                                                <tr>
                                                    <td class="ms29-subject-name">English</td>
                                                    <td><input type="number" min="0" step="any" name="marks[0]" max="40" value="<?= $array[0] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[1]" max="5" value="<?= $array[1] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[2]" max="5" value="<?= $array[2] ?? '' ?>"></td>
                                                    <td style="display:none"><input type="hidden" name="marks[3]" value="<?= $array[3] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[4]" max="40" value="<?= $array[4] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[5]" max="5" value="<?= $array[5] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[6]" max="5" value="<?= $array[6] ?? '' ?>"></td>
                                                    <td style="display:none"><input type="hidden" name="marks[7]" value="<?= $array[7] ?? '' ?>"></td>
                                                    <td style="display:none"><input type="hidden" name="marks[8]" value="<?= $array[8] ?? '' ?>"></td>
                                                    <td style="display:none"><input type="hidden" name="marks[9]" value="<?= $array[9] ?? '' ?>"></td>
                                                </tr>

                                                <!-- ================= ROW 2 : 10–19 ================= -->
                                                <tr>
                                                    <td class="ms29-subject-name">Hindi</td>
                                                    <td><input type="number" min="0" step="any" name="marks[10]" max="40" value="<?= $array[10] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[11]" max="5" value="<?= $array[11] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[12]" max="5" value="<?= $array[12] ?? '' ?>"></td>
                                                    <td style="display:none"><input type="hidden" name="marks[13]" value="<?= $array[13] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[14]" max="40" value="<?= $array[14] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[15]" max="5" value="<?= $array[15] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[16]" max="5" value="<?= $array[16] ?? '' ?>"></td>
                                                    <td style="display:none"><input type="hidden" name="marks[17]" value="<?= $array[17] ?? '' ?>"></td>
                                                    <td style="display:none"><input type="hidden" name="marks[18]" value="<?= $array[18] ?? '' ?>"></td>
                                                    <td style="display:none"><input type="hidden" name="marks[19]" value="<?= $array[19] ?? '' ?>"></td>
                                                </tr>

                                                <!-- ================= ROW 3 : 20–29 ================= -->
                                                <tr>
                                                    <td class="ms29-subject-name">Punjabi</td>
                                                    <td><input type="number" min="0" step="any" name="marks[20]" max="40" value="<?= $array[20] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[21]" max="5" value="<?= $array[21] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[22]" max="5" value="<?= $array[22] ?? '' ?>"></td>
                                                    <td style="display:none"><input type="hidden" name="marks[23]" value="<?= $array[23] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[24]" max="40" value="<?= $array[24] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[25]" max="5" value="<?= $array[25] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[26]" max="5" value="<?= $array[26] ?? '' ?>"></td>
                                                    <td style="display:none"><input type="hidden" name="marks[27]" value="<?= $array[27] ?? '' ?>"></td>
                                                    <td style="display:none"><input type="hidden" name="marks[28]" value="<?= $array[28] ?? '' ?>"></td>
                                                    <td style="display:none"><input type="hidden" name="marks[29]" value="<?= $array[29] ?? '' ?>"></td>
                                                </tr>

                                                <!-- ================= ROW 4 : 30–39 ================= -->
                                                <tr>
                                                    <td class="ms29-subject-name">Maths</td>
                                                    <td><input type="number" min="0" step="any" name="marks[30]" max="40" value="<?= $array[30] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[31]" max="5" value="<?= $array[31] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[32]" max="5" value="<?= $array[32] ?? '' ?>"></td>
                                                    <td style="display:none"><input type="hidden" name="marks[33]" value="<?= $array[33] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[34]" max="40" value="<?= $array[34] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[35]" max="5" value="<?= $array[35] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[36]" max="5" value="<?= $array[36] ?? '' ?>"></td>
                                                    <td style="display:none"><input type="hidden" name="marks[37]" value="<?= $array[37] ?? '' ?>"></td>
                                                    <td style="display:none"><input type="hidden" name="marks[38]" value="<?= $array[38] ?? '' ?>"></td>
                                                    <td style="display:none"><input type="hidden" name="marks[39]" value="<?= $array[39] ?? '' ?>"></td>
                                                </tr>

                                                <!-- ================= ROW 5 : 40–49 ================= -->
                                                <tr>
                                                    <td class="ms29-subject-name">EVS</td>
                                                    <td><input type="number" min="0" step="any" name="marks[40]" max="40" value="<?= $array[40] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[41]" max="5" value="<?= $array[41] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[42]" max="5" value="<?= $array[42] ?? '' ?>"></td>
                                                    <td style="display:none"><input type="hidden" name="marks[43]" value="<?= $array[43] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[44]" max="40" value="<?= $array[44] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[45]" max="5" value="<?= $array[45] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[46]" max="5" value="<?= $array[46] ?? '' ?>"></td>
                                                    <td style="display:none"><input type="hidden" name="marks[47]" value="<?= $array[47] ?? '' ?>"></td>
                                                    <td style="display:none"><input type="hidden" name="marks[48]" value="<?= $array[48] ?? '' ?>"></td>
                                                    <td style="display:none"><input type="hidden" name="marks[49]" value="<?= $array[49] ?? '' ?>"></td>
                                                </tr>

                                                <!-- ================= ROW 6 : 50–59 (G. TOTAL – HIDDEN) ================= -->
                                                    <tr style="display:none">
                                                        <td colspan="10">
                                                            <input type="hidden" name="marks[50]" value="<?= $array[50] ?? '' ?>">
                                                            <input type="hidden" name="marks[51]" value="<?= $array[51] ?? '' ?>">
                                                            <input type="hidden" name="marks[52]" value="<?= $array[52] ?? '' ?>">
                                                            <input type="hidden" name="marks[53]" value="<?= $array[53] ?? '' ?>">
                                                            <input type="hidden" name="marks[54]" value="<?= $array[54] ?? '' ?>">
                                                            <input type="hidden" name="marks[55]" value="<?= $array[55] ?? '' ?>">
                                                            <input type="hidden" name="marks[56]" value="<?= $array[56] ?? '' ?>">
                                                            <input type="hidden" name="marks[57]" value="<?= $array[57] ?? '' ?>">
                                                            <input type="hidden" name="marks[58]" value="<?= $array[58] ?? '' ?>">
                                                            <input type="hidden" name="marks[59]" value="<?= $array[59] ?? '' ?>">
                                                        </td>
                                                    </tr>

                                                <!-- ================= ROW 7 : 60–69 ================= -->
                                                <tr>
                                                    <td class="ms29-subject-name">Drawing</td>
                                                    <td><input type="number" min="0" step="any" name="marks[60]" max="40" value="<?= $array[60] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[61]" max="5" value="<?= $array[61] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[62]" max="5" value="<?= $array[62] ?? '' ?>"></td>
                                                    <td style="display:none"><input type="hidden" name="marks[63]" value="<?= $array[63] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[64]" max="40" value="<?= $array[64] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[65]" max="5" value="<?= $array[65] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[66]" max="5" value="<?= $array[66] ?? '' ?>"></td>
                                                    <td style="display:none"><input type="hidden" name="marks[67]" value="<?= $array[67] ?? '' ?>"></td>
                                                    <td style="display:none"><input type="hidden" name="marks[68]" value="<?= $array[68] ?? '' ?>"></td>
                                                    <td style="display:none"><input type="hidden" name="marks[69]" value="<?= $array[69] ?? '' ?>"></td>
                                                </tr>

                                                <!-- ================= ROW 8 : 70–79 ================= -->
                                                <tr>
                                                    <td class="ms29-subject-name">G.K. / Apt</td>
                                                    <td><input type="number" min="0" step="any" name="marks[70]" max="40" value="<?= $array[70] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[71]" max="5" value="<?= $array[71] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[72]" max="5" value="<?= $array[72] ?? '' ?>"></td>
                                                    <td style="display:none"><input type="hidden" name="marks[73]" value="<?= $array[73] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[74]" max="40" value="<?= $array[74] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[75]" max="5" value="<?= $array[75] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[76]" max="5" value="<?= $array[76] ?? '' ?>"></td>
                                                    <td style="display:none"><input type="hidden" name="marks[77]" value="<?= $array[77] ?? '' ?>"></td>
                                                    <td style="display:none"><input type="hidden" name="marks[78]" value="<?= $array[78] ?? '' ?>"></td>
                                                    <td style="display:none"><input type="hidden" name="marks[79]" value="<?= $array[79] ?? '' ?>"></td>
                                                </tr>

                                          <!-- ================= ROW 9 : 80–89 (PERCENTAGE – HIDDEN) ================= -->
                                            <tr style="display:none">
                                                <td colspan="10">
                                                    <input type="hidden" name="marks[80]" value="<?= $array[80] ?? '' ?>">
                                                    <input type="hidden" name="marks[81]" value="<?= $array[81] ?? '' ?>">
                                                    <input type="hidden" name="marks[82]" value="<?= $array[82] ?? '' ?>">
                                                    <input type="hidden" name="marks[83]" value="<?= $array[83] ?? '' ?>">
                                                    <input type="hidden" name="marks[84]" value="<?= $array[84] ?? '' ?>">
                                                    <input type="hidden" name="marks[85]" value="<?= $array[85] ?? '' ?>">
                                                    <input type="hidden" name="marks[86]" value="<?= $array[86] ?? '' ?>">
                                                    <input type="hidden" name="marks[87]" value="<?= $array[87] ?? '' ?>">
                                                    <input type="hidden" name="marks[88]" value="<?= $array[88] ?? '' ?>">
                                                    <input type="hidden" name="marks[89]" value="<?= $array[89] ?? '' ?>">
                                                </td>
                                            </tr>
                                            </tbody>
                                        </table>

                                        <div class="ms29-support-legend">
                                            A - Very Supportive | B - Show Support in Some Areas | C - Need to be involved
                                        </div>
                                        <!-- ================= PARENTAL SUPPORT ================= -->
                                        <div class="ms29-section-title">PARENTAL SUPPORT</div>

                                        <table class="ms29-support-table">
                                            <tr>
                                                <td>Uniform</td>
                                                <td>
                                                    <select name="marks[90]">
                                                        <option value="">Select</option>
                                                        <option value="A" <?= (($array[90] ?? '') === 'A') ? 'selected' : '' ?>>A - Very Supportive</option>
                                                        <option value="B" <?= (($array[90] ?? '') === 'B') ? 'selected' : '' ?>>B - Show Support in Some Areas</option>
                                                        <option value="C" <?= (($array[90] ?? '') === 'C') ? 'selected' : '' ?>>C - Need to be involved</option>
                                                    </select>
                                                </td>
                                                <td>
                                                    <select name="marks[91]">
                                                        <option value="">Select</option>
                                                        <option value="A" <?= (($array[91] ?? '') === 'A') ? 'selected' : '' ?>>A - Very Supportive</option>
                                                        <option value="B" <?= (($array[91] ?? '') === 'B') ? 'selected' : '' ?>>B - Show Support in Some Areas</option>
                                                        <option value="C" <?= (($array[91] ?? '') === 'C') ? 'selected' : '' ?>>C - Need to be involved</option>
                                                    </select>
                                                </td>
                                            </tr>

                                            <tr>
                                                <td>Circular Follow Up</td>
                                                <td>
                                                    <select name="marks[92]">
                                                        <option value="">Select</option>
                                                        <option value="A" <?= (($array[92] ?? '') === 'A') ? 'selected' : '' ?>>A - Very Supportive</option>
                                                        <option value="B" <?= (($array[92] ?? '') === 'B') ? 'selected' : '' ?>>B - Show Support in Some Areas</option>
                                                        <option value="C" <?= (($array[92] ?? '') === 'C') ? 'selected' : '' ?>>C - Need to be involved</option>
                                                    </select>
                                                </td>
                                                <td>
                                                    <select name="marks[93]">
                                                        <option value="">Select</option>
                                                        <option value="A" <?= (($array[93] ?? '') === 'A') ? 'selected' : '' ?>>A - Very Supportive</option>
                                                        <option value="B" <?= (($array[93] ?? '') === 'B') ? 'selected' : '' ?>>B - Show Support in Some Areas</option>
                                                        <option value="C" <?= (($array[93] ?? '') === 'C') ? 'selected' : '' ?>>C - Need to be involved</option>
                                                    </select>
                                                </td>
                                            </tr>

                                            <tr>
                                                <td>Home Work / Act. Submission</td>
                                                <td>
                                                    <select name="marks[94]">
                                                        <option value="">Select</option>
                                                        <option value="A" <?= (($array[94] ?? '') === 'A') ? 'selected' : '' ?>>A - Very Supportive</option>
                                                        <option value="B" <?= (($array[94] ?? '') === 'B') ? 'selected' : '' ?>>B - Show Support in Some Areas</option>
                                                        <option value="C" <?= (($array[94] ?? '') === 'C') ? 'selected' : '' ?>>C - Need to be involved</option>
                                                    </select>
                                                </td>
                                                <td>
                                                    <select name="marks[95]">
                                                        <option value="">Select</option>
                                                        <option value="A" <?= (($array[95] ?? '') === 'A') ? 'selected' : '' ?>>A - Very Supportive</option>
                                                        <option value="B" <?= (($array[95] ?? '') === 'B') ? 'selected' : '' ?>>B - Show Support in Some Areas</option>
                                                        <option value="C" <?= (($array[95] ?? '') === 'C') ? 'selected' : '' ?>>C - Need to be involved</option>
                                                    </select>
                                                </td>
                                            </tr>

                                            <tr>
                                                <td>PTM / Attendance</td>
                                                <td>
                                                    <select name="marks[96]">
                                                        <option value="">Select</option>
                                                        <option value="A" <?= (($array[96] ?? '') === 'A') ? 'selected' : '' ?>>A - Very Supportive</option>
                                                        <option value="B" <?= (($array[96] ?? '') === 'B') ? 'selected' : '' ?>>B - Show Support in Some Areas</option>
                                                        <option value="C" <?= (($array[96] ?? '') === 'C') ? 'selected' : '' ?>>C - Need to be involved</option>
                                                    </select>
                                                </td>
                                                <td>
                                                    <select name="marks[97]">
                                                        <option value="">Select</option>
                                                        <option value="A" <?= (($array[97] ?? '') === 'A') ? 'selected' : '' ?>>A - Very Supportive</option>
                                                        <option value="B" <?= (($array[97] ?? '') === 'B') ? 'selected' : '' ?>>B - Show Support in Some Areas</option>
                                                        <option value="C" <?= (($array[97] ?? '') === 'C') ? 'selected' : '' ?>>C - Need to be involved</option>
                                                    </select>
                                                </td>
                                            </tr>
                                        </table>



                                        <!-- ================= TEACHERS ASSESSMENT ================= -->
                                        <div class="ms29-section-title">TEACHERS ASSESSMENT</div>

                                        <table class="ms29-assessment-table">

                                            <tr>
                                                <td>Personal Hygiene</td>
                                                <td>
                                                    <select name="marks[98]">
                                                        <option value="">Select</option>
                                                        <option value="A" <?= (($array[98] ?? '') === 'A') ? 'selected' : '' ?>>A - Very Supportive</option>
                                                        <option value="B" <?= (($array[98] ?? '') === 'B') ? 'selected' : '' ?>>B - Show Support in Some Areas</option>
                                                        <option value="C" <?= (($array[98] ?? '') === 'C') ? 'selected' : '' ?>>C - Need to be involved</option>
                                                    </select>
                                                </td>
                                                <td>
                                                    <select name="marks[99]">
                                                        <option value="">Select</option>
                                                        <option value="A" <?= (($array[99] ?? '') === 'A') ? 'selected' : '' ?>>A - Very Supportive</option>
                                                        <option value="B" <?= (($array[99] ?? '') === 'B') ? 'selected' : '' ?>>B - Show Support in Some Areas</option>
                                                        <option value="C" <?= (($array[99] ?? '') === 'C') ? 'selected' : '' ?>>C - Need to be involved</option>
                                                    </select>
                                                </td>
                                            </tr>

                                            <tr>
                                                <td>Punctuality</td>
                                                <td>
                                                    <select name="marks[100]">
                                                        <option value="">Select</option>
                                                        <option value="A" <?= (($array[100] ?? '') === 'A') ? 'selected' : '' ?>>A - Very Supportive</option>
                                                        <option value="B" <?= (($array[100] ?? '') === 'B') ? 'selected' : '' ?>>B - Show Support in Some Areas</option>
                                                        <option value="C" <?= (($array[100] ?? '') === 'C') ? 'selected' : '' ?>>C - Need to be involved</option>
                                                    </select>
                                                </td>
                                                <td>
                                                    <select name="marks[101]">
                                                        <option value="">Select</option>
                                                        <option value="A" <?= (($array[101] ?? '') === 'A') ? 'selected' : '' ?>>A - Very Supportive</option>
                                                        <option value="B" <?= (($array[101] ?? '') === 'B') ? 'selected' : '' ?>>B - Show Support in Some Areas</option>
                                                        <option value="C" <?= (($array[101] ?? '') === 'C') ? 'selected' : '' ?>>C - Need to be involved</option>
                                                    </select>
                                                </td>
                                            </tr>

                                            <tr>
                                                <td>Value System</td>
                                                <td>
                                                    <select name="marks[102]">
                                                        <option value="">Select</option>
                                                        <option value="A" <?= (($array[102] ?? '') === 'A') ? 'selected' : '' ?>>A - Very Supportive</option>
                                                        <option value="B" <?= (($array[102] ?? '') === 'B') ? 'selected' : '' ?>>B - Show Support in Some Areas</option>
                                                        <option value="C" <?= (($array[102] ?? '') === 'C') ? 'selected' : '' ?>>C - Need to be involved</option>
                                                    </select>
                                                </td>
                                                <td>
                                                    <select name="marks[103]">
                                                        <option value="">Select</option>
                                                        <option value="A" <?= (($array[103] ?? '') === 'A') ? 'selected' : '' ?>>A - Very Supportive</option>
                                                        <option value="B" <?= (($array[103] ?? '') === 'B') ? 'selected' : '' ?>>B - Show Support in Some Areas</option>
                                                        <option value="C" <?= (($array[103] ?? '') === 'C') ? 'selected' : '' ?>>C - Need to be involved</option>
                                                    </select>
                                                </td>
                                            </tr>

                                            <tr>
                                                <td>Self-confidence</td>
                                                <td>
                                                    <select name="marks[104]">
                                                        <option value="">Select</option>
                                                        <option value="A" <?= (($array[104] ?? '') === 'A') ? 'selected' : '' ?>>A - Very Supportive</option>
                                                        <option value="B" <?= (($array[104] ?? '') === 'B') ? 'selected' : '' ?>>B - Show Support in Some Areas</option>
                                                        <option value="C" <?= (($array[104] ?? '') === 'C') ? 'selected' : '' ?>>C - Need to be involved</option>
                                                    </select>
                                                </td>
                                                <td>
                                                    <select name="marks[105]">
                                                        <option value="">Select</option>
                                                        <option value="A" <?= (($array[105] ?? '') === 'A') ? 'selected' : '' ?>>A - Very Supportive</option>
                                                        <option value="B" <?= (($array[105] ?? '') === 'B') ? 'selected' : '' ?>>B - Show Support in Some Areas</option>
                                                        <option value="C" <?= (($array[105] ?? '') === 'C') ? 'selected' : '' ?>>C - Need to be involved</option>
                                                    </select>
                                                </td>
                                            </tr>

                                        </table>


                                        <!-- ================= REMARKS ================= -->
                                        <div class="ms29-remarks-box">
                                            <div class="ms29-remarks-title">Class Teacher's Remarks:</div>
                                            <div class="ms29-remarks-content">
                                                <textarea name="marks[106]"><?= $array[106] ?? '' ?></textarea>
                                            </div>
                                        </div>

                                        <!-- ================= PROMOTION ================= -->
                                        <div class="ms29-promotion-box">
                                            <strong>Promoted to Class:</strong>
                                            <span class="ms29-promotion-value">
                                                <select name="marks[107]">
                                                    <option value="na" <?= (($array[107] ?? '') === 'na') ? 'selected' : '' ?>>
                                                        Not Promoted
                                                    </option>

                                                    <?php
                                                    $stmtClasses = $conn_rjllearning_specific->prepare(
                                                        "SELECT classname FROM class_info ORDER BY id ASC"
                                                    );
                                                    $stmtClasses->execute();
                                                    $classes = $stmtClasses->fetchAll(PDO::FETCH_ASSOC);

                                                    foreach ($classes as $cls) {
                                                        $className = $cls['classname'];
                                                        $selected = (!empty($array[107]) && $array[107] === $className) ? 'selected' : '';
                                                        echo '<option value="' . htmlspecialchars($className) . '" ' . $selected . '>'
                                                            . htmlspecialchars($className) .
                                                            '</option>';
                                                    }
                                                    ?>
                                                </select>
                                            </span>
                                        </div>



                                    </div>
                                </div>

                            </div>
                        </div>

                        <!-- ================= TERM I ================= -->
                        <div class="accordion-item">
                            <button type="button" class="accordion-header">
                                📘 TERM – I
                            </button>

                            <div class="accordion-body">

                                <div class="ms29-page  term-1">

                                    <div class="ms29-page-title">
                                        ACADEMIC PERFORMANCE: SCHOLASTIC AREAS
                                    </div>

                                    <!-- ================= TERM I ================= -->
                                    <table class="ms29-term1-table" style="width:100%; table-layout:fixed;">

                                        <thead>
                                            <tr class="ms29-term-row-head">
                                                <th colspan="7">TERM - I (100 MARKS)</th>
                                            </tr>

                                            <tr class="ms29-term-header">
                                                <th style="text-align:center">Subject</th>
                                                <th style="text-align:center">PT-1<br>40</th>
                                                <th style="text-align:center">Ass.<br>5</th>
                                                <th style="text-align:center">N.B.<br>5</th>
                                                <th style="text-align:center">PT-2<br>40</th>
                                                <th style="text-align:center">Ass.<br>5</th>
                                                <th style="text-align:center">N.B.<br>5</th>
                                            </tr>
                                        </thead>

                                        <tbody>

                                            <!-- ================= ENGLISH (108–115) ================= -->
                                            <tr>
                                                <td class="ms29-subject-name">English</td>
                                                <td><input type="number" min="0" step="any" name="marks[108]" max="40" value="<?= $array[108] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[109]" max="5" value="<?= $array[109] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[110]" max="5" value="<?= $array[110] ?? '' ?>"></td>
                                                <td style="display:none"><input type="hidden" min="0" step="any" name="marks[111]" value="<?= $array[111] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[112]" max="40" value="<?= $array[112] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[113]" max="5" value="<?= $array[113] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[114]" max="5" value="<?= $array[114] ?? '' ?>"></td>
                                                <td style="display:none"><input type="hidden" min="0" step="any" name="marks[115]" value="<?= $array[115] ?? '' ?>"></td>
                                            </tr>

                                            <!-- ================= HINDI (116–123) ================= -->
                                            <tr>
                                                <td class="ms29-subject-name">Hindi</td>
                                                <td><input type="number" min="0" step="any" name="marks[116]" max="40" value="<?= $array[116] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[117]" max="5" value="<?= $array[117] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[118]" max="5" value="<?= $array[118] ?? '' ?>"></td>
                                                <td style="display:none"><input type="hidden" min="0" step="any" name="marks[119]" value="<?= $array[119] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[120]" max="40" value="<?= $array[120] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[121]" max="5" value="<?= $array[121] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[122]" max="5" value="<?= $array[122] ?? '' ?>"></td>
                                                <td style="display:none"><input type="hidden" min="0" step="any" name="marks[123]" value="<?= $array[123] ?? '' ?>"></td>
                                            </tr>

                                            <!-- ================= PUNJABI (124–131) ================= -->
                                            <tr>
                                                <td class="ms29-subject-name">Punjabi</td>
                                                <td><input type="number" min="0" step="any" name="marks[124]" max="40" value="<?= $array[124] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[125]" max="5" value="<?= $array[125] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[126]" max="5" value="<?= $array[126] ?? '' ?>"></td>
                                                <td style="display:none"><input type="hidden" min="0" step="any" name="marks[127]" value="<?= $array[127] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[128]" max="40" value="<?= $array[128] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[129]" max="5" value="<?= $array[129] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[130]" max="5" value="<?= $array[130] ?? '' ?>"></td>
                                                <td style="display:none"><input type="hidden" min="0" step="any" name="marks[131]" value="<?= $array[131] ?? '' ?>"></td>
                                            </tr>

                                            <!-- ================= MATHS (132–139) ================= -->
                                            <tr>
                                                <td class="ms29-subject-name">Maths</td>
                                                <td><input type="number" min="0" step="any" name="marks[132]" max="40" value="<?= $array[132] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[133]" max="5" value="<?= $array[133] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[134]" max="5" value="<?= $array[134] ?? '' ?>"></td>
                                                <td style="display:none"><input type="hidden" min="0" step="any" name="marks[135]" value="<?= $array[135] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[136]" max="40" value="<?= $array[136] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[137]" max="5" value="<?= $array[137] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[138]" max="5" value="<?= $array[138] ?? '' ?>"></td>
                                                <td style="display:none"><input type="hidden" min="0" step="any" name="marks[139]" value="<?= $array[139] ?? '' ?>"></td>
                                            </tr>

                                            <!-- ================= EVS (140–147) ================= -->
                                            <tr>
                                                <td class="ms29-subject-name">EVS</td>
                                                <td><input type="number" min="0" step="any" name="marks[140]" max="40" value="<?= $array[140] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[141]" max="5" value="<?= $array[141] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[142]" max="5" value="<?= $array[142] ?? '' ?>"></td>
                                                <td style="display:none"><input type="hidden" min="0" step="any" name="marks[143]" value="<?= $array[143] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[144]" max="40" value="<?= $array[144] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[145]" max="5" value="<?= $array[145] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[146]" max="5" value="<?= $array[146] ?? '' ?>"></td>
                                                <td style="display:none"><input type="hidden" min="0" step="any" name="marks[147]" value="<?= $array[147] ?? '' ?>"></td>
                                            </tr>
                                            
                                        <tr style="display:none;">
                                            <td class="ms29-subject-name">G. Total</td>

                                            <td><input type="hidden" name="marks[148]" value="<?= $array[148] ?? '' ?>"></td>
                                            <td><input type="hidden" name="marks[149]" value="<?= $array[149] ?? '' ?>"></td>
                                            <td><input type="hidden" name="marks[150]" value="<?= $array[150] ?? '' ?>"></td>
                                            <td><input type="hidden" name="marks[151]" value="<?= $array[151] ?? '' ?>"></td>

                                            <td><input type="hidden" name="marks[152]" value="<?= $array[152] ?? '' ?>"></td>
                                            <td><input type="hidden" name="marks[153]" value="<?= $array[153] ?? '' ?>"></td>
                                            <td><input type="hidden" name="marks[154]" value="<?= $array[154] ?? '' ?>"></td>
                                            <td><input type="hidden" name="marks[155]" value="<?= $array[155] ?? '' ?>"></td>
                                        </tr>

                                            <!-- ================= DRAWING (156–163) ================= -->
                                            <tr>
                                                <td class="ms29-subject-name">Drawing</td>
                                                <td><input type="number" min="0" step="any" name="marks[156]" max="40" value="<?= $array[156] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[157]" max="5" value="<?= $array[157] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[158]" max="5" value="<?= $array[158] ?? '' ?>"></td>
                                                <td style="display:none"><input type="hidden" min="0" step="any" name="marks[159]" value="<?= $array[159] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[160]" max="40" value="<?= $array[160] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[161]" max="5" value="<?= $array[161] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[162]" max="5" value="<?= $array[162] ?? '' ?>"></td>
                                                <td style="display:none"><input type="hidden" min="0" step="any" name="marks[163]" value="<?= $array[163] ?? '' ?>"></td>
                                            </tr>

                                            <!-- ================= G.K. / APT (164–171) ================= -->
                                            <tr>
                                                <td class="ms29-subject-name">G.K. / Apt</td>
                                                <td><input type="number" min="0" step="any" name="marks[164]" max="40" value="<?= $array[164] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[165]" max="5" value="<?= $array[165] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[166]" max="5" value="<?= $array[166] ?? '' ?>"></td>
                                                <td style="display:none"><input type="hidden" min="0" step="any" name="marks[167]" value="<?= $array[167] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[168]" max="40" value="<?= $array[168] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[169]" max="5" value="<?= $array[169] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[170]" max="5" value="<?= $array[170] ?? '' ?>"></td>
                                                <td style="display:none"><input type="hidden" min="0" step="any" name="marks[171]" value="<?= $array[171] ?? '' ?>"></td>
                                            </tr>

                                            <!-- ================= PERCENTAGE (172) ================= -->
                                            <tr style="display:none">
                                                <td colspan="8">
                                                    <input type="hidden" min="0" step="any" name="marks[172]" value="<?= $array[172] ?? '' ?>">
                                                </td>
                                            </tr>

                                        </tbody>
                                    </table>






                                </div>

                            </div>
                        </div>

                    </div>

            </div> <!-- END ms29-term-wrapper accordion -->



            <div>
                <div class="ms29-section-gap"></div>

                <!-- ================= CO-SCHOLASTICS ================= -->
                <div class="ms29-subhead">CO-SCHOLASTICS AREAS</div>

                <table class="ms29-health-table">
                    <tr>
                        <th colspan="4" class="ms29-left-head">HEALTH STATUS</th>
                    </tr>

                    <tr>
                        <td class="ms29-left-cell">Height</td>
                        <td><input type="text" name="marks[173]" value="<?= $array[173] ?? '' ?>"></td>

                        <td class="ms29-left-cell">Weight</td>
                        <td><input type="text" name="marks[174]" value="<?= $array[174] ?? '' ?>"></td>
                    </tr>

                    <tr>
                        <td class="ms29-left-cell">Blood Group</td>
                        <td colspan="3">
                            <input type="text" name="marks[175]" value="<?= $array[175] ?? '' ?>">
                        </td>
                    </tr>
                </table>



                <div class="ms29-section-gap"></div>

                <!-- ================= ATTENDANCE ================= -->

                <div class="ms29-attendance-filter">
                    <label>From:</label>
                    <input type="date" id="att_from">

                    <label>To:</label>
                    <input type="date" id="att_to">

                    <label>Term:</label>
                    <select id="att_term">
                        <option value="t1">TERM I</option>
                        <option value="t2" selected>TERM II</option>
                    </select>

                    <button type="button" id="fetchAttendance">Fetch Attendance</button>
                </div>
                <table class="ms29-attendance-table">
                    <tr>
                        <th class="ms29-left-head">ATTENDANCE</th>
                        <th>TERM I</th>
                        <th>TERM II</th>
                        <th>TOTAL</th>
                    </tr>

                    <tr>
                        <td class="ms29-left-cell">No of Working Days</td>
                        <td><input type="number" id="wd_t1" name="marks[176]" value="<?= $array[176] ?? '' ?>"></td>
                        <td><input type="number" id="wd_t2" name="marks[177]" value="<?= $array[177] ?? '' ?>"></td>
                        <td><input type="number" id="wd_total" name="marks[178]" value="<?= $array[178] ?? '' ?>"></td>
                    </tr>

                    <tr>
                        <td class="ms29-left-cell">Days Attended</td>
                        <td><input type="number" id="ad_t1" name="marks[179]" value="<?= $array[179] ?? '' ?>"></td>
                        <td><input type="number" id="ad_t2" name="marks[180]" value="<?= $array[180] ?? '' ?>"></td>
                        <td><input type="number" id="ad_total" name="marks[181]" value="<?= $array[181] ?? '' ?>"></td>
                    </tr>

                    <tr>
                        <td class="ms29-left-cell">Attendance %</td>
                        <td><input type="text" id="per_t1" name="marks[182]" value="<?= $array[182] ?? '' ?>"></td>
                        <td><input type="text" id="per_t2" name="marks[183]" value="<?= $array[183] ?? '' ?>"></td>
                        <td><input type="text" id="per_total" name="marks[184]" value="<?= $array[184] ?? '' ?>"></td>
                    </tr>
                </table>





                <div class="ms29-section-gap"></div>

                <!-- ================= COMPETITION ================= -->
                <table class="ms29-competition-table">
                    <tr>
                        <th class="ms29-left-head">Competition</th>
                    </tr>

                    <tr>
                        <td class="ms29-competition-box">
                            <textarea name="marks[185]"><?= $array[185] ?? '' ?></textarea>
                        </td>
                    </tr>

                    <tr>
                        <td class="ms29-competition-box">
                            <textarea name="marks[186]"><?= $array[186] ?? '' ?></textarea>
                        </td>
                    </tr>
                </table>

                <!-- ================= TEACHER REMARK ================= -->
                <div class="ms29-teacher-remark-label">
                    <strong>Class Teacher's Remarks:</strong>
                </div>

                <div class="ms29-teacher-remark-line">
                    <textarea name="marks[187]"><?= $array[187] ?? '' ?></textarea>
                </div>


            </div>

            <div class="ms29-page">

                <div class="ms29-right-title">
                    BASED ON PORTFOLIO, SUBJECT ENRICHMENT<br>
                    Activities and Class Activities
                </div>

                <!-- ================= LINGUISTIC INTELLIGENCE ================= -->
                <div class="ms29-intel-title">1. Linguistic Intelligence</div>

                <!-- ENGLISH -->
                <table class="ms29-skill-table">
                    <tr>
                        <th class="ms29-left-head">ENGLISH</th>
                        <th>TERM I</th>
                        <th>TERM II</th>
                    </tr>
                    <tr>
                        <td class="ms29-skill-text">Able to read and understand the text easily.</td>
                        <td><?php ms_level_dropdown('marks[188]', $array[188] ?? ''); ?></td>
                        <td><?php ms_level_dropdown('marks[189]', $array[189] ?? ''); ?></td>
                    </tr>
                    <tr>
                        <td class="ms29-skill-text">Remember written and spoken information.</td>
                        <td><?php ms_level_dropdown('marks[190]', $array[190] ?? ''); ?></td>
                        <td><?php ms_level_dropdown('marks[191]', $array[191] ?? ''); ?></td>
                    </tr>
                    <tr>
                        <td class="ms29-skill-text">Able to write grammatically correct sentences.</td>
                        <td><?php ms_level_dropdown('marks[192]', $array[192] ?? ''); ?></td>
                        <td><?php ms_level_dropdown('marks[193]', $array[193] ?? ''); ?></td>
                    </tr>
                    <tr>
                        <td class="ms29-skill-text">Is able to explain text well.</td>
                        <td><?php ms_level_dropdown('marks[194]', $array[194] ?? ''); ?></td>
                        <td><?php ms_level_dropdown('marks[195]', $array[195] ?? ''); ?></td>
                    </tr>
                </table>

                <!-- HINDI -->
                <table class="ms29-skill-table">
                    <tr>
                        <th class="ms29-left-head">HINDI</th>
                        <th>TERM I</th>
                        <th>TERM II</th>
                    </tr>
                    <tr>
                        <td class="ms29-skill-text">Able to read and understand the text easily.</td>
                        <td><?php ms_level_dropdown('marks[196]', $array[196] ?? ''); ?></td>
                        <td><?php ms_level_dropdown('marks[197]', $array[197] ?? ''); ?></td>
                    </tr>
                    <tr>
                        <td class="ms29-skill-text">Remember written and spoken information.</td>
                        <td><?php ms_level_dropdown('marks[198]', $array[198] ?? ''); ?></td>
                        <td><?php ms_level_dropdown('marks[199]', $array[199] ?? ''); ?></td>
                    </tr>
                    <tr>
                        <td class="ms29-skill-text">Able to write grammatically correct sentences.</td>
                        <td><?php ms_level_dropdown('marks[200]', $array[200] ?? ''); ?></td>
                        <td><?php ms_level_dropdown('marks[201]', $array[201] ?? ''); ?></td>
                    </tr>
                    <tr>
                        <td class="ms29-skill-text">Is able to explain text well.</td>
                        <td><?php ms_level_dropdown('marks[202]', $array[202] ?? ''); ?></td>
                        <td><?php ms_level_dropdown('marks[203]', $array[203] ?? ''); ?></td>
                    </tr>
                </table>

                <!-- PUNJABI -->
                <table class="ms29-skill-table">
                    <tr>
                        <th class="ms29-left-head">PUNJABI</th>
                        <th>TERM I</th>
                        <th>TERM II</th>
                    </tr>
                    <tr>
                        <td class="ms29-skill-text">Able to read and understand the text easily.</td>
                        <td><?php ms_level_dropdown('marks[204]', $array[204] ?? ''); ?></td>
                        <td><?php ms_level_dropdown('marks[205]', $array[205] ?? ''); ?></td>
                    </tr>
                    <tr>
                        <td class="ms29-skill-text">Remember written and spoken information.</td>
                        <td><?php ms_level_dropdown('marks[206]', $array[206] ?? ''); ?></td>
                        <td><?php ms_level_dropdown('marks[207]', $array[207] ?? ''); ?></td>
                    </tr>
                    <tr>
                        <td class="ms29-skill-text">Able to write grammatically correct sentences.</td>
                        <td><?php ms_level_dropdown('marks[208]', $array[208] ?? ''); ?></td>
                        <td><?php ms_level_dropdown('marks[209]', $array[209] ?? ''); ?></td>
                    </tr>
                    <tr>
                        <td class="ms29-skill-text">Is able to explain text well.</td>
                        <td><?php ms_level_dropdown('marks[210]', $array[210] ?? ''); ?></td>
                        <td><?php ms_level_dropdown('marks[211]', $array[211] ?? ''); ?></td>
                    </tr>
                </table>

                <!-- ================= LOGICAL MATHEMATICAL INTELLIGENCE ================= -->
                <div class="ms29-intel-title">2. Logical Mathematical Intelligence</div>

                <!-- MATHEMATICS -->
                <table class="ms29-skill-table">
                    <tr>
                        <th class="ms29-left-head">MATHEMATICS</th>
                        <th>TERM I</th>
                        <th>TERM II</th>
                    </tr>
                    <tr>
                        <td class="ms29-skill-text">Enjoy working with numbers, has number sense.</td>
                        <td><?php ms_level_dropdown('marks[212]', $array[212] ?? ''); ?></td>
                        <td><?php ms_level_dropdown('marks[213]', $array[213] ?? ''); ?></td>
                    </tr>
                    <tr>
                        <td class="ms29-skill-text">Able to do accurate calculation.</td>
                        <td><?php ms_level_dropdown('marks[214]', $array[214] ?? ''); ?></td>
                        <td><?php ms_level_dropdown('marks[215]', $array[215] ?? ''); ?></td>
                    </tr>
                    <tr>
                        <td class="ms29-skill-text">Ability to think conceptually.</td>
                        <td><?php ms_level_dropdown('marks[216]', $array[216] ?? ''); ?></td>
                        <td><?php ms_level_dropdown('marks[217]', $array[217] ?? ''); ?></td>
                    </tr>
                    <tr>
                        <td class="ms29-skill-text">Discern logical or numeric patterns.</td>
                        <td><?php ms_level_dropdown('marks[218]', $array[218] ?? ''); ?></td>
                        <td><?php ms_level_dropdown('marks[219]', $array[219] ?? ''); ?></td>
                    </tr>
                </table>

            </div>





            <div class="ms29-wrapper">

                <!-- ================= LEFT PAGE ================= -->
                <div class="ms29-page">

                    <!-- ENVIRONMENTAL STUDIES / SCIENCE -->
                    <table class="ms29-rounded-table">
                        <tr>
                            <th class="ms29-left-head">ENVIRONMENTAL STUDIES / SCIENCE</th>
                            <th class="ms29-center-head">TERM I</th>
                            <th class="ms29-center-head">TERM II</th>
                        </tr>

                        <tr>
                            <td class="ms29-skill-text">Show awareness to the topic.</td>
                            <td><?php ms_level_dropdown('marks[220]', $array[220] ?? ''); ?></td>
                            <td><?php ms_level_dropdown('marks[221]', $array[221] ?? ''); ?></td>
                        </tr>

                        <tr>
                            <td class="ms29-skill-text">Able to interpret diagrams.</td>
                            <td><?php ms_level_dropdown('marks[222]', $array[222] ?? ''); ?></td>
                            <td><?php ms_level_dropdown('marks[223]', $array[223] ?? ''); ?></td>
                        </tr>

                        <tr>
                            <td class="ms29-skill-text">Enjoy in knowing facts and exploring new ideas.</td>
                            <td><?php ms_level_dropdown('marks[224]', $array[224] ?? ''); ?></td>
                            <td><?php ms_level_dropdown('marks[225]', $array[225] ?? ''); ?></td>
                        </tr>

                        <tr>
                            <td class="ms29-skill-text">Comprehensive understanding of diversity.</td>
                            <td><?php ms_level_dropdown('marks[226]', $array[226] ?? ''); ?></td>
                            <td><?php ms_level_dropdown('marks[227]', $array[227] ?? ''); ?></td>
                        </tr>
                    </table>

                    <!-- COMPUTER EDUCATION -->
                    <table class="ms29-rounded-table">
                        <tr>
                            <th class="ms29-left-head">COMPUTER EDUCATION</th>
                            <th class="ms29-center-head">TERM I</th>
                            <th class="ms29-center-head">TERM II</th>
                        </tr>

                        <tr>
                            <td class="ms29-skill-text">Have excellent problem solving skill.</td>
                            <td><?php ms_level_dropdown('marks[228]', $array[228] ?? ''); ?></td>
                            <td><?php ms_level_dropdown('marks[229]', $array[229] ?? ''); ?></td>
                        </tr>

                        <tr>
                            <td class="ms29-skill-text">Understanding of Concept.</td>
                            <td><?php ms_level_dropdown('marks[230]', $array[230] ?? ''); ?></td>
                            <td><?php ms_level_dropdown('marks[231]', $array[231] ?? ''); ?></td>
                        </tr>

                        <tr>
                            <td class="ms29-skill-text">Able to do creative work and designs documents well.</td>
                            <td><?php ms_level_dropdown('marks[232]', $array[232] ?? ''); ?></td>
                            <td><?php ms_level_dropdown('marks[233]', $array[233] ?? ''); ?></td>
                        </tr>
                    </table>

                    <div class="ms29-intel-title">3. Visual - Spatial Intelligence</div>

                    <!-- DRAWING / ART & CRAFT -->
                    <table class="ms29-rounded-table">
                        <tr>
                            <th class="ms29-left-head">DRAWING / ART &amp; CRAFT</th>
                            <th class="ms29-center-head">TERM I</th>
                            <th class="ms29-center-head">TERM II</th>
                        </tr>

                        <tr>
                            <td class="ms29-skill-text">Enjoy Drawing, painting and visual Art.</td>
                            <td><?php ms_level_dropdown('marks[234]', $array[234] ?? ''); ?></td>
                            <td><?php ms_level_dropdown('marks[235]', $array[235] ?? ''); ?></td>
                        </tr>

                        <tr>
                            <td class="ms29-skill-text">Participation in Art and Craft work.</td>
                            <td><?php ms_level_dropdown('marks[236]', $array[236] ?? ''); ?></td>
                            <td><?php ms_level_dropdown('marks[237]', $array[237] ?? ''); ?></td>
                        </tr>
                    </table>

                    <div class="ms29-intel-title">4. Bodily Kinesthetics Intelligence</div>

                    <!-- HEALTH AND FITNESS -->
                    <table class="ms29-rounded-table">
                        <tr>
                            <th class="ms29-left-head">HEALTH AND FITNESS</th>
                            <th class="ms29-center-head">TERM I</th>
                            <th class="ms29-center-head">TERM II</th>
                        </tr>

                        <tr>
                            <td class="ms29-skill-text">Actively participates in sports.</td>
                            <td><?php ms_level_dropdown('marks[238]', $array[238] ?? ''); ?></td>
                            <td><?php ms_level_dropdown('marks[239]', $array[239] ?? ''); ?></td>
                        </tr>

                        <tr>
                            <td class="ms29-skill-text">Ability to co-ordinates physical movements.</td>
                            <td><?php ms_level_dropdown('marks[240]', $array[240] ?? ''); ?></td>
                            <td><?php ms_level_dropdown('marks[241]', $array[241] ?? ''); ?></td>
                        </tr>

                        <tr>
                            <td class="ms29-skill-text">Have a good sense of timing.</td>
                            <td><?php ms_level_dropdown('marks[242]', $array[242] ?? ''); ?></td>
                            <td><?php ms_level_dropdown('marks[243]', $array[243] ?? ''); ?></td>
                        </tr>

                        <tr>
                            <td class="ms29-skill-text">Understands and follow instruction.</td>
                            <td><?php ms_level_dropdown('marks[244]', $array[244] ?? ''); ?></td>
                            <td><?php ms_level_dropdown('marks[245]', $array[245] ?? ''); ?></td>
                        </tr>
                    </table>

                    <div class="ms29-intel-title">5. Musical Rhythmic Intelligence</div>

                    <!-- MUSIC / DANCE -->
                    <table class="ms29-rounded-table">
                        <tr>
                            <th class="ms29-left-head">MUSIC / DANCE</th>
                            <th class="ms29-center-head">TERM I</th>
                            <th class="ms29-center-head">TERM II</th>
                        </tr>

                        <tr>
                            <td class="ms29-skill-text">Enjoy singing and playing musical instruments.</td>
                            <td><?php ms_level_dropdown('marks[246]', $array[246] ?? ''); ?></td>
                            <td><?php ms_level_dropdown('marks[247]', $array[247] ?? ''); ?></td>
                        </tr>

                        <tr>
                            <td class="ms29-skill-text">Remember songs and melodies.</td>
                            <td><?php ms_level_dropdown('marks[248]', $array[248] ?? ''); ?></td>
                            <td><?php ms_level_dropdown('marks[249]', $array[249] ?? ''); ?></td>
                        </tr>

                        <tr>
                            <td class="ms29-skill-text">Follow dance steps well.</td>
                            <td><?php ms_level_dropdown('marks[250]', $array[250] ?? ''); ?></td>
                            <td><?php ms_level_dropdown('marks[251]', $array[251] ?? ''); ?></td>
                        </tr>

                        <tr>
                            <td class="ms29-skill-text">Is compatible with other dancers.</td>
                            <td><?php ms_level_dropdown('marks[252]', $array[252] ?? ''); ?></td>
                            <td><?php ms_level_dropdown('marks[253]', $array[253] ?? ''); ?></td>
                        </tr>
                    </table>

                </div>

                <!-- ================= RIGHT PAGE ================= -->
                <div class="ms29-page">

                    <div class="ms29-intel-title">6. Naturalistic Intelligence</div>

                    <table class="ms29-rounded-table">
                        <tr>
                            <th class="ms29-left-head"></th>
                            <th class="ms29-center-head">TERM I</th>
                            <th class="ms29-center-head">TERM II</th>
                        </tr>

                        <tr>
                            <td class="ms29-skill-text">Categorize information easily.</td>
                            <td><?php ms_level_dropdown('marks[254]', $array[254] ?? ''); ?></td>
                            <td><?php ms_level_dropdown('marks[255]', $array[255] ?? ''); ?></td>
                        </tr>

                        <tr>
                            <td class="ms29-skill-text">Enjoy camping, exploring the outdoor trips.</td>
                            <td><?php ms_level_dropdown('marks[256]', $array[256] ?? ''); ?></td>
                            <td><?php ms_level_dropdown('marks[257]', $array[257] ?? ''); ?></td>
                        </tr>

                        <tr>
                            <td class="ms29-skill-text">Have ability to adapt a changing environment.</td>
                            <td><?php ms_level_dropdown('marks[258]', $array[258] ?? ''); ?></td>
                            <td><?php ms_level_dropdown('marks[259]', $array[259] ?? ''); ?></td>
                        </tr>

                        <tr>
                            <td class="ms29-skill-text">Likes to keep the environment clean.</td>
                            <td><?php ms_level_dropdown('marks[260]', $array[260] ?? ''); ?></td>
                            <td><?php ms_level_dropdown('marks[261]', $array[261] ?? ''); ?></td>
                        </tr>
                    </table>

                    <div class="ms29-intel-title">7. Interpersonal Intelligence</div>

                    <table class="ms29-rounded-table">
                        <tr>
                            <th class="ms29-left-head"></th>
                            <th class="ms29-center-head">TERM I</th>
                            <th class="ms29-center-head">TERM II</th>
                        </tr>

                        <tr>
                            <td class="ms29-skill-text">Follow self discipline.</td>
                            <td><?php ms_level_dropdown('marks[262]', $array[262] ?? ''); ?></td>
                            <td><?php ms_level_dropdown('marks[263]', $array[263] ?? ''); ?></td>
                        </tr>

                        <tr>
                            <td class="ms29-skill-text">See situation from different perspective.</td>
                            <td><?php ms_level_dropdown('marks[264]', $array[264] ?? ''); ?></td>
                            <td><?php ms_level_dropdown('marks[265]', $array[265] ?? ''); ?></td>
                        </tr>

                        <tr>
                            <td class="ms29-skill-text">Can deal with distractions.</td>
                            <td><?php ms_level_dropdown('marks[266]', $array[266] ?? ''); ?></td>
                            <td><?php ms_level_dropdown('marks[267]', $array[267] ?? ''); ?></td>
                        </tr>
                    </table>

                    <div class="ms29-intel-title">8. Intrapersonal Intelligence</div>

                    <table class="ms29-rounded-table">
                        <tr>
                            <th class="ms29-left-head"></th>
                            <th class="ms29-center-head">TERM I</th>
                            <th class="ms29-center-head">TERM II</th>
                        </tr>

                        <tr>
                            <td class="ms29-skill-text">Communicate well verbally.</td>
                            <td><?php ms_level_dropdown('marks[268]', $array[268] ?? ''); ?></td>
                            <td><?php ms_level_dropdown('marks[269]', $array[269] ?? ''); ?></td>
                        </tr>

                        <tr>
                            <td class="ms29-skill-text">Create positive relationship with others.</td>
                            <td><?php ms_level_dropdown('marks[270]', $array[270] ?? ''); ?></td>
                            <td><?php ms_level_dropdown('marks[271]', $array[271] ?? ''); ?></td>
                        </tr>

                        <tr>
                            <td class="ms29-skill-text">Accomplishes small task well.</td>
                            <td><?php ms_level_dropdown('marks[272]', $array[272] ?? ''); ?></td>
                            <td><?php ms_level_dropdown('marks[273]', $array[273] ?? ''); ?></td>
                        </tr>
                    </table>

                    <div class="ms29-intel-title">9. Existential Intelligence</div>

                    <table class="ms29-rounded-table">
                        <tr>
                            <th class="ms29-left-head">Value Education / Yoga and Meditation</th>
                            <th class="ms29-center-head">TERM I</th>
                            <th class="ms29-center-head">TERM II</th>
                        </tr>

                        <tr>
                            <td class="ms29-skill-text">Observes values taught in the class.</td>
                            <td><?php ms_level_dropdown('marks[274]', $array[274] ?? ''); ?></td>
                            <td><?php ms_level_dropdown('marks[275]', $array[275] ?? ''); ?></td>
                        </tr>

                        <tr>
                            <td class="ms29-skill-text">Meditates regularly and enjoys Yoga practice.</td>
                            <td><?php ms_level_dropdown('marks[276]', $array[276] ?? ''); ?></td>
                            <td><?php ms_level_dropdown('marks[277]', $array[277] ?? ''); ?></td>
                        </tr>

                        <tr>
                            <td class="ms29-skill-text">Can think critically and objectively.</td>
                            <td><?php ms_level_dropdown('marks[278]', $array[278] ?? ''); ?></td>
                            <td><?php ms_level_dropdown('marks[279]', $array[279] ?? ''); ?></td>
                        </tr>
                    </table>

                </div>

            </div>



            <div class="ms29-form-actions">
                <button type="submit" id="submit_marksheet" class="ms29-submit-btn">
                    Save Marksheet
                </button>


                <div class="ms29-message ms29-success" id="ms29SuccessMsg">
                    Marksheet saved successfully.
                </div>

                <div class="ms29-message ms29-error" id="ms29ErrorMsg">
                    Failed to save marksheet. Please try again.
                </div>
            </div>
</body>

</html>

<script>
    document.addEventListener("DOMContentLoaded", function() {

        document.querySelectorAll(".accordion-header").forEach(header => {
            header.addEventListener("click", function() {
                this.parentElement.classList.toggle("active");
            });
        });

    });
</script>



<script>
    $(document).ready(function() {

        // Bind submit on the correct form ID
        $("#upload_marksheet_form").on("submit", function(e) {
            e.preventDefault();

            var form = this;
            var formData = new FormData(form);

            $.ajax({
                url: "https://rejoicebilldesk.in/rjllearning_development/resources/marksheet_template/template29/process.php",
                type: "POST",
                data: formData,
                processData: false,
                contentType: false,
                cache: false,

                beforeSend: function() {
                    // Disable submit button only (safe)
                    $("#submit_marksheet")
                        .prop("disabled", true)
                        .html("Saving...");
                },

                success: function(resp) {
                    try {
                        var response = JSON.parse(resp);

                        if (response.status == 1) {
                            // Success UI
                            $("#ms29SuccessMsg").fadeIn(300);
                            $("#ms29ErrorMsg").hide();

                            setTimeout(function() {
                                $("#ms29SuccessMsg").fadeOut(300);
                            }, 3000);
                        } else {
                            throw response.message || "Save failed";
                        }

                    } catch (e) {
                        showError(e);
                    }

                    $("#submit_marksheet")
                        .prop("disabled", false)
                        .html("Save Marksheet");
                },

                error: function() {
                    showError("Server error. Please try again.");
                    $("#submit_marksheet")
                        .prop("disabled", false)
                        .html("Save Marksheet");
                }
            });
        });

        function showError(msg) {
            $("#ms29ErrorMsg").text(msg).fadeIn(300);

            setTimeout(function() {
                $("#ms29ErrorMsg").fadeOut(300);
            }, 5000);
        }

    });
</script>



<script>
    $(function() {

        // Set academic session dates by default
        $("#att_from").val(ACADEMIC_SESSION.start);
        $("#att_to").val(ACADEMIC_SESSION.end);

        function calculateTotal() {
            const wd1 = parseInt($("#wd_t1").val()) || 0;
            const wd2 = parseInt($("#wd_t2").val()) || 0;
            const ad1 = parseInt($("#ad_t1").val()) || 0;
            const ad2 = parseInt($("#ad_t2").val()) || 0;

            const totalWd = wd1 + wd2;
            const totalAd = ad1 + ad2;

            let totalPer = totalWd > 0 ?
                ((totalAd / totalWd) * 100).toFixed(2) :
                "0.00";

            $("#wd_total").val(totalWd);
            $("#ad_total").val(totalAd);
            $("#per_total").val(totalPer + "%");
        }

        $("#fetchAttendance").on("click", function() {

            const from = $("#att_from").val();
            const to = $("#att_to").val();
            const term = $("#att_term").val(); // t1 or t2

            if (!from || !to) {
                alert("Please select date range");
                return;
            }

            let working = 0;
            let present = 0;

            STUDENT_ATTENDANCE.forEach(row => {
                if (row.date >= from && row.date <= to) {
                    working++;
                    if (row.value === 'P') present++;
                }
            });

            let percentage = working > 0 ?
                ((present / working) * 100).toFixed(2) :
                "0.00";

            if (term === "t1") {
                // TERM I
                $("#wd_t1").val(working);
                $("#ad_t1").val(present);
                $("#per_t1").val(percentage + "%");
            } else {
                // TERM II
                $("#wd_t2").val(working);
                $("#ad_t2").val(present);
                $("#per_t2").val(percentage + "%");
            }

            // ✅ TOTAL ALWAYS AUTO
            calculateTotal();
        });

    });
</script>