<?php
$pageName  = "Create an Account";
include($_SERVER['DOCUMENT_ROOT'] . "/auth/header.php");

if (@$_SESSION['acct_email']) {
    header("Location:./account/dashboard.php");
}



//   // force user to use pc
//   function isMobileDevice() {
//     return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo
// |fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i"
// , $_SERVER["HTTP_USER_AGENT"]);
// }

if (isset($_POST['regSubmit'])) {
    $userid = "1202" . (substr(number_format(time() * rand(), 0, '', ''), 0, 6));
    $acct_fullname = $_POST['acct_fullname'];
    $acct_type = "Regular User";
    $acct_status = "hold";
    $acct_email = $_POST['acct_email'];
    $acct_phone = $_POST['acct_phone'];
    $acct_password = $_POST['acct_password'];
    $confirmPassword = $_POST['confirmPassword'];



    if ($acct_password !== $confirmPassword) {
        toast_alert('error', 'Password does not matched');
    } else {
        //checking exiting email
        $usersVerified = "SELECT * FROM users WHERE acct_email=:acct_email or acct_phone=:acct_phone";
        $stmt = $conn->prepare($usersVerified);
        $stmt->execute([
            'acct_email' => $acct_email,
            'acct_phone' => $acct_phone
        ]);

        if ($stmt->rowCount() > 0) {
            toast_alert('error', 'Email/Phone Number Already Exit');
        } else {
            

            //INSERT INTO DATABASE
            $registered = "INSERT INTO users (userid,acct_fullname,acct_type,acct_password,acct_email,acct_phone,acct_status) VALUES(:userid,:acct_fullname,:acct_type,:acct_password,:acct_email,:acct_phone,:acct_status)";
            $reg = $conn->prepare($registered);
            $reg->execute([
                'userid' => $userid,
                'acct_fullname' => $acct_fullname,
                'acct_type' => $acct_type,
                'acct_password' => password_hash((string)$acct_password, PASSWORD_BCRYPT),
                'acct_email' => $acct_email,
                'acct_phone' => $acct_phone,
                'acct_status' => $acct_status
            ]);


            if (true) {


                // $full_name = $firstname . " " . $lastname;
                // $APP_NAME = WEB_TITLE;
                // $APP_URL = WEB_URL;
                // $SITE_ADDRESS = $page['url_address'];
                // $message = $sendMail->RegisterMsg($full_name, $acct_no, $acct_status, $APP_NAME, $APP_URL, $SITE_ADDRESS);
                // // User Email
                // $subject = "Welcome to " . "-" . $APP_NAME;
                // $email_message->send_mail($acct_email, $message, $subject);



                $msg1 = "
                <div class='alert alert-warning'>
                
                <script type='text/javascript'>
                     
                        function Redirect() {
                        window.location='./login.php';
                        }
                        document.write ('');
                        setTimeout('Redirect()', 7000);
                     
                        </script>
                        
                <center><img src='../assets/images/loading.gif' width='180px'  /></center>
                 
                <center>	<strong style='color:black;'>Account Successfully Registered... You will receive email notification once your account has been approved!. Please wait while we redirect you!
                       </strong></center>
                
                  </div>
                ";
            } else {
                toast_alert("error", "Invalid details");
            }
        }
    }
}

?>



