<?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();
$sql1 = "SELECT * FROM courses ";
$result1 = mysqli_query($conn, $sql1);
if (mysqli_num_rows($result1) > 0) {
    while ($row1 = mysqli_fetch_assoc($result1)) {
        $prerequisite_courses = array();
        $sql10 = "SELECT * FROM prerequisite_courses WHERE course_id='" . $row1["course_id"] . "' ";
        $result10 = mysqli_query($conn, $sql10);
        if (mysqli_num_rows($result10) > 0) {
            while ($row10 = mysqli_fetch_assoc($result10)) {
                $sql20 = "SELECT * FROM courses WHERE course_id='" . $row10["pre_requisite_course"] . "' ";
                $result20 = mysqli_query($conn, $sql20);
                if (mysqli_num_rows($result20) > 0) {
                    while ($row20 = mysqli_fetch_assoc($result20)) {
                        $Price = "12,000";

                        $sql100 = "SELECT * FROM FeatureCourses WHERE course_id='" . $row20["course_id"] . "' ";
                        $result100 = mysqli_query($conn, $sql100);
                        if (mysqli_num_rows($result100) > 0) {
                            while ($row100 = mysqli_fetch_assoc($result100)) {
                                $Price = $row100["Price"];
                            }
                        }
                        $curl_post_data = array(
                            'course_id' => $row10["course_id"],
                            'course_name' => $row20["course_name"],
                            'course_image' => $row20["course_image"],
                            'course_description' => $row20["course_description"],
                            'expected_outcome' => $row20["expected_outcome"],
                            'delivery_mode' => $row20["delivery_mode"],
                            'minimum_age' => $row20["minimum_age"],
                            'required_sessions' => $row20["required_sessions"],
                            'active' => $row20["active"],
                            'Price' => $Price,
                        );
                        array_push($prerequisite_courses, $curl_post_data);
                    }
                }
            }
        }
        $tags_courses = array();
        $course_tag = "";
        $i = 1;
        $sql10 = "SELECT * FROM tags_courses WHERE course_id='" . $row1["course_id"] . "' ";
        $result10 = mysqli_query($conn, $sql10);
        if (mysqli_num_rows($result10) > 0) {
            while ($row10 = mysqli_fetch_assoc($result10)) {
                $sql20 = "SELECT * FROM course_tags WHERE course_tags_id='" . $row10["course_tags_id"] . "' ";
                $result20 = mysqli_query($conn, $sql20);
                if (mysqli_num_rows($result20) > 0) {
                    while ($row20 = mysqli_fetch_assoc($result20)) {
                        $curl_post_data = array(
                            'tags_courses_id' => $row10["tags_courses_id"],
                            'course_id' => $row1["course_id"],
                            'course_tags_id' => $row20["course_tags_id"],
                            'course_tag' => $row20["course_tag"],

                        );
                        array_push($tags_courses, $curl_post_data);
                        if ($i === 1) {
                            $course_tag = $row20["course_tag"];
                        }
                        $i = $i + 1;
                    }
                }
            }
        }
        $Price = "";
        $Featured = "False";
        $FeatureCourses_id = "";
        $FeatureLocations = array();
        $sql10 = "SELECT * FROM FeatureCourses WHERE course_id='" . $row1["course_id"] . "' ";
        $result10 = mysqli_query($conn, $sql10);
        if (mysqli_num_rows($result10) > 0) {
            while ($row10 = mysqli_fetch_assoc($result10)) {
                $FeatureCourses_id = $row10["FeatureCourses_id"];
                $Price = $row10["Price"];
                $Featured = "True";
            }
        }
        $sql10 = "SELECT * FROM FeatureLocations WHERE course_id='" . $row1["course_id"] . "' ";
        $result10 = mysqli_query($conn, $sql10);
        if (mysqli_num_rows($result10) > 0) {
            while ($row10 = mysqli_fetch_assoc($result10)) {
                $sql20 = "SELECT * FROM digicenters WHERE center_id='" . $row10["center_id"] . "' ";
                $result20 = mysqli_query($conn, $sql20);
                if (mysqli_num_rows($result20) > 0) {
                    while ($row20 = mysqli_fetch_assoc($result20)) {
                        $curl_post_data = array(
                            'course_id' => $row10["course_id"],
                            'center_id' => $row20["center_id"],
                            'center_name' => $row20["center_name"],
                            'center_email' => $row20["center_email"],
                            'center_phone' => $row20["center_phone"],
                            'center_location' => $row20["center_location"],
                            'center_lat' => $row20["center_lat"],
                            'center_long' => $row20["center_long"],
                            'center_image' => $row20["center_image"],
                        );
                        array_push($FeatureLocations, $curl_post_data);
                    }
                }
            }
        }
        $theme_data = array(
            'primary' => '#feb139',
            'secondary' => '#38aae1',
            'accent' => '#ffd700',
            'background' => '#fff8e1'
        );
        $curl_post_data = array(
            'course_id' => $row1["course_id"],
            'course_name' => $row1["course_name"],
            'course_image' => $row1["course_image"],
            'course_description' => $row1["course_description"],
            'expected_outcome' => $row1["expected_outcome"],
            'delivery_mode' => $row1["delivery_mode"],
            'minimum_age' => $row1["minimum_age"],
            'required_sessions' => $row1["required_sessions"],
            'active' => $row1["active"],
            'Price' => $Price,
            'Featured' => $Featured,
            'course_tag' => $course_tag,
            'FeatureCourses_id' => $FeatureCourses_id,
            'prerequisite_courses' => $prerequisite_courses,
            'tags_courses' => $tags_courses,
            'theme' => $theme_data,
            'FeatureLocations' => $FeatureLocations,
        );
        array_push($data, $curl_post_data);
    }
}
echo json_encode($data);
