<?php
// Retrieve form data
$enrollmentSerialno = $_POST['q118_enrollmentSerialno'];
$companyName = $_POST['q98_companyName'];
$surname = $_POST['q4_surname'];
$firstName = $_POST['q26_firstName'];
$middleName = $_POST['q27_middleName'];
$gender = $_POST['q22_gender'];
$phoneNumber = $_POST['q23_phoneNumber23'];
$emailAddress = $_POST['q24_emailAddress'];
$dateOfBirth = $_POST['q9_dateOf']['year'] . '-' . $_POST['q9_dateOf']['month'] . '-' . $_POST['q9_dateOf']['day'];
$groupOfPlan = $_POST['q106_groupOf'];
$typeOfPlan = $_POST['q30_typeOf30'];
$hmoId = $_POST['q110_hmoId'];
$streetAddress = $_POST['q20_address']['addr_line1'];
$streetAddress2 = $_POST['q20_address']['addr_line2'];
$city = $_POST['q20_address']['city'];
$state = $_POST['q20_address']['state'];
$postCode = $_POST['q20_address']['postal'];
$employeeId = $_POST['q29_employeeId'];
$hospitals = $_POST['q31_hospitals'];
$genotype = $_POST['q_genotype'];
$bloodGroup = $_POST['q_bloodgroup'];
$allergies = $_POST['q_allergies'];
$surgicalHistory = $_POST['q_surgical'];

// File upload
$targetDir = "uploads/"; // Specify the folder where uploaded files will be stored
$targetFile = $targetDir . basename($_FILES["input_99"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($targetFile,PATHINFO_EXTENSION));

// Check if file was uploaded successfully
if (isset($_FILES["input_99"]) && $_FILES["input_99"]["error"] === UPLOAD_ERR_OK) {
    // Move uploaded file to destination
    if (move_uploaded_file($_FILES["input_99"]["tmp_name"], $targetFile)) {
        echo "The file ". htmlspecialchars( basename( $_FILES["input_99"]["name"])). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
} else {
    echo "File upload failed or no file was selected.";
}

// Connect to MySQL database
$servername = "localhost";
$username = "healthas_regportal";
$password = "password";
$dbname = "healthas_regportal";

$conn = new mysqli($servername, $username, $password, $dbname);

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

// Sanitize input (not shown in this example, but recommended)

// Insert data into database
$sql = "INSERT INTO registration_form (enrollment_serial_no, company_name, surname, first_name, middle_name, gender, phone_number, email_address, date_of_birth, group_of_plan, type_of_plan, hmo_id, address_street, address_city, address_state, postal_code, employee_id, hospitals, genotype, blood_group, allergies, surgical_history, passport_link)
VALUES ('$enrollmentSerialno', '$companyName', '$surname', '$firstName', '$middleName', '$gender', '$phoneNumber', '$emailAddress', '$dateOfBirth', '$groupOfPlan', '$typeOfPlan', '$hmoId', '$streetAddress', '$city', '$state', '$postCode', '$employeeId', '$hospitals', '$genotype', '$bloodGroup', '$allergies', '$surgicalHistory', '$targetFile')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>
