<?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/template30/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'];

# Get full student info
$stmt = $conn_rjllearning_specific->prepare(
    "SELECT * FROM students WHERE institution_id = ? AND id = ?"
);
$stmt->execute([$institution_id, $student_id]);
$Resultstudents = $stmt->fetch(PDO::FETCH_ASSOC);

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

$branch_id  = $Resultstudents['branch_id'];

# Check if marksheet is locked (using branch_id)
$lock_sql = "SELECT class_id, section_id, student_list
             FROM lock_marksheet_cache 
             WHERE institution_id = ? 
             AND branch_id = ?
             AND marksheet_id = ?";

$lock_stmt = $conn_rjllearning_specific->prepare($lock_sql);
$lock_stmt->execute([$institution_id, $branch_id, $marksheet_id]);

$lock_rows = $lock_stmt->fetchAll(PDO::FETCH_ASSOC);

$locked_found = false;

if (!empty($lock_rows)) {

    foreach ($lock_rows as $lock_row) {

        $student_list = json_decode($lock_row['student_list'], true);

        if (in_array((string)$student_id, $student_list)) {

            # Student found in locked list
            $class_id   = $lock_row['class_id'];
            $section_id = $lock_row['section_id'];

            $locked_found = true;
            break;
        }
    }

    if (!$locked_found) {
        errorreturn("Student not allowed. Marksheet is locked.");
    }

} else {

    # Use student's actual class/section
    $class_id   = $Resultstudents['class_id'];
    $section_id = $Resultstudents['section_id'];
}

