<?php
require_once 'config.php';
requireRole('admin');

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

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['new_status'])) {
    $conn = connectDB();
    $new_status = $_POST['new_status'] === '1' ? '1' : '0';
    
    // Update the setting
    $update_sql = "UPDATE settings SET setting_value = '$new_status' WHERE setting_name = 'show_results'";
    
    if ($conn->query($update_sql)) {
        echo json_encode(['success' => true, 'status' => $new_status]);
    } else {
        echo json_encode(['success' => false, 'error' => $conn->error]);
    }
    
    $conn->close();
} else {
    echo json_encode(['success' => false, 'error' => 'Invalid request']);
}
?>