<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Check if file was uploaded without errors
    if (isset($_FILES["importFile"]) && $_FILES["importFile"]["error"] == 0) {
        $fileName = $_FILES["importFile"]["name"];
        $tempName = $_FILES["importFile"]["tmp_name"];
        
        // Move uploaded file to a temporary location
        move_uploaded_file($tempName, "../uploads/$fileName");

        // Process the uploaded file and insert data into the database
        $filePath = "../uploads/$fileName";

        // Open the uploaded CSV file for reading
        if (($handle = fopen($filePath, "r")) !== FALSE) {
            
           include_once './dbconn.php';
            
        
            // Prepare a SQL statement for inserting data
            $sql = "INSERT INTO web_app_enrollee_data (enrollment_serial_no, company_name, first_name, middle_name, surname, gender, phone_number, email_address, date_of_birth, group_of_plan, type_of_plan, hmo_id, principal_id, address_street, address_city, address_state, lga,  employee_id, hospitals, genotype, blood_group, allergies, has_pre_existing_condition, pre_existing_condition, has_surgical_history, surgical_history, Passport_link, status, hmo_level) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
            $stmt = mysqli_prepare($conn, $sql);

            // Bind parameters to the prepared statement
            mysqli_stmt_bind_param($stmt, "sssssssssssssssssssssssssssss", $enrollment_serial_no, $company_name, $first_name, $middle_name, $surname, $gender, $phone_number, $email_address, $date_of_birth, $group_of_plan, $type_of_plan, $hmo_id, $principal_id, $address_street, $address_city, $address_state, $lga,  $employee_id, $hospitals, $genotype, $blood_group, $allergies, $has_pre_existing_condition, $pre_existing_condition, $has_surgical_history, $surgical_history, $Passport_link, $status, $hmo_level);
            
            // Set a flag to skip the first line
            $isFirstLine = true;

            // Read each row of the CSV file and insert data into the database
            while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                // Skip the first line
                if ($isFirstLine) {
                    $isFirstLine = false;
                    continue;
                }

                // Extract data from the CSV row
                $enrollment_serial_no = $data[0];
                $company_name = $data[1];
                $first_name = $data[2];
                $middle_name = $data[3];
                $surname = $data[4];
                $gender = $data[5];
                $phone_number = $data[6];
                $email_address = $data[7];
                $date_of_birth = $data[8];
                $group_of_plan = $data[9];
                $type_of_plan = $data[10];
                $hmo_id = $data[11];
                $principal_id = $data[12];
                $address_street = $data[13];
                $address_city = $data[14];
                $address_state = $data[15];
                $lga = $data[16];
                $employee_id = $data[17];
                $hospitals = $data[18];
                $genotype = $data[19];
                $blood_group = $data[20];
                $allergies = $data[21];
                $has_pre_existing_condition = $data[22];
                $pre_existing_condition = $data[23];
                $has_surgical_history = $data[24];
                $surgical_history = $data[25];
                $Passport_link = "https://healthassur.com/enrollment/uploads/" . $data[0] . ".jpg";
                $status = $data[27];
                $hmo_level = $data[28];

                // Execute the prepared statement to insert data
                mysqli_stmt_execute($stmt);
            }

            // Close the prepared statement and database connection
            mysqli_stmt_close($stmt);
            mysqli_close($conn);

            // Close the CSV file
            fclose($handle);

            // Redirect back to the page with a success message
            
            header("Location: index.php?success");
            
            exit();
        } else {
            echo "Error opening file.";
        }
    } else {
        // Handle file upload error
        echo "Error uploading file.";
    }
}
?>

<?php

$url="http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

  if(strpos($url, 'success') == true){


 echo '<script>
    setTimeout(function() {
        swal({
            title: "Success!",
            text: "Data Updated successfully",
            type: "success"
        }, function() {
            window.location = "#0";
        });
    }, 1000);
  </script>';
  }
  ?>
  
  