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

    $id = $data["course_id"] ?? null;
    $category = $data["category"] ?? null;
    $title = $data["title"] ?? null;

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


        $check_rs = Database::search("SELECT * FROM `course` WHERE `id` = '" . $id . "'");
        if ($check_rs->num_rows > 0) {

            try {
                Database::iud("UPDATE `course` SET `category_id` = '" . $category . "' , `title` = '" . $title . "' WHERE `id` = '" . $id . "' ");
                $response["status"] = true;
                $response["message"] = "Course Update Successfull!";
            } catch (Exception $e) {
                $response["status"] = false;
                $response["message"] = $e->getMessage();
            }
        } else {
            $response["status"] = false;
            $response["message"] = "Something Went Wrong!";
        }
    }
} catch (Exception $e) {
    $response["status"] = false;
    $response["message"] = $e->getMessage();
}

echo json_encode($response);
exit;
