<?php
    include_once($_SERVER['DOCUMENT_ROOT'].'/_ps2020/_global/dir_root.php');
    include($_SERVER['DOCUMENT_ROOT'] . "/_ps2020/config/connect.php");

    $email = $_REQUEST['email'];
    $user_id = 0;
    $username = "Not-Found";
    $verifyCode = "Not-Found";
    $active = 0;

    // random Code generator (for 'verify_code')
        function getRandomCode() {

            $n=10;
            $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
            $randomString = '';
          
            for ($i = 0; $i < $n; $i++) {
                $index = rand(0, strlen($characters) - 1);
                $randomString .= $characters[$index];
            }
          
            return $randomString;
        }

    // (Set) Verify-Code
    $verifyCode = getRandomCode();



    // Find whether 'Email' is correct
    $result = $conn->query("SELECT * FROM user_account WHERE email = '$email'");
        if($result->num_rows != 0){ // Email exist

            // Get ID & Username
            while($row = $result->fetch_assoc()){
                $user_id = $row['user_id'];
                $username = $row['username'];
                $active = $row['active'];
                //Now that we have got the username
                //we need to create a verify Code for them
            }

            // Set Verify-Code
            if ($active != -1){
                $result = $conn->query("UPDATE user_account SET verifyCode = '$verifyCode' WHERE email = '$email'");

                    if($result){
                        // Send Email
                        $email = $_REQUEST['email'];
                        $name = "(no-reply) GEESE";
                        $subject = 'Reset Password - GEESE';
                        $content = "
                            <h1 style='font-size: 22px;'><b><center>Reset Password</center></b></h1>

                            <p>Hi " . $username . ",</p>

                            <p>We are team behined project GEESE. It has come to our notice that you have forgotten your password.</p>
                
                            <p>So we would like to reset the password for you, so you can continue to enjoy exploring our platform.</p>

                            <p>To reset the password, please follow the link below and you will redirected to the reset Password Page</p>

                            <p style='display: flex; justify-content: center; margin-top: 10px;'>
                                <center>
                                    <a href='https://ps2020.cdms.westernsydney.edu.au/_ps2020/resetPassword_verify.php?id=" . $user_id . "&vCode=" . $verifyCode . "' target='_blank' style='border: 1px solid #0561B3; background-color: #548b54; color: #fff; text-decoration: none; font-size: 18px; padding: 10px 20px;'>Reset Password</a>
                                </center>
                            </p>
                        ";

                        // Mailing [function]
                            include_once dir_rootPHP."php/request/send_email.php";

                            if($mail->send()){
                                echo 1;
                            }else{
                                echo 0;
                            }
                    }
                    else{
                        echo 0;
                    }
            }
            else
                echo 2;
        }
?>