<?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();
$sql1 = "SELECT * FROM mentorCourse WHERE center_id='" . $_GET["center_id"] . "'  ";
$result1 = mysqli_query($conn, $sql1);
$certifiedCourses = mysqli_num_rows($result1);
if (mysqli_num_rows($result1) > 0) {
    while ($row1 = mysqli_fetch_assoc($result1)) {

        $CompletedLessons = 0;
        $TotalNoLessons = 0;
        $centerCourse_id = 0;

        $sql_Nlsn = "SELECT * FROM mentorLesson WHERE   mentor_id='" . $row1['mentor_id'] . "' AND mentorCourse_id='" . $row1['mentorCourse_id'] . "'";
        $result_Nlsn = mysqli_query($conn, $sql_Nlsn);
        $TotalNoLessons = mysqli_num_rows($result_Nlsn);

        $sql_clp = "SELECT * FROM mentorLesson WHERE   mentor_id='" . $row1['mentor_id'] . "' AND mentorCourse_id='" . $row1['mentorCourse_id'] . "' ORDER BY dated DESC LIMIT 1";
        $result_clp = mysqli_query($conn, $sql_clp);
        if (mysqli_num_rows($result_clp) > 0) {
            while ($row_clp = mysqli_fetch_assoc($result_clp)) {
                $centerCourse_id = $row_clp["centerCourse_id"];
                $sql_10 = "SELECT * FROM MentorStudentAttendance WHERE  mentorLesson_id='" . $row_clp['mentorLesson_id'] . "'";
            }
        }
        $sql_clp = "SELECT * FROM mentorLesson WHERE   mentor_id='" . $row1['mentor_id'] . "' AND mentorCourse_id='" . $row1['mentorCourse_id'] . "'";
        $result_clp = mysqli_query($conn, $sql_clp);
        $TotalNoLessons = mysqli_num_rows($result_clp);

        $Status = "not_scheduled";
        $Percentage = 0;
        if ($TotalNoLessons > 0) {
            $assignedClasses = $assignedClasses + 1;
            $Status = "Scheduled";
            $Percentage = 0;
        }
        $ClassName = "";
        $sql_ccrs = "SELECT * FROM centerCourse WHERE   centerCourse_id='" . $centerCourse_id . "'  ";
        $result_ccrs = mysqli_query($conn, $sql_ccrs);
        if (mysqli_num_rows($result_ccrs) > 0) {
            while ($row_ccrs = mysqli_fetch_assoc($result_ccrs)) {
                $sql_cls = "SELECT * FROM centerClasses WHERE centerClasses_id='" . $row_ccrs["centerClasses_id"] . "' ";
                $result_cls = mysqli_query($conn, $sql_cls);
                if (mysqli_num_rows($result_cls) > 0) {
                    while ($row_cls = mysqli_fetch_assoc($result_cls)) {
                        $class_name = "";
                        $StreamName = "";
                        $sql_nm = "SELECT * FROM classes WHERE class_id='" . $row_cls["class_id"] . "' ";
                        $result_nm = mysqli_query($conn, $sql_nm);
                        if (mysqli_num_rows($result_nm) > 0) {
                            while ($row_nm = mysqli_fetch_assoc($result_nm)) {
                                $class_name = $row_nm["class_name"];
                            }
                        }
                        $sql_nm = "SELECT * FROM Streams WHERE Streams_id='" . $row_cls["Streams_id"] . "' ";
                        $result_nm = mysqli_query($conn, $sql_nm);
                        if (mysqli_num_rows($result_nm) > 0) {
                            while ($row_nm = mysqli_fetch_assoc($result_nm)) {
                                $StreamName = $row_nm["StreamName"];
                            }
                        }
                        $ClassName = $class_name . " " . $StreamName;
                    }
                }
            }
        }

        $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)) {

                $curl_post_data = array(
                    'mentorCourse_id' => $row1["mentorCourse_id"],
                    'course_id' => $row1["course_id"],
                    'course_name' => $row10["course_name"],
                    'course_image' => $row10["course_image"],
                    'CompletedLessons' => $CompletedLessons,
                    'ClassName' => $ClassName,
                    'TotalNoLessons' => $TotalNoLessons,
                    'Status' => $Status,
                    'Percentage' => $Percentage,
                );
                array_push($courses, $curl_post_data);
            }
        }
    }
}

echo json_encode($data);