<div class="container-fluid h-100 awrp anuphan">
   
        <form  class="bg-sm-none px-lg-10 mt-5 p-0 mx-auto shadow-none anuphan post-form mx-auto border-none" method="POST" enctype="multipart/form-data" >
            <h3 class="anuphan-3 mt-4">
                Create OTC Business Account
            </h3>
            <p class="anuphan-2 grey-color-3">
                Sign Up with Email
            </p>
            
            <center> <?php if (isset($msg1)) echo $msg1; ?></center>
            
            
            <label class="anuphan xsmall mt-4 mb-1">Full Name</label>
            <div class="form-wrapper m-0">
                <input type="text" name="acct_fullname" id="acct_fullname" placeholder="Enter Full Name" required>
                <small class="error-text eml-err" style="display: none;"></small>
            </div>
    
            <label class="anuphan xsmall mt-4 mb-1">Personal Email</label>
            <div class="form-wrapper m-0">
                <input type="email" name="acct_email" id="id_email" placeholder="Enter Email" required>
                <small class="error-text eml-err" style="display: none;"></small>
            </div>
            
            <label class="anuphan xsmall mt-4 mb-1">Phone Number</label>
            <div class="form-wrapper m-0">
                <input inputmode="numeric" required pattern="[0-9]+" name="acct_phone" required  minlength="7"  minlength="15"  autocomplete="off" placeholder="+1 440 *** ****">
                <small class="error-text eml-err" style="display: none;"></small>
            </div>
            <p class="text-right psw-req" style="display: none;">
                <small class="grey-color-2">start with country code (eg +1,+44)</small>
            </p>
            
            
            <label class="anuphan xsmall mt-4 mb-1">Password</label>
            <div class="form-wrapper mb-1 m-0">
                <input type="password" name="acct_password" id="id_password" placeholder="***********" class="has-icon" autocomplete="new-password" required>
                <span class="input-icon-wrapper pr-3 pointer psw-c-icon" onclick="passwordViewToggle()">
                    <i class="bi bi-eye-slash grey-color p-t-st input-icon"></i>
                </span>
            </div>
            
             <label class="anuphan xsmall mt-4 mb-1">Confirm Password</label>
            <div class="form-wrapper mb-1 m-0">
                <input type="password" name="confirmPassword" id="id_password2" placeholder="***********" class="has-icon" autocomplete="new-password" required>
                <span class="input-icon-wrapper pr-3 pointer psw-c-icon" onclick="passwordViewToggle2()">
                    <i class="bi bi-eye-slash grey-color p-t-st input-icon"></i>
                </span>
            </div>
            <small class="error-text psw-err" style="display: none;"></small>
            <p class="text-right psw-req" style="display: none;">
                <small class="grey-color-2">At least 8 characters</small>
            </p>
            <div class="d-flex mt-4">
                
                <input type="checkbox" id="agree" required class="mr-2 top-2" >
                <small class="grey-color-3 anuphan-2">
                    By creating an account, I agree to Binance's 
                    <a href="#">Terms of Service</a> and <a href="#">Privacy Policy.</a>
                </small>
            </div>
            <button type="submit" name="regSubmit" class="mt-4 py-3 anuphan-2">Create account</button>
            <div class="loader justify-content-center lpb mt-4 w-100 btn-grey add-radius-sm" style="height: 52px; display: none;">
                <span class="bar" style="border-radius: 0px;"></span>
                <span class="bar" style="border-radius: 0px;"></span>
                <span class="bar" style="border-radius: 0px;"></span>
                <span class="bar" style="border-radius: 0px;"></span>
            </div>
            <p class="m-0 mt-4">
                <small class="grey-color-3 xsmall">Already have an account?</small>
                <a href="./login.php" class="primary-color anuphan-2" style="text-decoration: underline !important;">Log In</a>
            </p>
        </form>
    
        <!-- --------------- -->
       
   
</div>

<script>
    
    function passwordViewToggle2(){
    var passwordInput = $('#id_password2')[0];
    if (passwordInput.type == 'text'){
        passwordInput.type = 'password'
        $('.p-t-st').removeClass('bi-eye'); $('.p-t-st').addClass('bi-eye-slash'); 
    }
    else{
        passwordInput.type = 'text';
        $('.p-t-st').removeClass('bi-eye-slash'); $('.p-t-st').addClass('bi-eye');
    }
}
</script>

<?php

include_once($_SERVER['DOCUMENT_ROOT'] . "/auth/footer.php");

?>