<?php
// Database configuration
$servername = "localhost";
$username = "vanburen_dev";
$password = "vanburenDev@#20241215";
$dbname = "vbtech";

// CORS headers
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Allow-Headers: Content-Type");
header("Content-Type: application/json");

// Create a connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check the connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Get JSON input
$data = json_decode(file_get_contents("php://input"), true);

// Check if JSON decoding was successful
if ($data === null) {
    die("Invalid JSON input");
}

// Retrieve data from JSON input
$hospital_name = $data['hospital_name'] ?? null;
$hospital_admin_name = $data['hospital_admin_name'] ?? null;
$email_address = $data['email_address'] ?? null;
$address = $data['address'] ?? null;
$ph_no = $data['ph_no'] ?? null;
$hospital_unique_id = $data['hospital_unique_id'] ?? null;

// Prepare and bind
$stmt = $conn->prepare("INSERT INTO hospital_data (hospital_name, hospital_admin_name, email_address, address, ph_no, hospital_unique_id) VALUES (?, ?, ?, ?, ?, ?)");
$stmt->bind_param("ssssss", $hospital_name, $hospital_admin_name, $email_address, $address, $ph_no, $hospital_unique_id);

// Execute the query and handle the response
if ($stmt->execute()) {
    http_response_code(200);
    echo json_encode(["message" => "New record created successfully"]);
} else {
    http_response_code(500);
    echo json_encode(["error" => "Error: " . $stmt->error]);
}

// Close the statement and connection
$stmt->close();
$conn->close();
?>