# Get class and section info
$class_info   = getClass($conn_rjllearning_specific, $institution_id, $class_id);
$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/template30/style_template30.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">

                        <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 ================= -->
                                        <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>

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

                                                    <th>PT-4<br>40</th>
                                                    <th>Ass.<br>5</th>
                                                    <th>N.B.<br>5</th>
                                                </tr>
                                            </thead>

                                            <tbody>
                                                <!-- ROW 1 -->
                                                <tr>
                                                    <td>English</td>
                                                    <td><input type="number" min="0" step="any" name="marks[0]" max="40"
                                                            value="<?= $array[0] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[1]" max="5"
                                                            value="<?= $array[1] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[2]" max="5"
                                                            value="<?= $array[2] ?? '' ?>"></td>
                                                    <input type="hidden" name="marks[3]" value="">

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

                                                <!-- ROW 2 -->
                                                <tr>
                                                    <td>Hindi</td>
                                                    <td><input type="number" min="0" step="any" name="marks[10]"
                                                            max="40" value="<?= $array[10] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[11]" max="5"
                                                            value="<?= $array[11] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[12]" max="5"
                                                            value="<?= $array[12] ?? '' ?>"></td>
                                                    <input type="hidden" name="marks[13]" value="">

                                                    <td><input type="number" min="0" step="any" name="marks[14]"
                                                            max="40" value="<?= $array[14] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[15]" max="5"
                                                            value="<?= $array[15] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[16]" max="5"
                                                            value="<?= $array[16] ?? '' ?>"></td>
                                                    <input type="hidden" name="marks[17]" value="">
                                                    <input type="hidden" name="marks[18]" value="">
                                                    <input type="hidden" name="marks[19]" value="">

                                                </tr>

                                                <!-- ROW 3 -->
                                                <tr>
                                                    <td>Punjabi</td>
                                                    <td><input type="number" min="0" step="any" name="marks[20]"
                                                            max="40" value="<?= $array[20] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[21]" max="5"
                                                            value="<?= $array[21] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[22]" max="5"
                                                            value="<?= $array[22] ?? '' ?>"></td>
                                                    <input type="hidden" name="marks[23]" value="">

                                                    <td><input type="number" min="0" step="any" name="marks[24]"
                                                            max="40" value="<?= $array[24] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[25]" max="5"
                                                            value="<?= $array[25] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[26]" max="5"
                                                            value="<?= $array[26] ?? '' ?>"></td>
                                                    <input type="hidden" name="marks[27]" value="">
                                                    <input type="hidden" name="marks[28]" value="">
                                                    <input type="hidden" name="marks[29]" value="">

                                                </tr>

                                                <!-- ROW 4 -->
                                                <tr>
                                                    <td>Maths</td>
                                                    <td><input type="number" min="0" step="any" name="marks[30]"
                                                            max="40" value="<?= $array[30] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[31]" max="5"
                                                            value="<?= $array[31] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[32]" max="5"
                                                            value="<?= $array[32] ?? '' ?>"></td>
                                                    <input type="hidden" name="marks[33]" value="">


                                                    <td><input type="number" min="0" step="any" name="marks[34]"
                                                            max="40" value="<?= $array[34] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[35]" max="5"
                                                            value="<?= $array[35] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[36]" max="5"
                                                            value="<?= $array[36] ?? '' ?>"></td>
                                                    <input type="hidden" name="marks[37]" value="">
                                                    <input type="hidden" name="marks[38]" value="">
                                                    <input type="hidden" name="marks[39]" value="">

                                                </tr>

                                                <!-- ROW 5 -->
                                                <tr>
                                                    <td>EVS</td>
                                                    <td><input type="number" min="0" step="any" name="marks[40]"
                                                            max="40" value="<?= $array[40] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[41]" max="5"
                                                            value="<?= $array[41] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[42]" max="5"
                                                            value="<?= $array[42] ?? '' ?>"></td>
                                                    <input type="hidden" name="marks[43]" value="">


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

                                                </tr>

                                                <!-- ROW 6 -->
                                                <tr>
                                                    <td>Drawing</td>
                                                    <td><input type="number" min="0" step="any" name="marks[50]"
                                                            max="40" value="<?= $array[50] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[51]" max="5"
                                                            value="<?= $array[51] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[52]" max="5"
                                                            value="<?= $array[52] ?? '' ?>"></td>
                                                    <input type="hidden" name="marks[53]" value="">


                                                    <td><input type="number" min="0" step="any" name="marks[54]"
                                                            max="40" value="<?= $array[54] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[55]" max="5"
                                                            value="<?= $array[55] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[56]" max="5"
                                                            value="<?= $array[56] ?? '' ?>"></td>
                                                    <input type="hidden" name="marks[57]" value="">
                                                    <input type="hidden" name="marks[58]" value="">
                                                    <input type="hidden" name="marks[59]" value="">

                                                </tr>

                                                <!-- ROW 7 -->
                                                <tr>
                                                    <td>Computer</td>
                                                    <td><input type="number" min="0" step="any" name="marks[60]"
                                                            max="40" value="<?= $array[60] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[61]" max="5"
                                                            value="<?= $array[61] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[62]" max="5"
                                                            value="<?= $array[62] ?? '' ?>"></td>
                                                    <input type="hidden" name="marks[63]" value="">


                                                    <td><input type="number" min="0" step="any" name="marks[64]"
                                                            max="40" value="<?= $array[64] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[65]" max="5"
                                                            value="<?= $array[65] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[66]" max="5"
                                                            value="<?= $array[66] ?? '' ?>"></td>
                                                    <input type="hidden" name="marks[67]" value="">
                                                    <input type="hidden" name="marks[68]" value="">
                                                    <input type="hidden" name="marks[69]" value="">

                                                </tr>

                                                <!-- ROW 8 -->
                                                <tr>
                                                    <td>GK</td>
                                                    <td><input type="number" min="0" step="any" name="marks[70]"
                                                            max="40" value="<?= $array[70] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[71]" max="5"
                                                            value="<?= $array[71] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[72]" max="5"
                                                            value="<?= $array[72] ?? '' ?>"></td>
                                                    <input type="hidden" name="marks[73]" value="">


                                                    <td><input type="number" min="0" step="any" name="marks[74]"
                                                            max="40" value="<?= $array[74] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[75]" max="5"
                                                            value="<?= $array[75] ?? '' ?>"></td>
                                                    <td><input type="number" min="0" step="any" name="marks[76]" max="5"
                                                            value="<?= $array[76] ?? '' ?>"></td>
                                                    <input type="hidden" name="marks[77]" value="">
                                                    <input type="hidden" name="marks[78]" value="">
                                                    <input type="hidden" name="marks[79]" value="">

                                                </tr>

                                                <!-- ROW 9 -->
                                                <!--<tr>-->
                                                <!--    <td></td>-->
                                                <!--    <td><input type="number" min="0" step="any" name="marks[80]" max="40" value="<?= $array[80] ?? '' ?>"></td>-->
                                                <!--    <td><input type="number" min="0" step="any" name="marks[81]" max="5" value="<?= $array[81] ?? '' ?>"></td>-->
                                                <!--    <td><input type="number" min="0" step="any" name="marks[82]" max="5" value="<?= $array[82] ?? '' ?>"></td>-->
                                                <!--    <input type="hidden" name="marks[83]" value="">-->


                                                <!--    <td><input type="number" min="0" step="any" name="marks[84]" max="40" 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="5" value="<?= $array[86] ?? '' ?>"></td>-->
                                                <!--    <input type="hidden" name="marks[87]" value="">-->
                                                <!--    <input type="hidden" name="marks[88]" value="">-->
                                                <!--    <input type="hidden" name="marks[89]" value="">-->

                                                <!--</tr>-->

                                                <!-- ROW 10 -->
                                                <!--<tr>-->
                                                <!--    <td></td>-->
                                                <!--    <td><input type="number" min="0" step="any" name="marks[90]" max="40" 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="5" value="<?= $array[92] ?? '' ?>"></td>-->
                                                <!--    <input type="hidden" name="marks[93]" value="">-->


                                                <!--    <td><input type="number" min="0" step="any" name="marks[94]" max="40" value="<?= $array[94] ?? '' ?>"></td>-->
                                                <!--    <td><input type="number" min="0" step="any" name="marks[95]" max="5" value="<?= $array[95] ?? '' ?>"></td>-->
                                                <!--    <td><input type="number" min="0" step="any" name="marks[96]" max="5" value="<?= $array[96] ?? '' ?>"></td>-->
                                                <!--    <input type="hidden" name="marks[97]" value="">-->
                                                <!--    <input type="hidden" name="marks[98]" value="">-->
                                                <!--    <input type="hidden" name="marks[99]" value="">-->

                                                <!--</tr>-->
                                            </tbody>

                                        </table>

                                        <!-- ================= PARENTAL SUPPORT ================= -->
                                        <div class="ms29-section-title">PARENTAL SUPPORT</div>

                                        <table class="ms29-support-table">
                                            <tr>
                                                <td>Uniform</td>
                                                <td><?php ms30_grade_dropdown('marks[100]', $array[100] ?? ''); ?></td>
                                                <td><?php ms30_grade_dropdown('marks[101]', $array[101] ?? ''); ?></td>
                                            </tr>

                                            <tr>
                                                <td>Circular Follow Up</td>
                                                <td><?php ms30_grade_dropdown('marks[102]', $array[102] ?? ''); ?></td>
                                                <td><?php ms30_grade_dropdown('marks[103]', $array[103] ?? ''); ?></td>
                                            </tr>

                                            <tr>
                                                <td>Home Work / Act. Submission</td>
                                                <td><?php ms30_grade_dropdown('marks[104]', $array[104] ?? ''); ?></td>
                                                <td><?php ms30_grade_dropdown('marks[105]', $array[105] ?? ''); ?></td>
                                            </tr>

                                            <tr>
                                                <td>PTM / Attendance</td>
                                                <td><?php ms30_grade_dropdown('marks[106]', $array[106] ?? ''); ?></td>
                                                <td><?php ms30_grade_dropdown('marks[107]', $array[107] ?? ''); ?></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[]">-->
                                                <!--<option value="">Select</option>-->
                                                <td><?php ms30_grade_dropdown('marks[108]', $array[108] ?? ''); ?></td>
                                                <!--<option value="A" <?= (($array[108] ?? '') === 'A') ? 'selected' : '' ?>>A - Very Supportive</option>-->
                                                <!--<option value="B" <?= (($array[108] ?? '') === 'B') ? 'selected' : '' ?>>B - Show Support in Some Areas</option>-->
                                                <!--<option value="C" <?= (($array[108] ?? '') === 'C') ? 'selected' : '' ?>>C - Need to be involved</option>-->
                                                <!--</select>-->
                                                <!--</td>-->
                                                <td><?php ms30_grade_dropdown('marks[109]', $array[109] ?? ''); ?></td>
                                            </tr>

                                            <tr>
                                                <td>Punctuality</td>
                                                <td><?php ms30_grade_dropdown('marks[110]', $array[110] ?? ''); ?></td>
                                                <td><?php ms30_grade_dropdown('marks[111]', $array[111] ?? ''); ?></td>
                                            </tr>

                                            <tr>
                                                <td>Value System</td>
                                                <td><?php ms30_grade_dropdown('marks[112]', $array[112] ?? ''); ?></td>
                                                <td><?php ms30_grade_dropdown('marks[113]', $array[113] ?? ''); ?></td>
                                            </tr>

                                            <tr>
                                                <td>Self-confidence</td>
                                                <td><?php ms30_grade_dropdown('marks[114]', $array[114] ?? ''); ?></td>
                                                <td><?php ms30_grade_dropdown('marks[115]', $array[115] ?? ''); ?></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[116]"><?= $array[116] ?? '' ?></textarea>
                                            </div>
                                        </div>

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


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



                            <div class="accordion-item">
                                <button type="button" class="accordion-header">
                                    📘 Periodic 1 + Term 1 (Half Yearly)
                                </button>
                                <div class="accordion-body">
                                    
                                    <div style="margin-bottom:10px;">
                                            <label>
                                                <input type="checkbox" class="lt-toggle" data-periodic="p1">
                                                Late Admission – Periodic 1
                                            </label>
                                        
                                            <label style="margin-left:15px;">
                                                <input type="checkbox" class="lt-toggle" data-periodic="p2">
                                                Late Admission – Periodic 2
                                            </label>
                                        </div>

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

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

                                        <!-- ================= TERM I ================= -->
                                        <table class="ms29-term1-table">

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

                                            <tr class="ms29-term-header">
                                                <th>Subject</th>
                                                <th>PT-1<br>40</th>
                                                <th>Ass.<br>5</th>
                                                <th>N.B.<br>5</th>
                                                <th>PT-2<br>40</th>
                                                <th>Ass.<br>5</th>
                                                <th>N.B.<br>5</th>
                                            </tr>

                                            <!-- English -->
                                            <tr>
                                                <td class="ms29-subject-name">English</td>
                                                <td><input type="number" min="0" step="any" name="marks[118]" max="40"
                                                        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="5"
                                                        value="<?= $array[120] ?? '' ?>"></td>
                                                <input type="hidden" name="marks[121]" value="">

                                                <td><input type="number" min="0" step="any" name="marks[122]" max="40"
                                                        value="<?= $array[122] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[123]" max="5"
                                                        value="<?= $array[123] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[124]" max="5"
                                                        value="<?= $array[124] ?? '' ?>"></td>
                                                <input type="hidden" name="marks[125]" value="">
                                            </tr>


                                            <!-- Hindi -->
                                            <tr>
                                                <td class="ms29-subject-name">Hindi</td>
                                                <td><input type="number" min="0" step="any" name="marks[126]" max="40"
                                                        value="<?= $array[126] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[127]" max="5"
                                                        value="<?= $array[127] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[128]" max="5"
                                                        value="<?= $array[128] ?? '' ?>"></td>
                                                <input type="hidden" name="marks[129]" value="">
                                                <td><input type="number" min="0" step="any" name="marks[130]" max="40"
                                                        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="5"
                                                        value="<?= $array[132] ?? '' ?>"></td>
                                                <input type="hidden" name="marks[133]" value="">
                                            </tr>

                                            <!-- Punjabi -->
                                            <tr>
                                                <td class="ms29-subject-name">Punjabi</td>
                                                <td><input type="number" min="0" step="any" name="marks[134]" max="40"
                                                        value="<?= $array[134] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[135]" max="5"
                                                        value="<?= $array[135] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[136]" max="5"
                                                        value="<?= $array[136] ?? '' ?>"></td>
                                                <input type="hidden" name="marks[137]" value="">
                                                <td><input type="number" min="0" step="any" name="marks[138]" max="40"
                                                        value="<?= $array[138] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[139]" max="5"
                                                        value="<?= $array[139] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[140]" max="5"
                                                        value="<?= $array[140] ?? '' ?>"></td>
                                                <input type="hidden" name="marks[141]" value="">
                                            </tr>

                                            <!-- Maths -->
                                            <tr>
                                                <td class="ms29-subject-name">Maths</td>
                                                <td><input type="number" min="0" step="any" name="marks[142]" max="40"
                                                        value="<?= $array[142] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[143]" max="5"
                                                        value="<?= $array[143] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[144]" max="5"
                                                        value="<?= $array[144] ?? '' ?>"></td>
                                                <input type="hidden" name="marks[145]" value="">
                                                <td><input type="number" min="0" step="any" name="marks[146]" max="40"
                                                        value="<?= $array[146] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[147]" max="5"
                                                        value="<?= $array[147] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[148]" max="5"
                                                        value="<?= $array[148] ?? '' ?>"></td>
                                                <input type="hidden" name="marks[149]" value="">
                                            </tr>


                                            <!-- EVS -->
                                            <tr>
                                                <td class="ms29-subject-name">EVS</td>
                                                <td><input type="number" min="0" step="any" name="marks[150]" max="40"
                                                        value="<?= $array[150] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[151]" max="5"
                                                        value="<?= $array[151] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[152]" max="5"
                                                        value="<?= $array[152] ?? '' ?>"></td>
                                                <input type="hidden" name="marks[153]" value="">
                                                <td><input type="number" min="0" step="any" name="marks[154]" max="40"
                                                        value="<?= $array[154] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[155]" max="5"
                                                        value="<?= $array[155] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[156]" max="5"
                                                        value="<?= $array[156] ?? '' ?>"></td>
                                                <input type="hidden" name="marks[157]" value="">
                                            </tr>

                                            <!-- G. Total -->
                                            <!--<tr>-->
                                            <!--    <td class="ms29-subject-name">G. Total</td>-->
                                            <!--    <td><input type="number" name="marks[]" max="40" value="<?= $array[158] ?? '' ?>"></td>-->
                                            <!--    <td><input type="number" name="marks[]" max="5"  value="<?= $array[159] ?? '' ?>"></td>-->
                                            <!--    <td><input type="number" name="marks[]" max="5"  value="<?= $array[160] ?? '' ?>"></td>-->
                                            <!--    <td><input type="number" name="marks[]" max="50" value="<?= $array[161] ?? '' ?>"></td>-->
                                            <!--    <td><input type="number" name="marks[]" max="40" value="<?= $array[162] ?? '' ?>"></td>-->
                                            <!--    <td><input type="number" name="marks[]" max="5"  value="<?= $array[163] ?? '' ?>"></td>-->
                                            <!--    <td><input type="number" name="marks[]" max="5"  value="<?= $array[164] ?? '' ?>"></td>-->
                                            <!--    <td><input type="number" name="marks[]" max="50" value="<?= $array[165] ?? '' ?>"></td>-->
                                            <!--</tr>-->

                                            <input type="hidden" name="marks[158]" value="">
                                            <input type="hidden" name="marks[159]" value="">
                                            <input type="hidden" name="marks[160]" value="">
                                            <input type="hidden" name="marks[161]" value="">
                                            <input type="hidden" name="marks[162]" value="">
                                            <input type="hidden" name="marks[163]" value="">
                                            <input type="hidden" name="marks[164]" value="">
                                            <input type="hidden" name="marks[165]" value="">

                                            <!-- Drawing -->
                                            <tr>
                                                <td class="ms29-subject-name">Drawing</td>
                                                <td><input type="number" min="0" step="any" name="marks[166]" max="40"
                                                        value="<?= $array[166] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[167]" max="5"
                                                        value="<?= $array[167] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[168]" max="5"
                                                        value="<?= $array[168] ?? '' ?>"></td>
                                                <input type="hidden" name="marks[169]" value="">
                                                <td><input type="number" min="0" step="any" name="marks[170]" max="40"
                                                        value="<?= $array[170] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[171]" max="5"
                                                        value="<?= $array[171] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[172]" max="5"
                                                        value="<?= $array[172] ?? '' ?>"></td>
                                                <input type="hidden" name="marks[173]" value="">
                                            </tr>

                                            <!-- Computer -->
                                            <tr>
                                                <td class="ms29-subject-name">Computer</td>
                                                <td><input type="number" min="0" step="any" name="marks[174]" max="40"
                                                        value="<?= $array[174] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[175]" max="5"
                                                        value="<?= $array[175] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[176]" max="5"
                                                        value="<?= $array[176] ?? '' ?>"></td>
                                                <input type="hidden" name="marks[177]" value="">
                                                <td><input type="number" min="0" step="any" name="marks[178]" max="40"
                                                        value="<?= $array[178] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[179]" max="5"
                                                        value="<?= $array[179] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[180]" max="5"
                                                        value="<?= $array[180] ?? '' ?>"></td>
                                                <input type="hidden" name="marks[181]" value="">
                                            </tr>


                                            <!-- G.K. / Apt -->
                                            <tr>
                                                <td class="ms29-subject-name">G.K. / Apt</td>
                                                <td><input type="number" min="0" step="any" name="marks[182]" max="40"
                                                        value="<?= $array[182] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[183]" max="5"
                                                        value="<?= $array[183] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[184]" max="5"
                                                        value="<?= $array[184] ?? '' ?>"></td>
                                                <input type="hidden" name="marks[185]" value="">
                                                <td><input type="number" min="0" step="any" name="marks[186]" max="40"
                                                        value="<?= $array[186] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[187]" max="5"
                                                        value="<?= $array[187] ?? '' ?>"></td>
                                                <td><input type="number" min="0" step="any" name="marks[188]" max="5"
                                                        value="<?= $array[188] ?? '' ?>"></td>
                                                <input type="hidden" name="marks[189]" value="">
                                            </tr>

                                            <!--Percentage -->
                                            <!--<tr class="ms29-percentage-row">-->
                                            <!--    <td class="ms29-subject-name">Percentage</td>-->
                                            <!--    <td colspan="8">-->
                                            <!--        <input type="text" name="marks[]" value="<?= $array[190] ?? '' ?>">-->
                                            <!--    </td>-->
                                            <!--</tr>-->


                                        </table>





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

                    </div>

                    <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[191]" value="<?= $array[191] ?? '' ?>"></td>
                                <td class="ms29-left-cell">Weight</td>
                                <td><input type="text" name="marks[192]" value="<?= $array[192] ?? '' ?>"></td>
                            </tr>
                            <tr>
                                <td class="ms29-left-cell">Blood Group</td>
                                <td colspan="3">
                                    <input type="text" name="marks[193]" value="<?= $array[193] ?? '' ?>">
                                </td>
                            </tr>
                        </table>

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

                        <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[194]" value="<?= $array[194] ?? '' ?>">
                                </td>
                                <td><input type="number" id="wd_t2" name="marks[195]" value="<?= $array[195] ?? '' ?>">
                                </td>
                                <td><input type="number" id="wd_total" name="marks[196]"
                                        value="<?= $array[196] ?? '' ?>"></td>
                            </tr>

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

                            <tr>
                                <td class="ms29-left-cell">Attendance %</td>
                                <td><input type="text" id="per_t1" name="marks[200]" value="<?= $array[200] ?? '' ?>">
                                </td>
                                <td><input type="text" id="per_t2" name="marks[201]" value="<?= $array[201] ?? '' ?>">
                                </td>
                                <td><input type="text" id="per_total" name="marks[202]"
                                        value="<?= $array[202] ?? '' ?>"></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[203]"><?= $array[203] ?? '' ?></textarea>
                                </td>
                            </tr>
                            <tr>
                                <td class="ms29-competition-box">
                                    <textarea name="marks[204]"><?= $array[204] ?? '' ?></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[205]"><?= $array[205] ?? '' ?></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 ms30_level_dropdown('marks[206]', $array[206] ?? ''); ?></td>
                                <td><?php ms30_level_dropdown('marks[207]', $array[207] ?? ''); ?></td>
                            </tr>
                            <tr>
                                <td class="ms29-skill-text">Remember written and spoken information.</td>
                                <td><?php ms30_level_dropdown('marks[208]', $array[208] ?? ''); ?></td>
                                <td><?php ms30_level_dropdown('marks[209]', $array[209] ?? ''); ?></td>
                            </tr>
                            <tr>
                                <td class="ms29-skill-text">Able to write grammatically correct sentences.</td>
                                <td><?php ms30_level_dropdown('marks[210]', $array[210] ?? ''); ?></td>
                                <td><?php ms30_level_dropdown('marks[211]', $array[211] ?? ''); ?></td>
                            </tr>
                            <tr>
                                <td class="ms29-skill-text">Is able to explain text well.</td>
                                <td><?php ms30_level_dropdown('marks[212]', $array[212] ?? ''); ?></td>
                                <td><?php ms30_level_dropdown('marks[213]', $array[213] ?? ''); ?></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 ms30_level_dropdown('marks[214]', $array[214] ?? ''); ?></td>
                                <td><?php ms30_level_dropdown('marks[215]', $array[215] ?? ''); ?></td>
                            </tr>
                            <tr>
                                <td class="ms29-skill-text">Remember written and spoken information.</td>
                                <td><?php ms30_level_dropdown('marks[216]', $array[216] ?? ''); ?></td>
                                <td><?php ms30_level_dropdown('marks[217]', $array[217] ?? ''); ?></td>
                            </tr>
                            <tr>
                                <td class="ms29-skill-text">Able to write grammatically correct sentences.</td>
                                <td><?php ms30_level_dropdown('marks[218]', $array[218] ?? ''); ?></td>
                                <td><?php ms30_level_dropdown('marks[219]', $array[219] ?? ''); ?></td>
                            </tr>
                            <tr>
                                <td class="ms29-skill-text">Is able to explain text well.</td>
                                <td><?php ms30_level_dropdown('marks[220]', $array[220] ?? ''); ?></td>
                                <td><?php ms30_level_dropdown('marks[221]', $array[221] ?? ''); ?></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 ms30_level_dropdown('marks[222]', $array[222] ?? ''); ?></td>
                                <td><?php ms30_level_dropdown('marks[223]', $array[223] ?? ''); ?></td>
                            </tr>
                            <tr>
                                <td class="ms29-skill-text">Remember written and spoken information.</td>
                                <td><?php ms30_level_dropdown('marks[224]', $array[224] ?? ''); ?></td>
                                <td><?php ms30_level_dropdown('marks[225]', $array[225] ?? ''); ?></td>
                            </tr>
                            <tr>
                                <td class="ms29-skill-text">Able to write grammatically correct sentences.</td>
                                <td><?php ms30_level_dropdown('marks[226]', $array[226] ?? ''); ?></td>
                                <td><?php ms30_level_dropdown('marks[227]', $array[227] ?? ''); ?></td>
                            </tr>
                            <tr>
                                <td class="ms29-skill-text">Is able to explain text well.</td>
                                <td><?php ms30_level_dropdown('marks[228]', $array[228] ?? ''); ?></td>
                                <td><?php ms30_level_dropdown('marks[229]', $array[229] ?? ''); ?></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 ms30_level_dropdown('marks[230]', $array[230] ?? ''); ?></td>
                                <td><?php ms30_level_dropdown('marks[231]', $array[231] ?? ''); ?></td>
                            </tr>
                            <tr>
                                <td class="ms29-skill-text">Able to do accurate calculation.</td>
                                <td><?php ms30_level_dropdown('marks[232]', $array[232] ?? ''); ?></td>
                                <td><?php ms30_level_dropdown('marks[233]', $array[233] ?? ''); ?></td>
                            </tr>
                            <tr>
                                <td class="ms29-skill-text">Ability to think conceptually.</td>
                                <td><?php ms30_level_dropdown('marks[234]', $array[234] ?? ''); ?></td>
                                <td><?php ms30_level_dropdown('marks[235]', $array[235] ?? ''); ?></td>
                            </tr>
                            <tr>
                                <td class="ms29-skill-text">Discern logical or numeric patterns.</td>
                                <td><?php ms30_level_dropdown('marks[236]', $array[236] ?? ''); ?></td>
                                <td><?php ms30_level_dropdown('marks[237]', $array[237] ?? ''); ?></td>
                            </tr>
                        </table>


                    </div>




                    <div class="ms29-wrapper">

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

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

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

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

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

                                <tr>
                                    <td class="ms29-skill-text">Comprehensive understanding of diversity.</td>
                                    <td><?php ms30_level_dropdown('marks[244]', $array[244] ?? ''); ?></td>
                                    <td><?php ms30_level_dropdown('marks[245]', $array[245] ?? ''); ?></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 ms30_level_dropdown('marks[246]', $array[246] ?? ''); ?></td>
                                    <td><?php ms30_level_dropdown('marks[247]', $array[247] ?? ''); ?></td>
                                </tr>

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

                                <tr>
                                    <td class="ms29-skill-text">Able to do creative work and designs documents well.
                                    </td>
                                    <td><?php ms30_level_dropdown('marks[250]', $array[250] ?? ''); ?></td>
                                    <td><?php ms30_level_dropdown('marks[251]', $array[251] ?? ''); ?></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 ms30_level_dropdown('marks[252]', $array[252] ?? ''); ?></td>
                                    <td><?php ms30_level_dropdown('marks[253]', $array[253] ?? ''); ?></td>
                                </tr>

                                <tr>
                                    <td class="ms29-skill-text">Participation in Art and Craft work.</td>
                                    <td><?php ms30_level_dropdown('marks[254]', $array[254] ?? ''); ?></td>
                                    <td><?php ms30_level_dropdown('marks[255]', $array[255] ?? ''); ?></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 ms30_level_dropdown('marks[256]', $array[256] ?? ''); ?></td>
                                    <td><?php ms30_level_dropdown('marks[257]', $array[257] ?? ''); ?></td>
                                </tr>

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

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

                                <tr>
                                    <td class="ms29-skill-text">Understands and follow instruction.</td>
                                    <td><?php ms30_level_dropdown('marks[262]', $array[262] ?? ''); ?></td>
                                    <td><?php ms30_level_dropdown('marks[263]', $array[263] ?? ''); ?></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 ms30_level_dropdown('marks[264]', $array[264] ?? ''); ?></td>
                                    <td><?php ms30_level_dropdown('marks[265]', $array[265] ?? ''); ?></td>
                                </tr>

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

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

                                <tr>
                                    <td class="ms29-skill-text">Is compatible with other dancers.</td>
                                    <td><?php ms30_level_dropdown('marks[270]', $array[270] ?? ''); ?></td>
                                    <td><?php ms30_level_dropdown('marks[271]', $array[271] ?? ''); ?></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 ms30_level_dropdown('marks[272]', $array[272] ?? ''); ?></td>
                                    <td><?php ms30_level_dropdown('marks[273]', $array[273] ?? ''); ?></td>
                                </tr>

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

                                <tr>
                                    <td class="ms29-skill-text">Has ability to adapt a changing environment.</td>
                                    <td><?php ms30_level_dropdown('marks[276]', $array[276] ?? ''); ?></td>
                                    <td><?php ms30_level_dropdown('marks[277]', $array[277] ?? ''); ?></td>
                                </tr>

                                <tr>
                                    <td class="ms29-skill-text">Likes to keep the environment clean.</td>
                                    <td><?php ms30_level_dropdown('marks[278]', $array[278] ?? ''); ?></td>
                                    <td><?php ms30_level_dropdown('marks[279]', $array[279] ?? ''); ?></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 ms30_level_dropdown('marks[280]', $array[280] ?? ''); ?></td>
                                    <td><?php ms30_level_dropdown('marks[281]', $array[281] ?? ''); ?></td>
                                </tr>

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

                                <tr>
                                    <td class="ms29-skill-text">Can deal with distractions.</td>
                                    <td><?php ms30_level_dropdown('marks[284]', $array[284] ?? ''); ?></td>
                                    <td><?php ms30_level_dropdown('marks[285]', $array[285] ?? ''); ?></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 ms30_level_dropdown('marks[286]', $array[286] ?? ''); ?></td>
                                    <td><?php ms30_level_dropdown('marks[287]', $array[287] ?? ''); ?></td>
                                </tr>

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

                                <tr>
                                    <td class="ms29-skill-text">Accomplishes small task well.</td>
                                    <td><?php ms30_level_dropdown('marks[290]', $array[290] ?? ''); ?></td>
                                    <td><?php ms30_level_dropdown('marks[291]', $array[291] ?? ''); ?></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 ms30_level_dropdown('marks[292]', $array[292] ?? ''); ?></td>
                                    <td><?php ms30_level_dropdown('marks[293]', $array[293] ?? ''); ?></td>
                                </tr>

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

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

        // Collapse ALL by default
        document.querySelectorAll('.accordion-item').forEach(item => {
            item.classList.remove('active');
        });

        // Toggle on click
        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/template30/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,1,2,
            10,11,12,
            20,21,22,
            30,31,32,
            40,41,42,
            50,51,52,
            60,61,62,
            70,71,72
        ],

        p4: [
            4,5,6,
            14,15,16,
            24,25,26,
            34,35,36,
            44,45,46,
            54,55,56,
            64,65,66,
            74,75,76
        ]
    };

    const LT_MAP_T1 = {
    p1: [
        118,119,120,
        126,127,128,
        134,135,136,
        142,143,144,
        150,151,152,
        166,167,168,
        174,175,176,
        182,183,184
    ],

    p2: [
        122,123,124,
        130,131,132,
        138,139,140,
        146,147,148,
        154,155,156,
        170,171,172,
        178,179,180,
        186,187,188
    ]
};


    /* ---------- APPLY LT ---------- */

    function applyLT(map, periodic, isChecked) {

        if(!map[periodic]) return;

        map[periodic].forEach(index => {

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

            // skip hidden fields
            if(input.attr("type") === "hidden") return;

            if (isChecked) {

                // store previous value
                input.data("prev", input.val());

                input.attr("type","text");
                input.val("LT");
                input.prop("readonly",true);
                input.addClass("lt-mark");

            } 
            else {

                // restore old value
                const prev = input.data("prev") || "";

                input.attr("type","number");
                input.val(prev);
                input.prop("readonly",false);
                input.removeClass("lt-mark");

            }

        });

    }


    /* ---------- EVENTS ---------- */

    $(document).on("change",".lt-toggle",function(){

        const periodic = $(this).data("periodic");

        if(periodic === "p1" || periodic === "p2"){
            applyLT(LT_MAP_T1, periodic, this.checked);
        }

        if(periodic === "p3" || periodic === "p4"){
            applyLT(LT_MAP_T2, periodic, this.checked);
        }

    });

});
</script>