<?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 = "SELECT * FROM teachers WHERE center_id='" . $_GET["center_id"] . "'";

$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
      
 
            $teachers = array();
            $center_name;
            $center_email;
            $center_phone;
            $center_location;
            $sql2 = "SELECT * FROM digicenters WHERE center_id='" . $row["center_id"] . "' ";
            $result2 = mysqli_query($conn, $sql2);
            if (mysqli_num_rows($result2) > 0) {
                while ($row2 = mysqli_fetch_assoc($result2)) {
                    $center_name = $row2["center_name"];
                    $center_email = $row2["center_email"];
                    $center_phone = $row2["center_phone"];
                    $center_location = $row2["center_location"];
                }
            }
            


            $certifiedCourses = 0;

            $selfPacedCourses = 0;
            $finishedClasses = 0;
            $assignedClasses = 0;
                    // $sql_pre = "SELECT * FROM centerCourse WHERE center_id='" . $row10["center_id"] . "' ";
                    // $result_pre = mysqli_query($conn, $sql_pre);
                    // if (mysqli_num_rows($result_pre) > 0) {
                    //     while ($row_pre = mysqli_fetch_assoc($result_pre)) {
            $lessons = array();
            $sql_clp = "SELECT * FROM teacherLesson WHERE   teacher_id='" . $row['teacher_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 StudentAttendance WHERE  teacherLesson_id='" . $row_clp['teacherLesson_id'] . "'";
                    $result_10 = mysqli_query($conn, $sql_10);
                    $AttendanceSize = mysqli_num_rows($result_10);

                    $sql_10 = "SELECT * FROM studentReport WHERE  teacherLesson_id='" . $row_clp['teacherLesson_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,
                            'teacherLesson_id' => $row_clp["teacherLesson_id"],
                            'timeSlots_id' => $row_clp["timeSlots_id"],
                            'centerCourse_id' => $row_clp["centerCourse_id"],
                            'teacher_id' => $row_clp["teacher_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 self_paced_teachers WHERE  teacher_id='" . $row["teacher_id"] . "' ";
            $result_pre_one = mysqli_query($conn, $sql_pre_one);
            $selfPacedCourses = mysqli_num_rows($result_pre_one);

            $sql_pre_one = "SELECT * FROM teacherCourse WHERE  teacher_id='" . $row["teacher_id"] . "' ";
            $result_pre_one = mysqli_query($conn, $sql_pre_one);
            $certifiedCourses = mysqli_num_rows($result_pre_one);

            $curl_post_data = array(
 
                'teacher_id' => $row["teacher_id"],
                'TeacherFirstName' => $row["TeacherFirstName"],
                'TeacherLastName' => $row["TeacherLastName"],
                'TeacherEmail' => $row["TeacherEmail"],
                'TeacherImage' => $row["TeacherImage"],
                'center_id' => $row["center_id"],
                'TeacherPhone' => $row["TeacherPhone"],
                'Location' => $row["Location"],
                'Lat' => $row["Lat"],
                'long' => $row["long"],
                'center_name' => $center_name,
                'center_email' => $center_email,
                'center_phone' => $center_phone,
                'center_location' => $center_location,
                'certifiedCourses' => $certifiedCourses,
                'selfPacedCourses' => $selfPacedCourses,
                'finishedClasses' => $finishedClasses,
                'assignedClasses' => $assignedClasses,
            );

            array_push($data, $curl_post_data);
                 
    }
}
echo json_encode($data);



?>