<?php
session_start();

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

	$conn= new mysqli('das115.truehost.cloud','honyehtx_honyehtx','B7PcCzi46xP(*8','honyehtx_mavy')or die("Could not connect to mysql".mysqli_error($conn));

$errors = [];

//if user signup button
        if(isset($_POST['signup'])){
            $name = mysqli_real_escape_string($conn, $_POST['name']);
            $email = mysqli_real_escape_string($conn, $_POST['email']);
            $country = mysqli_real_escape_string($conn, $_POST['country']);
            $acctype = mysqli_real_escape_string($conn, $_POST['acctype']);
            $currency = mysqli_real_escape_string($conn, $_POST['currency']);
            $number = mysqli_real_escape_string($conn, $_POST['number']);
            $password = mysqli_real_escape_string($conn, $_POST['password']);
            $confirmpassword = mysqli_real_escape_string($conn, $_POST['confirmpassword']);

            if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                     $errors['email'] = "Invalid email format!";
             }

            if($password !== $confirmpassword){
                $errors['password'] = "Confirm password do not matched!";
            }
            //CHECK IF EMAIL IS 1
            $email_check = "SELECT * FROM user WHERE email = '$email'";
            $res = mysqli_query($conn, $email_check);
            if(mysqli_num_rows($res) > 0){
                $errors['email'] = "Email already exist!";
            }
            //CHECK IF NO ERRORS DEN INSERT TO DB
            if(count($errors) === 0)
            {
              //$encpass = password_hash($password, PASSWORD_BCRYPT);
              $encpass = $password;
                $code = rand(100000, 999999); 
                $deposit = 0.00;
                $bonus = 0.00;
                $profit = 0.00;
                $balance = 0.00;

                $insert_data = "INSERT INTO user (name,  email, country, number, acctype, currency, password, code, deposit, bonus, balance, profits)
                                values('$name', '$email', '$country','$number', '$acctype', '$currency','$encpass', '$code', 0.00, 0.00, 0.00, 0.00)";
                $data_check = mysqli_query($conn, $insert_data);
                if ($data_check) {
                        $_SESSION['email'] = $email;
                        header('location: login.php');
                        exit();
                    } else {
                        $errors['db'] = "Failed to insert data. Try again.";
                    }

                                  



            }

        }
        //END OF INSERTING DATA TO database

?>
    



