<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization");
include '../../../config/index.php';
$data = array();
$sql_cms = "SELECT * FROM centerMentors WHERE center_id='" . $_GET["center_id"] . "'  AND Deleted='false'";

$result_cms = mysqli_query($conn, $sql_cms);
if (mysqli_num_rows($result_cms) > 0) {
    while ($row_cms = mysqli_fetch_assoc($result_cms)) {
        $sql = "SELECT *  FROM mentors WHERE mentor_id='" . $row_cms['mentor_id'] . "'";
 
        $result = mysqli_query($conn, $sql);
        $all_data = [];
        if (mysqli_num_rows($result) > 0) {
            while ($row = mysqli_fetch_assoc($result)) {

                $finishedClasses = 0;
                $assignedClasses = 0;
                $sql_pre = "SELECT * FROM mentorCourse WHERE mentor_id='" . $row["mentor_id"] . "' ";
                $result_pre = mysqli_query($conn, $sql_pre);
                $certifiedCourses = mysqli_num_rows($result_pre);
                if (mysqli_num_rows($result_pre) > 0) {
                    while ($row_pre = mysqli_fetch_assoc($result_pre)) {
                        $lessons = array();
                        $sql_clp = "SELECT * FROM mentorLesson WHERE   mentor_id='" . $row['mentor_id'] . "' AND mentorCourse_id='" . $row_pre['mentorCourse_id'] . "'";
                        $result_clp = mysqli_query($conn, $sql_clp);
                        $TotalLessons = mysqli_num_rows($result_clp);
                        if (mysqli_num_rows($result_clp) > 0) {
                            while ($row_clp = mysqli_fetch_assoc($result_clp)) {
                                $sql_10 = "SELECT * FROM MentorStudentAttendance WHERE  mentorLesson_id='" . $row_clp['mentorLesson_id'] . "'";
                                $result_10 = mysqli_query($conn, $sql_10);
                                $AttendanceSize = mysqli_num_rows($result_10);

                                $sql_10 = "SELECT * FROM MentorstudentReport WHERE  mentorLesson_id='" . $row_clp['mentorLesson_id'] . "'";
                                $result_10 = mysqli_query($conn, $sql_10);
                                $ReportSize = mysqli_num_rows($result_10);

                                if ($AttendanceSize != 0 && $ReportSize != 0) {
                                    $curl_post_data = array(
                                        'AttendanceSize' => $AttendanceSize,
                                        'ReportSize' => $ReportSize,
                                        'mentorLesson_id' => $row_clp["mentorLesson_id"],
                                        'timeSlots_id' => $row_clp["timeSlots_id"],
                                        'mentorCourse_id' => $row_clp["mentorCourse_id"],
                                        'mentor_id' => $row_clp["mentor_id"],
                                        'centerCourse_id' => $row_clp["centerCourse_id"],
                                        'course_lesson_plan_id' => $row_clp["course_lesson_plan_id"],
                                        'date_of_lesson' => $row_clp["date_of_lesson"],
                                        'status' => $row_clp["status"]
                                    );
                                    array_push($lessons, $curl_post_data);
                                }
                            }
                        }
                        if ($TotalLessons === sizeof($lessons)) {
                            $finishedClasses = $finishedClasses + 1;
                        }
                        if ($TotalLessons > 0) {
                            $assignedClasses = $assignedClasses + 1;
                        }
                    }
                }
                $sql_pre_one = "SELECT * FROM selfPacedMentors WHERE  mentor_id='" . $row["mentor_id"] . "' ";
                $result_pre_one = mysqli_query($conn, $sql_pre_one);
                $selfPacedCourses = mysqli_num_rows($result_pre_one);


                $curl_post_data = array(
                    'mentor_id' => $row["mentor_id"],
                    'mentor_name' => $row["mentor_name"],
                    'mentor_email' => $row["mentor_email"],
                    'mentor_image' => $row["mentor_image"],
                    'mentor_phone' => $row["mentor_phone"],
                    'mentor_password' => $row["mentor_password"],
                    'supervisor' => $row["supervisor"],
                    'supervisor_id' => $row["supervisor_id"],
                    'deleted' => $row["deleted"],
                    'location' => $row["location"],
                    'external' => $row["external"],
                    'lat' => $row["lat"],
                    'long' => $row["long"],
                    'flag_1' => $row["flag_1"],
                    'flag_2' => $row["flag_2"],
                    'certifiedCourses' => $certifiedCourses,
                    'selfPacedCourses' => $selfPacedCourses,
                    'finishedClasses' => $finishedClasses,
                    'assignedClasses' => $assignedClasses,

                );
                array_push($data, $curl_post_data);

            }
        }

    }
}
echo json_encode($data);
?>