<?php
session_start();

if (!isset($_SESSION['clinic_logged_in']) || $_SESSION['clinic_logged_in'] !== true) {
    header('Location: index.php');
    exit;
}

// Optional: access admin ID like this
$admin_id = $_SESSION['clinic_id'];
include 'dbconn.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['id'])) {
    $id = intval($_POST['id']);

    $study_type = $_POST['study_type'];
    $num_arms = $_POST['num_arms'];
    $randomization_method = $_POST['randomization_method'];
    $blinding_type = $_POST['blinding_type'];
    $study_duration = $_POST['study_duration'];
    $sample_size = $_POST['sample_size'];
    $num_sites = $_POST['num_sites'];
    $target_population = $_POST['target_population'];
    $inclusion_criteria = $_POST['inclusion_criteria'];
    $exclusion_criteria = $_POST['exclusion_criteria'];

    $sql = "UPDATE study_design SET 
                study_type = ?, 
                num_arms = ?, 
                randomization_method = ?, 
                blinding_type = ?, 
                study_duration = ?, 
                sample_size = ?, 
                num_sites = ?, 
                target_population = ?, 
                inclusion_criteria = ?, 
                exclusion_criteria = ?
            WHERE id = ?";

    $stmt = $mysqli->prepare($sql);
    $stmt->bind_param(
        "sissiiisssi",
        $study_type,
        $num_arms,
        $randomization_method,
        $blinding_type,
        $study_duration,
        $sample_size,
        $num_sites,
        $target_population,
        $inclusion_criteria,
        $exclusion_criteria,
        $id
    );

    if ($stmt->execute()) {
        echo "<script>alert('Study Design updated successfully.'); window.location.href='study_design.php?id=$id';</script>";
    } else {
        echo "<script>alert('Error updating Study Design.'); window.history.back();</script>";
    }

    $stmt->close();
    $mysqli->close();
} else {
    echo "<script>alert('Invalid request.'); window.history.back();</script>";
}
