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

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require '../../../vendor/autoload.php';
include '../../../config/index.php';
$data = file_get_contents('php://input');
$obj = json_decode($data, true);

function generatePassword($length = 6)
{
    $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    $count = mb_strlen($chars);

    for ($i = 0, $result = ''; $i < $length; $i++) {
        $index = rand(0, $count - 1);
        $result .= mb_substr($chars, $index, 1);
    }

    return $result;
}
function getCurrentClasses($class_id, $Streams_id, $center_id)
{
    include '../../../config/index.php';

    if ($Streams_id !== "") {
        $sql10 = "SELECT * FROM centerClasses WHERE class_id='" . $class_id . "' AND Streams_id='" . $Streams_id . "' AND center_id='" . $center_id . "' ORDER BY dated DESC LIMIT 1";
        // return (" sql 10" . $sql10);

        $result10 = mysqli_query($conn, $sql10);
        if (mysqli_num_rows($result10) > 0) {
            while ($row10 = mysqli_fetch_assoc($result10)) {
                $todaysDate = date("Y-m-d");
                $StartDate = date("Y-m-d", strtotime($row_enr["StartDate"]));

                $year = date("Y-m-d", strtotime($todaysDate));

                $sql_enr = "SELECT * FROM curriculumSession WHERE  curriculumEnrollment_id='" . $row10["curriculumEnrollment_id"] . "'  ";

                $result_enr = mysqli_query($conn, $sql_enr);
                if (mysqli_num_rows($result_enr) > 0) {
                    while ($row_enr = mysqli_fetch_assoc($result_enr)) {
                        $StartDate = date("Y", strtotime($row_enr["EndDate"]));

                        $year = date("Y", strtotime($todaysDate));
                        // echo " sql year" .$year;
                        //  echo " sql StartDate" .$StartDate;
                        // if ($StartDate >= $year) {
                        //     return ($row10["centerClasses_id"]  );

                        // }
                        if ($StartDate >= $year) {

                            $curl_post_data = array(
                                'centerClasses_id' => $row10["centerClasses_id"],
                                'curriculumEnrollment_id' => $row_enr["curriculumEnrollment_id"],
                                'SessionName' => $row_enr["SessionName"],
                                'StartDate' => $row_enr["StartDate"],
                                'EndDate' => $row_enr["EndDate"]

                            );

                            return ($row10["centerClasses_id"]);
                        }
                    }
                }
            }
        } else {
            $sql10 = "SELECT * FROM centerClasses WHERE class_id='" . $class_id . "'  AND center_id='" . $center_id . "' ";

            $result10 = mysqli_query($conn, $sql10);
            if (mysqli_num_rows($result10) > 0) {
                while ($row10 = mysqli_fetch_assoc($result10)) {
                    $todaysDate = date("Y-m-d");
                    $StartDate = date("Y-m-d", strtotime($row_enr["StartDate"]));

                    $year = date("Y-m-d", strtotime($todaysDate));

                    $sql_enr = "SELECT * FROM curriculumSession WHERE  curriculumEnrollment_id='" . $row10["curriculumEnrollment_id"] . "'  ";

                    $result_enr = mysqli_query($conn, $sql_enr);
                    if (mysqli_num_rows($result_enr) > 0) {
                        while ($row_enr = mysqli_fetch_assoc($result_enr)) {
                            $StartDate = date("Y", strtotime($row_enr["EndDate"]));

                            $year = date("Y", strtotime($todaysDate));
                            // return (" StartDate  " .  $StartDate. " : year  " .  $year);
                            if ($StartDate >= $year) {
                                $curl_post_data = array(
                                    'centerClasses_id' => $row10["centerClasses_id"],
                                    'curriculumEnrollment_id' => $row_enr["curriculumEnrollment_id"],
                                    'SessionName' => $row_enr["SessionName"],
                                    'StartDate' => $row_enr["StartDate"],
                                    'EndDate' => $row_enr["EndDate"]

                                );

                                return ($row10["centerClasses_id"]);
                            }
                        }
                    }
                }
            }
        }
    } else {
        $sql10 = "SELECT * FROM centerClasses WHERE class_id='" . $class_id . "'  AND center_id='" . $center_id . "' ";
        $result10 = mysqli_query($conn, $sql10);
        if (mysqli_num_rows($result10) > 0) {
            while ($row10 = mysqli_fetch_assoc($result10)) {
                $todaysDate = date("Y-m-d");
                $StartDate = date("Y-m-d", strtotime($row_enr["StartDate"]));

                $year = date("Y-m-d", strtotime($todaysDate));

                $sql_enr = "SELECT * FROM curriculumSession WHERE  curriculumEnrollment_id='" . $row10["curriculumEnrollment_id"] . "'  ";

                $result_enr = mysqli_query($conn, $sql_enr);
                if (mysqli_num_rows($result_enr) > 0) {
                    while ($row_enr = mysqli_fetch_assoc($result_enr)) {
                        $StartDate = date("Y", strtotime($row_enr["EndDate"]));

                        $year = date("Y", strtotime($todaysDate));
                        if ($StartDate >= $year) {
                            $curl_post_data = array(
                                'centerClasses_id' => $row10["centerClasses_id"],
                                'curriculumEnrollment_id' => $row_enr["curriculumEnrollment_id"],
                                'SessionName' => $row_enr["SessionName"],
                                'StartDate' => $row_enr["StartDate"],
                                'EndDate' => $row_enr["EndDate"]

                            );

                            return ($row10["centerClasses_id"]);
                        }
                    }
                }
            }
        }
    }
}
$class_id = $_GET['class_id'];
$Streams_id = $_GET['Streams_id'];
// $Streams_id = "";
$center_id = $_GET['center_id'];
$centerClasses_id = getCurrentClasses(class_id: $class_id, Streams_id: $Streams_id, center_id: $center_id);


