<?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["subject_id"] ?? null;
    $course_id = $data["course_id"] ?? null;
    $subject = $data["subject"] ?? null;
    $price = $data["price"] ?? null;

    if (empty($course_id)) {
        $response["message"] = "Please Select Course";
    } else if (empty($subject)) {
        $response["message"] = "Please Enter Subject Name";
    }else if(empty($price)){
        $response["message"] = "Please Enter Price";
    } else {


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

            try {
                Database::iud("UPDATE `subject` SET `course_id` = '" . $course_id . "' , `title` = '" . $subject . "' , `price`  = '".$price."' 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;
