<?php
require("../includes/db_connect.php");
require_once("includes/admin_session_token.php");

$accountId = $_SESSION['adm_accountId'];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

if ((!empty($_POST['accountId']))) {

global $conn;

$accountId = $_POST['accountId'];

$usr_sql = "SELECT * FROM users WHERE accountId =? LIMIT 1";
$usr_sth = $conn->prepare($usr_sql, [PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY]);
$usr_sth->execute([$accountId]);
$users = $usr_sth->fetchAll();

function send_vcode($accountId, $email, $token, $fullName)
{

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "accountId=$accountId&email=$email&token=$token&name=$fullName");
    curl_setopt($ch, CURLOPT_URL, 'https://www.rocketta.io/email/send_verification_mail');
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
    curl_setopt($ch, CURLOPT_FORBID_REUSE, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
    curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
    curl_exec($ch);
    curl_close($ch);
    die("Email sent!");
}

    if (count($users) > 0) {
        foreach ($users as $key => $value) {
            $email = $value['email'];
            $email_confirmation = $value['email_confirmation'];
            $email_tutorial = ($value['email_confirmation'] == 1) ? true : false ;
            $fullName = $value['fullname'];
            $status = $value['status'];
    
            if ($email_confirmation == 0) {
                $token = 'qwertzuiopasdfghjklyxcvbnmQWERTZUIOPASDFGHJKLYXCVBNM0123456789!$*';
                $token = str_shuffle($token);
                $token = substr($token, 0, 100);
                $user_sql = "UPDATE users SET access_token=?, email_confirmation=? WHERE accountId=?";
                $update_user = $conn->prepare($user_sql)->execute([$token, 0, $accountId]);
                if ($update_user) {
                    send_vcode($accountId, $email, $token, $fullName);
                } else {
                    die("Unable to send verification mail. Please try again.");
                } 
            } else {
                die("Email already verified!");
            }
        }
    } else {
        die("Invalid Account ID. Please check and try again.");
    }
  }
}
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>Send Verification Email</title>
    <?php require_once("includes/header.php"); ?>

    <div class="content ht-100v pd-0">
        <div class="content-header">
        </div><!-- content-header -->

        <div class="content-body">
            <div class="container pd-x-0">
                <div class="d-sm-flex align-items-center justify-content-between mg-b-20 mg-lg-b-25 mg-xl-b-30">
                    <div>
                        <nav aria-label="breadcrumb">
                            <ol class="breadcrumb breadcrumb-style1 mg-b-10">
                                <li class="breadcrumb-item"><a href="#">Send email</a></li>
                            </ol>
                        </nav>
                        <h4 class="mg-b-0 tx-spacing--1">Send email</h4>
                    </div>

                </div>

                <div class="row row-xs">
                    <div class="col-lg-12">
                        <div class="card">
                            <div class="card-body">

                                <div class="col-md-6">
                                    <div style="display: none;" id="feedbackPrompt"></div>
                                    
                                    <form action="" method="POST" accept-charset="utf-8">

                                    <div class="form-group" style="margin-top: 20px">
                                        <label>Account ID</label>
                                        <input type="text" name="accountId" class="form-control"
                                            placeholder="Enter account ID here">
                                        <div class="invalid-feedback accountId_feedback">Account ID cannot be empty.
                                        </div>
                                    </div>
                                    <button class="btn btn-primary" type="submit">Send</button>
                                    </form>
                                </div>


                            </div>
                        </div>
                    </div>
                </div>
                <!-- end row -->
            </div><!-- container -->
        </div>
    </div>


    </div><!-- container -->
    </div>
    </div>

    <?php require_once("includes/footer.php"); ?>
    <script type="text/javascript">
    $(document).ready(function() {
        var classes = $("#mn-create-trade").prop('class');
        $("#mn-create-trade").prop('class', classes + ' active pcoded-trigger');

    });
    </script>
    <script src="../js/admin/create-trade.js"></script>
    </body>
</html>