<?php
session_start();

require("../includes/app_credentials.php");
require("includes/email-credentials.php");

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';

require("../includes/db_connect.php");

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['accountId']) && isset($_POST['trade_type'])) {

	$accountId = $_POST['accountId'];
	$trade_type = $_POST['trade_type'];
    $email = "";
    
    if ($trade_type != "monthly" && $trade_type != "quarterly" && $trade_type != "annually") {
        die("Invalid request!");
    }

	$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();

	if (count($users) == 0) {
		die("Account not found!");
	}else {
        foreach ($users as $key => $value) {
            $email = $value['email'];
        }
    }

    $message_title = "";
    $message_content = "";

    switch ($trade_type) {
        case 'monthly':
            $message_title = "Monthly Trade Closed";
            $message_content = "Please be advised that the 30-day locked trading period has ended, resulting in the closure of your trading activities. Additionally, your monthly Bot subscription has expired, and you are kindly requested to withdraw your profits within the next 72 hours.";
            break;
        case 'quarterly':
            $message_title = "Quarterly Trade Closed";
            $message_content = "Please be advised that the 90-day locked trading period has ended, resulting in the closure of your trading activities. Additionally, your quarterly Bot subscription has expired, and you are kindly requested to withdraw your profits within the next 72 hours.";
            break;
        default:
            $message_title = "Annually Trade Closed";
            $message_content = "Please be advised that the 1 year locked trading period has ended, resulting in the closure of your trading activities. Additionally, your annually Bot subscription has expired, and you are kindly requested to withdraw your profits within the next 72 hours.";
            break;
    }

	$mail = new PHPMailer();

	try {

		//Server settings                          
		$mail->isSMTP();
		$mail->Host = $smtp_host;
		$mail->SMTPAuth = true;
		$mail->Username = $smtp_username;
		$mail->Password = $smtp_password;
		$mail->SMTPSecure = $smtp_encryption;
		$mail->Port = $smtp_port;

		//Recipients
		$mail->setFrom($smtp_email, $web_app_name);
		$mail->addAddress($email);

		//Content
		$mail->isHTML(true);
		$mail->Subject = $message_title;

		$mail->Body = "<div style=\"color:#222;font-family:'Roboto','Helvetica','Arial',sans-serif!important;font-size:14px;font-weight:400;line-height:19px;margin:0 auto!important;min-width:100%;padding:0;text-align:center!important;width:100%!important\">

		<table class=\"m_1863363065879357917body\" style=\"background:#ebeff6;border-collapse:collapse;border-spacing:0;color:#222;font-family:'Roboto','Helvetica','Arial',sans-serif!important;font-size:14px;font-weight:400;height:100%;line-height:19px;margin:0;padding:0;text-align:left;vertical-align:top;width:100%\">

		<tbody>
		<tr style=\"font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top\">

		<td align=\"center\" valign=\"top\" class=\"m_1863363065879357917center\" style=\"border-collapse:collapse!important;color:#222;font-family:'Roboto','Helvetica','Arial',sans-serif!important;font-size:14px;font-weight:400;line-height:19px;margin:0 auto;padding:0;text-align:center;vertical-align:top;word-break:normal\">

		<center style=\"font-family:'Roboto','Helvetica','Arial',sans-serif!important;min-width:580px;width:100%\">


		<table class=\"m_1863363065879357917container\" style=\"border-collapse:collapse;border-spacing:0;font-family:'Roboto','Helvetica','Arial',sans-serif!important;margin:0 auto;padding:0;text-align:inherit;vertical-align:top;width:580px\">
		<tbody>

		<tr style=\"font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top\">

		<td class=\"m_1863363065879357917pt30\" style=\"border-collapse:collapse!important;color:#222;font-family:'Roboto','Helvetica','Arial',sans-serif!important;font-size:14px;font-weight:400;line-height:19px;margin:0;padding:0;padding-top:30px;text-align:left;vertical-align:top;word-break:normal\">

		<table class=\"m_1863363065879357917row m_1863363065879357917brad-top\" style=\"background:#fff;border-collapse:collapse;border-spacing:0;border-top-left-radius:3px!important;border-top-right-radius:3px!important;display:block;font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top;width:100%\">

		<tbody>

		<tr style=\"font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top\">

		<td class=\"m_1863363065879357917wrapper m_1863363065879357917offset-by-one m_1863363065879357917last m_1863363065879357917pt30\" style=\"border-collapse:collapse!important;color:#222;font-family:'Roboto','Helvetica','Arial',sans-serif!important;font-size:14px;font-weight:400;line-height:19px;margin:0;padding:10px 20px 0 0;padding-left:50px;padding-right:0;padding-top:30px;text-align:left;vertical-align:top;word-break:normal\">

		<table class=\"m_1863363065879357917ten m_1863363065879357917columns\" style=\"border-collapse:collapse;border-spacing:0;font-family:'Roboto','Helvetica','Arial',sans-serif!important;margin:0;padding:0;text-align:left;vertical-align:top;width:480px\">

		<tbody>

		<tr style=\"font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top\">

		<td class=\"m_1863363065879357917six m_1863363065879357917sub-columns m_1863363065879357917pb0\" style=\"border-collapse:collapse!important;color:#222;font-family:'Roboto','Helvetica','Arial',sans-serif!important;font-size:14px;font-weight:400;line-height:19px;margin:0;min-width:0;padding:0 0 10px;padding-bottom:0;padding-right:10px;text-align:left;vertical-align:top;width:50%;word-break:normal\">

		<a href=\"https://www.rocketta.io\" style=\"color:#2ba6cb;font-family:'Roboto','Helvetica','Arial',sans-serif!important;text-decoration:none\" target=\"_blank\">
		<img src=\"https://rocketta.io/img/logo-black.png\" class=\"m_1863363065879357917logo CToWUd\" style=\"border:none;clear:both;display:block;float:none;font-family:'Roboto','Helvetica','Arial',sans-serif!important;max-width:200px;outline:none;padding-bottom:0;padding-left:0;padding-top:0;text-decoration:none;width:auto\">
		</a>
		</td>
		</tr>
		</tbody>
		</table>
		</td>
		</tr>
		</tbody>
		</table>


		<table class=\"m_1863363065879357917row\" style=\"background:#fff;border-collapse:collapse;border-spacing:0;display:block;font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top;width:100%\">
		<tbody><tr style=\"font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top\">
		<td class=\"m_1863363065879357917wrapper m_1863363065879357917last m_1863363065879357917offset-by-one m_1863363065879357917pt80\" style=\"border-collapse:collapse!important;color:#222;font-family:'Roboto','Helvetica','Arial',sans-serif!important;font-size:14px;font-weight:400;line-height:19px;margin:0;padding:10px 20px 0 0;padding-left:50px;padding-right:0;padding-top:80px;text-align:left;vertical-align:top;word-break:normal\">
		<table class=\"m_1863363065879357917ten m_1863363065879357917columns\" style=\"border-collapse:collapse;border-spacing:0;font-family:'Roboto','Helvetica','Arial',sans-serif!important;margin:0;padding:0;text-align:left;vertical-align:top;width:480px\">
		<tbody><tr style=\"font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top\">
		<td class=\"m_1863363065879357917text-pad-small m_1863363065879357917pb0\" style=\"border-collapse:collapse!important;color:#222;font-family:'Roboto','Helvetica','Arial',sans-serif!important;font-size:14px;font-weight:400;line-height:19px;margin:0;padding:0 0 10px;padding-bottom:0;text-align:left;vertical-align:top;word-break:normal\">
		
		<p class=\"m_1863363065879357917text\" style=\"color:#222327;font-family:Helvetica!important;font-size:18px;font-weight:400;line-height:28px;margin:0;padding:0;text-align:left\">$message_content</p>
		</td>
		</tr>
		</tbody>
		</table>
		</td>

		</tr>
		<tr style=\"margin-left:10px;margin-top:10px;border-bottom:1px solid #EAF1FB;font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top\">
		<td class=\"m_1863363065879357917text-pad-small m_1863363065879357917pb50\" style=\"border-collapse:collapse!important;color:#222;font-family:'Roboto','Helvetica','Arial',sans-serif!important;font-size:14px;font-weight:400;line-height:19px;margin:0;padding:0 0 10px;padding-bottom:50px;text-align:left;vertical-align:top;word-break:normal\">

		</td>
		</tr>

		<tr style=\"font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top\">

		<td class=\"m_1863363065879357917wrapper m_1863363065879357917last m_1863363065879357917offset-by-one m_1863363065879357917pt40\" style=\"border-collapse:collapse!important;color:#222;font-family:'Roboto','Helvetica','Arial',sans-serif!important;font-size:14px;font-weight:400;line-height:19px;margin:0;padding:10px 20px 0 0;padding-left:50px;padding-right:0;padding-top:40px;text-align:left;vertical-align:top;word-break:normal\">
		<table class=\"m_1863363065879357917ten m_1863363065879357917columns\" style=\"border-collapse:collapse;border-spacing:0;font-family:'Roboto','Helvetica','Arial',sans-serif!important;margin:0;padding:0;text-align:left;vertical-align:top;width:480px\">
		<tbody>

		<tr style=\"font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top\">
		<td class=\"m_1863363065879357917text-pad-small m_1863363065879357917pb40\" style=\"border-collapse:collapse!important;color:#222;font-family:'Roboto','Helvetica','Arial',sans-serif!important;font-size:14px;font-weight:400;line-height:19px;margin:0;padding:0 0 10px;padding-bottom:40px;text-align:left;vertical-align:top;word-break:normal\">
		<p class=\"m_1863363065879357917small-text m_1863363065879357917pt5 m_1863363065879357917pb5\" style=\"color:#474a50;font-family:Helvetica!important;font-size:14px;font-weight:400;line-height:22px;margin:0;padding:0;padding-bottom:5px;padding-top:5px;text-align:left\">If you have any questions, you can contact us at&nbsp;<a href=\"mailto:support@rocketta.io\" style=\"color:#3c7858!important;font-family:Helvetica!important;text-decoration:none\" target=\"_blank\">support@rocketta.io</a>.
		</p>
		</td>
		</tr>
		</tbody>
		</table>
		</td>
		</tr>
		</tbody>
		</table>


		<table class=\"m_1863363065879357917row m_1863363065879357917footer\" style=\"background:#ebeff6;border-collapse:collapse;border-spacing:0;display:block;font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top;width:100%\">
		<tbody>
		<tr style=\"font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top\">
		<td class=\"m_1863363065879357917wrapper m_1863363065879357917last m_1863363065879357917offset-by-one m_1863363065879357917pt50\" style=\"border-collapse:collapse!important;color:#222;font-family:'Roboto','Helvetica','Arial',sans-serif!important;font-size:14px;font-weight:400;line-height:19px;margin:0;padding:10px 20px 0 0;padding-left:50px;padding-right:0;padding-top:50px;text-align:left;vertical-align:top;word-break:normal\">
		<table class=\"m_1863363065879357917ten m_1863363065879357917columns\" style=\"border-collapse:collapse;border-spacing:0;font-family:'Roboto','Helvetica','Arial',sans-serif!important;margin:0;padding:0;text-align:left;vertical-align:top;width:480px\">
		<tbody>
		<tr style=\"font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top\">
		<td class=\"m_1863363065879357917pb50 m_1863363065879357917text-pad-small\" style=\"border-collapse:collapse!important;color:#222;font-family:'Roboto','Helvetica','Arial',sans-serif!important;font-size:14px;font-weight:400;line-height:19px;margin:0;padding:0 0 10px;padding-bottom:50px;text-align:left;vertical-align:top;word-break:normal\">



		<div style=\"font-family:'Roboto','Helvetica','Arial',sans-serif!important\">
		<p class=\"m_1863363065879357917address-text\" style=\"color:#474a50!important;font-family:Helvetica!important;font-size:14px!important;font-weight:400;line-height:19px;margin:0;padding:0;text-align:left\">10 Wandsworth Rd, London, LND SW8 2LL</p>
		</div>
		<p class=\"m_1863363065879357917subscribe\" style=\"color:#222;font-family:'Roboto','Helvetica','Arial',sans-serif!important;font-size:14px;font-weight:400;line-height:19px;margin:0;padding:0;padding-top:10px;text-align:left!important\"><a href=\"https://www.rocketta.io/terms-and-conditions\" class=\"m_1863363065879357917left\" style=\"color:#474a50;display:inline-block;font-family:Helvetica!important;font-weight:400;text-align:left;text-decoration:underline\" target=\"_blank\" data-saferedirecturl=\"https://www.rocketta.io/terms-and-conditions\"> Terms and Conditions</a>
		</p>
		</td>
		</tr>
		</tbody>
		</table>
		</td>
		</tr>
		</tbody>
		</table>




		</td>
		</tr>
		</tbody>
		</table>
		</center>
		</td>
		</tr>
		</tbody>
		</table>
		</div>";

		$mail->send();
		createLog("email sent", "e", "logs");

		$datetime = new DateTime(convertDate(date('Y-m-d H:i:s')));

		$user_sql = "UPDATE users SET email_date=? WHERE accountId=?";

		$newDate = $datetime->format('Y-m-d H:i:s');

		$update_user = $conn->prepare($user_sql)->execute([$newDate, $accountId]);
	} catch (Exception $e) {
		createLog("Unable to send verification mail: " . $mail->ErrorInfo, "e", "logs");
	}
} else {
	die("Invalid request");
}

function convertDate($date)
{
	$dt = str_replace('/', '-', $date);

	return date('Y-m-d H:i:s', strtotime($dt));
}

function createLog($text, $level = 'i', $file = 'logs')
{
	/*switch (strtolower($level)) {
				case 'e':
				case 'error':
				$level='ERROR';
				break;
				case 'i':
				case 'info':
				$level='INFO';
				break;
				case 'd':
				case 'debug':
				$level='DEBUG';
				break;
				default:
				$level='INFO';
			}
			error_log(date("[d-m-Y H:i:s]")."\t[".$level."]\t[".basename(__FILE__)."]\t".$text."\n", 3, $file);*/
}