<?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 = file_get_contents('php://input');

$obj = json_decode($data, true);
// echo json_encode($obj["Courses"]);

$sql_c = "select * from SelfPacedClasses   WHERE class_id='" . $_GET["class_id"] . "'  ";
$result_c = mysqli_query($conn, $sql_c);
if (mysqli_num_rows($result_c) === 0) {

    $sql_we = 'INSERT INTO SelfPacedClasses VALUES (NULL,
        "' . $_GET["class_id"] . '",
        "",
        "' . $obj["PassMark"] . '",
        "' . $obj["Published"] . '",
        "true",
        "true",
        "true",
        now())';
    if (mysqli_query($conn, $sql_we)) {
        $SelfPacedClasses_id = mysqli_insert_id($conn);
        foreach ($obj["Courses"] as $course) {
            $sql_10 = 'INSERT INTO SelfPacedCourses VALUES (NULL,
                "' . $SelfPacedClasses_id . '",
                "' . $course["course_id"] . '",
                "' . $course["customPrice"] . '",
                "true",
                "false",
                "false",
                "false",
                now())';
            if (mysqli_query($conn, $sql_10)) {
                $SelfPacedCourses_id = mysqli_insert_id($conn);
                if ($course["order"] === 1) {
                    $sql_up = 'UPDATE SelfPacedClasses SET StarterCourse_id="' . $SelfPacedCourses_id . '" WHERE SelfPacedClasses_id="' . $SelfPacedClasses_id . '" ';
                    if (mysqli_query($conn, $sql_up)) {
                    }
                }
            }
        }
        $curl_post_data = array(
            'Success' => 'true',
            'Reason' => 'Class Succesfully Created'
        );
        echo json_encode($curl_post_data);
    }
} else {
    $curl_post_data = array(
        'Success' => 'false',
        'Reason' => 'Class Already Created, Edit class Instead'
    );
    echo json_encode($curl_post_data);
}


?>
