<?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/template32/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/template32/style_template32.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-panel-header"></div>

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

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

                                        <!-- ================= TERM II MARKS ================= -->
                                        <!-- ✅ Optional Subject Selection -->
                                        <div class="ms29-subject-toggle" style="margin-bottom:10px; font-size:13px;">
                                            <strong>Optional Subjects:</strong>

                                            <!-- ✅ Hidden 0 will submit even if unchecked -->
                                            <input type="hidden" name="subject_enabled[hindi]" value="0">
                                            <label style="margin-right:10px;">
                                                <input type="checkbox" class="ms29-toggle-subject" data-target="row-hindi-t2" name="subject_enabled[hindi]" value="1" checked>
                                                Hindi
                                            </label>

                                            <input type="hidden" name="subject_enabled[evs]" value="0">
                                            <label style="margin-right:10px;">
                                                <input type="checkbox" class="ms29-toggle-subject" data-target="row-evs-t2" name="subject_enabled[evs]" value="1" checked>
                                                EVS (Oral)
                                            </label>

                                            <input type="hidden" name="subject_enabled[gk]" value="0">
                                            <label style="margin-right:10px;">
                                                <input type="checkbox" class="ms29-toggle-subject" data-target="row-gk-t2" name="subject_enabled[gk]" value="1" checked>
                                                GK & Rhymes
                                            </label>

                                            <input type="hidden" name="subject_enabled[drawing]" value="0">
                                            <label style="margin-right:10px;">
                                                <input type="checkbox" class="ms29-toggle-subject" data-target="row-drawing-t2" name="subject_enabled[drawing]" value="1" checked>
                                                Drawing
                                            </label>
                                        </div>

                                        <div style="margin-bottom:10px;">
                                            <label>
                                                <input type="checkbox" class="lt-toggle" data-periodic="p3">
                                                Late Admission – Periodic 3
                                            </label>
                                        
                                            <label style="margin-left:15px;">
                                                <input type="checkbox" class="lt-toggle" data-periodic="p4">
                                                Late Admission – Periodic 4
                                            </label>
                                        </div>
                                        <table class="ms29-marks-table">
                                            <thead>
                                                <tr>
                                                    <th colspan="4" class="ms29-term-head">TERM - II </th>
                                                </tr>
                                                <tr>
                                                    <th colspan="2">Subject</th>
                                                    <th>Periodic -3</th>
                                                    <th>Periodic -4</th>
                                                </tr>
                                            </thead>

                                            <tbody>

                                                <!-- ✅ English (ALWAYS ON) -->
                                                <tr>
                                                    <td rowspan="2" class="ms29-subject-name">English</td>
                                                    <td>Oral</td>
                                                    <td><input type="number" min="0" step="any" name="marks[0]" min="0" max="30" value="<?= $array[0] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[1]" min="0" max="30" value="<?= $array[1] ?? '' ?>"></td>
                                                </tr>
                                                <tr>
                                                    <td>Written</td>
                                                    <td><input type="number" min="0" step="any" name="marks[2]" min="0" max="50" value="<?= $array[2] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[3]" min="0" max="50" value="<?= $array[3] ?? '' ?>"></td>
                                                </tr>

                                                <!-- ✅ Hindi (OPTIONAL) -->
                                                <tr class="row-hindi-t2">
                                                    <td rowspan="2" class="ms29-subject-name">Hindi</td>
                                                    <td>Oral</td>
                                                    <td><input type="number" min="0" step="any" name="marks[4]" min="0" max="30" value="<?= $array[4] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[5]" min="0" max="30" value="<?= $array[5] ?? '' ?>"></td>
                                                </tr>
                                                <tr class="row-hindi-t2">
                                                    <td>Written</td>
                                                    <td><input type="number" min="0" step="any" name="marks[6]" min="0" max="50" value="<?= $array[6] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[7]" min="0" max="50" value="<?= $array[7] ?? '' ?>"></td>
                                                </tr>

                                                <!-- ✅ Maths (ALWAYS ON) -->
                                                <tr>
                                                    <td rowspan="2" class="ms29-subject-name">Maths</td>
                                                    <td>Oral</td>
                                                    <td><input type="number" min="0" step="any" name="marks[8]" min="0" max="30" value="<?= $array[8] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[9]" min="0" max="30" value="<?= $array[9] ?? '' ?>"></td>
                                                </tr>
                                                <tr>
                                                    <td>Written</td>
                                                    <td><input type="number" min="0" step="any" name="marks[10]" min="0" max="50" value="<?= $array[10] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[11]" min="0" max="50" value="<?= $array[11] ?? '' ?>"></td>
                                                </tr>

                                                <!-- ✅ EVS (OPTIONAL) -->
                                                <tr class="row-evs-t2">
                                                    <td colspan="2" class="ms29-subject-name">EVS (Oral)</td>
                                                    <td><input type="number" min="0" step="any" name="marks[12]" min="0" max="30" value="<?= $array[12] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[13]" min="0" max="30" value="<?= $array[13] ?? '' ?>"></td>
                                                </tr>

                                                <!-- ✅ GK & Rhymes (OPTIONAL) -->
                                                <tr class="row-gk-t2">
                                                    <td colspan="2" class="ms29-subject-name">GK & Rhymes</td>
                                                    <td><input type="number" min="0" step="any" name="marks[14]" min="0" max="30" value="<?= $array[14] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[15]" min="0" max="30" value="<?= $array[15] ?? '' ?>"></td>
                                                </tr>

                                                <!-- ✅ Drawing (OPTIONAL) -->
                                                <tr class="row-drawing-t2">
                                                    <td colspan="2" class="ms29-subject-name">Drawing</td>
                                                    <td><input type="number" min="0" step="any" name="marks[16]" min="0" max="30" value="<?= $array[16] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[17]" min="0" max="30" value="<?= $array[17] ?? '' ?>"></td>
                                                </tr>
                                                <!-- ✅ Overall Grade (HIDDEN, NOT SHOWN IN TABLE) -->
                                                <input type="hidden" name="marks[18]" value="<?= $array[18] ?? '' ?>">
                                                <input type="hidden" name="marks[19]" value="<?= $array[19] ?? '' ?>">
                                            </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[20]">
                <option value="">Select</option>
                <option value="A" <?= (($array[20] ?? '') === 'A') ? 'selected' : '' ?>>A - Very Supportive</option>
                <option value="B" <?= (($array[20] ?? '') === 'B') ? 'selected' : '' ?>>B - Show Support in Some Areas</option>
                <option value="C" <?= (($array[20] ?? '') === 'C') ? 'selected' : '' ?>>C - Need to be involved</option>
            </select>
        </td>
        <td>
            <select name="marks[21]">
                <option value="">Select</option>
                <option value="A" <?= (($array[21] ?? '') === 'A') ? 'selected' : '' ?>>A - Very Supportive</option>
                <option value="B" <?= (($array[21] ?? '') === 'B') ? 'selected' : '' ?>>B - Show Support in Some Areas</option>
                <option value="C" <?= (($array[21] ?? '') === 'C') ? 'selected' : '' ?>>C - Need to be involved</option>
            </select>
        </td>
    </tr>

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

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

    <tr>
        <td>PTM / Attendance</td>
        <td>
            <select name="marks[26]">
                <option value="">Select</option>
                <option value="A" <?= (($array[26] ?? '') === 'A') ? 'selected' : '' ?>>A - Very Supportive</option>
                <option value="B" <?= (($array[26] ?? '') === 'B') ? 'selected' : '' ?>>B - Show Support in Some Areas</option>
                <option value="C" <?= (($array[26] ?? '') === 'C') ? 'selected' : '' ?>>C - Need to be involved</option>
            </select>
        </td>
        <td>
            <select name="marks[27]">
                <option value="">Select</option>
                <option value="A" <?= (($array[27] ?? '') === 'A') ? 'selected' : '' ?>>A - Very Supportive</option>
                <option value="B" <?= (($array[27] ?? '') === 'B') ? 'selected' : '' ?>>B - Show Support in Some Areas</option>
                <option value="C" <?= (($array[27] ?? '') === '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[28]">
                                                        <option value="">Select</option>
                                                        <option value="A" <?= (($array[28] ?? '') === 'A') ? 'selected' : '' ?>>A - Very Supportive</option>
                                                        <option value="B" <?= (($array[28] ?? '') === 'B') ? 'selected' : '' ?>>B - Show Support in Some Areas</option>
                                                        <option value="C" <?= (($array[28] ?? '') === 'C') ? 'selected' : '' ?>>C - Need to be involved</option>
                                                    </select>
                                                </td>
                                                <td>
                                                    <select name="marks[29]">
                                                        <option value="">Select</option>
                                                        <option value="A" <?= (($array[29] ?? '') === 'A') ? 'selected' : '' ?>>A - Very Supportive</option>
                                                        <option value="B" <?= (($array[29] ?? '') === 'B') ? 'selected' : '' ?>>B - Show Support in Some Areas</option>
                                                        <option value="C" <?= (($array[29] ?? '') === 'C') ? 'selected' : '' ?>>C - Need to be involved</option>
                                                    </select>
                                                </td>
                                            </tr>

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

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

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



                                        <!-- ================= PROMOTION ================= -->
                                        <div class="ms29-promotion-box">
                                            <strong>Promoted to Class:</strong>
                                            <span class="ms29-promotion-value">
                                                <select name="marks[37]">
                                                    <option value="na" <?= (($array[37] ?? '') === '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[37]) && $array[37] === $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">
                                📘 Periodic 1 + Term 1
                            </button>

                            <div class="accordion-body">

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

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

                                    <!-- ================= TERM I ================= -->
                                    <!-- ✅ Optional Subject Selection (TERM -1) -->
                                    <div class="ms29-subject-toggle" style="margin-bottom:10px; font-size:13px;">
                                        <strong>Optional Subjects:</strong>

                                        <label style="margin-right:10px;">
                                            <input type="checkbox" class="ms29-toggle-subject-t1" data-target="row-hindi-t1" checked>
                                            Hindi
                                        </label>

                                        <label style="margin-right:10px;">
                                            <input type="checkbox" class="ms29-toggle-subject-t1" data-target="row-evs-t1" checked>
                                            EVS (Oral)
                                        </label>

                                        <label style="margin-right:10px;">
                                            <input type="checkbox" class="ms29-toggle-subject-t1" data-target="row-gk-t1" checked>
                                            GK & Rhymes
                                        </label>

                                        <label style="margin-right:10px;">
                                            <input type="checkbox" class="ms29-toggle-subject-t1" data-target="row-drawing-t1" checked>
                                            Drawing
                                        </label>
                                    </div>
                                    
                                    <!-- ✅ Late Admission Toggle (TERM -1) -->
                            <div style="margin-bottom:10px; font-size:13px;">
                                <strong>Late Admission:</strong>
                            
                                <label style="margin-right:15px;">
                                    <input type="checkbox" class="lt-toggle-t1" data-periodic="p1">
                                    Periodic -1 (Late Admission)
                                </label>
                            
                                <label>
                                    <input type="checkbox" class="lt-toggle-t1" data-periodic="p2">
                                    Periodic -2 (Late Admission)
                                </label>
                            </div>


                                    <table class="ms29-marks-table">
                                        <thead>
                                            <tr>
                                                <th colspan="4" class="ms29-term-head">TERM -1 </th>
                                            </tr>
                                            <tr>
                                                <th colspan="2">Subject</th>
                                                <th>Periodic -1</th>
                                                <th>Periodic -2</th>
                                            </tr>
                                        </thead>

                                        <tbody>

                                            <!-- ✅ English (ALWAYS ON) -->
                                            <tr>
                                                <td rowspan="2" class="ms29-subject-name">English</td>
                                                <td>Oral</td>
                                                <td><input type="number" min="0" step="any" name="marks[38]" min="0" max="30" value="<?= $array[38] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[39]" min="0" max="30" value="<?= $array[39] ?? '' ?>"></td>
                                            </tr>
                                            <tr>
                                                <td>Written</td>
                                                <td><input type="number" min="0" step="any" name="marks[40]" min="0" max="50" value="<?= $array[40] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[41]" min="0" max="50" value="<?= $array[41] ?? '' ?>"></td>
                                            </tr>

                                            <!-- ✅ Hindi (OPTIONAL VISIBILITY ONLY) -->
                                            <tr class="row-hindi-t1">
                                                <td rowspan="2" class="ms29-subject-name">Hindi</td>
                                                <td>Oral</td>
                                                <td><input type="number" min="0" step="any" name="marks[42]" min="0" max="30" value="<?= $array[42] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[43]" min="0" max="30" value="<?= $array[43] ?? '' ?>"></td>
                                            </tr>
                                            <tr class="row-hindi-t1">
                                                <td>Written</td>
                                                <td><input type="number" min="0" step="any" name="marks[44]" min="0" max="50" value="<?= $array[44] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[45]" min="0" max="50" value="<?= $array[45] ?? '' ?>"></td>
                                            </tr>

                                            <!-- ✅ Maths (ALWAYS ON) -->
                                            <tr>
                                                <td rowspan="2" class="ms29-subject-name">Maths</td>
                                                <td>Oral</td>
                                                <td><input type="number" min="0" step="any" name="marks[46]" min="0" max="30" value="<?= $array[46] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[47]" min="0" max="30" value="<?= $array[47] ?? '' ?>"></td>
                                            </tr>
                                            <tr>
                                                <td>Written</td>
                                                <td><input type="number" min="0" step="any" name="marks[48]" min="0" max="50" value="<?= $array[48] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[49]" min="0" max="50" value="<?= $array[49] ?? '' ?>"></td>
                                            </tr>

                                            <!-- ✅ EVS (OPTIONAL VISIBILITY ONLY) -->
                                            <tr class="row-evs-t1">
                                                <td colspan="2" class="ms29-subject-name">EVS (Oral)</td>
                                                <td><input type="number" min="0" step="any" name="marks[50]" min="0" max="30" value="<?= $array[50] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[51]" min="0" max="30" value="<?= $array[51] ?? '' ?>"></td>
                                            </tr>

                                            <!-- ✅ GK & Rhymes (OPTIONAL VISIBILITY ONLY) -->
                                            <tr class="row-gk-t1">
                                                <td colspan="2" class="ms29-subject-name">GK & Rhymes</td>
                                                <td><input type="number" min="0" step="any" name="marks[52]" min="0" max="30" value="<?= $array[52] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[53]" min="0" max="30" value="<?= $array[53] ?? '' ?>"></td>
                                            </tr>

                                            <!-- ✅ Drawing (OPTIONAL VISIBILITY ONLY) -->
                                            <tr class="row-drawing-t1">
                                                <td colspan="2" class="ms29-subject-name">Drawing</td>
                                                <td><input type="number" min="0" step="any" name="marks[54]" min="0" max="30" value="<?= $array[54] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[55]" min="0" max="30" value="<?= $array[55] ?? '' ?>"></td>
                                            </tr>
                                            <!-- ✅ Overall Grade (HIDDEN, NOT SHOWN IN TABLE) -->
                                            <input type="hidden" name="marks[56]" value="<?= $array[56] ?? '' ?>">
                                            <input type="hidden" name="marks[57]" value="<?= $array[57] ?? '' ?>">
                                        </tbody>
                                    </table>






                                </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[58]" value="<?= $array[58] ?? '' ?>"></td>

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

                    <tr>
                        <td class="ms29-left-cell">Blood Group</td>
                        <td colspan="3">
                            <input type="text" name="marks[60]" value="<?= $array[60] ?? '' ?>">
                        </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[61]" value="<?= $array[61] ?? '' ?>"></td>
                        <td><input type="number" id="wd_t2" name="marks[62]" value="<?= $array[62] ?? '' ?>"></td>
                        <td><input type="number" id="wd_total" name="marks[63]" value="<?= $array[63] ?? '' ?>"></td>
                    </tr>

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

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

                    <tr>
                        <td class="ms29-competition-box">
                            <textarea name="marks[71]"><?= $array[71] ?? '' ?></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[72]"><?= $array[72] ?? '' ?></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. Verbal-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">Understands what he/she reads.</td>
                        <td><?php ms29_level_dropdown('marks[73]', $array[73] ?? ''); ?></td>
                        <td><?php ms29_level_dropdown('marks[74]', $array[74] ?? ''); ?></td>
                    </tr>

                    <tr>
                        <td class="ms29-skill-text">Can spell Correctly.</td>
                        <td><?php ms29_level_dropdown('marks[75]', $array[75] ?? ''); ?></td>
                        <td><?php ms29_level_dropdown('marks[76]', $array[76] ?? ''); ?></td>
                    </tr>

                    <tr>
                        <td class="ms29-skill-text">Has good listening skills</td>
                        <td><?php ms29_level_dropdown('marks[77]', $array[77] ?? ''); ?></td>
                        <td><?php ms29_level_dropdown('marks[78]', $array[78] ?? ''); ?></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 express himself clearly</td>
                        <td><?php ms29_level_dropdown('marks[79]', $array[79] ?? ''); ?></td>
                        <td><?php ms29_level_dropdown('marks[80]', $array[80] ?? ''); ?></td>
                    </tr>

                    <tr>
                        <td class="ms29-skill-text">Is able to read the text easily</td>
                        <td><?php ms29_level_dropdown('marks[81]', $array[81] ?? ''); ?></td>
                        <td><?php ms29_level_dropdown('marks[82]', $array[82] ?? ''); ?></td>
                    </tr>

                    <tr>
                        <td class="ms29-skill-text">Has good listening skills.</td>
                        <td><?php ms29_level_dropdown('marks[83]', $array[83] ?? ''); ?></td>
                        <td><?php ms29_level_dropdown('marks[84]', $array[84] ?? ''); ?></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 puzzles and mathematics activities</td>
                        <td><?php ms29_level_dropdown('marks[85]', $array[85] ?? ''); ?></td>
                        <td><?php ms29_level_dropdown('marks[86]', $array[86] ?? ''); ?></td>
                    </tr>

                    <tr>
                        <td class="ms29-skill-text">Likes working with numbers</td>
                        <td><?php ms29_level_dropdown('marks[87]', $array[87] ?? ''); ?></td>
                        <td><?php ms29_level_dropdown('marks[88]', $array[88] ?? ''); ?></td>
                    </tr>

                    <tr>
                        <td class="ms29-skill-text">Works systematically</td>
                        <td><?php ms29_level_dropdown('marks[89]', $array[89] ?? ''); ?></td>
                        <td><?php ms29_level_dropdown('marks[90]', $array[90] ?? ''); ?></td>
                    </tr>
                </table>

                <!-- ================= MUSICAL RHYTHMIC INTELLIGENCE ================= -->
                <div class="ms29-intel-title">3. MUSICAL RHYTHMIC INTELLIGENCE</div>

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

                    <tr>
                        <td class="ms29-skill-text">Participate in school Assembly</td>
                        <td><?php ms29_level_dropdown('marks[91]', $array[91] ?? ''); ?></td>
                        <td><?php ms29_level_dropdown('marks[92]', $array[92] ?? ''); ?></td>
                    </tr>

                    <tr>
                        <td class="ms29-skill-text">Learn and Recite Rhymes Well</td>
                        <td><?php ms29_level_dropdown('marks[93]', $array[93] ?? ''); ?></td>
                        <td><?php ms29_level_dropdown('marks[94]', $array[94] ?? ''); ?></td>
                    </tr>

                    <tr>
                        <td class="ms29-skill-text">Follow Dance Steps Well</td>
                        <td><?php ms29_level_dropdown('marks[95]', $array[95] ?? ''); ?></td>
                        <td><?php ms29_level_dropdown('marks[96]', $array[96] ?? ''); ?></td>
                    </tr>
                </table>

            </div>






            <div class="ms29-wrapper">

                <!-- ================= LEFT PAGE ================= -->
                <div class="ms29-page">
                    <div class="ms29-intel-title">4. BODILY KINESTHETICS INTELLIGENCE</div>

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

                        <tr>
                            <td class="ms29-skill-text">Participate in sports Day and Class games</td>
                            <td><?php ms29_level_dropdown('marks[97]', $array[97] ?? ''); ?></td>
                            <td><?php ms29_level_dropdown('marks[98]', $array[98] ?? ''); ?></td>
                        </tr>

                        <tr>
                            <td class="ms29-skill-text">Understand and Follow Instructions </td>
                            <td><?php ms29_level_dropdown('marks[99]', $array[99] ?? ''); ?></td>
                            <td><?php ms29_level_dropdown('marks[100]', $array[100] ?? ''); ?></td>
                        </tr>
                    </table>


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

                        <tr>
                            <td class="ms29-skill-text">Write neatly and legibly </td>
                            <td><?php ms29_level_dropdown('marks[101]', $array[101] ?? ''); ?></td>
                            <td><?php ms29_level_dropdown('marks[102]', $array[102] ?? ''); ?></td>
                        </tr>

                        <tr>
                            <td class="ms29-skill-text">Dresses well and its tidy </td>
                            <td><?php ms29_level_dropdown('marks[103]', $array[103] ?? ''); ?></td>
                            <td><?php ms29_level_dropdown('marks[104]', $array[104] ?? ''); ?></td>
                        </tr>
                    </table>


                    <!-- ✅ Correct title -->
                    <div class="ms29-intel-title">5. Visual - Spatial Intelligence</div>

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

                        <tr>
                            <td class="ms29-skill-text">Able to draw and colour picture.</td>
                            <td><?php ms29_level_dropdown('marks[105]', $array[105] ?? ''); ?></td>
                            <td><?php ms29_level_dropdown('marks[106]', $array[106] ?? ''); ?></td>
                        </tr>

                        <tr>
                            <td class="ms29-skill-text">Enjoying craft activities.</td>
                            <td><?php ms29_level_dropdown('marks[107]', $array[107] ?? ''); ?></td>
                            <td><?php ms29_level_dropdown('marks[108]', $array[108] ?? ''); ?></td>
                        </tr>

                        <tr>
                            <td class="ms29-skill-text">Able to learn through multimedia.</td>
                            <td><?php ms29_level_dropdown('marks[109]', $array[109] ?? ''); ?></td>
                            <td><?php ms29_level_dropdown('marks[110]', $array[110] ?? ''); ?></td>
                        </tr>
                    </table>


                    <div class="ms29-intel-title">6. 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">Is friendly and Co operative.</td>
                            <td><?php ms29_level_dropdown('marks[111]', $array[111] ?? ''); ?></td>
                            <td><?php ms29_level_dropdown('marks[112]', $array[112] ?? ''); ?></td>
                        </tr>

                        <tr>
                            <td class="ms29-skill-text">Listen to others.</td>
                            <td><?php ms29_level_dropdown('marks[113]', $array[113] ?? ''); ?></td>
                            <td><?php ms29_level_dropdown('marks[114]', $array[114] ?? ''); ?></td>
                        </tr>
                    </table>

                </div>

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

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

                    <div class="ms29-intel-title">7. 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">Can do things independently.</td>
                            <td><?php ms29_level_dropdown('marks[115]', $array[115] ?? ''); ?></td>
                            <td><?php ms29_level_dropdown('marks[116]', $array[116] ?? ''); ?></td>
                        </tr>

                        <tr>
                            <td class="ms29-skill-text">Complete task without being distracted.</td>
                            <td><?php ms29_level_dropdown('marks[117]', $array[117] ?? ''); ?></td>
                            <td><?php ms29_level_dropdown('marks[118]', $array[118] ?? ''); ?></td>
                        </tr>
                    </table>


                    <div class="ms29-intel-title">8. NATURAL INTELLIGENCE AWARENESS</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">Enjoy and exploring outdoor trips.</td>
                            <td><?php ms29_level_dropdown('marks[119]', $array[119] ?? ''); ?></td>
                            <td><?php ms29_level_dropdown('marks[120]', $array[120] ?? ''); ?></td>
                        </tr>

                        <tr>
                            <td class="ms29-skill-text">Show concern for environmental issues.</td>
                            <td><?php ms29_level_dropdown('marks[121]', $array[121] ?? ''); ?></td>
                            <td><?php ms29_level_dropdown('marks[122]', $array[122] ?? ''); ?></td>
                        </tr>
                    </table>


                    <div class="ms29-intel-title">9. EXISTENTIAL 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">Learn values taught in the class.</td>
                            <td><?php ms29_level_dropdown('marks[123]', $array[123] ?? ''); ?></td>
                            <td><?php ms29_level_dropdown('marks[124]', $array[124] ?? ''); ?></td>
                        </tr>

                        <tr>
                            <td class="ms29-skill-text">Meditates regularly and enjoy yoga practice.</td>
                            <td><?php ms29_level_dropdown('marks[125]', $array[125] ?? ''); ?></td>
                            <td><?php ms29_level_dropdown('marks[126]', $array[126] ?? ''); ?></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() {

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

        // Optional subject toggle
        document.querySelectorAll(".ms29-toggle-subject").forEach(cb => {
            cb.addEventListener("change", function() {
                document.querySelectorAll("." + this.dataset.target)
                    .forEach(row => row.style.display = this.checked ? "" : "none");
            });
        });

    });
</script>


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

        // ✅ TERM-II Optional Subject Hide/Show (DO NOT disable inputs)
        $(".ms29-toggle-subject").on("change", function() {
            const target = $(this).data("target");
            const isChecked = $(this).is(":checked");

            $("." + target).each(function() {
                if (isChecked) {
                    $(this).show();
                } else {
                    $(this).hide();
                }
            });
        });

        // ✅ Apply on load
        $(".ms29-toggle-subject").trigger("change");


        // ✅ TERM-1 Optional Subject Hide/Show (do NOT disable inputs)
        $(".ms29-toggle-subject-t1").on("change", function() {
            const target = $(this).data("target");
            const isChecked = $(this).is(":checked");

            $("." + target).each(function() {
                if (isChecked) {
                    $(this).show();
                } else {
                    $(this).hide();
                }
            });
        });

        // ✅ Apply on page load
        $(".ms29-toggle-subject-t1").trigger("change");

        // 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/template32/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>
<script>
$(document).ready(function () {

    /* ---------- INDEX MAPS ---------- */

    const LT_MAP_T2 = {
        p3: [0,2,4,6,8,10,12,14,16],
        p4: [1,3,5,7,9,11,13,15,17]
    };

    const LT_MAP_T1 = {
        p1: [38,40,42,44,46,48,50,52,54],
        p2: [39,41,43,45,47,49,51,53,55]
    };

    /* ---------- APPLY LT ---------- */
    function applyLT(map, periodic, isChecked) {

        map[periodic].forEach(index => {

            const input = $(`input[name="marks[${index}]"]`);

            if (isChecked) {
                // 🔥 IMPORTANT FIX
                input.attr("type", "text");
                input.val("LT");
                input.prop("readonly", true);
                input.addClass("lt-mark");
            } else {
                // revert back
                input.val("");
                input.attr("type", "number");
                input.prop("readonly", false);
                input.removeClass("lt-mark");
            }

        });
    }

    /* ---------- EVENTS ---------- */
    $(document).on("change", ".lt-toggle", function () {
        applyLT(LT_MAP_T2, $(this).data("periodic"), this.checked);
    });

    $(document).on("change", ".lt-toggle-t1", function () {
        applyLT(LT_MAP_T1, $(this).data("periodic"), this.checked);
    });

    /* ---------- AUTO DETECT ON LOAD ---------- */
    function autoDetectLT(map, checkboxClass) {
        Object.keys(map).forEach(periodic => {
            const first = $(`input[name="marks[${map[periodic][0]}]"]`);
            if (first.val() === "LT") {
                $(`.${checkboxClass}[data-periodic="${periodic}"]`)
                    .prop("checked", true)
                    .trigger("change");
            }
        });
    }

    autoDetectLT(LT_MAP_T2, "lt-toggle");
    autoDetectLT(LT_MAP_T1, "lt-toggle-t1");

});
</script>