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

$sql_sfc = "select * from SelfPacedCourses WHERE SelfPacedCourses_id='" . $_GET["SelfPacedCourses_id"] . "'  ";

$result_sfc = mysqli_query($conn, $sql_sfc);
if (mysqli_num_rows($result_sfc) > 0) {
    while ($row_sfc = mysqli_fetch_assoc($result_sfc)) {
        $sql_crs = "select * from courses WHERE course_id='" . $row_sfc["courses_id"] . "'  ";
        $result_crs = mysqli_query($conn, $sql_crs);
        if (mysqli_num_rows($result_crs) > 0) {
            while ($row_crs = mysqli_fetch_assoc($result_crs)) {
                $course_lesson_plans = array();

                $class_name = "Clicker";
                $sql = "select * from SelfPacedClasses  WHERE SelfPacedClasses_id='" . $row_sfc["SelfPacedClasses_id"] . "'  ";
                $result = mysqli_query($conn, $sql);
                if (mysqli_num_rows($result) > 0) {
                    while ($row = mysqli_fetch_assoc($result)) {
                        $sql_20 = "select * from classes WHERE class_id='" . $row["class_id"] . "'  ";
                        $result_20 = mysqli_query($conn, $sql_20);
                        if (mysqli_num_rows($result_20) > 0) {
                            while ($row_20 = mysqli_fetch_assoc($result_20)) {
                                $class_name = $row_20["class_name"];
                            }
                        }
                    }
                }
                $sql = "select * from course_lesson_plan  WHERE course_id='" . $row_sfc["courses_id"] . "'  ";
                $result = mysqli_query($conn, $sql);
                if (mysqli_num_rows($result) > 0) {
                    while ($row = mysqli_fetch_assoc($result)) {
                        array_push($course_lesson_plans, $row);
                    }
                }
                $theme_data = array(
                    'primary' => '#feb139',
                    'secondary' => '#38aae1',
                    'accent' => '#ffd700',
                    'background' => '#fff8e1'
                );
                if ($class_name === 'Crib') {
                    $theme_data = array(
                        'primary' => '#feb139',
                        'secondary' => '#38aae1',
                        'accent' => '#ffd700',
                        'background' => '#fff8e1'
                    );
                } else if ($class_name === 'Clicker') {
                    $theme_data = array(
                        'primary' => '#38aae1',
                        'secondary' => '#feb139',
                        'accent' => '#87ceeb',
                        'background' => '#f0f8ff'
                    );
                } else if ($class_name === 'Quest') {
                    $theme_data = array(
                        'primary' => '#25476a',
                        'secondary' => '#38aae1',
                        'accent' => '#feb139',
                        'background' => '#f8fafc'
                    );
                }

                $description = '';
                if (file_exists("/home/sendusco/app/" . $row_cls["course_description"])) {
                    $description = file_get_contents("/home/sendusco/app/" . $row_cls["course_description"]);
                }
                $curl_post_data = array(
                    'course_id' => $row_crs["course_id"],
                    'course_name' => $row_crs["course_name"],
                    'course_image' => $row_crs["course_image"],
                    'course_description' => $description,
                    'expected_outcome' => $row_crs["expected_outcome"],
                    'delivery_mode' => $row_crs["delivery_mode"],
                    'minimum_age' => $row_crs["minimum_age"],
                    'required_sessions' => $row_crs["required_sessions"],
                    'SelfPacedCourses_id' => $row_sfc["SelfPacedCourses_id"],
                    'SelfPacedClasses_id' => $row_sfc["SelfPacedClasses_id"],
                    'orderNo' => $row_sfc["orderNo"],
                    'Price' => $row_sfc["Price"],
                    'Active' => $row_sfc["Active"],
                    'class_name' => $class_name,
                    'theme' => $theme_data,
                    'Lessons' => $course_lesson_plans,
                );
                // array_push($data, $curl_post_data);
                echo json_encode($curl_post_data);
            }
        }
    }
}


// echo json_encode($data);
