<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header('Access-Control-Allow-Headers: X-Requested-With');
include '../../config/index.php';
$data = array();
$sql_d12 = "SELECT * FROM students WHERE student_id='" . $_GET['student_id'] . "'";

$result_d12 = mysqli_query($conn, $sql_d12);

if (mysqli_num_rows($result_d12) > 0) {
    while ($row_d12 = mysqli_fetch_assoc($result_d12)) {

        $sql_clp = "SELECT * FROM centerClasses WHERE  centerClasses_id='" . $row_d12['centerClasses_id'] . "'";

        $result_clp = mysqli_query($conn, $sql_clp);

        if (mysqli_num_rows($result_clp) > 0) {
            while ($row_clp = mysqli_fetch_assoc($result_clp)) {

                $sql_10 = "SELECT * FROM classes WHERE  class_id='" . $row_clp['class_id'] . "'";

                $result_10 = mysqli_query($conn, $sql_10);

                if (mysqli_num_rows($result_10) > 0) {
                    while ($row_10 = mysqli_fetch_assoc($result_10)) {
                        $class_name = $row_10['class_name'];
                    }
                }
            }
        }
        $GradedAssignments = array();
        $sql_10 = "SELECT * FROM MentorStudentAssignment WHERE student_id='" . $_GET['student_id'] . "'  AND Status='Graded'";
        $result_10 = mysqli_query($conn, $sql_10);

        if (mysqli_num_rows($result_10) > 0) {
            while ($row_10 = mysqli_fetch_assoc($result_10)) {
                array_push($GradedAssignments, $row_10);
            }
        }
        $IssuedAssignments = array();
        $sql_10 = "SELECT * FROM MentorStudentAssignment WHERE student_id='" . $_GET['student_id'] . "'    AND Status!='Not Issued' AND Status!='Pending'";
        $result_10 = mysqli_query($conn, $sql_10);

        if (mysqli_num_rows($result_10) > 0) {
            while ($row_10 = mysqli_fetch_assoc($result_10)) {
                // array_push( $IssuedAssignments, $row_10 );

                $MentorStudentAssignmentRating_id;
                $Relevance;
                $WorkingCode;
                $Creativity;
                $TimelyCompletion;
                $Completion;
                $MentorComments;
                $RatingDate;
                $sql_20 = "select * from MentorStudentAssignmentRating WHERE MentorStudentAssignment_id='" . $row_10['MentorStudentAssignment_id'] . "' ";
                $result_20 = mysqli_query($conn, $sql_20);
                if (mysqli_num_rows($result_20) > 0) {
                    while ($row_20 = mysqli_fetch_assoc($result_20)) {
                        $MentorStudentAssignmentRating_id = $row_20['MentorStudentAssignmentRating_id'];
                        $Relevance = $row_20['Relevance'];
                        $WorkingCode = $row_20['WorkingCode'];
                        $Creativity = $row_20['Creativity'];
                        $TimelyCompletion = $row_20['TimelyCompletion'];
                        $Completion = $row_20['Completion'];
                        $MentorComments = $row_20['MentorComments'];
                        $RatingDate = $row_20['dated'];
                    }
                }
                $curl_post_data = array(
                    'MentorStudentAssignment_id' => $row_10['MentorStudentAssignment_id'],
                    'student_id' => $row_10['student_id'],
                    'course_lesson_plan_id' => $row_10['course_lesson_plan_id'],
                    'Assignment' => $row_10['Assignment'],
                    'Status' => $row_10['Status'],
                    'Reason' => $row_10['Reason'],
                    'MentorStudentAssignmentRating_id' => $MentorStudentAssignmentRating_id,
                    'Relevance' => $Relevance,
                    'WorkingCode' =>  $WorkingCode,
                    'Creativity' =>  $Creativity,
                    'TimelyCompletion' => $TimelyCompletion,
                    'Completion' =>  $Completion,
                    'MentorComments' => $MentorComments,
                    'RatingDate' => $RatingDate,
                );
                array_push($IssuedAssignments, $curl_post_data);
            }
        }
        $AssignmentsRating = array();
        $sql_10 = "select * from MentorStudentAssignment WHERE student_id='" . $row_d12['student_id'] . "'     ";
        $result_10 = mysqli_query($conn, $sql_10);

        if (mysqli_num_rows($result_10) > 0) {
            while ($row_10 = mysqli_fetch_assoc($result_10)) {
                $course_name;
                $course_image;

                $class_name;
                $lesson_session;
                $sql_clp = "SELECT * FROM course_lesson_plan WHERE  course_lesson_plan_id='" . $row_10['course_lesson_plan_id'] . "'";

                $result_clp = mysqli_query($conn, $sql_clp);

                if (mysqli_num_rows($result_clp) > 0) {
                    while ($row_clp = mysqli_fetch_assoc($result_clp)) {
                        $lesson_session = $row_clp['lesson_session'];
                        $sql_crs = "SELECT * FROM courses WHERE  course_id='" . $row_clp['course_id'] . "'";

                        $result_crs = mysqli_query($conn, $sql_crs);

                        if (mysqli_num_rows($result_crs) > 0) {
                            while ($row_crs = mysqli_fetch_assoc($result_crs)) {
                                $course_name = $row_crs['course_name'];
                                $course_image = $row_crs['course_image'];
                            }
                        }
                    }
                }
                $sql_20 = "select * from MentorStudentAssignmentRating WHERE MentorStudentAssignment_id='" . $row_10['MentorStudentAssignment_id'] . "' ";
                $result_20 = mysqli_query($conn, $sql_20);

                if (mysqli_num_rows($result_20) > 0) {
                    while ($row_20 = mysqli_fetch_assoc($result_20)) {
                        $TeacherName = "";
                        $TeacherEmail = "";
                        $TeacherImage = "";
                        $sql_stnt = "SELECT * FROM MentorStudentAssignment WHERE  MentorStudentAssignment_id='" . $row_20['MentorStudentAssignment_id'] . "'";
                        $result_stnt = mysqli_query($conn, $sql_stnt);
                        if (mysqli_num_rows($result_stnt) > 0) {
                            while ($row_stnt = mysqli_fetch_assoc($result_stnt)) {
                                $sql_tlsn = "SELECT * FROM mentorLesson WHERE  mentorLesson_id='" . $row_stnt['mentorLesson_id'] . "'";
                                $result_tlsn = mysqli_query($conn, $sql_tlsn);
                                if (mysqli_num_rows($result_tlsn) > 0) {
                                    while ($row_tlsn = mysqli_fetch_assoc($result_tlsn)) {
                                        $sql_crs = "SELECT * FROM mentors WHERE  mentor_id='" . $row_tlsn['mentor_id'] . "'";
                                        $result_crs = mysqli_query($conn, $sql_crs);
                                        if (mysqli_num_rows($result_crs) > 0) {
                                            while ($row_crs = mysqli_fetch_assoc($result_crs)) {
                                                $TeacherName = $row_crs['mentor_name'];
                                                $TeacherEmail = $row_crs['mentor_email'];
                                                $TeacherImage = $row_crs['mentor_image'];
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        $curl_post_data = array(
                            'MentorStudentAssignment_id' => $row_10['MentorStudentAssignment_id'],
                            'MentorStudentAssignmentRating_id' => $row_20['MentorStudentAssignmentRating_id'],
                            'Relevance' => $row_20['Relevance'],
                            'WorkingCode' => $row_20['WorkingCode'],
                            'Creativity' => $row_20['Creativity'],
                            'TimelyCompletion' => $row_20['TimelyCompletion'],
                            'Completion' => $row_20['Completion'],
                            'MentorComments' => $row_20['MentorComments'],
                            'RatingDate' => $row_20['dated'],
                            'course_lesson_plan_id' => $row_10['course_lesson_plan_id'],

                            'TeacherName'  => $TeacherName,
                            'TeacherEmail'  => $TeacherEmail,
                            'TeacherImage'  => $TeacherImage,
                        );
                        array_push($AssignmentsRating, $curl_post_data);
                    }
                }
                $curl_post_data = array(
                    'MentorStudentAssignment_id' => $row_10['MentorStudentAssignment_id'],
                    'student_id' => $row_10['student_id'],
                    'mentorLesson_id' => $row_10['mentorLesson_id'],
                    'centerCourse_id' => $row_10['centerCourse_id'],
                    'course_lesson_plan_id' => $row_10['course_lesson_plan_id'],
                    'Assignment' => $row_10['Assignment'],
                    'Status' => $row_10['Status'],
                    'Reason' => $row_10['Reason'],
                    'dated' => $row_10['dated'],
                    'course_name' => $course_name,
                    'course_image' =>  $course_image,
                    'class_name' => $class_name,
                    'lesson_session' =>  $lesson_session,
                    'AssignmentsRating' =>  $AssignmentsRating,
                    'GradedAssignments' =>  $GradedAssignments,
                    'IssuedAssignments' =>  $IssuedAssignments,
                );
                array_push($data, $curl_post_data);
            }
        }
    }
}
$dates = array_column($data, 'dated');
array_multisort($dates, SORT_DESC, $data);

echo json_encode($data);
