<?php

/* Attempt to connect to MySQL database */
/* Attempt to connect to MySQL database */
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'restax');

define('DB_PASSWORD', 'restax@123');

define('DB_NAME', 'Restax'); 

$con = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);


  $course_id=$request->getParsedBody()['course_id'];
  $exams_id = $request->getParsedBody()['exams_id'];
  $paper_shortcode = $request->getParsedBody()['paper_shortcode'];
  $slots=$request->getParsedBody()['slots'];
  $roll_number = $request->getParsedBody()['roll_number'];
  $status=$request->getParsedBody()['status'];
  $file = $request->getParsedBody()['file'];
  $file_path=$request->getParsedBody()['file_path'];
 $response=array();
 $data=array();

$grade="B";
$percentage="75";
 $allowed = array("pdf" => "application/pdf");
        $file = $_FILES["file"]["name"];
        $filetype = $_FILES["file"]["type"];
        $filesize = $_FILES["file"]["size"];
    
          // Verify file extension
        $ext = pathinfo($file, PATHINFO_EXTENSION);
        if(!array_key_exists($ext, $allowed)) die("Error: Please select a valid file format.");
    
        // Verify file size - 20MB maximum
        $maxsize = 20 * 1024 * 1024;
        if($filesize > $maxsize) die("Error: File size is larger than the allowed limit.");
    
        // Verify MYME type of the file
        if(in_array($filetype, $allowed)){
            // Check whether file exists before uploading it
            if(file_exists("../answersheetbeforeevaluate/" . $file)){
                $data=$file . " is already exists.";
                echo json_encode($response=[array("Message"=>$data)]);
            } else{
                move_uploaded_file($_FILES["file"]["tmp_name"], "../answersheetbeforeevaluate/" . $file);
                 $sql="INSERT INTO pwe_answersheet_submit (course_id,exams_id,papers_shortcode,slots,roll_number,status,score,state,file,file_path,Grade,percentage) VALUES ('$course_id','$exams_id','$paper_shortcode','$slots','$roll_number','$status','$file','$file_path','$grade','$percentge')";
        $res=mysqli_query($con,$sql);
        if ($res=true) {
          $data=
        array(   
 "course_id"=>$course_id,
  "exams_id" => $exams_id,
  "paper_shortcode" => $paper_shortcode,
  "file"=>$file,
         );
    
    $message=$data;

$response=array('status' => "true",'Message' => "Answer Sheet submited successfully",'Answersheet' => $message);
echo json_encode($response);
        }
        else{
          $response=array('status' => "false",'Message' => "Schedule Exam Failed");
         echo json_encode($response);
        }
            } 
        } else{
            $data=" Error: There was a problem uploading your file."." ".$file." "." Please try again.";
                echo json_encode($response=[array("Message"=>$data)]);
        }
?>