<?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') {
    // Use $mysqli from dbconn.php
    // Get form data safely
    $study_title = $mysqli->real_escape_string($_POST['study_title']);
    $study_acronym = $mysqli->real_escape_string($_POST['study_acronym']);
    $protocol_number = $mysqli->real_escape_string($_POST['protocol_number']);
    $sponsor_name = $mysqli->real_escape_string($_POST['sponsor_name']);
    $cro_name = $mysqli->real_escape_string($_POST['cro_name']);
    $study_type = $mysqli->real_escape_string($_POST['study_type']);
    $study_phase = $mysqli->real_escape_string($_POST['study_phase']);
    $therapeutic_area = $mysqli->real_escape_string($_POST['therapeutic_area']);
    $study_objectives = $mysqli->real_escape_string($_POST['study_objectives']);
    $study_summary = $mysqli->real_escape_string($_POST['study_summary']);

    // Insert query
    $sql = "INSERT INTO basic_information (
        study_title, study_acronym, protocol_number, sponsor_name, cro_name,
        study_type, study_phase, therapeutic_area, study_objectives, study_summary, clinic_id
    ) VALUES (
        '$study_title', '$study_acronym', '$protocol_number', '$sponsor_name', '$cro_name',
        '$study_type', '$study_phase', '$therapeutic_area', '$study_objectives', '$study_summary','$admin_id'
    )";

  if ($mysqli->query($sql) === TRUE) {
    echo "<script>
        alert('Record inserted successfully');
        window.location.href = 'basic_information.php';
    </script>";
} else {
    echo "<script>
        alert('Error: " . $mysqli->error . "');
        window.location.href = 'basic_information.php'; // or stay on same page
    </script>";
}
$mysqli->close();
}
?>