<?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") {
    $id = intval($_POST['id']);

    $study_title       = $_POST['study_title'];
    $study_acronym     = $_POST['study_acronym'];
    $protocol_number   = $_POST['protocol_number'];
    $sponsor_name      = $_POST['sponsor_name'];
    $cro_name          = $_POST['cro_name'];
    $study_type        = $_POST['study_type'];
    $study_phase       = $_POST['study_phase'];
    $therapeutic_area  = $_POST['therapeutic_area'];
    $study_objectives  = $_POST['study_objectives'];
    $study_summary     = $_POST['study_summary'];

    // Prepare and execute update
    $stmt = $mysqli->prepare("
        UPDATE basic_information SET 
            study_title = ?, 
            study_acronym = ?, 
            protocol_number = ?, 
            sponsor_name = ?, 
            cro_name = ?, 
            study_type = ?, 
            study_phase = ?, 
            therapeutic_area = ?, 
            study_objectives = ?, 
            study_summary = ?
        WHERE id = ?
    ");

    $stmt->bind_param(
        "ssssssssssi",
        $study_title,
        $study_acronym,
        $protocol_number,
        $sponsor_name,
        $cro_name,
        $study_type,
        $study_phase,
        $therapeutic_area,
        $study_objectives,
        $study_summary,
        $id
    );

     if ($stmt->execute()) {
            echo "<script>
                alert('details updated successfully.');
                window.location.href = 'basic_information.php'; // Change to your target page
            </script>";
        } else {
            echo "Error updating record: " . $stmt->error;
        }


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