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


$TotalHours = 0;
$TotalLessons = 0;
$TaughtLessons = 0;
$HoursDone = 0;
$ClaimedLessons = 0;
$ApprovedLessons = 0;
$RejectedLessons = 0;
$PaidLessons = 0;
$Percentage = 0;
$RemainingPercentage = 0;
$Earnings = 0;
$ClaimedLessons = 0;
$ClaimedLessons = 0;

$ClaimRequest = "false";
$ClaimRequestDate = 0;
$ClaimAmount = 0;
$ClaimThreshold = 0;
$Advance = 0;
$AdvanceRequestDate = 0;
$AdvanceThreshold = 0;
$MaxAdvance = 0;
$Approved = 0;
$Rejected = 0;
$Reason = 0;
$PaymentMade = "false";
$PaymentMadeDate = 0;
$AdvanceApproved = 0;
$AdvanceRejected = 0;
$AdvanceReason = 0;
$AdvancePaymentMade = 0;
$AdvancePaymentMadeDate = 0;
$AdvanceAmountPaid = 0;
$ClaimAmountPaid = 0;
$AdvanceInvoice = "";
$ClaimInvoice = "";
$mentorCourseEarnings_id = "";

$sql20 = "SELECT * FROM mentorCourse ";

$result_20 = mysqli_query($conn, $sql20);
if (mysqli_num_rows($result_20) > 0) {
    while ($row_20 = mysqli_fetch_assoc($result_20)) {
        $course_lesson_plan_ids = array();
        $CourseLogs = array();
        $sql_mce = "SELECT * FROM mentorCourseEarnings WHERE   mentorCourse_id='" . $row_20["mentorCourse_id"] . "'";
        $result_mce = mysqli_query($conn, $sql_mce);
        if (mysqli_num_rows($result_mce) > 0) {
            while ($row_mce = mysqli_fetch_assoc($result_mce)) {
                $mentorCourseEarnings_id = $row_mce["mentorCourseEarnings_id"];
                $ClaimRequest = $row_mce["ClaimRequest"];
                $ClaimRequestDate = $row_mce["ClaimRequestDate"];
                $ClaimAmount = $row_mce["ClaimAmount"];
                if (empty($ClaimAmount)) {
                    $ClaimAmount = 0;
                }
                $ClaimThreshold = $row_mce["ClaimThreshold"];
                $Advance = $row_mce["Advance"];
                $AdvanceRequestDate = $row_mce["AdvanceRequestDate"];
                $AdvanceThreshold = $row_mce["AdvanceThreshold"];
                $MaxAdvance = $row_mce["MaxAdvance"];
                $Approved = $row_mce["Approved"];
                $Rejected = $row_mce["Rejected"];
                $Reason = $row_mce["Reason"];
                $PaymentMade = $row_mce["PaymentMade"];
                $PaymentMadeDate = $row_mce["PaymentMadeDate"];
                $AdvanceApproved = $row_mce["AdvanceApproved"];
                $AdvanceRejected = $row_mce["AdvanceRejected"];
                $AdvanceReason = $row_mce["AdvanceReason"];
                $AdvancePaymentMade = $row_mce["AdvancePaymentMade"];
                $AdvancePaymentMadeDate = $row_mce["AdvancePaymentMadeDate"];
                $AdvanceAmountPaid = $row_mce["AdvanceAmountPaid"];
                $ClaimAmountPaid = $row_mce["ClaimAmountPaid"];
                $AdvanceInvoice = $row_mce["flag_1"];
                $ClaimInvoice = $row_mce["flag_2"];
                $sql_clp = "SELECT * FROM mentorCourseEarningsLogs WHERE   mentorCourseEarnings_id='" . $row_mce['mentorCourseEarnings_id'] . "'";
                $result_clp = mysqli_query($conn, $sql_clp);
                if (mysqli_num_rows($result_clp) > 0) {
                    while ($row_clp = mysqli_fetch_assoc($result_clp)) {
                        array_push($CourseLogs, $row_clp);
                    }
                }
            }
        }


        $lessons = array();
        $lesson_session;
        $course_lesson_plan_id;
        $date_of_lesson;
        $course_name;
        $course_image;
        $center_name;
        $class_name;
        $SessionName;
        $YearName;
        $StartDate;
        $EndDate;
        $centerClasses_id;
        $mentor_name;
        $mentor_email;
        $mentor_image;
        $mentor_phone;
        $sql_clp = "SELECT * FROM courses WHERE  course_id='" . $row_20['course_id'] . "'";
        $result_clp = mysqli_query($conn, $sql_clp);
        if (mysqli_num_rows($result_clp) > 0) {
            while ($row_clp = mysqli_fetch_assoc($result_clp)) {
                $course_name = $row_clp["course_name"];
                $course_image = $row_clp["course_image"];
            }
        }
        $sql_clp = "SELECT * FROM mentors WHERE  mentor_id='" . $row_20['mentor_id'] . "'";
        $result_clp = mysqli_query($conn, $sql_clp);
        if (mysqli_num_rows($result_clp) > 0) {
            while ($row_clp = mysqli_fetch_assoc($result_clp)) {
                $mentor_name = $row_clp["mentor_name"];
                $mentor_email = $row_clp["mentor_email"];
                $mentor_image = $row_clp["mentor_image"];
                $mentor_phone = $row_clp["mentor_phone"];
            }
        }
        $sql_clp = "SELECT * FROM mentorLesson WHERE   mentor_id='" . $row_20['mentor_id'] . "' AND  mentorCourse_id='" . $row_20['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)) {
                $Period;
                $StartTime;
                $EndTime;
                array_push($lessons, $row_clp);
                $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 = "08:00:00";
                        $EndTime = "09:00:00";
                        if ($row_10["StartTime"] != "") {
                            $StartTime = $row_10["StartTime"];
                        }
                        if ($row_10["EndTime"] != "") {
                            $EndTime = $row_10["EndTime"];
                        }


                    }
                }
                $Starttimestamp = strtotime($StartTime);
                $Endtimestamp = strtotime($EndTime);
                $Difference = abs($Endtimestamp - $Starttimestamp) / 3600;

                $TotalHours = $TotalHours + $Difference;


                $centerClasses_id;
                $sql_d12 = "SELECT * FROM centerCourse WHERE centerCourse_id='" . $row_clp["centerCourse_id"] . "'";
                $result_d12 = mysqli_query($conn, $sql_d12);
                if (mysqli_num_rows($result_d12) > 0) {
                    while ($row_d12 = mysqli_fetch_assoc($result_d12)) {

                        $centerClasses_id = $row_d12["centerClasses_id"];

                        $sql_cs = "SELECT * FROM curriculumSession WHERE  curriculumSession_id='" . $row_d12['curriculumSession_id'] . "'";
                        $result_cs = mysqli_query($conn, $sql_cs);
                        if (mysqli_num_rows($result_cs) > 0) {
                            while ($row_cs = mysqli_fetch_assoc($result_cs)) {
                                $SessionName = $row_cs["SessionName"];
                                $StartDate = $row_cs["StartDate"];
                                $EndDate = $row_cs["EndDate"];
                                $sql_10 = "SELECT * FROM curriculumEnrollment WHERE  curriculumEnrollment_id='" . $row_cs['curriculumEnrollment_id'] . "'";
                                $result_10 = mysqli_query($conn, $sql_10);
                                if (mysqli_num_rows($result_10) > 0) {
                                    while ($row_10 = mysqli_fetch_assoc($result_10)) {
                                        $YearName = $row_10["YearName"];
                                    }
                                }
                            }
                        }
                        $sql_cs = "SELECT * FROM centerClasses WHERE  centerClasses_id='" . $row_d12['centerClasses_id'] . "'";
                        $result_cs = mysqli_query($conn, $sql_cs);
                        if (mysqli_num_rows($result_cs) > 0) {
                            while ($row_cs = mysqli_fetch_assoc($result_cs)) {

                                $sql_10 = "SELECT * FROM classes WHERE  class_id='" . $row_cs['class_id'] . "'";
                                $result_10 = mysqli_query($conn, $sql_10);
                                if (mysqli_num_rows($result_10) > 0) {
                                    while ($row_10 = mysqli_fetch_assoc($result_10)) {
                                        $class_name = $row_10["class_name"];
                                    }
                                }
                            }
                        }

                        $sql_cs = "SELECT * FROM digicenters WHERE  center_id='" . $row_d12['center_id'] . "'";
                        $result_cs = mysqli_query($conn, $sql_cs);
                        if (mysqli_num_rows($result_cs) > 0) {
                            while ($row_cs = mysqli_fetch_assoc($result_cs)) {
                                $center_name = $row_cs["center_name"];
                            }
                        }
                    }
                }


                $sql_10 = "SELECT * FROM course_lesson_plan WHERE  course_lesson_plan_id='" . $row_clp['course_lesson_plan_id'] . "'";
                $result_10 = mysqli_query($conn, $sql_10);
                if (mysqli_num_rows($result_10) > 0) {
                    while ($row_10 = mysqli_fetch_assoc($result_10)) {
                        $lesson_session = $row_10["lesson_session"];
                    }
                }
                $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;
                }


            }
            $Percentage = 0;
            if ($TaughtLessons > 0) {
                $Percentage = ($TaughtLessons / $TotalLessons) * 100;


            }
            $RemainingPercentage = 100 - $Percentage;
        }


        if ($ClaimRequest == "true" && $PaymentMade !== "true") {
            $curl_post_data = array(
                'mentor_name' => $mentor_name,
                'mentor_email' => $mentor_email,
                'mentor_image' => $mentor_image,
                'mentor_phone' => $mentor_phone,
                'mentorCourseEarnings_id' => $mentorCourseEarnings_id,
                'course_lesson_plan_id' => $course_lesson_plan_id,
                'date_of_lesson' => $date_of_lesson,
                'course_name' => $course_name,
                'course_image' => $course_image,
                'class_name' => $class_name,
                'YearName' => $YearName,
                'StartDate' => $StartDate,
                'EndDate' => $EndDate,
                'center_name' => $center_name,
                'SessionName' => $SessionName,
                'centerClasses_id' => $centerClasses_id,
                'TotalHours' => $TotalHours,
                'HoursDone' => $HoursDone,
                'TotalLessons' => $TotalLessons,
                'TaughtLessons' => $TaughtLessons,
                'ClaimedLessons' => $ClaimedLessons,
                'ApprovedLessons' => $ApprovedLessons,
                'RejectedLessons' => $RejectedLessons,
                'PaidLessons' => $PaidLessons,
                'Percentage' => $Percentage,
                'RemainingPercentage' => $RemainingPercentage,
                'Earnings' => $Earnings,
                'ClaimRequest' => $ClaimRequest,
                'ClaimRequestDate' => $ClaimRequestDate,
                'ClaimAmount' => $ClaimAmount,
                'ClaimThreshold' => $ClaimThreshold,
                'Advance' => $Advance,
                'AdvanceRequestDate' => $AdvanceRequestDate,
                'AdvanceThreshold' => $AdvanceThreshold,
                'MaxAdvance' => $MaxAdvance,
                'Approved' => $Approved,
                'Rejected' => $Rejected,
                'Reason' => $Reason,
                'PaymentMade' => $PaymentMade,
                'PaymentMadeDate' => $PaymentMadeDate,
                'AdvanceApproved' => $AdvanceApproved,
                'AdvanceRejected' => $AdvanceRejected,
                'AdvanceInvoice' => $AdvanceInvoice,
                'ClaimInvoice' => $ClaimInvoice,
                'AdvanceReason' => $AdvanceReason,
                'AdvancePaymentMade' => $AdvancePaymentMade,
                'AdvancePaymentMadeDate' => $AdvancePaymentMadeDate,
                'AdvanceAmountPaid' => $AdvanceAmountPaid,
                'ClaimAmountPaid' => $ClaimAmountPaid,
                'CourseLogs' => $CourseLogs,
                'lessons' => $lessons,

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


    }
}
echo json_encode($data);
?>