foreach ($obj as $x => $z) {

    $learner_name = mysqli_real_escape_string($conn, $z["learner_name"]);
    $learner_last_name = mysqli_real_escape_string($conn, $z["learner_last_name"]);
    $learner_gender = mysqli_real_escape_string($conn, $z["learner_gender"]);
    $date_of_birth = mysqli_real_escape_string($conn, $z["date_of_birth"]);
    $Password = mysqli_real_escape_string($conn, $z["password"]);
    if (isset($Password)) {
        $sql = "INSERT INTO students VALUES (NULL,
            '',
            '" . $_GET['center_id'] . "',
            '" . $centerClasses_id . "',
            '$learner_name',
            '$learner_last_name',
            '$learner_gender',
            '',
            '',
            '',
            '" . $date_of_birth . "',
            'assets/img/fav.png',
            'true',
            'false',
            '',
            '" . md5(trim($Password)) . "',
            'false', 
            'false', 
            'false',
            'false',
            'false', 
            'false', 
            now())";
        if (mysqli_query($conn, $sql)) {
            $student_id = mysqli_insert_id($conn);
            $username = "DS00" . $student_id;
            $sql_Query = 'UPDATE students SET
                                username="' . $username . '"
                                WHERE student_id="' . $student_id . '"';
            if (mysqli_query($conn, $sql_Query)) {
            }
            $sql_cc = "select * from centerCourse WHERE centerClasses_id='" . $centerClasses_id . "'    ";

            $result_cc = mysqli_query($conn, $sql_cc);
            if (mysqli_num_rows($result_cc) > 0) {
                while ($row_cc = mysqli_fetch_assoc($result_cc)) {
                    $sql_10 = "SELECT * FROM mentorCourse  WHERE  course_id='" . $row_cc['course_id'] . "' AND center_id='" . $row_cc['center_id'] . "' ";

                    $result_10 = mysqli_query($conn, $sql_10);
                    if (mysqli_num_rows($result_10) > 0) {
                        while ($row_10 = mysqli_fetch_assoc($result_10)) {
                            $sql_tl = "select * from mentorLesson WHERE mentor_id='" . $row_10['mentor_id'] . "' AND mentorCourse_id='" . $row_10['mentorCourse_id'] . "' AND centerCourse_id='" . $row_cc['centerCourse_id'] . "'   ";

                            $result_tl = mysqli_query($conn, $sql_tl);
                            if (mysqli_num_rows($result_tl) > 0) {
                                while ($row_tl = mysqli_fetch_assoc($result_tl)) {
                                    $sql_tla = "select * from mentorLessonAssignment WHERE Status='true' AND  mentorLesson_id='" . $row_tl['mentorLesson_id'] . "'";

                                    $result_tla = mysqli_query($conn, $sql_tla);
                                    if (mysqli_num_rows($result_tla) == 0) {
                                        $sql_sa = 'INSERT INTO MentorStudentAssignment VALUES(NULL,
                                        "' . $student_id . '",
                                        "' . $row_tl["mentorLesson_id"] . '",
                                        "' . $row_tl["centerCourse_id"] . '",
                                        "' . $row_tl["course_lesson_plan_id"] . '",
                                        "",
                                        "Issued",
                                        "",
                                        now(),
                                        now())';
                                        if (mysqli_query($conn, $sql_sa)) {
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    } else {
        echo "not set";
    }
}
$curl_post_data = array(
    'Success' => 'true',
    'Reason' => 'You Have Succesfully Added Students',
    'student_id' => ''
);
echo json_encode($curl_post_data);
