<?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'; // your DB connection file

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $id = intval($_POST['id']);
    $ip_name = $_POST['ip_name'];
    $ip_manufacturer = $_POST['ip_manufacturer'];
    $ip_formulation = $_POST['ip_formulation'];
    $mode_of_administration = $_POST['mode_of_administration'];
    $storage_requirements = $_POST['storage_requirements'];
    $ip_responsibility = $_POST['ip_responsibility'];

    // Prepare update query
    $stmt = $mysqli->prepare("UPDATE investigational_products SET ip_name=?, ip_manufacturer=?, ip_formulation=?, mode_of_administration=?, storage_requirements=?, ip_responsibility=? WHERE id=?");

    if ($stmt) {
        $stmt->bind_param("ssssssi", $ip_name, $ip_manufacturer, $ip_formulation, $mode_of_administration, $storage_requirements, $ip_responsibility, $id);

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

        $stmt->close();
    } else {
        echo "Query preparation failed: " . $mysqli->error;
    }

    $mysqli->close();
} else {
    echo "Invalid request method.";
}
?>
