<?php
// index served started as 1 for array
//fetching table of cols from marksheeet_data

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 . 'admin_api/modules/marksheet_template/template5/students_info.php';

// require_once $base_path_to_learning .'admin_api/modules/marksheet_template/template5/template5_marks_upload.php';

// Decode JSON input data
// $postdata = json_decode(file_get_contents("php://input"), true);

$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']) or
    !isset($postdata['api_signature']) or !checkIfIssetAndNotEmpty($postdata['api_signature']) or
    !isset($postdata['jwt_token']) or  !checkIfIssetAndNotEmpty($postdata['jwt_token'])
) {
    errorreturn("All * fields are required.");
}
validateApiSignature($postdata['api_signature']); // Authenticate JWT token
$jwttoken = $postdata['jwt_token'];
$jwt_validate = authenticate_jwt($jwttoken);

// Extract user details from JWT token
$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 and get specific database name
$specific_db_name = validateUserAccess($conn_rjllearning, $userid, $role, $workspace_id, ["superadmin", "admin", "principal", "teacher"], null);
if (!$specific_db_name) {
    errorreturn('Access denied.');
} else {
    $conn_rjllearning_specific = dynamicDbConnection($specific_db_name);
}
$api_signature = $postdata['api_signature'];
$institution_id = $postdata['institution_id'];
$student_id = $postdata['student_id'];
$marksheet_id = $postdata['marksheet_id'];


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

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

$branch_id = $Resultstudents['branch_id'];

$class_id = $Resultstudents['class_id'];

$section_id = $Resultstudents['section_id'];
// Prepare the SQL statement

$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);

