<?php
session_start();
require_once 'config.php';

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

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

$id = $_GET['id'] ?? 0;

// Fetch record
$stmt = $pdo->prepare("SELECT * FROM papers WHERE id = ?");
$stmt->execute([$id]);
$paper = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$paper) {
    die("Record not found.");
}

$email = $paper['email'];
$author = $paper['author_name'];

$mail = new PHPMailer(true);

try {
    // SMTP Configuration
    $mail->isSMTP();
    $mail->Host       = 'smtp.gmail.com';
    $mail->SMTPAuth   = true;
    $mail->Username   = 'fui.helpdesk@fui.edu.pk'; // Your Gmail
    $mail->Password   = 'giwdhfvwqgoowdbt';   // Gmail App Password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    $mail->Port       = 587;

    // Sender & Recipient
    $mail->setFrom('fui.helpdesk@fui.edu.pk', 'Abstract Submission Status');
    $mail->addAddress($email, $author);

    // Email Content
    $mail->isHTML(true);
    $mail->Subject = 'Paper Rejection Notification';

    $mail->Body = '
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>ARC 2026 Submission Confirmation</title>
</head>
<body style="margin:0; padding:0; background-color:#f4f6f9; font-family:Arial, Helvetica, sans-serif;">
    
    <table width="100%" cellpadding="0" cellspacing="0" border="0" style="background-color:#f4f6f9; padding:30px 0;">
        <tr>
            <td align="center">

                <table width="600" cellpadding="0" cellspacing="0" border="0" style="background:#ffffff; border-radius:10px; overflow:hidden; box-shadow:0 2px 10px rgba(0,0,0,0.1);">
                    
                    <!-- Header -->
                    <tr>
                        <td align="center" style="background:#0d6efd; padding:25px;">
                            <h1 style="margin:0; color:#ffffff; font-size:28px;">
                                ARC 2026
                            </h1>
                            <p style="margin:5px 0 0; color:#dbe7ff; font-size:14px;">
                                Abstract Submission Confirmation
                            </p>
                        </td>
                    </tr>

                    <!-- Content -->
                    <tr>
                        <td style="padding:40px 35px;">
                            <h2 style="color:#333333; margin-top:0;">
                                Dear '.$author.',
                            </h2>

                            <p style="font-size:16px; color:#555555; line-height:1.7;">
                                Thank you for submitting your abstract to
                                <strong>ARC 2026</strong>.
                            </p>

                            <div style="background:#e8f5e9; border-left:5px solid #28a745; padding:15px 20px; margin:25px 0;">
                                <p style="margin:0; font-size:16px; color:#155724;">
                                    <strong>✓ Status:</strong> Successfully Received
                                </p>
                            </div>

                            <p style="font-size:16px; color:#555555; line-height:1.7;">
                                Your submission has been received successfully and is now under review.
                                Our committee will evaluate your abstract and notify you regarding the next steps.
                            </p>

                            <p style="font-size:16px; color:#555555; line-height:1.7;">
                                We appreciate your interest in participating in ARC 2026 and look forward to your contribution.
                            </p>

                            <br>

                            <p style="font-size:16px; color:#333333; margin-bottom:0;">
                                Regards,<br>
                                <strong>ARC Conference Team</strong>
                            </p>
                        </td>
                    </tr>

                    <!-- Footer -->
                    <tr>
                        <td align="center" style="background:#f8f9fa; padding:20px; color:#777777; font-size:13px;">
                            © 2026 ARC Conference. All rights reserved.
                        </td>
                    </tr>

                </table>

            </td>
        </tr>
    </table>

</body>
</html>';

    $mail->send();
	
	$update = $pdo->prepare("UPDATE papers SET status='Reject' WHERE id=?");
	$update->execute([$id]);

	$_SESSION['success_msg'] = "Email sent successfully to {$email}";
	header("Location: dashboard.php");
	exit();

} catch (Exception $e) {
    echo "Email could not be sent. Error: " . $mail->ErrorInfo;
}
?>