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


    $course_lesson_plan_ids=array();
    $sql_d12="SELECT * FROM centerCourse WHERE centerCourse_id='".$_GET["centerCourse_id"]."'";
 
    $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 course_lesson_plan WHERE  course_id='".$row_d12['course_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( $course_lesson_plan_ids, $row_clp["course_lesson_plan_id"]);
                }
            }
        }
    }
 
    $Dates=explode(",",$_GET["DateTimeSlots"]);
    $days_of_week = array();
    $time_slots = array();
    foreach($Dates as $dated) {
        $currentDate=explode("****",$dated);
     
        array_push( $days_of_week, $currentDate[0]);
        array_push( $time_slots, $currentDate[1]);
    }
    echo  "zize : ".sizeof($days_of_week)."\n";
    $lesson_days=array();
    $slot_days=array();
    $sql_d12="SELECT * FROM curriculumSession WHERE curriculumSession_id='".$_GET["curriculumSession_id"]."' ";
    echo $sql_d12;
    $result_d12 = mysqli_query($conn, $sql_d12);  
    if (mysqli_num_rows($result_d12) > 0) {
        while($row_d12 = mysqli_fetch_assoc($result_d12)) { 

            $dateFrom =new \DateTime($row_d12["StartDate"]);
            $dateTo = new \DateTime($row_d12["EndDate"]);

            // $dates = [];
            // $week_days=array();
            $nowDate= $dateFrom;
            $loopdate =$nowDate->modify("-1 days");
            if (1 != $dateFrom->format('N')) {
                $dateFrom->modify($val);
            }
            while ($loopdate <= $dateTo) {
                $loopdate->modify('+1 day');
                $day_of_week_tdy = $loopdate->format('l');
                $i=0;
                foreach ($days_of_week as $dayweek) {
                    if($day_of_week_tdy==$dayweek){
                        array_push( $lesson_days, $loopdate->format('Y-m-d'));
                        array_push( $slot_days, $time_slots[$i]);
                    }
                    $i=$i+1;
                }
            }
        }

    }
    $reference_id=0;
    foreach($course_lesson_plan_ids as $course_lesson_plan_id) {
        $sql_add_home_lesson = "INSERT INTO teacherLesson VALUES (NULL,
            '".$slot_days[$reference_id]."', 
            '".$_GET["teacher_id"]."',
            '".$_GET["centerCourse_id"]."', 
            '".$course_lesson_plan_id."',
            '".$lesson_days[$reference_id]."',  
            'true', 'false', 'false', 'false', 'false', now())";
            echo $sql_add_home_lesson ;
        if (mysqli_query($conn, $sql_add_home_lesson)) {
            $reference_id=$reference_id+1;
            // unset($each_course["lesson_days"][0]);
            echo 'reference : '.$reference_id;
        }else{
            echo ''; 
        }

    }
    $curl_post_data = array(
        'Success' => 'true',
        'Existd' => 'true',
    );
    array_push($data, $curl_post_data );
    echo json_encode($data);
?>