<?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/template31/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">
                                📕 Periodic 2 + Final (Term II)
                            </button>

                            <div class="accordion-body">
                                <div class="ms29-right-panel term-2">

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

                                    <!-- ================= TERM II MARKS ================= -->
                                    <table class="ms29-marks-table">
                                        <thead>
                                            <tr>
                                                <th colspan="5" class="ms29-term-head">TERM -II (100 MARKS)</th>
                                            </tr>
                                            <tr>
                                                <th>Subject</th>
                                                <th>PT<br>40/4=10</th>
                                                <th>Subject<br>Enrichment<br>5</th>
                                                <th>Note<br>Book<br>5</th>
                                                <th>Half<br>Yearly<br>80</th>
                                            </tr>
                                        </thead>

                                        <tbody>

                                            <!-- ✅ English (0-5) -->
                                            <tr>
                                                <td class="ms29-subject-name">English</td>

                                                <td><input type="number" min="0" step="any" name="marks[0]" max="10" 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><input type="number" min="0" step="any" name="marks[3]" max="80" value="<?= $array[3] ?? '' ?>"></td>

                                                <!-- ✅ send hidden GT + Grade -->
                                                <input type="hidden" name="marks[4]" value="">
                                                <input type="hidden" name="marks[5]" value="">
                                            </tr>

                                            <!-- ✅ Hindi (6-11) -->
                                            <tr>
                                                <td class="ms29-subject-name">Hindi</td>

                                                <td><input type="number" min="0" step="any" name="marks[6]" max="10" value="<?= $array[6] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[7]" max="5" value="<?= $array[7] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[8]" max="5" value="<?= $array[8] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[9]" max="80" value="<?= $array[9] ?? '' ?>"></td>

                                                <input type="hidden" name="marks[10]" value="">
                                                <input type="hidden" name="marks[11]" value="">
                                            </tr>

                                            <!-- ✅ Punjabi (12-17) -->
                                            <tr>
                                                <td class="ms29-subject-name">Punjabi</td>

                                                <td><input type="number" min="0" step="any" name="marks[12]" max="10" value="<?= $array[12] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[13]" max="5" value="<?= $array[13] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[14]" max="5" value="<?= $array[14] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[15]" max="80" value="<?= $array[15] ?? '' ?>"></td>

                                                <input type="hidden" name="marks[16]" value="">
                                                <input type="hidden" name="marks[17]" value="">
                                            </tr>

                                            <!-- ✅ Maths (18-23) -->
                                            <tr>
                                                <td class="ms29-subject-name">Maths</td>

                                                <td><input type="number" min="0" step="any" name="marks[18]" max="10" value="<?= $array[18] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[19]" max="5" value="<?= $array[19] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[20]" max="5" value="<?= $array[20] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[21]" max="80" value="<?= $array[21] ?? '' ?>"></td>

                                                <input type="hidden" name="marks[22]" value="">
                                                <input type="hidden" name="marks[23]" value="">
                                            </tr>

                                            <!-- ✅ Science (24-29) -->
                                            <tr>
                                                <td class="ms29-subject-name">Science</td>

                                                <td><input type="number" min="0" step="any" name="marks[24]" max="10" 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><input type="number" min="0" step="any" name="marks[27]" max="80" value="<?= $array[27] ?? '' ?>"></td>

                                                <input type="hidden" name="marks[28]" value="">
                                                <input type="hidden" name="marks[29]" value="">
                                            </tr>

                                            <!-- ✅ S.st (30-35) -->
                                            <tr>
                                                <td class="ms29-subject-name">S.st</td>

                                                <td><input type="number" min="0" step="any" name="marks[30]" max="10" 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><input type="number" min="0" step="any" name="marks[33]" max="80" value="<?= $array[33] ?? '' ?>"></td>

                                                <input type="hidden" name="marks[34]" value="">
                                                <input type="hidden" name="marks[35]" value="">
                                            </tr>

                                            <!-- ❌ G.Total NOT SHOWN (36-39 NULL) -->
                                            <input type="hidden" name="marks[36]" value="">
                                            <input type="hidden" name="marks[37]" value="">
                                            <input type="hidden" name="marks[38]" value="">
                                            <input type="hidden" name="marks[39]" value="">

                                            <!-- ✅ Drawing (40-45) -->
                                            <tr>
                                                <td class="ms29-subject-name">Drawing</td>

                                                <td><input type="number" min="0" step="any" name="marks[40]" value="<?= $array[40] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[41]" value="<?= $array[41] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[42]" value="<?= $array[42] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[43]" value="<?= $array[43] ?? '' ?>"></td>

                                                <input type="hidden" name="marks[44]" value="">
                                                <input type="hidden" name="marks[45]" value="">
                                            </tr>

                                            <!-- ✅ Computer (46-51) -->
                                            <tr>
                                                <td class="ms29-subject-name">Computer</td>

                                                <td><input type="number" min="0" step="any" name="marks[46]" value="<?= $array[46] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[47]" value="<?= $array[47] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[48]" value="<?= $array[48] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[49]" value="<?= $array[49] ?? '' ?>"></td>

                                                <input type="hidden" name="marks[50]" value="">
                                                <input type="hidden" name="marks[51]" value="">
                                            </tr>

                                            <!-- ✅ Aptitude (52-57) -->
                                            <tr>
                                                <td class="ms29-subject-name">Aptitude</td>

                                                <td><input type="number" min="0" step="any" name="marks[52]" value="<?= $array[52] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[53]" value="<?= $array[53] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[54]" value="<?= $array[54] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[55]" value="<?= $array[55] ?? '' ?>"></td>

                                                <input type="hidden" name="marks[56]" value="">
                                                <input type="hidden" name="marks[57]" value="">
                                            </tr>

                                            <!-- ❌ Percentage NOT SHOWN (58 NULL) -->
                                            <input type="hidden" name="marks[58]" value="">

                                        </tbody>
                                    </table>

                                    
                                    <div class="ms29-section-title">PARENTAL SUPPORT</div>
                                    
                                    <table class="ms29-support-table">
                                        <tr>
                                            <td>Uniform</td>
                                            <td>
                                                <select name="marks[59]">
                                                    <option value="">Select</option>
                                                    <option value="A" <?= (($array[59] ?? '') === 'A') ? 'selected' : '' ?>>A - Very Supportive</option>
                                                    <option value="B" <?= (($array[59] ?? '') === 'B') ? 'selected' : '' ?>>B - Show Support in Some Areas</option>
                                                    <option value="C" <?= (($array[59] ?? '') === 'C') ? 'selected' : '' ?>>C - Need to be involved</option>
                                                </select>
                                            </td>
                                            <td>
                                                <select name="marks[60]">
                                                    <option value="">Select</option>
                                                    <option value="A" <?= (($array[60] ?? '') === 'A') ? 'selected' : '' ?>>A - Very Supportive</option>
                                                    <option value="B" <?= (($array[60] ?? '') === 'B') ? 'selected' : '' ?>>B - Show Support in Some Areas</option>
                                                    <option value="C" <?= (($array[60] ?? '') === 'C') ? 'selected' : '' ?>>C - Need to be involved</option>
                                                </select>
                                            </td>
                                        </tr>
                                    
                                        <tr>
                                            <td>Circular Follow Up</td>
                                            <td>
                                                <select name="marks[61]">
                                                    <option value="">Select</option>
                                                    <option value="A" <?= (($array[61] ?? '') === 'A') ? 'selected' : '' ?>>A - Very Supportive</option>
                                                    <option value="B" <?= (($array[61] ?? '') === 'B') ? 'selected' : '' ?>>B - Show Support in Some Areas</option>
                                                    <option value="C" <?= (($array[61] ?? '') === 'C') ? 'selected' : '' ?>>C - Need to be involved</option>
                                                </select>
                                            </td>
                                            <td>
                                                <select name="marks[62]">
                                                    <option value="">Select</option>
                                                    <option value="A" <?= (($array[62] ?? '') === 'A') ? 'selected' : '' ?>>A - Very Supportive</option>
                                                    <option value="B" <?= (($array[62] ?? '') === 'B') ? 'selected' : '' ?>>B - Show Support in Some Areas</option>
                                                    <option value="C" <?= (($array[62] ?? '') === 'C') ? 'selected' : '' ?>>C - Need to be involved</option>
                                                </select>
                                            </td>
                                        </tr>
                                    
                                        <tr>
                                            <td>Home Work / Act. Submission</td>
                                            <td>
                                                <select name="marks[63]">
                                                    <option value="">Select</option>
                                                    <option value="A" <?= (($array[63] ?? '') === 'A') ? 'selected' : '' ?>>A - Very Supportive</option>
                                                    <option value="B" <?= (($array[63] ?? '') === 'B') ? 'selected' : '' ?>>B - Show Support in Some Areas</option>
                                                    <option value="C" <?= (($array[63] ?? '') === 'C') ? 'selected' : '' ?>>C - Need to be involved</option>
                                                </select>
                                            </td>
                                            <td>
                                                <select name="marks[64]">
                                                    <option value="">Select</option>
                                                    <option value="A" <?= (($array[64] ?? '') === 'A') ? 'selected' : '' ?>>A - Very Supportive</option>
                                                    <option value="B" <?= (($array[64] ?? '') === 'B') ? 'selected' : '' ?>>B - Show Support in Some Areas</option>
                                                    <option value="C" <?= (($array[64] ?? '') === 'C') ? 'selected' : '' ?>>C - Need to be involved</option>
                                                </select>
                                            </td>
                                        </tr>
                                    
                                        <tr>
                                            <td>PTM / Attendance</td>
                                            <td>
                                                <select name="marks[65]">
                                                    <option value="">Select</option>
                                                    <option value="A" <?= (($array[65] ?? '') === 'A') ? 'selected' : '' ?>>A - Very Supportive</option>
                                                    <option value="B" <?= (($array[65] ?? '') === 'B') ? 'selected' : '' ?>>B - Show Support in Some Areas</option>
                                                    <option value="C" <?= (($array[65] ?? '') === 'C') ? 'selected' : '' ?>>C - Need to be involved</option>
                                                </select>
                                            </td>
                                            <td>
                                                <select name="marks[66]">
                                                    <option value="">Select</option>
                                                    <option value="A" <?= (($array[66] ?? '') === 'A') ? 'selected' : '' ?>>A - Very Supportive</option>
                                                    <option value="B" <?= (($array[66] ?? '') === 'B') ? 'selected' : '' ?>>B - Show Support in Some Areas</option>
                                                    <option value="C" <?= (($array[66] ?? '') === 'C') ? 'selected' : '' ?>>C - Need to be involved</option>
                                                </select>
                                            </td>
                                        </tr>
                                   
                                    </table>

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

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

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

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

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

                                        <tr>
                                            <td>Self-confidence</td>
                                            <td>
                                                <select name="marks[73]">
                                                    <option value="">Select</option>
                                                    <option value="A" <?= (($array[73] ?? '') === 'A') ? 'selected' : '' ?>>A - Very Supportive</option>
                                                    <option value="B" <?= (($array[73] ?? '') === 'B') ? 'selected' : '' ?>>B - Show Support in Some Areas</option>
                                                    <option value="C" <?= (($array[73] ?? '') === 'C') ? 'selected' : '' ?>>C - Need to be involved</option>
                                                </select>
                                            </td>
                                            <td>
                                                <select name="marks[74]">
                                                    <option value="">Select</option>
                                                    <option value="A" <?= (($array[74] ?? '') === 'A') ? 'selected' : '' ?>>A - Very Supportive</option>
                                                    <option value="B" <?= (($array[74] ?? '') === 'B') ? 'selected' : '' ?>>B - Show Support in Some Areas</option>
                                                    <option value="C" <?= (($array[74] ?? '') === '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[75]"><?= $array[75] ?? '' ?></textarea>
                                        </div>
                                    </div>

                                    <!-- ================= PROMOTION ================= -->
                                    <!-- ================= PROMOTION ================= -->
                                    <div class="ms29-promotion-box">
                                        <strong>Promoted to Class:</strong>
                                        <span class="ms29-promotion-value">
                                            <select name="marks[76]">
                                                <option value="na" <?= (($array[76] ?? '') === '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[76]) && $array[76] === $className) ? 'selected' : '';
                                                    echo '<option value="' . htmlspecialchars($className) . '" ' . $selected . '>'
                                                        . htmlspecialchars($className) .
                                                        '</option>';
                                                }
                                                ?>
                                            </select>
                                        </span>
                                    </div>

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

                        <!-- ================= TERM I ================= -->
                        <div class="accordion-item">
                            <button type="button" class="accordion-header">
                                📘 Periodic 1 + Term 1 (Half Yearly)
                            </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-marks-table">
                                        <thead>
                                            <tr>
                                                <th colspan="5" class="ms29-term-head">TERM -I (100 MARKS)</th>
                                            </tr>
                                            <tr>
                                                <th>Subject</th>
                                                <th>PT<br>40/4=10</th>
                                                <th>Subject<br>Enrichment<br>5</th>
                                                <th>Note<br>Book<br>5</th>
                                                <th>Half<br>Yearly<br>80</th>
                                            </tr>
                                        </thead>

                                        <tbody>

                                            <!-- ✅ English (77-82) -->
                                            <tr>
                                                <td class="ms29-subject-name">English</td>

                                                <td><input type="number" min="0" step="any" name="marks[77]" max="10" value="<?= $array[77] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[78]" max="5" value="<?= $array[78] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[79]" max="5" value="<?= $array[79] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[80]" max="80" value="<?= $array[80] ?? '' ?>"></td>

                                                <input type="hidden" name="marks[81]" value="">
                                                <input type="hidden" name="marks[82]" value="">
                                            </tr>

                                            <!-- ✅ Hindi (83-88) -->
                                            <tr>
                                                <td class="ms29-subject-name">Hindi</td>

                                                <td><input type="number" min="0" step="any" name="marks[83]" max="10" value="<?= $array[83] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[84]" max="5" value="<?= $array[84] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[85]" max="5" value="<?= $array[85] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[86]" max="80" value="<?= $array[86] ?? '' ?>"></td>

                                                <input type="hidden" name="marks[87]" value="">
                                                <input type="hidden" name="marks[88]" value="">
                                            </tr>

                                            <!-- ✅ Punjabi (89-94) -->
                                            <tr>
                                                <td class="ms29-subject-name">Punjabi</td>

                                                <td><input type="number" min="0" step="any" name="marks[89]" max="10" value="<?= $array[89] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[90]" max="5" value="<?= $array[90] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[91]" max="5" value="<?= $array[91] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[92]" max="80" value="<?= $array[92] ?? '' ?>"></td>

                                                <input type="hidden" name="marks[93]" value="">
                                                <input type="hidden" name="marks[94]" value="">
                                            </tr>

                                            <!-- ✅ Maths (95-100) -->
                                            <tr>
                                                <td class="ms29-subject-name">Maths</td>

                                                <td><input type="number" min="0" step="any" name="marks[95]" max="10" value="<?= $array[95] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[96]" max="5" value="<?= $array[96] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[97]" max="5" value="<?= $array[97] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[98]" max="80" value="<?= $array[98] ?? '' ?>"></td>

                                                <input type="hidden" name="marks[99]" value="">
                                                <input type="hidden" name="marks[100]" value="">
                                            </tr>

                                            <!-- ✅ Science (101-106) -->
                                            <tr>
                                                <td class="ms29-subject-name">Science</td>

                                                <td><input type="number" min="0" step="any" name="marks[101]" max="10" value="<?= $array[101] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[102]" max="5" value="<?= $array[102] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[103]" max="5" value="<?= $array[103] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[104]" max="80" value="<?= $array[104] ?? '' ?>"></td>

                                                <input type="hidden" name="marks[105]" value="">
                                                <input type="hidden" name="marks[106]" value="">
                                            </tr>

                                            <!-- ✅ S.st (107-112) -->
                                            <tr>
                                                <td class="ms29-subject-name">S.st</td>

                                                <td><input type="number" min="0" step="any" name="marks[107]" max="10" value="<?= $array[107] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[108]" max="5" 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="80" value="<?= $array[110] ?? '' ?>"></td>

                                                <input type="hidden" name="marks[111]" value="">
                                                <input type="hidden" name="marks[112]" value="">
                                            </tr>

                                            <!-- ✅ G.Total (ONLY 4 FIELDS, ALL NULL) (113-116) -->
                                            <tr style="display:none;">
                                                <td>G.Total</td>
                                                <input type="hidden" name="marks[113]" value="">
                                                <input type="hidden" name="marks[114]" value="">
                                                <input type="hidden" name="marks[115]" value="">
                                                <input type="hidden" name="marks[116]" value="">
                                            </tr>

                                            <!-- ✅ Drg. (117-122) -->
                                            <tr>
                                                <td class="ms29-subject-name">Drg.</td>

                                                <td><input type="number" min="0" step="any" name="marks[117]" max="10" value="<?= $array[117] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[118]" max="5" value="<?= $array[118] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[119]" max="5" value="<?= $array[119] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[120]" max="80" value="<?= $array[120] ?? '' ?>"></td>

                                                <input type="hidden" name="marks[121]" value="">
                                                <input type="hidden" name="marks[122]" value="">
                                            </tr>

                                            <!-- ✅ Computer (123-128) -->
                                            <tr>
                                                <td class="ms29-subject-name">Computer</td>

                                                <td><input type="number" min="0" step="any" name="marks[123]" max="10" value="<?= $array[123] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[124]" max="5" 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="80" value="<?= $array[126] ?? '' ?>"></td>

                                                <input type="hidden" name="marks[127]" value="">
                                                <input type="hidden" name="marks[128]" value="">
                                            </tr>

                                            <!-- ✅ Phy.Edu./Apt (129-134) -->
                                            <tr>
                                                <td class="ms29-subject-name">Phy.Edu./Apt</td>

                                                <td><input type="number" min="0" step="any" name="marks[129]" max="10" value="<?= $array[129] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[130]" max="5" value="<?= $array[130] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[131]" max="5" value="<?= $array[131] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[132]" max="80" value="<?= $array[132] ?? '' ?>"></td>

                                                <input type="hidden" name="marks[133]" value="">
                                                <input type="hidden" name="marks[134]" value="">
                                            </tr>

                                            <!-- ✅ Percentage (HIDDEN, NOT SHOWN) -->
                                            <input type="hidden" name="marks[135]" value="<?= $array[135] ?? '' ?>">


                                        </tbody>
                                    </table>



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







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

                    </div>



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

                <!-- ================= CO-SCHOLASTICS ================= -->
                <div class="ms29-subhead">CO-SCHOLASTIC 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[136]" value="<?= $array[136] ?? '' ?>"></td>

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

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

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

                <!-- ================= ATTENDANCE FILTER ================= -->
                <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>

                <!-- ================= ATTENDANCE ================= -->
                <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[139]" value="<?= $array[139] ?? '' ?>"></td>
                        <td><input type="number" id="wd_t2" name="marks[140]" value="<?= $array[140] ?? '' ?>"></td>
                        <td><input type="number" id="wd_total" name="marks[141]" value="<?= $array[141] ?? '' ?>"></td>
                    </tr>

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

                    <tr>
                        <td class="ms29-left-cell">Attendance %</td>
                        <td><input type="text" id="per_t1" name="marks[145]" value="<?= $array[145] ?? '' ?>"></td>
                        <td><input type="text" id="per_t2" name="marks[146]" value="<?= $array[146] ?? '' ?>"></td>
                        <td><input type="text" id="per_total" name="marks[147]" value="<?= $array[147] ?? '' ?>"></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[148]"><?= $array[148] ?? '' ?></textarea>
                        </td>
                    </tr>

                    <tr>
                        <td class="ms29-competition-box">
                            <textarea name="marks[149]"><?= $array[149] ?? '' ?></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[150]"><?= $array[150] ?? '' ?></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 ms29_level_dropdown('marks[151]', $array[151] ?? ''); ?></td>
                        <td><?php ms29_level_dropdown('marks[152]', $array[152] ?? ''); ?></td>
                    </tr>

                    <tr>
                        <td class="ms29-skill-text">Remember written and spoken information.</td>
                        <td><?php ms29_level_dropdown('marks[153]', $array[153] ?? ''); ?></td>
                        <td><?php ms29_level_dropdown('marks[154]', $array[154] ?? ''); ?></td>
                    </tr>

                    <tr>
                        <td class="ms29-skill-text">Able to write grammatically correct sentences.</td>
                        <td><?php ms29_level_dropdown('marks[155]', $array[155] ?? ''); ?></td>
                        <td><?php ms29_level_dropdown('marks[156]', $array[156] ?? ''); ?></td>
                    </tr>

                    <tr>
                        <td class="ms29-skill-text">Is able to explain text well.</td>
                        <td><?php ms29_level_dropdown('marks[157]', $array[157] ?? ''); ?></td>
                        <td><?php ms29_level_dropdown('marks[158]', $array[158] ?? ''); ?></td>
                    </tr>

                    <tr>
                        <td class="ms29-skill-text">Can comprehend and extract relevant information using reading skills</td>
                        <td><?php ms29_level_dropdown('marks[159]', $array[159] ?? ''); ?></td>
                        <td><?php ms29_level_dropdown('marks[160]', $array[160] ?? ''); ?></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 ms29_level_dropdown('marks[161]', $array[161] ?? ''); ?></td>
                        <td><?php ms29_level_dropdown('marks[162]', $array[162] ?? ''); ?></td>
                    </tr>

                    <tr>
                        <td class="ms29-skill-text">Remember written and spoken information.</td>
                        <td><?php ms29_level_dropdown('marks[163]', $array[163] ?? ''); ?></td>
                        <td><?php ms29_level_dropdown('marks[164]', $array[164] ?? ''); ?></td>
                    </tr>

                    <tr>
                        <td class="ms29-skill-text">Able to write grammatically correct sentences.</td>
                        <td><?php ms29_level_dropdown('marks[165]', $array[165] ?? ''); ?></td>
                        <td><?php ms29_level_dropdown('marks[166]', $array[166] ?? ''); ?></td>
                    </tr>

                    <tr>
                        <td class="ms29-skill-text">Is able to explain text well.</td>
                        <td><?php ms29_level_dropdown('marks[167]', $array[167] ?? ''); ?></td>
                        <td><?php ms29_level_dropdown('marks[168]', $array[168] ?? ''); ?></td>
                    </tr>

                    <tr>
                        <td class="ms29-skill-text">Can comprehend and extract relevant information using reading skills</td>
                        <td><?php ms29_level_dropdown('marks[169]', $array[169] ?? ''); ?></td>
                        <td><?php ms29_level_dropdown('marks[170]', $array[170] ?? ''); ?></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 ms29_level_dropdown('marks[171]', $array[171] ?? ''); ?></td>
                        <td><?php ms29_level_dropdown('marks[172]', $array[172] ?? ''); ?></td>
                    </tr>

                    <tr>
                        <td class="ms29-skill-text">Remember written and spoken information.</td>
                        <td><?php ms29_level_dropdown('marks[173]', $array[173] ?? ''); ?></td>
                        <td><?php ms29_level_dropdown('marks[174]', $array[174] ?? ''); ?></td>
                    </tr>

                    <tr>
                        <td class="ms29-skill-text">Able to write grammatically correct sentences.</td>
                        <td><?php ms29_level_dropdown('marks[175]', $array[175] ?? ''); ?></td>
                        <td><?php ms29_level_dropdown('marks[176]', $array[176] ?? ''); ?></td>
                    </tr>

                    <tr>
                        <td class="ms29-skill-text">Is able to explain text well.</td>
                        <td><?php ms29_level_dropdown('marks[177]', $array[177] ?? ''); ?></td>
                        <td><?php ms29_level_dropdown('marks[178]', $array[178] ?? ''); ?></td>
                    </tr>

                    <tr>
                        <td class="ms29-skill-text">Can comprehend and extract relevant information using reading skills</td>
                        <td><?php ms29_level_dropdown('marks[179]', $array[179] ?? ''); ?></td>
                        <td><?php ms29_level_dropdown('marks[180]', $array[180] ?? ''); ?></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 ms29_level_dropdown('marks[181]', $array[181] ?? ''); ?></td>
                        <td><?php ms29_level_dropdown('marks[182]', $array[182] ?? ''); ?></td>
                    </tr>

                    <tr>
                        <td class="ms29-skill-text">Able to do accurate calculation.</td>
                        <td><?php ms29_level_dropdown('marks[183]', $array[183] ?? ''); ?></td>
                        <td><?php ms29_level_dropdown('marks[184]', $array[184] ?? ''); ?></td>
                    </tr>

                    <tr>
                        <td class="ms29-skill-text">Ability to think conceptually.</td>
                        <td><?php ms29_level_dropdown('marks[185]', $array[185] ?? ''); ?></td>
                        <td><?php ms29_level_dropdown('marks[186]', $array[186] ?? ''); ?></td>
                    </tr>

                    <tr>
                        <td class="ms29-skill-text">Application of the concept in the real life situation</td>
                        <td><?php ms29_level_dropdown('marks[187]', $array[187] ?? ''); ?></td>
                        <td><?php ms29_level_dropdown('marks[188]', $array[188] ?? ''); ?></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">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">Is Curious to discover facts</td>
                            <td><?php ms29_level_dropdown('marks[189]', $array[189] ?? ''); ?></td>
                            <td><?php ms29_level_dropdown('marks[190]', $array[190] ?? ''); ?></td>
                        </tr>

                        <tr>
                            <td class="ms29-skill-text">Able to analyze and interpret experiments and demonstration </td>
                            <td><?php ms29_level_dropdown('marks[191]', $array[191] ?? ''); ?></td>
                            <td><?php ms29_level_dropdown('marks[192]', $array[192] ?? ''); ?></td>
                        </tr>

                        <tr>
                            <td class="ms29-skill-text">Can draw conclusion from what he observes.</td>
                            <td><?php ms29_level_dropdown('marks[193]', $array[193] ?? ''); ?></td>
                            <td><?php ms29_level_dropdown('marks[194]', $array[194] ?? ''); ?></td>
                        </tr>

                    </table>


                    <!-- SOCIAL STUDIES-->
                    <table class="ms29-rounded-table">
                        <tr>
                            <th class="ms29-left-head">SOCIAL STUDIES</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 interest in knowing facts </td>
                            <td><?php ms29_level_dropdown('marks[195]', $array[195] ?? ''); ?></td>
                            <td><?php ms29_level_dropdown('marks[196]', $array[196] ?? ''); ?></td>
                        </tr>

                        <tr>
                            <td class="ms29-skill-text">Enthusiastically participates in all activities </td>
                            <td><?php ms29_level_dropdown('marks[197]', $array[197] ?? ''); ?></td>
                            <td><?php ms29_level_dropdown('marks[198]', $array[198] ?? ''); ?></td>
                        </tr>

                        <tr>
                            <td class="ms29-skill-text">Like interpretation and case studies.</td>
                            <td><?php ms29_level_dropdown('marks[199]', $array[199] ?? ''); ?></td>
                            <td><?php ms29_level_dropdown('marks[200]', $array[200] ?? ''); ?></td>
                        </tr>

                        <tr>
                            <td class="ms29-skill-text">Enjoy doing map work. accurately and neatly.</td>
                            <td><?php ms29_level_dropdown('marks[201]', $array[201] ?? ''); ?></td>
                            <td><?php ms29_level_dropdown('marks[202]', $array[202] ?? ''); ?></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 ms29_level_dropdown('marks[203]', $array[203] ?? ''); ?></td>
                            <td><?php ms29_level_dropdown('marks[204]', $array[204] ?? ''); ?></td>
                        </tr>

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

                        <tr>
                            <td class="ms29-skill-text">Able to do creative work and designs documents well.</td>
                            <td><?php ms29_level_dropdown('marks[207]', $array[207] ?? ''); ?></td>
                            <td><?php ms29_level_dropdown('marks[208]', $array[208] ?? ''); ?></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 ms29_level_dropdown('marks[209]', $array[209] ?? ''); ?></td>
                            <td><?php ms29_level_dropdown('marks[210]', $array[210] ?? ''); ?></td>
                        </tr>

                        <tr>
                            <td class="ms29-skill-text">Participation in Art and Craft work.</td>
                            <td><?php ms29_level_dropdown('marks[211]', $array[211] ?? ''); ?></td>
                            <td><?php ms29_level_dropdown('marks[212]', $array[212] ?? ''); ?></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 ms29_level_dropdown('marks[213]', $array[213] ?? ''); ?></td>
                            <td><?php ms29_level_dropdown('marks[214]', $array[214] ?? ''); ?></td>
                        </tr>

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

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

                        <tr>
                            <td class="ms29-skill-text">Understands and follow instruction.</td>
                            <td><?php ms29_level_dropdown('marks[219]', $array[219] ?? ''); ?></td>
                            <td><?php ms29_level_dropdown('marks[220]', $array[220] ?? ''); ?></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 ms29_level_dropdown('marks[221]', $array[221] ?? ''); ?></td>
                            <td><?php ms29_level_dropdown('marks[222]', $array[222] ?? ''); ?></td>
                        </tr>

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

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

                        <tr>
                            <td class="ms29-skill-text">Is compatible with other dancers.</td>
                            <td><?php ms29_level_dropdown('marks[227]', $array[227] ?? ''); ?></td>
                            <td><?php ms29_level_dropdown('marks[228]', $array[228] ?? ''); ?></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 ms29_level_dropdown('marks[229]', $array[229] ?? ''); ?></td>
                            <td><?php ms29_level_dropdown('marks[230]', $array[230] ?? ''); ?></td>
                        </tr>

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

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

                        <tr>
                            <td class="ms29-skill-text">Likes to keep the environment clean.</td>
                            <td><?php ms29_level_dropdown('marks[235]', $array[235] ?? ''); ?></td>
                            <td><?php ms29_level_dropdown('marks[236]', $array[236] ?? ''); ?></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 ms29_level_dropdown('marks[237]', $array[237] ?? ''); ?></td>
                            <td><?php ms29_level_dropdown('marks[238]', $array[238] ?? ''); ?></td>
                        </tr>

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

                        <tr>
                            <td class="ms29-skill-text">Can deal with distractions.</td>
                            <td><?php ms29_level_dropdown('marks[241]', $array[241] ?? ''); ?></td>
                            <td><?php ms29_level_dropdown('marks[242]', $array[242] ?? ''); ?></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 ms29_level_dropdown('marks[243]', $array[243] ?? ''); ?></td>
                            <td><?php ms29_level_dropdown('marks[244]', $array[244] ?? ''); ?></td>
                        </tr>

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

                        <tr>
                            <td class="ms29-skill-text">Accomplishes small task well.</td>
                            <td><?php ms29_level_dropdown('marks[247]', $array[247] ?? ''); ?></td>
                            <td><?php ms29_level_dropdown('marks[248]', $array[248] ?? ''); ?></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 ms29_level_dropdown('marks[249]', $array[249] ?? ''); ?></td>
                            <td><?php ms29_level_dropdown('marks[250]', $array[250] ?? ''); ?></td>
                        </tr>

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

                        <tr>
                            <td class="ms29-skill-text">Can think critically and objectively.</td>
                            <td><?php ms29_level_dropdown('marks[253]', $array[253] ?? ''); ?></td>
                            <td><?php ms29_level_dropdown('marks[254]', $array[254] ?? ''); ?></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/template31/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>