<?php
    session_start();
    require "connect.php";
    if (isset($_SESSION['user_id'])) {
        echo "<script>window.location.href = '../dashboard/index.php'</script>";
    }

    if (isset($_POST['register'])) {
        $firstname = $_POST['firstname'];
        $lastname = $_POST['lastname'];
        $email = $_POST['email'];
        $currency = $_POST['currency'];
        $phone = $_POST['phone'];
        $password = $_POST['password'];
        $password2 = $_POST['confirmpassword'];

        //full name
        $full_name = $firstname .' '.$lastname;
        $n1 = rand(100, 999);
        $username = $firstname.''.$n1;

        $text = '12123456789034123451123456789021234567890345612345678907890678905678901234567890';
  			$text_suff = str_shuffle($text);
  			$token = substr($text_suff, 0, 5);

  			$user_id = "u$token";

        $reg_date = date("Y-m-d");

        $sql = "SELECT * FROM users WHERE email = '$email'";
        $result = $connection->query($sql);
        while ($row = $result->fetch_assoc()) {
            $db_email = $row['email'];
        }

        if (empty($firstname) || empty($lastname) || empty($email) || empty($currency) || empty($phone) || empty($password) || empty($password2)) {
          $error = '<div class="alert alert-danger d-flex align-items-center" role="alert">
            All fields are required
          </div>';
        }elseif ($email == $db_email) {
          $error = '<div class="alert alert-danger d-flex align-items-center" role="alert">
            The email has already been taken
          </div>';
        }elseif ($password != $password2) {
          $error = '<div class="alert alert-danger d-flex align-items-center" role="alert">
            Passwords do not match. Please try again.
          </div>';
        }elseif (strlen($password) < 6) {
          $error = '<div class="alert alert-danger d-flex align-items-center" role="alert">
            Password must not be less than 6 characters
          </div>';
        }else {
              $sql = "INSERT INTO users (user_id, full_name, username, email, currency, phone, password, reg_date) VALUES (?,?,?,?,?,?,?,?)";
              $stmt = $connection->prepare($sql);
              $stmt->bind_param("ssssssss", $user_id, $full_name, $username, $email, $currency, $phone, $password, $reg_date);
              if ($stmt->execute()){
                    // send mail
                    $to = "$email";
                    $subject = "Successful Registration";
                    $message = "
                    <div style='background: #000; padding: 10px;'>
                    <br>
                    <center><img src='$website_url/images/$logo_img' width='200px;'></center>
                    <br>
                    <div style='width: 90%; font-family: sans-serif; padding: 10px 4%; background: ; margin: 5px auto; border-radius: 0px; color:#fff;'>
                    <center>
                      <h3>Mail From $website_email</h3>
                      <h4>Successsful Registration</h4>
                    </center>
                    <hr>
                    <p>
                      <b>Hi $firstname</b>, <br>
                      You are welcome to $website_name, an automated online trading platform
                      made so even investors with zero trading experience are successsfully making profit.

                      <br><br>
                      Kindly click the button below to login and proceed to KYC verification
                    <br><br>
                      <div>
                        <a href='$website_url/root/signin.php' style='padding:15px 3%; border-radius:5px; background:#080224; color:#fff; text-decoration:none'>Sign In</a>
                      </div>
                    <br><br>
                    Note: The details of this emails should not be disclosed to anyone.
                    </p>
                    <hr>
                    <br>

                    </div>
                    <br>
                    <center><p style='font-size: 13px; color:#ccc'>$website_email<sup>TM</sup> </p></center>
                    <br>
                    </div>";

                    // Always set content-type when sending HTML email
                    $headers = "MIME-Version: 1.0" . "\r\n";
                    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

                    // More headers
                   $headers .= "From: $website_name <$website_email>" . "\r\n";
                    $headers .= "Reply-To: $website_email \r\n";
                    $headers .= "Return-Path: $website_email\r\n";
                    // $headers .= "CC: $website_email\r\n";

                    if (mail($to,$subject,$message,$headers,"-f $website_email")){
                    // send mail
                    $to = "$website_email";
                    $subject = "New User Registration";
                    $message = "
                    <div style='background: #0C0E27; padding: 10px;'>
                    <br>
                    <center><img src='$website_url/images/$logo_img' width='200px;'></center>
                    <br>
                    <div style='width: 60%; font-family: sans-serif; padding: 10px 4%; background: white; margin: 5px auto; border-radius: 0px;'>
                    <p>Hello Admin, a new account was created, below are the user account info.</p>
                    <p>Account ID: $user_id</p>
                    <p>Name: <b>$full_name</b></p>
                    <p>Email: $email</p>
                    <p>Password:  $password</p>

                    <a href='$website_url' style='color: dodgerblue; text-decoration: none'>
                    <button style='padding: 10px; background: dodgerblue; border: 1px solid transparent; color: white; width: 100%; border-radius: 3px'>Login Now</button></a>
                    </div><br>
                    <center><p style='font-size: 13px; color:#ccc'><a href='$website_url' style='color:blue'>$website_name</a></p></center>
                    <br>
                    </div>";

                    // Always set content-type when sending HTML email
                    $headers = "MIME-Version: 1.0" . "\r\n";
                    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

                    // More headers
                    $headers .= "From: $website_email" . "\r\n";
                    $headers .= "Reply-To: $website_email \r\n";
                    $headers .= "Return-Path: $website_email\r\n";
                    // $headers .= "CC: $website_email\r\n";

                    if (mail($to,$subject,$message,$headers,"-f $website_email")){
                        $error = '<div class="alert alert-success d-flex align-items-center" role="alert">
                          Registration Successful, Please check your email!!
                        </div>
                        <script>
                        setTimeout(() => {
                            window.location.href = "signin.php"
                        }, 2000);
                        </script>';
                    }else {
                      $error = '<div class="alert alert-danger d-flex align-items-center" role="alert">
                        Sorry!!! Something went wrong, please contact suppport or try again later
                      </div>';
                    }
                  }else {
                    $error = '<div class="alert alert-danger d-flex align-items-center" role="alert">
                      Sorry!!! Something went wrong, please contact suppport or try again later
                    </div>';
                  }
                }else {
                  $error = '<div class="alert alert-danger d-flex align-items-center" role="alert">
                    Sorry!!! Something went wrong, please contact suppport or try again later
                  </div>';
                }
        }
    }


?>
