<?php
include('../includes/db.php');

$id = $_POST['course_id'];
$status = $_POST['status'];
$code = $_POST['code'];
$name = $_POST['name'];
$modules = $_POST['modules'];

$sql = "UPDATE courses SET status=?, code=?, _course_name=?, modules=? WHERE id=?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ssssi", $status, $code, $name, $modules, $id);

if ($stmt->execute()) {
    echo "success";
} else {
    echo "Database error: " . $stmt->error;
}
?>
<?php
header('Content-Type: application/json; charset=utf-8');
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli("localhost","root","","edencamp_eden_exam");
$mysqli->set_charset("utf8mb4");

if (($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST') {
  echo json_encode(['ok'=>false,'error'=>'POST required']); exit;
}

$id            = (int)($_POST['id'] ?? 0);
$status        = trim($_POST['status'] ?? '');
$code          = trim($_POST['code'] ?? '');
$course_name   = trim($_POST['course_name'] ?? '');   // from the form
$duration      = (int)($_POST['duration'] ?? 0);
$modules       = trim($_POST['modules'] ?? '');
$description   = trim($_POST['description'] ?? '');
$full_desc     = trim($_POST['course_description'] ?? '');

if ($id <= 0 || $status==='' || $code==='' || $course_name==='' || $duration<=0) {
  echo json_encode(['ok'=>false,'error'=>'Missing required fields']); exit;
}

// detect which name column(s) exist
$has = [];
$stmt = $mysqli->prepare("
  SELECT COLUMN_NAME
  FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE()
    AND TABLE_NAME = 'courses'
    AND COLUMN_NAME IN ('course_name','name','status','code','duration','modules','description','course_description')
");
$stmt->execute();
$res = $stmt->get_result();
while ($r = $res->fetch_assoc()) { $has[$r['COLUMN_NAME']] = true; }
$stmt->close();

// build SET clause dynamically
$sets = []; $types=''; $params=[];

if (isset($has['status']))               { $sets[]='`status`=?';               $types.='s'; $params[]=&$status; }
if (isset($has['code']))                 { $sets[]='`code`=?';                 $types.='s'; $params[]=&$code; }
if (isset($has['course_name']))          { $sets[]='`course_name`=?';          $types.='s'; $params[]=&$course_name; }
if (isset($has['name']))                 { $sets[]='`name`=?';                 $types.='s'; $params[]=&$course_name; } // mirror into name if present
if (isset($has['duration']))             { $sets[]='`duration`=?';             $types.='i'; $params[]=&$duration; }
if (isset($has['modules']))              { $sets[]='`modules`=?';              $types.='s'; $params[]=&$modules; }
if (isset($has['description']))          { $sets[]='`description`=?';          $types.='s'; $params[]=&$description; }
if (isset($has['course_description']))   { $sets[]='`course_description`=?';   $types.='s'; $params[]=&$full_desc; }

if (!$sets) { echo json_encode(['ok'=>false,'error'=>'No updatable columns found']); exit; }

$sql = "UPDATE `courses` SET ".implode(', ', $sets)." WHERE id=?";
$types .= 'i';
$params[] = &$id;

$stmt = $mysqli->prepare($sql);

// bind params dynamically
$bind = []; $bind[] = &$types;
foreach ($params as $k => &$v) { $bind[] = &$v; }
call_user_func_array([$stmt, 'bind_param'], $bind);

$stmt->execute();
$stmt->close();

echo json_encode(['ok'=>true, 'id'=>$id, 'course_name'=>$course_name, 'code'=>$code, 'status'=>$status]);
