<?php
require "config.php";
session_start();

$teacher_id = $_SESSION['teacher_id'] ?? null;
if(!$teacher_id){ exit("Unauthorized"); }

$id = $_POST['id'] ?? null;
$current = $_POST['status'] ?? null;

if(!$id || !$current){ exit("Missing data"); }

$new = ($current=='published'?'draft':'published');

$stmt = $dbh->prepare("UPDATE exams SET status=:st WHERE id=:id AND teacher_id=:tid");
$ok = $stmt->execute([':st'=>$new, ':id'=>$id, ':tid'=>$teacher_id]);

echo $ok ? "Status toggled to $new." : "Failed to toggle status.";