$ExecuteSqlTemplate = $conn_rjllearning_specific->prepare("SELECT examination_name,template_id,academic_session 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'];


// Fetching 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]);

// Fetch the result as an associative array
$fetchmarksheet_data = $stmt->fetch(PDO::FETCH_ASSOC);

if ($fetchmarksheet_data) {
    // Unserialize the 'data' column
    $arrayfetch = json_decode($fetchmarksheet_data['data'], true);

    // Check the number of columns in the fetched data
    if (count($arrayfetch) != $cols) {
        // Deleting any pre-existing entries
        $deleteExistingData = $conn_rjllearning_specific->prepare("DELETE FROM student_marksheet_data WHERE student_id = ? AND marksheet_id = ?");
        if (!$deleteExistingData->execute([$student_id, $marksheet_id])) {
            errorreturn("Failed to delete existing data.");
        }
        for ($i = 1; $i <= $cols; $i++) {
            $array[$i] = 0;
        }
        $array[17] = "";
        $array[18] = "";
    } else {
        $array = array();
        $counter = 1;
        foreach ($arrayfetch as $element) {
            $array[$counter] = $element;
            $counter++;
        }
    }
} else {
    $deleteExistingData = $conn_rjllearning_specific->prepare("DELETE FROM student_marksheet_data WHERE student_id = ? AND marksheet_id = ?");
    if (!$deleteExistingData->execute([$student_id, $marksheet_id])) {
        errorreturn("Failed to delete existing data.");
    }
    for ($i = 1; $i <= $cols; $i++) {
        $array[$i] = 0;
    }
    $array[17] = "";
    $array[18] = "";
}
?>
<!doctype html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Progress Report - Garden Valley International School</title>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js" integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600&display=swap');
        
        :root {
            --primary-color: #4a90e2;
            --secondary-color: #2c3e50;
            --background-color: #f0f4f8;
            --card-background: #ffffff;
            --border-color: #d1d9e6;
            --accent-color-1: #ff6b6b;
            --accent-color-2: #feca57;
            --accent-color-3: #54a0ff;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Poppins', sans-serif;
            line-height: 1.3;
            color: var(--secondary-color);
            background-color: var(--background-color);
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            padding: 5px;
        }

        .container {
            background-color: var(--card-background);
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
            width: 210mm;
            height: auto;
            border: 1px solid var(--primary-color);
            position: relative;
            display: flex;
            flex-direction: column;
        }

        .header {
            text-align: center;
            margin-bottom: 15px;
            border-bottom: 2px solid var(--primary-color);
            padding-bottom: 10px;
            background: linear-gradient(45deg, var(--accent-color-1), var(--accent-color-2), var(--accent-color-3));
            color: white;
            padding: 15px;
            border-radius: 6px 6px 0 0;
        }

        .logo {
            max-width: 80px;
            margin-bottom: 10px;
            background-color: aliceblue;
            border: 2px solid white;
        }

        h1 {
            color: white;
            margin-bottom: 5px;
            font-size: 1.8em;
            font-weight: 600;
            text-shadow: 1px 1px 2px rgba(0,0,0,0.3);
        }

        .school-info {
            font-size: 0.9em;
            color: white;
            opacity: 0.9;
        }

        .report-title {
            font-size: 1.5em;
            font-weight: 600;
            text-align: center;
            margin: 15px 0 10px;
            color: var(--primary-color);
            text-transform: uppercase;
            border-bottom: 2px solid var(--accent-color-2);
            padding-bottom: 5px;
        }

        .student-info {
            display: flex;
            justify-content: space-between;
            margin-bottom: 15px;
            font-size: 0.95em;
            border: 2px solid var(--accent-color-3);
            padding: 12px;
            border-radius: 6px;
            background-color: #f8f9fa;
        }

        .student-info div {
            flex: 1;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 15px;
            font-size: 0.95em;
            border: 2px solid var(--primary-color);
        }

        th, td {
            padding: 10px;
            text-align: left;
            border: 1px solid var(--border-color);
        }

        th {
            background-color: var(--primary-color);
            color: white;
            font-weight: 600;
        }

        tr:nth-child(even) {
            background-color: #f2f6f9;
        }

        .remarks, .signatures {
            margin-top: 15px;
            font-size: 0.95em;
        }

        .remarks {
            border: 2px solid var(--accent-color-1);
            padding: 12px;
            border-radius: 6px;
            background-color: #fff5f5;
        }

        .remarks h3, .signatures h3 {
            color: var(--accent-color-1);
            margin-bottom: 8px;
            font-size: 1.1em;
        }

        .signatures {
            display: flex;
            justify-content: space-between;
            margin-top: 25px;
        }

        .signature-line {
            border-top: 2px solid var(--primary-color);
            width: 150px;
            margin-top: 20px;
        }

        .date {
            margin-top: 15px;
            font-style: italic;
            font-size: 0.95em;
            text-align: right;
        }

        .footer {
            margin-top: auto;
            padding-top: 10px;
            border-top: 2px solid var(--accent-color-2);
            font-size: 0.9em;
            color: var(--secondary-color);
            text-align: center;
        }

        .forms_cp_inputtext {
            width: 100%;
            padding: 5px;
            border: 1px solid var(--border-color);
            border-radius: 4px;
        }

        .text-danger {
            color: var(--accent-color-1);
        }

        .text-capitalise {
            text-transform: capitalize;
        }

        .texttransformupper {
            text-transform: uppercase;
        }

        .mt-2 {
            margin-top: 0.5rem;
        }

        .form-control {
            display: block;
            width: 100%;
            padding: 0.375rem 0.75rem;
            font-size: 1rem;
            line-height: 1.5;
            color: #495057;
            background-color: #fff;
            background-clip: padding-box;
            border: 1px solid #ced4da;
            border-radius: 0.25rem;
            transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
        }

        #submit_marksheet {
            background-color: var(--primary-color);
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 1em;
            margin-top: 20px;
        }

        #submit_marksheet:hover {
            background-color: #3a7bd5;
        }

        #error_div {
            color: var(--accent-color-1);
            padding: 10px;
            margin-top: 10px;
            border: 1px solid var(--accent-color-1);
            border-radius: 4px;
            background-color: #fff5f5;
        }

        #displayadd_homework_success {
            color: #28a745;
            padding: 10px;
            margin-top: 10px;
            border: 1px solid #28a745;
            border-radius: 4px;
            background-color: #f0fff4;
        }

        @media print {
            body {
                background-color: white;
                margin: 0;
                padding: 0;
            }
            .container {
                box-shadow: none;
                border: none;
                width: 210mm;
                height: auto;
                page-break-after: always;
            }
            @page {
                size: A4;
                margin: 0;
            }
            #submit_marksheet {
                display: none;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <header class="header">
            <img src="<?php echo $institutions['logo']; ?>" alt="School Logo" class="logo">
            <h1><?php echo $institutions['institution_name']; ?></h1>
            <div class="school-info">
                <p><?php echo $branch['branch_code']; ?> | <?php echo $branch['address']; 
                    if ($branch['pincode'] != "") {
                        echo " - " . $branch['pincode'];
                    } ?> | <?php echo $branch['contact_number']; 
                    if ($branch['email'] != "") {
                        echo " | " . strtolower($branch['email']);
                    } ?></p>
            </div>
        </header>

        <div class="report-title">Progress Report</div>
        <p style="text-align: center; margin-bottom: 15px; font-size: 0.95em; color: var(--accent-color-3);">Academic Session <?php echo $academic_session; ?></p>

        <div class="student-info">
            <div>
                <p><strong>Name:</strong> <?php echo $Resultstudents['name']; ?></p>
                <p><strong>Class:</strong> <?php echo $class_info['class_name']; ?></p>
                <p><strong>Father's Name:</strong> <?php echo $Resultstudents['fathername']; ?></p>
            </div>
            <div>
                <p><strong>Admission Number:</strong> <?php echo $Resultstudents['admission_number']; ?></p>
                <p><strong>Section:</strong> <?php echo $section_info['section_name']; ?></p>
                <p><strong>Mother's Name:</strong> <?php echo $Resultstudents['mothername']; ?></p>
            </div>
            <div>
                <p><strong>Height:</strong> -</p>
                <p><strong>Weight:</strong> -</p>
                <p><strong>D.O.B:</strong> <?php echo $Resultstudents['dob']; ?></p>
            </div>
        </div>

        <form name="upload_student_marksheet" id="upload_student_marksheet">
            <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 $jwttoken; ?>">
            <input type="hidden" name="api_signature" value="<?php echo $api_signature; ?>">
            
            <table id="template_table_edit">
                <?php $temp_count = 1; ?>
                <tr>
                    <td>Height</td>
                    <td><input type="text" class="forms_cp_inputtext texttransformupper" name="<?php echo $temp_count; ?>" value="<?php echo $array[$temp_count]; ?>" maxlength="10" required></td>
                </tr>
                <tr>
                    <td>Weight</td>
                    <td><input type="text" class="forms_cp_inputtext texttransformupper" name="<?php echo ++$temp_count; ?>" value="<?php echo $array[$temp_count]; ?>" maxlength="10" required></td>
                </tr>
                <tr>
                    <th>ASSESSMENTS</th>
                    <th class="">GRADE</th>
                </tr>

                <tr>
                    <th class="text-danger">ENGLISH</th>
                    <th></th>
                </tr>
                <tr>
                    <td>Can write nearly</td>
                    <td><input type="text" class="forms_cp_inputtext texttransformupper" name="<?php echo ++$temp_count; ?>" value="<?php echo $array[$temp_count]; ?>" maxlength="3" required></td>
                </tr>
                <tr>
                    <td>Can narrate stories effectively</td>
                    <td><input type="text" class="forms_cp_inputtext texttransformupper" name="<?php echo ++$temp_count; ?>" value="<?php echo $array[$temp_count]; ?>" maxlength="3" required></td>
                </tr>
                <tr>
                    <td>Can recite poems with expressions</td>
                    <td><input type="text" class="forms_cp_inputtext texttransformupper" name="<?php echo ++$temp_count; ?>" value="<?php echo $array[$temp_count]; ?>" maxlength="3" required></td>
                </tr>
                <tr>
                    <td>Can recognize and read properly</td>
                    <td><input type="text" class="forms_cp_inputtext texttransformupper" name="<?php echo ++$temp_count; ?>" value="<?php echo $array[$temp_count]; ?>" maxlength="3" required></td>
                </tr>

                <tr>
                    <th class="text-danger">MATHEMATICS</th>
                    <th></th>
                </tr>
                <tr>
                    <td>Can identify the numbers correctly</td>
                    <td><input type="text" class="forms_cp_inputtext texttransformupper" name="<?php echo ++$temp_count; ?>" value="<?php echo $array[$temp_count]; ?>" maxlength="3" required></td>
                </tr>
                <tr>
                    <td>Can grasp the combination of numbers</td>
                    <td><input type="text" class="forms_cp_inputtext texttransformupper" name="<?php echo ++$temp_count; ?>" value="<?php echo $array[$temp_count]; ?>" maxlength="3" required></td>
                </tr>
                <tr>
                    <td>Can count and write in sequence</td>
                    <td><input type="text" class="forms_cp_inputtext texttransformupper" name="<?php echo ++$temp_count; ?>" value="<?php echo $array[$temp_count]; ?>" maxlength="3" required></td>
                </tr>

                <tr>
                    <th class="text-danger">PUNJABI</th>
                    <th></th>
                </tr>
                <tr>
                    <td>Can express the word</td>
                    <td><input type="text" class="forms_cp_inputtext texttransformupper" name="<?php echo ++$temp_count; ?>" value="<?php echo $array[$temp_count]; ?>" maxlength="3" required></td>
                </tr>
                <tr>
                    <td>Can write properly</td>
                    <td><input type="text" class="forms_cp_inputtext texttransformupper" name="<?php echo ++$temp_count; ?>" value="<?php echo $array[$temp_count]; ?>" maxlength="3" required></td>
                </tr>

                <tr>
                    <th class="text-danger">E.V.S. / GENERAL KNOWLEDGE</th>
                    <th></th>
                </tr>
                <tr>
                    <td>Can identify</td>
                    <td><input type="text" class="forms_cp_inputtext texttransformupper" name="<?php echo ++$temp_count; ?>" value="<?php echo $array[$temp_count]; ?>" maxlength="3" required></td>
                </tr>
                <tr>
                    <td>Can write properly</td>
                    <td><input type="text" class="forms_cp_inputtext texttransformupper" name="<?php echo ++$temp_count; ?>" value="<?php echo $array[$temp_count]; ?>" maxlength="3" required></td>
                </tr>
                <tr>
                    <td>Can express / understand the word / pictures</td>
                    <td><input type="text" class="forms_cp_inputtext texttransformupper" name="<?php echo ++$temp_count; ?>" value="<?php echo $array[$temp_count]; ?>" maxlength="3" required></td>
                </tr>

                <tr>
                    <th class="text-danger">ART & CRAFT</th>
                    <th></th>
                </tr>
                <tr>
                    <td>Enjoys drawing, colouring & pasting</td>
                    <td><input type="text" class="forms_cp_inputtext texttransformupper" name="<?php echo ++$temp_count; ?>" value="<?php echo $array[$temp_count]; ?>" maxlength="3" required></td>
                </tr>
                <tr>
                    <td>Follows instructions carefully</td>
                    <td><input type="text" class="forms_cp_inputtext texttransformupper" name="<?php echo ++$temp_count; ?>" value="<?php echo $array[$temp_count]; ?>" maxlength="3" required></td>
                </tr>

                <tr>
                    <th>ATTENDANCE</th>
                    <th><input type="text" class="forms_cp_inputtext texttransformupper" name="<?php echo ++$temp_count; ?>" value="<?php echo $array[$temp_count]; ?>" maxlength="3" required></th>
                </tr>
                <tr>
                    <th colspan="2">GENERAL REMARKS: <br>
                        <select class="form-control mt-2" id="remarks_marksheet" required name="<?php echo ++$temp_count; ?>">
                            <?php if ($array[$temp_count] == "") { ?>
                                <option disabled selected value="">Select</option>
                            <?php } else { ?>
                                <option value="<?php echo $array[$temp_count]; ?>" selected><?php echo $array[$temp_count]; ?></option>
                            <?php } ?>
                            <option value="Excellent">Excellent</option>
                            <option value="Good">Good</option>
                            <option value="Keep it up">Keep it up</option>
                            <option value="Dedicated">Dedicated</option>
                            <option value="Worked hard">Worked hard</option>
                            <option value="Capable of achieving a higher grade">Capable of achieving a higher grade</option>
                            <option value="Completes his/her work carefully and on time">Completes his/her work carefully and on time</option>
                            <option value="Good conceptual clarity">Good conceptual clarity</option>
                            <option value="Needs to focus a little more on concepts">Needs to focus a little more on concepts</option>
                        </select>
                    </th>
                </tr>
            </table>

            <table>
                <tr>
                    <th>COMPETENCIES</th>
                    <th class="">GRADE</th>
                </tr>

                <tr>
                    <th class="text-danger">PHYSICAL DEVELOPMENT</th>
                    <th></th>
                </tr>

                <tr>
                    <th class="text-capitalise">PART-I (HEALTHY & SAFE HABITS)</th>
                    <th></th>
                </tr>

                <tr>
                    <td>Shows a liking for and understanding of nutritious food and does not waste food</td>
                    <td><input type="text" class="forms_cp_inputtext texttransformupper" name="<?php echo ++$temp_count; ?>" value="<?php echo $array[$temp_count]; ?>" maxlength="3" required></td>
                </tr>

                <tr>
                    <td>Practices basic self- care and hygiene</td>
                    <td><input type="text" class="forms_cp_inputtext texttransformupper" name="<?php echo ++$temp_count; ?>" value="<?php echo $array[$temp_count]; ?>" maxlength="3" required></td>
                </tr>

                <tr>
                    <th class="text-capitalise">PART-II (SENSORIAL PERCEPTIONS)</th>
                    <th></th>
                </tr>
                <tr>
                    <td>Differentiates between shapes, colours and their shades</td>
                    <td><input type="text" class="forms_cp_inputtext texttransformupper" name="<?php echo ++$temp_count; ?>" value="<?php echo $array[$temp_count]; ?>" maxlength="3" required></td>
                </tr>
                <tr>
                    <td>Differentiates multiple smells and tastes</td>
                    <td><input type="text" class="forms_cp_inputtext texttransformupper" name="<?php echo ++$temp_count; ?>" value="<?php echo $array[$temp_count]; ?>" maxlength="3" required></td>
                </tr>
                <tr>
                    <td>Develops discriminations in the sense of touch</td>
                    <td><input type="text" class="forms_cp_inputtext texttransformupper" name="<?php echo ++$temp_count; ?>" value="<?php echo $array[$temp_count]; ?>" maxlength="3" required></td>
                </tr>

                <tr>
                    <th class="text-capitalise">PART-III (PHYSICAL FITNESS)</th>
                    <th></th>
                </tr>
                <tr>
                    <td>Shows balance , coordination and flexibility in various physical activities</td>
                    <td><input type="text" class="forms_cp_inputtext texttransformupper" name="<?php echo ++$temp_count; ?>" value="<?php echo $array[$temp_count]; ?>" maxlength="3" required></td>
                </tr>
                <tr>
                    <td>Shows precision and control in working with their hands and fingers</td>
                    <td><input type="text" class="forms_cp_inputtext texttransformupper" name="<?php echo ++$temp_count; ?>" value="<?php echo $array[$temp_count]; ?>" maxlength="3" required></td>
                </tr>
                <tr>
                    <td>Shows strength and endurance in carrying, walking and running</td>
                    <td><input type="text" class="forms_cp_inputtext texttransformupper" name="<?php echo ++$temp_count; ?>" value="<?php echo $array[$temp_count]; ?>" maxlength="3" required></td>
                </tr>

                <tr>
                    <th class="text-danger">SOCIO-EMOTIONAL & ETHICAL DEVELOPMENT</th>
                    <th></th>
                </tr>

                <tr>
                    <th class="text-capitalise">PART-I (EMOTIONAL INTELLIGENCE)</th>
                    <th></th>
                </tr>
                <tr>
                    <td>Recognises 'self' as an individual belong to a family and community</td>
                    <td><input type="text" class="forms_cp_inputtext texttransformupper" name="<?php echo ++$temp_count; ?>" value="<?php echo $array[$temp_count]; ?>" maxlength="3" required></td>
                </tr>
                <tr>
                    <td>Recognises different emotions and makes deliberate efforts to regulate them</td>
                    <td><input type="text" class="forms_cp_inputtext texttransformupper" name="<?php echo ++$temp_count; ?>" value="<?php echo $array[$temp_count]; ?>" maxlength="3" required></td>
                </tr>
                <tr>
                    <td>Interacts comfortably with other children and adults</td>
                    <td><input type="text" class="forms_cp_inputtext texttransformupper" name="<?php echo ++$temp_count; ?>" value="<?php echo $array[$temp_count]; ?>" maxlength="3" required></td>
                </tr>
                <tr>
                    <td>Shows kindness and helpfulness to others</td>
                    <td><input type="text" class="forms_cp_inputtext texttransformupper" name="<?php echo ++$temp_count; ?>" value="<?php echo $array[$temp_count]; ?>" maxlength="3" required></td>
                </tr>
            </table>

            <button type="submit" id="submit_marksheet">Upload Marksheet</button>
        </form>

        <div id="error_div" style="display:none;">
            <span id="error_message"></span>
        </div>
        <div id="displayadd_homework_success" style="display:none;">
            <span id="success_message"></span>
        </div>

        <div class="signatures">
            <div>
                <h3>Class Incharge</h3>
                <div class="signature-line"></div>
            </div>
            <div>
                <h3>Principal / Headmistress</h3>
                <div class="signature-line"></div>
            </div>
            <div>
                <h3>Parent</h3>
                <div class="signature-line"></div>
            </div>
        </div>

        <footer class="footer">
            <p>* In case of any discrepancies in marksheet, please contact school administration.</p>
        </footer>
    </div>
</body>
</html>
<script>
    $(document).ready(function() {
        $("#upload_student_marksheet").on('submit', function(e) {
            e.preventDefault(); // Prevent the default form submission

            $.ajax({
                type: 'POST',
                url: 'https://rejoicebilldesk.in/rjllearning_development/resources/marksheet_template/template3/process.php', // Directly using the URL
                data: new FormData(this), // Capture all form data including file uploads
                contentType: false,
                cache: false,
                processData: false,
                beforeSend: function() {
                    // Disable form fields and the submit button
                    $(".columns_number").attr('readonly', true);
                    $("#remarks_marksheet").attr('readonly', true);
                    $("#attendance_marksheet").attr('readonly', true);
                    $("#submit_marksheet").prop('disabled', true); // Ensure you're targeting the correct button

                    // Append loader to the submit button
                    $("#submit_marksheet").html('<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> Uploading...');
                },
                error: function(xhr, status, error) {
                    // Re-enable form fields and submit button in case of error
                    $(".columns_number").attr('readonly', false);
                    $("#remarks_marksheet").attr('readonly', false);
                    $("#attendance_marksheet").attr('readonly', false);
                    $("#submit_marksheet").prop('disabled', false);
                    $("#submit_marksheet").html('Upload Marksheet'); // Reset submit button text

                    // Show error message
                    $("#error_message").text('Marks upload failed, please try again later.');
                    $("#error_div").css('color', 'red').fadeIn(300);
                    setTimeout(function() {
                        $("#error_div").fadeOut(300);
                    }, 5000);
                },
                success: function(resp) {
                    try {
                        var response = JSON.parse(resp);

                        if (response.status === 1) { // Success
                            // Re-enable form fields and submit button
                            $(".columns_number").attr('readonly', false);
                            $("#remarks_marksheet").attr('readonly', false);
                            $("#attendance_marksheet").attr('readonly', false);
                            $("#submit_marksheet").prop('disabled', false);
                            $("#submit_marksheet").html('Upload Marksheet'); // Reset submit button text

                            // Show success message
                            $("#success_message").text("Upload successful.");
                            $("#displayadd_homework_success").fadeIn(300);
                            setTimeout(function() {
                                $("#displayadd_homework_success").fadeOut(300);
                            }, 3000);
                        } else { // Failure response from the server
                            throw new Error(response.response || 'Unknown error');
                        }
                    } catch (e) {
                        // If there's an error parsing or in the response
                        $(".columns_number").attr('readonly', false);
                        $("#remarks_marksheet").attr('readonly', false);
                        $("#attendance_marksheet").attr('readonly', false);
                        $("#submit_marksheet").prop('disabled', false);
                        $("#submit_marksheet").html('Upload Marksheet'); // Reset submit button text

                        // Show server error message
                        $("#error_message").text(e.message || 'Upload failed.');
                        $("#error_div").css('color', 'red').fadeIn(300);
                        setTimeout(function() {
                            $("#error_div").fadeOut(300);
                        }, 5000);
                    }
                }
            });
        });
    });
</script>