<?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();


$classes = array();
$sqlcls = "SELECT * FROM classes ";
$resultcls = mysqli_query($conn, $sqlcls);
if (mysqli_num_rows($resultcls) > 0) {
    while ($rowcls = mysqli_fetch_assoc($resultcls)) {
        $curriculumEnrollment = array();
        $sql = "SELECT * FROM centerClasses WHERE center_id='" . $_GET["center_id"] . "' AND  class_id='" . $rowcls["class_id"] . "' AND  curriculumEnrollment_id='" . $_GET["curriculumEnrollment_id"] . "' ";

        $result = mysqli_query($conn, $sql);
        if (mysqli_num_rows($result) > 0) {
            while ($row = mysqli_fetch_assoc($result)) {

                $class_name;
                $rowcls["class_description"];
                $centerCourses = array();
                $StreamName = "";
                $Streams_id = "";
                $sql1 = "SELECT * FROM classes WHERE class_id='" . $row["class_id"] . "' ";
                $result1 = mysqli_query($conn, $sql1);
                if (mysqli_num_rows($result1) > 0) {
                    while ($row1 = mysqli_fetch_assoc($result1)) {
                        $class_name = $row1["class_name"];
                        $rowcls["class_description"] = $row1["class_description"];
                    }
                }
                $centerCourses = array();
                $sql1 = "SELECT * FROM Streams WHERE Streams_id='" . $row["Streams_id"] . "' ";
                $result1 = mysqli_query($conn, $sql1);
                if (mysqli_num_rows($result1) > 0) {
                    while ($row1 = mysqli_fetch_assoc($result1)) {
                        $StreamName = $row1["StreamName"];
                        $Streams_id = $row1["Streams_id"];
                    }
                }
                $SessionName = "";
                $curriculumSession_id = "";
                $sql1 = "SELECT * FROM curriculumSession WHERE curriculumEnrollment_id='" . $row["curriculumEnrollment_id"] . "' ";
                $result1 = mysqli_query($conn, $sql1);
                if (mysqli_num_rows($result1) > 0) {
                    while ($row1 = mysqli_fetch_assoc($result1)) {
                        $SessionName = $row1["SessionName"];
                        $curriculumSession_id = $row1["curriculumSession_id"];
                    }
                }
                $sql1 = "SELECT * FROM centerCourse WHERE centerClasses_id='" . $row["centerClasses_id"] . "' ";
                $result1 = mysqli_query($conn, $sql1);
                if (mysqli_num_rows($result1) > 0) {
                    while ($row1 = mysqli_fetch_assoc($result1)) {
                        $sql10 = "SELECT * FROM courses WHERE course_id='" . $row1["course_id"] . "' ";
                        $result10 = mysqli_query($conn, $sql10);
                        if (mysqli_num_rows($result10) > 0) {
                            while ($row10 = mysqli_fetch_assoc($result10)) {
                                $Status = "Inactive";
                                $Teachers = array();
                                $MentorName = "";
                                $MentorImage = "";
                                $MentorEmail = "";
                                $Percentage = 0;
                                $StartDate = "";
                                $EndDate = "";
                                $sql_cntr = "SELECT * FROM digicenters WHERE center_id='" . $row1["center_id"] . "' ";
                                $result_cntr = mysqli_query($conn, $sql_cntr);
                                if (mysqli_num_rows($result_cntr) > 0) {
                                    while ($row_cntr = mysqli_fetch_assoc($result_cntr)) {


                                        if ($row_cntr["Type"] === "digifunzi") {

                                            $sql_mcrs = "SELECT * FROM mentorCourse WHERE center_id='" . $row1["center_id"] . "' AND course_id='" . $row1["course_id"] . "'";
                                            $result_mcrs = mysqli_query($conn, $sql_mcrs);
                                            if (mysqli_num_rows($result_mcrs) > 0) {
                                                while ($row_mcrs = mysqli_fetch_assoc($result_mcrs)) {
                                                    $Percentage = 0;
                                                    $TotalNoHours = 0;
                                                    $TotalNoLessons = 0;
                                                    $HoursDone = 0;
                                                    $TaughtLessons = 0;
                                                    $sql_clp = "SELECT * FROM mentorLesson WHERE   mentor_id='" . $row_mcrs['mentor_id'] . "' AND mentorCourse_id='" . $row_mcrs['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)) {
                                                            $StartDate = $row_clp["date_of_lesson"];
                                                            $EndDate = $row_clp["date_of_lesson"];
                                                            $Period;
                                                            $StartTime;
                                                            $EndTime;
                                                            $date_of_lesson = $row_clp["date_of_lesson"];
                                                            $sql_10 = "SELECT * FROM timeSlots WHERE  timeSlots_id='" . $row_clp['timeSlots_id'] . "'";
                                                            $result_10 = mysqli_query($conn, $sql_10);
                                                            if (mysqli_num_rows($result_10) > 0) {
                                                                while ($row_10 = mysqli_fetch_assoc($result_10)) {
                                                                    $Period = $row_10["Period"];
                                                                    $StartTime = $row_10["StartTime"];
                                                                    $EndTime = $row_10["EndTime"];
                                                                }
                                                            }
                                                            $Starttimestamp = strtotime($StartTime);
                                                            $Endtimestamp = strtotime($EndTime);
                                                            $Difference = abs($Endtimestamp - $Starttimestamp) / 3600;

                                                            $TotalHours = $TotalHours + $Difference;


                                                            $centerClasses_id;

                                                            $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) {
                                                                $todays_date = date("Y-m-d");
                                                                $dateTimestamp1 = strtotime($todays_date);
                                                                $dateTimestamp2 = strtotime($row_clp["date_of_lesson"]);
                                                                if ($dateTimestamp1 > $dateTimestamp2) {
                                                                }
                                                                $HoursDone = $HoursDone + $Difference;
                                                                $TaughtLessons = $TaughtLessons + 1;
                                                            }
                                                        }
                                                        $Percent = 0;
                                                        if ($TaughtLessons > 0) {
                                                            $Percent = ($TaughtLessons / $TotalLessons) * 100;
                                                        }
                                                        $Percentage = $Percent;
                                                    }

                                                    $sql20 = "SELECT * FROM mentors WHERE mentor_id='" . $row_mcrs["mentor_id"] . "'    ";
                                                    $result20 = mysqli_query($conn, $sql20);
                                                    if (mysqli_num_rows($result20) > 0) {
                                                        while ($row20 = mysqli_fetch_assoc($result20)) {
                                                            $MentorName = $row20["mentor_name"];
                                                            $MentorImage = $row20["mentor_image"];
                                                            $MentorEmail = $row20["mentor_email"];
                                                        }
                                                    }
                                                }
                                            }
                                        } else {
                                            $sql20 = "SELECT * FROM teacherCourse WHERE teacher_id='" . $_GET["teacher_id"] . "' AND course_id='" . $row1["course_id"] . "'";
                                            $Percentage = 0;
                                            $result_20 = mysqli_query($conn, $sql20);
                                            if (mysqli_num_rows($result_20) > 0) {
                                                while ($row_20 = mysqli_fetch_assoc($result_20)) {
                                                    $sql_mntr = "SELECT * FROM teachers WHERE   teacher_id='" . $row_20["teacher_id"] . "'     ";
                                                    $result_mntr = mysqli_query($conn, $sql_mntr);
                                                    if (mysqli_num_rows($result_mntr) > 0) {
                                                        while ($row_mntr = mysqli_fetch_assoc($result_mntr)) {
                                                            $MentorName = $row_mntr["TeacherFirstName"] . " " . $row_mntr["TeacherLastName"];

                                                            $MentorImage = $row_mntr["TeacherImage"];
                                                            $MentorEmail = $row_mntr["TeacherEmail"];
                                                            $TotalNoHours = 0;
                                                            $TotalNoLessons = 0;
                                                            $HoursDone = 0;
                                                            $TaughtLessons = 0;
                                                            $sql_mce = "SELECT * FROM teacherLesson WHERE  centerCourse_id= '" . $row_d12['centerCourse_id'] . "'AND teacher_id='" . $row_mntr['teacher_id'] . "'  ";

                                                            $result_mce = mysqli_query($conn, $sql_mce);
                                                            $TotalLessons = mysqli_num_rows($result_mce);
                                                            if (mysqli_num_rows($result_mce) > 0) {
                                                                while ($row_mce = mysqli_fetch_assoc($result_mce)) {
                                                                    $StartDate = $row_mce["date_of_lesson"];
                                                                    $EndDate = $row_mce["date_of_lesson"];
                                                                    $Period;
                                                                    $StartTime;
                                                                    $EndTime;
                                                                    $sql_time = "SELECT * FROM timeSlots WHERE   timeSlots_id='" . $row_mce["timeSlots_id"] . "'   ";
                                                                    $result_time = mysqli_query($conn, $sql_time);
                                                                    if (mysqli_num_rows($result_time) > 0) {
                                                                        while ($row_time = mysqli_fetch_assoc($result_time)) {
                                                                            $Period = $row_time["Period"];
                                                                            $StartTime = "08:00:00";
                                                                            $EndTime = "09:00:00";
                                                                            if ($row_time["StartTime"] != "") {
                                                                                $StartTime = $row_time["StartTime"];
                                                                            }
                                                                            if ($row_time["EndTime"] != "") {
                                                                                $EndTime = $row_time["EndTime"];
                                                                            }
                                                                        }
                                                                    }
                                                                    $Starttimestamp = strtotime($StartTime);
                                                                    $Endtimestamp = strtotime($EndTime);
                                                                    $Difference = abs($Endtimestamp - $Starttimestamp) / 3600;

                                                                    $TotalNoHours = $TotalNoHours + $Difference;

                                                                    $sql_10 = "SELECT * FROM teacherstudentAttendance WHERE  teacherLesson_id='" . $row_mce['teacherLesson_id'] . "'";
                                                                    $result_10 = mysqli_query($conn, $sql_10);
                                                                    $AttendanceSize = mysqli_num_rows($result_10);

                                                                    $sql_10 = "SELECT * FROM teacherstudentReport WHERE  teacherLesson_id='" . $row_mce['teacherLesson_id'] . "'";
                                                                    $result_10 = mysqli_query($conn, $sql_10);
                                                                    $ReportSize = mysqli_num_rows($result_10);

                                                                    if ($AttendanceSize != 0 && $ReportSize != 0) {
                                                                        $todays_date = date("Y-m-d");
                                                                        $dateTimestamp1 = strtotime($todays_date);
                                                                        $dateTimestamp2 = strtotime($row_clp["date_of_lesson"]);
                                                                        if ($dateTimestamp1 > $dateTimestamp2) {
                                                                        }
                                                                        $HoursDone = $HoursDone + $Difference;
                                                                        $TaughtLessons = $TaughtLessons + 1;
                                                                    }
                                                                }
                                                                $Percent = 0;
                                                                if ($TaughtLessons > 0) {
                                                                    $Percent = ($TaughtLessons / $TotalLessons) * 100;
                                                                }
                                                                $Percentage = $Percent;
                                                            }
                                                        }
                                                    }
                                                }

                                                // array_push($data, $curl_post_data);
                                                // $TotalHours = 0;

                                            }
                                        }
                                        // $curl_post_data = array(

                                        //     'MentorName' => $MentorName,
                                        //     'MentorEmail' => $MentorEmail,
                                        //     'MentorImage' => $MentorImage,
                                        //     'Percentage' => $Percentage,
                                        //     'StartDate' => $StartDate,
                                        //     'EndDate' => $EndDate,
                                        // );

                                        // array_push($Teachers, $curl_post_data);
                                    }
                                }
                                $sql_stnt = "select * from students WHERE centerClasses_id='" . $row1['centerClasses_id'] . "'   ";
                                $result_stdnt = mysqli_query($conn, $sql_stnt);
                                $NoStudents = mysqli_num_rows($result_stdnt);
                                $curl_post_data = array(
                                    'centerCourse_id' => $row1["centerCourse_id"],
                                    'course_name' => $row10["course_name"],
                                    'curriculumSession_id' => $row1["curriculumSession_id"],
                                    'course_id' => $row1["course_id"],
                                    'center_id' => $row1["center_id"],
                                    'NoStudents' => $NoStudents,
                                    'Status' => $Status,
                                    'MentorName' => $MentorName,
                                    'MentorEmail' => $MentorEmail,
                                    'MentorImage' => $MentorImage,
                                    'Percentage' => $Percentage,
                                    'StartDate' => $StartDate,
                                    'EndDate' => $EndDate,
                                );

                                array_push($centerCourses, $curl_post_data);
                            }
                        }
                    }
                }

                // if (date("Y", strtotime("now")) === date('Y', strtotime($row_enr["StartDate"]))) {

                $curl_post_data = array(
                    'centerClasses_id' => $row["centerClasses_id"],
                    'curriculumEnrollment_id' => $row["curriculumEnrollment_id"],
                    'curriculumClasses_id' => $row["curriculumClasses_id"],
                    'class_name' => $class_name,
                    'class_description' => $datafile,
                    'class_image' => "assets/img/fav.png",
                    'StreamName' => $StreamName,
                    'Streams_id' => $Streams_id,
                    'SessionName' => $SessionName,
                    'curriculumSession_id' => $curriculumSession_id,
                    'centerCourses' => $centerCourses,

                );

                array_push($curriculumEnrollment, $curl_post_data);
                // }

                $StreamName = "";
                $class_name = "";
            }


            $datafile;
            if (file_exists("/home/sendusco/app/" . $rowcls["class_description"])) {
                $datafile = file_get_contents("/home/sendusco/app/" . $rowcls["class_description"]);
            } else if (file_exists("/home/sendusco/digifunzi.com/" . $rowcls["class_description"])) {
                $datafile = file_get_contents("/home/sendusco/digifunzi.com/" . $rowcls["class_description"]);
            } else if (file_exists("/home/sendusco/dev.digifunzi.com/" . $rowcls["class_description"])) {
                $datafile = file_get_contents("/home/sendusco/dev.digifunzi.com/" . $rowcls["class_description"]);
            } else {
                $datafile = "";
            }
            $curl_post_data = array(
                'class_id' => $rowcls["class_id"],
                'class_name' => $rowcls["class_name"],
                'expected_outcome' => $rowcls["expected_outcome"],
                'class_type' => $rowcls["class_type"],
                'class_description' => $datafile,
                'age_from' => $rowcls["age_from"],
                'age_to' => $rowcls["age_to"],
                'required_sessions' => $rowcls["required_sessions"],
                'active' => $rowcls["active"],
                'class_image' => "assets/img/fav.png",
                'curriculumEnrollment' => $curriculumEnrollment,

            );

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







echo json_encode($data);
