<?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();
$SelfPacedCourses = array();


$sql_tlsn10 = "SELECT * FROM selfPacedAdultStudent WHERE AdultStudent_id='" . $_GET['AdultStudent_id'] . "' ";
$result_tlsn10 = mysqli_query($conn, $sql_tlsn10);
if (mysqli_num_rows($result_tlsn10) > 0) {
    while ($row_tlsn10 = mysqli_fetch_assoc($result_tlsn10)) {
        // $sql_spc = "SELECT * FROM SelfPacedClasses WHERE SelfPacedClasses_id='" . $row_tlsn10["SelfPacedClasses_id"] . "'";
        // $result_spc = mysqli_query($conn, $sql_spc);
        // if (mysqli_num_rows($result_spc) > 0) {
        //     while ($row_spc = mysqli_fetch_assoc($result_spc)) {
        $sql_d12 = "SELECT * FROM SelfPacedCourses WHERE SelfPacedCourses_id='" . $row_tlsn10["SelfPacedCourses_id"] . "' ORDER BY orderNo ASC";
        $result_d12 = mysqli_query($conn, $sql_d12);
        if (mysqli_num_rows($result_d12) > 0) {
            while ($row_d12 = mysqli_fetch_assoc($result_d12)) {
                $sql_clp = "SELECT * FROM courses WHERE  course_id='" . $row_d12['courses_id'] . "'";
                $result_clp = mysqli_query($conn, $sql_clp);
                if (mysqli_num_rows($result_clp) > 0) {
                    while ($row_clp = mysqli_fetch_assoc($result_clp)) {
                        $AllLessons = 0;
                        $Percentage = 0;
                        $NotBookedPercentage = 0;

                        $sql_civ = "SELECT * FROM selfPacedAdultStudentAssignment WHERE  SelfPacedCourses_id='" . $row_tlsn10['SelfPacedCourses_id'] . "' AND   selfPacedAdultStudent_id='" . $row_tlsn10['selfPacedAdultStudent_id'] . "'";
                        $result_civ = mysqli_query($conn, $sql_civ);
                        $AllLessonsNew = mysqli_num_rows($result_civ);
                        $AllLessons = (int)$AllLessons + (int)$AllLessonsNew;

                        $AllSessions = 0;
                        $AdultStudentSessions = array();
                        $sql_spass = "SELECT * FROM selfPacedAdultStudentSession WHERE  SelfPacedCourses_id='" . $row_tlsn10['SelfPacedCourses_id'] . "' AND selfPacedAdultStudent_id='" . $row_tlsn10['selfPacedAdultStudent_id'] . "'";
                        $result_spass = mysqli_query($conn, $sql_spass);
                        if (mysqli_num_rows($result_spass) > 0) {
                            while ($row_spass = mysqli_fetch_assoc($result_spass)) {


                                $sql_maol = "SELECT * FROM MentorAdultOnlineSession WHERE  AdultStudent_id='" . $row_tlsn10['AdultStudent_id'] . "' AND selfPacedAdultStudentSession_id='" . $row_spass['selfPacedAdultStudentSession_id'] . "'  ORDER BY dated DESC LIMIT 1";
                                $result_maol = mysqli_query($conn, $sql_maol);
                                if (mysqli_num_rows($result_maol) > 0) {
                                    while ($row_maol = mysqli_fetch_assoc($result_maol)) {
                                        if (!in_array($row_maol["selfPacedAdultStudentSession_id"], $AdultStudentSessions)) {
                                            $AllSessions++;
                                        }
                                        array_push($AdultStudentSessions,  $row_maol['selfPacedAdultStudentSession_id']);
                                    }
                                }
                            }
                        }
                        $Status = "IN-Progress";
                        $SessionsNotBooked = $AllLessons - $AllSessions;
                        if ($AllSessions > 0 && $AllLessons > 0) {
                            $Percentage = ($AllSessions / $AllLessons) * 100;
                        }
                        if ($SessionsNotBooked > 0 && $AllLessons > 0) {
                            $NotBookedPercentage = ($SessionsNotBooked / $AllLessons) * 100;
                        }
                        // $Percentage = ($AllSessions / $AllLessons) * 100;
                        // $NotBookedPercentage = ($SessionsNotBooked / $AllLessons) * 100;
                        if ($Percentage >= 100) {
                            $Status = "Completed";
                        }

                        if (!in_array($row_tlsn10["SelfPacedCourses_id"], $SelfPacedCourses)) {
                            $curl_post_data = array(
                                'SelfPacedCourses_id' => $row_d12["SelfPacedCourses_id"],
                                'SelfPacedClasses_id' => $row_d12["SelfPacedClasses_id"],
                                'orderNo' => $row_d12["orderNo"],
                                'course_name' => $row_clp["course_name"],
                                'course_image' => $row_clp["course_image"],
                                'course_id' => $row_clp["course_id"],
                                'Enrolled' => $row_tlsn10["dated"],
                                'Status' => $Status,
                                'Percentage' => $Percentage,
                                'NotBookedPercentage' => $NotBookedPercentage,
                                'AllLessons' => $AllLessons,
                                'AllSessions' => $AllSessions,
                                'SessionsNotBooked' => $SessionsNotBooked,
                            );
                            array_push($data, $curl_post_data);
                            $AllLessons = 0;
                            $Percentage = 0;
                            $AllSessions = 0;
                            $NotBookedPercentage = 0;
                            $Status = "IN-Progress";
                            array_push($SelfPacedCourses, $row_tlsn10["SelfPacedCourses_id"]);
                        }
                    }
                }
            }
        }
        //     }
        // }
    }
}
echo json_encode($data);
