<?php
session_start();

include "../../connect/db.php";


// Check if the form is submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // Collect form data
    $firstname = $mysqli->real_escape_string($_POST['firstname']);
    $lastname = $mysqli->real_escape_string($_POST['lastname']);
    $middlename = $mysqli->real_escape_string($_POST['middlename']);
    $nickname = $mysqli->real_escape_string($_POST['nickname']);
    $email = $mysqli->real_escape_string($_POST['email']);
    $phone = $mysqli->real_escape_string($_POST['phone']);
    $dob = $mysqli->real_escape_string($_POST['dob']);
    $zipcode = $mysqli->real_escape_string($_POST['zipcode']);
    $title = $mysqli->real_escape_string($_POST['title']);
    $gender = $mysqli->real_escape_string($_POST['gender']);
    $address = $mysqli->real_escape_string($_POST['address']);
    $country = $mysqli->real_escape_string($_POST['country']);
    $state = $mysqli->real_escape_string($_POST['state']);
    $city = $mysqli->real_escape_string($_POST['city']);
    $ssn = $mysqli->real_escape_string($_POST['ssn']);
    $usercurrency = $mysqli->real_escape_string($_POST['usercurrency']);
        $account_type = $mysqli->real_escape_string($_POST['account_type']);


    // Check if the email or SSN already exists
    $query = $mysqli->query("SELECT email FROM users WHERE email = '$email'");

        if ($query->num_rows == 0) {

            // Generate random account number
            $seed = str_split('9123456789');
            shuffle($seed);
            $rand = '';
            foreach (array_rand($seed, 10) as $k) $rand .= $seed[$k];
            $new_acc_num = $rand;

            // Prepare SQL statement for inserting into users table
            $sql = "INSERT INTO users (firstname, account_num, lastname, middlename, nickname, email, phone, dob, zipcode, title, gender, address, country, state, city, ssn, usercurrency,account_type, checking_balance, bsl_balance)
                    VALUES ('$firstname', '$new_acc_num', '$lastname', '$middlename', '$nickname', '$email', '$phone', '$dob', '$zipcode', '$title', '$gender', '$address', '$country', '$state', '$city', '$ssn', '$usercurrency','$account_type', '0.00', '0.00')";

            // Execute the users table insertion
            if ($mysqli->query($sql) === TRUE) {
                // Insert email into usercrypto table
                $sqlCrypto = "INSERT INTO usercrypto (user) VALUES ('$email')";
                if ($mysqli->query($sqlCrypto) === TRUE) {
                    // Now execute the $sqlCodes query
                    $sqlCodes = "INSERT INTO code (user) VALUES ('$new_acc_num')";
                    if ($mysqli->query($sqlCodes) === TRUE) {
                        $uploadDir = '../../uploads/' . $email;
                        if (!mkdir($uploadDir, 0777, true) && !is_dir($uploadDir)) {
                            echo 'Failed to create directory: ' . $uploadDir;
                        } else {
                           // include 'mail.php';
                            echo 'succesns';
                            $_SESSION['inactive'] = $email;
                        }
                    } else {
                        echo 'Error inserting into codes: ' . $mysqli->error;
                    }
                } else {
                    echo 'Error inserting into usercrypto: ' . $mysqli->error;
                }
            } else {
                echo 'Error inserting into users: ' . $mysqli->error;
            }
        } else {
            echo 'error_email';
        }
    
    // Close connection
    $mysqli->close();
}
?>
