<?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 centerCourse WHERE centerCourse_id='" . $_GET["centerCourse_id"] . "'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        $course_name;
        $teachers = array();
        $class_name;
        $SessionName;
        $sql1 = "SELECT * FROM courses WHERE course_id='" . $row["course_id"] . "' ";
        $result1 = mysqli_query($conn, $sql1);
        if (mysqli_num_rows($result1) > 0) {
            while ($row1 = mysqli_fetch_assoc($result1)) {
                $course_name =  $row1["course_name"];
                $course_image =  $row1["course_image"];
            }
        }
        $MentorName = "";
        $MentorImage = "";
        $MentorEmail = "";
        $Percentage = 0;
        $StartDate = "";
        $EndDate = "";
        $TeacherPresent = false;
        $TotalNoHours = 0;
        $TotalLessons = 0;
        $HoursDone = 0;
        $TaughtLessons = 0;
        $IssuedAssignments = 0;
        $GradedReports = 0;
        $AttendedClass = 0;
        $i = 0;
        $sql_ccs = "SELECT * FROM centerClasses WHERE centerClasses_id='" . $row["centerClasses_id"] . "' ";

        $result_ccs = mysqli_query($conn, $sql_ccs);
        if (mysqli_num_rows($result_ccs) > 0) {
            while ($row_ccs = mysqli_fetch_assoc($result_ccs)) {
                $sql_cntr = "SELECT * FROM digicenters WHERE center_id='" . $row_ccs["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='" . $row_cntr["center_id"] . "' AND course_id='" . $row["course_id"] . "'";

                            $result_mcrs = mysqli_query($conn, $sql_mcrs);
                            if (mysqli_num_rows($result_mcrs) > 0) {
                                while ($row_mcrs = mysqli_fetch_assoc($result_mcrs)) {
                                    $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)) {

                                            if ($i === 0) {
                                                $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;
                                            }

                                            $i = $i + 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)) {
                                            $TeacherPresent = true;

                                            $MentorName = $row20["mentor_name"];
                                            $MentorPhone = $row20["mentor_phone"];
                                            $MentorImage = $row20["mentor_image"];
                                            $MentorEmail = $row20["mentor_email"];
                                        }
                                    }
                                }
                            }
                    
                        }
                    }
                }
            }
        }
        $sql10 = "SELECT * FROM students WHERE centerClasses_id='" . $row["centerClasses_id"] . "' ";
        $result10 = mysqli_query($conn, $sql10);
        $NoStudents = mysqli_num_rows($result10);

        $curl_post_data = array(
            'centerCourse_id' => $row["centerCourse_id"],
            'centerClasses_id' => $row["centerClasses_id"],
            'curriculumSession_id' => $row["curriculumSession_id"],
            'course_id' => $row["course_id"],
            'course_name' => $course_name,
            'course_image' => $course_image,
            'MentorName' => $MentorName,
            'MentorPhone' => $MentorPhone,
            'MentorEmail' => $MentorEmail,
            'MentorImage' => $MentorImage,
            'Percentage' => $Percentage,
            'StartDate' => $StartDate,
            'EndDate' => $EndDate,
            'NoStudents' => $NoStudents,
            'TotalLessons' => $TotalLessons,
            'TaughtLessons' => $TaughtLessons,
            'TeacherPresent' => $TeacherPresent,
        );
        echo json_encode($curl_post_data);

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