<?php
session_start();
require_once __DIR__ . '/../../dripend/includes/db.php';


// Enable errors for debugging
ini_set('display_errors', 1);
error_reporting(E_ALL);

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    http_response_code(405);
    exit('Invalid request method');
}

// Collect POST values, trim whitespace
$patient_no = $_POST['patient_no'] ?? '';
$prefix = $_POST['prefix'] ?: null;
$first_name = trim($_POST['first_name'] ?? '');
$middle_name = trim($_POST['middle_name'] ?? null);
$last_name = trim($_POST['last_name'] ?? '');
$gender_id = $_POST['gender_id'] ?: null;
$mobile = trim($_POST['mobile'] ?? '');
$dob = $_POST['dob'] ?: null;
$age = $_POST['age'] ?: null;
$staff_status_id = $_POST['staff_status_id'] ?: null;
$child_status_id = $_POST['child_status_id'] ?: null;
$nhis_no = trim($_POST['nhis_no'] ?? null);
$other_insurance_id = $_POST['other_insurance_id'] ?: null;
$other_insurance_no = trim($_POST['other_insurance_no'] ?? null);
$moh_no = trim($_POST['moh_no'] ?? null);
$ghana_card_no = trim($_POST['ghana_card_no'] ?? null);
$occupation_id = $_POST['occupation_id'] ?: null;
$language_id = $_POST['language_id'] ?: null;
$education_id = $_POST['education_id'] ?: null;
$email = trim($_POST['email'] ?? null);
$marital_status_id = $_POST['marital_status_id'] ?: null;
$scheme_code = trim($_POST['scheme_code'] ?? null);
$tracking_no = trim($_POST['tracking_no'] ?? null);
$emergency_name = trim($_POST['emergency_name'] ?? null);
$emergency_number = trim($_POST['emergency_number'] ?? null);
$emergency_address = trim($_POST['emergency_address'] ?? null);
$emergency_relation_id = $_POST['emergency_relation_id'] ?: null;
$patient_address = trim($_POST['patient_address'] ?? null);
$religion = $_POST['religion'] ?: null;
$card_serial = trim($_POST['card_serial'] ?? null);

// Handle photo upload
$photo_name = null;
if (!empty($_FILES['photo']['name'])) {
    $ext = strtolower(pathinfo($_FILES['photo']['name'], PATHINFO_EXTENSION));
    if (in_array($ext, ['jpg','jpeg','png'])) {
        $photo_name = uniqid('patient_').'.'.$ext;
        move_uploaded_file($_FILES['photo']['tmp_name'], '../../../dripend/assets/uploads/patients/'.$photo_name);
    }
}

// Prepare SQL
$sql = "INSERT INTO patient (
patient_no, prefix, first_name, middle_name, last_name, gender_id, mobile, dob, age,
staff_status_id, child_status_id, nhis_no, other_insurance_id, other_insurance_no,
moh_no, ghana_card_no, occupation_id, language_id, education_id, email, marital_status_id,
scheme_code, tracking_no, emergency_name, emergency_number, emergency_address,
emergency_relation_id, patient_address, religion, card_serial, photo, created_at, updated_at
) VALUES (
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW()
)";

$stmt = $conn->prepare($sql);
if (!$stmt) {
    exit('Prepare failed: ' . $conn->error);
}

// Bind all variables (31 parameters)
$stmt->bind_param(
    "sisssissiiissssiiiissssssssssss",
    $patient_no,
    $prefix,
    $first_name,
    $middle_name,
    $last_name,
    $gender_id,
    $mobile,
    $dob,
    $age,
    $staff_status_id,
    $child_status_id,
    $nhis_no,
    $other_insurance_id,
    $other_insurance_no,
    $moh_no,
    $ghana_card_no,
    $occupation_id,
    $language_id,
    $education_id,
    $email,
    $marital_status_id,
    $scheme_code,
    $tracking_no,
    $emergency_name,
    $emergency_number,
    $emergency_address,
    $emergency_relation_id,
    $patient_address,
    $religion,
    $card_serial,
    $photo_name
);

// Execute SQL insert
if ($stmt->execute()) {


    // -----------------------------------------------------
    // Success Response to UI
    // -----------------------------------------------------
    $_SESSION['success'] = 'Patient registered successfully.';
    header('Location: add_new_patient.php?saved=1');
    exit;
}

else {

    // Log database error
    error_log("[DB] Patient insert failed: " . $stmt->error);
    exit("Error: Could not register patient. Please try again.");
}

?>
