<?php
include '../includes/connection.php';
 if (isset($_POST["Import"])) {

    $filename = $_FILES["file"]["tmp_name"];

    if ($_FILES["file"]["size"] > 0) {
        $file = fopen($filename, "r");

        $class_id = $_POST['class_id'];
        $exam_id = $_POST['exam_id'];
        $subject_id = $_POST['subject_id'];
        $year_of_study = date("Y");
        $year = date("Y");
        $result_added_by = $_POST['result_added_by'];
        $teacher_roll_no = $_POST['teacher_roll_no'];

        $student = mysqli_query($conn, "SELECT subject_id,class_id,teacher_roll_no,exam_id FROM marks_srms WHERE class_id='$class_id' and exam_id='$exam_id' AND subject_id='$subject_id' and teacher_roll_no='$teacher_roll_no'") or die(mysqli_error($conn));
        $row1 = mysqli_fetch_array($student);
        $check = $row1['subject_id'];

        if ($check != $subject_id) {
            while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE) {

                // Basic validation: skip rows with missing marks
                if (!isset($emapData[2]) || trim($emapData[2]) === '' || !is_numeric($emapData[2])) {
                    continue; // Skip invalid rows
                }

                $marks = (int) $emapData[2]; // Ensure it's treated as an integer

                // Get subject identity
                $sub_identity = mysqli_query($conn, "SELECT identity FROM general_subject where sub_id='$subject_id'") or die(mysqli_error($conn));
                $sub_i = mysqli_fetch_array($sub_identity);
                $identity_sub = $sub_i['identity'];

                // Get class level
                $Level = mysqli_query($conn, "SELECT * FROM class_srms where class_id='$class_id'") or die(mysqli_error($conn));
                $level_class = mysqli_fetch_array($Level);
                $check_level = $level_class['level'];

                // Grade & point calculation
                if ($check_level == "O-level") {
                    if ($marks >= 75) { $T_point = 1; $grade = "A"; $pass_marks = "EXCELLENT"; }
                    elseif ($marks >= 65) { $T_point = 2; $grade = "B"; $pass_marks = "VERY GOOD"; }
                    elseif ($marks >= 45) { $T_point = 3; $grade = "C"; $pass_marks = "GOOD"; }
                    elseif ($marks >= 30) { $T_point = 4; $grade = "D"; $pass_marks = "AVERAGE"; }
                    else { $T_point = 5; $grade = "F"; $pass_marks = "FAIL"; }
                } else {
                    if ($marks >= 80) { $T_point = 1; $grade = "A"; $pass_marks = "EXCELLENT"; }
                    elseif ($marks >= 70) { $T_point = 2; $grade = "B"; $pass_marks = "VERY GOOD"; }
                    elseif ($marks >= 60) { $T_point = 3; $grade = "C"; $pass_marks = "GOOD"; }
                    elseif ($marks >= 50) { $T_point = 4; $grade = "D"; $pass_marks = "AVERAGE"; }
                    elseif ($marks >= 40) { $T_point = 5; $grade = "E"; $pass_marks = "SATISFACTORY"; }
                    elseif ($marks >= 35) { $T_point = 6; $grade = "S"; $pass_marks = "SUBSIDIARY"; }
                    else { $T_point = 7; $grade = "F"; $pass_marks = "FAIL"; }
                }

                // Insert to DB
                $query = mysqli_query($conn, "INSERT INTO marks_srms 
                    (`teacher_roll_no`, `class_id`, `exam_id`, `grade`, `pass_marks`, `subject_id`, `student_roll_no`, `marks`, `identity_sub`, `point`, `result_added_by`, `year_of_study`, `Year`) 
                    VALUES 
                    ('$teacher_roll_no', '$class_id', '$exam_id', '$grade', '$pass_marks', '$subject_id', '$emapData[0]', '$marks', '$identity_sub', '$T_point', '$result_added_by', '$year_of_study', '$year')
                ");

                if (!$query) {
                    echo "<script>
                        alert(\"Error inserting data. Please check CSV format.\");
                        window.location = \"../admin.php?page=fgtutrs345gj789km231ExamTeachUIYTR7ttdeewz34&Id_2=$exam_id\"
                    </script>";
                    exit;
                }
            }

            fclose($file);

            echo "<script>
                alert(\"CSV File has been successfully Imported.\");
                window.location = \"../admin.php?page=fgtutrs345gj789km231ExamTeachUIYTR7ttdeewz34&Id_2=$exam_id\"
            </script>";
        } else {
            echo "<script>
                alert(\"Sorry, result already added.\");
                window.location = \"../admin.php?page=fgtutrs345gj789km231ExamTeachUIYTR7ttdeewz34&Id_2=$exam_id\"
            </script>";
        }

        mysqli_close($conn);
    }
}
?>
  
?>     