<?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'];
require 'dbconn.php';

$id = intval($_GET['id'] ?? 0);

// Sanitize and collect form inputs
$start_date        = trim($_POST['start_date'] ?? '');
$end_date          = trim($_POST['end_date'] ?? '');
$enrollment_plan   = trim($_POST['enrollment_plan'] ?? '');
$informed_consent  = trim($_POST['informed_consent'] ?? '');
$edc_system        = trim($_POST['edc_system'] ?? '');
$crf_format        = trim($_POST['crf_format'] ?? '');
$monitoring_plan   = trim($_POST['monitoring_plan'] ?? '');

// Validate required fields
if (empty($start_date) || empty($end_date) || empty($enrollment_plan) || empty($informed_consent) || empty($crf_format) || empty($monitoring_plan)) {
    echo "<script>alert('Please fill all required fields.'); window.history.back();</script>";
    exit;
}

// Update query using prepared statement
$sql = "UPDATE recruitment_data SET 
    start_date = ?, 
    end_date = ?, 
    enrollment_plan = ?, 
    informed_consent = ?, 
    edc_system = ?, 
    crf_format = ?, 
    monitoring_plan = ?
    WHERE id = ?";

$stmt = $mysqli->prepare($sql);
$stmt->bind_param("sssssssi", $start_date, $end_date, $enrollment_plan, $informed_consent, $edc_system, $crf_format, $monitoring_plan, $id);

if ($stmt->execute()) {
    echo "<script>alert('Record updated successfully.'); window.location.href='requirement_datacollection.php';</script>";
} else {
    echo "<script>alert('Update failed: " . $mysqli->error . "'); window.history.back();</script>";
}

$stmt->close();
$mysqli->close();
?>
