<?php
session_start();
include "connection.php";
include "validation.php";

header("Content-Type: application/json");

$response = [
    "status" => false,
    "message" => ""
];

try {
    $json = file_get_contents("php://input");
    $data = json_decode($json, true);

    $category = $data["category"] ?? null;
    $course = $data["course"] ?? null;

    if (empty($category)) {
        $response["message"] = "Please Select Category";
    } else if (empty($course)) {
        $response["message"] = "Please Enter Course Name";
    } else {


        $check_rs = Database::search("SELECT * FROM `course` WHERE `title` = '" . $course . "' AND `category_id` = '" . $category . "'");
        if ($check_rs->num_rows > 0) {
            $response["status"] = false;
            $response["message"] = "This Couse Already Exists!";
        } else {

            Database::iud("INSERT INTO `course` ( `category_id`,`title`) 
            VALUES ( '" . $category . "','" . $course . "')");

            $response["status"] = true;
            $response["message"] = "Course Added Successfully!";
        }
    }
} catch (Exception $e) {
    $response["status"] = false;
    $response["message"] = $e->getMessage();
}

echo json_encode($response);
exit;
