<?php

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

if($_SERVER['REQUEST_METHOD']==='POST' && !empty($_POST['accountId']) && !empty($_POST['email'])  && !empty($_POST['email_type']) ) {

	$accountId = $_POST['accountId'];
	$email = $_POST['email'];
	$email_type = $_POST['email_type'];

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

	if (count($users) > 0) {
		$sent_emails_sql = "UPDATE sent_emails SET email_type=? WHERE accountId=? AND email=?";
		$update_sent_email = $conn->prepare($sent_emails_sql)->execute([$email_type, $accountId, $email]);

	}else {
		$sent_emails_sql = "INSERT INTO sent_emails (accountId, email, email_type) VALUES (?,?,?)";
		$create_sent_mail = $conn->prepare($sent_emails_sql)->execute([$accountId, $email, $email_type]);
	}


}else {
	die("Invalid request");
}