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


$sql_c = "SELECT * FROM CourseIntroductionVideo   WHERE SelfPacedCourses_id='" . $obj["SelfPacedCourses_id"] . "'  ";

$result_c = mysqli_query($conn, $sql_c);
if (mysqli_num_rows($result_c) > 0) {
    while ($row_c = mysqli_fetch_assoc($result_c)) {
        $sql_we = 'UPDATE CourseIntroductionVideo SET
        VideoName= "' . $obj["VideoName"] . '",
        Video= "' . $obj["Video"] . '"
        WHERE CourseIntroductionVideo_id ="' . $row_c["CourseIntroductionVideo_id"] . '"';

        if (mysqli_query($conn, $sql_we)) {

            $curl_post_data = array(
                'Success' => 'true',
                'Reason' => 'You have succesfully Updated the Introduction Video'
            );
            echo json_encode($curl_post_data);
        } else {
            $curl_post_data = array(
                'Success' => 'false',
                'Reason' => 'Unable to succesfully Update the Introduction Video, Contact Admin for help'
            );
            echo json_encode($curl_post_data);
        }
    }
} else {
    $sql_we = 'INSERT INTO CourseIntroductionVideo VALUES (NULL,
        "' . $obj["SelfPacedCourses_id"] . '",
        "' . $obj["course_id"] . '",
        "' . $obj["VideoName"] . '",
        "",
        "' . $obj["Video"] . '",
        "true",
        "false",
        "false",
        "false",
        now())';;
    if (mysqli_query($conn, $sql_we)) {
        $SelfPacedClasses_id = mysqli_insert_id($conn);

        $curl_post_data = array(
            'Success' => 'true',
            'Reason' => 'You have succesfully Added an Introduction Video'
        );
        echo json_encode($curl_post_data);
    } else {
        $curl_post_data = array(
            'Success' => 'false',
            'Reason' => 'Unable to succesfully Update the Introduction Video, Contact Admin for help'
        );
        echo json_encode($curl_post_data);
    }
}
