<?php
session_start();
include "include/header.php";

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

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

$errors = [];
$success = '';

// Handle Send Email
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['send_mail'])) {
    $to_email = filter_var($_POST['to_email'] ?? '', FILTER_SANITIZE_EMAIL);
    $subject = htmlspecialchars(trim($_POST['subject'] ?? ''), ENT_QUOTES, 'UTF-8');
    $message = htmlspecialchars(trim($_POST['message'] ?? ''), ENT_QUOTES, 'UTF-8');

    if (empty($to_email) || !filter_var($to_email, FILTER_VALIDATE_EMAIL)) {
        $errors[] = 'অবৈধ ইমেইল ঠিকানা';
    }
    if (empty($subject)) {
        $errors[] = 'বিষয় প্রয়োজন';
    }
    if (empty($message)) {
        $errors[] = 'বার্তা প্রয়োজন';
    }

    if (empty($errors)) {
        $mail = new PHPMailer(true);
        try {
            // SMTP Settings
            $mail->isSMTP();
            $mail->Host       = 'mail.itgalaxy.academy';
            $mail->SMTPAuth   = true;
            $mail->Username   = 'support@itgalaxy.academy';
            $mail->Password   = getenv('SMTP_PASSWORD') ?: '*01723624071#'; // Use .env in production
            $mail->SMTPSecure = 'tls';
            $mail->Port       = 465;
            $mail->CharSet    = 'UTF-8';

            // Sender & Recipient
            $mail->setFrom('support@itgalaxy.academy', 'IT Galaxy Academy');
            $mail->addAddress($to_email);
            $mail->addReplyTo('support@itgalaxy.academy', 'IT Galaxy Academy');

            // Content
            $mail->isHTML(true);
            $mail->Subject = $subject;
            $mail->Body    = "
                <html>
                <body style='font-family: Arial, sans-serif; color: #333;'>
                    <div style='max-width: 600px; margin: auto; padding: 20px; border: 1px solid #eee; border-radius: 10px;'>
                        <h2 style='color: #1A237E; text-align: center;'>IT Galaxy Academy</h2>
                        <hr style='border: 1px solid #ddd;'>
                        <p><strong>প্রিয় গ্রাহক,</strong></p>
                        <div style='background: #f8f9fa; padding: 15px; border-left: 4px solid #FF5722; margin: 15px 0;'>
                            " . nl2br($message) . "
                        </div>
                        <p>ধন্যবাদ,<br><strong>IT Galaxy Academy Team</strong></p>
                        <hr style='border: 1px solid #ddd; margin-top: 30px;'>
                        <p style='font-size: 12px; color: #888; text-align: center;'>
                            © " . date('Y') . " IT Galaxy Academy. All rights reserved.<br>
                            <a href='https://itgalaxy.academy' style='color: #FF5722;'>www.itgalaxy.academy</a>
                        </p>
                    </div>
                </body>
                </html>
            ";

            $mail->send();
            $success = 'মেল সফলভাবে পাঠানো হয়েছে!';
        } catch (Exception $e) {
            $errors[] = 'মেল পাঠানো যায়নি। ত্রুটি: ' . $mail->ErrorInfo;
        }
    }

    // Store messages in session
    if ($success) {
        $_SESSION['result'] = $success;
        $_SESSION['status'] = 'ok';
    } elseif (!empty($errors)) {
        $_SESSION['result'] = implode('<br>', $errors);
        $_SESSION['status'] = 'error';
    }
    header("Location: send_mail.php");
    exit;
}
?>
<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ইমেইল পাঠান - IT Galaxy Academy</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Hind+Siliguri:wght@400;600&family=Roboto:wght@700&display=swap" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css" rel="stylesheet">
    <style>
        body {
            font-family: 'Hind Siliguri', sans-serif;
            background: #f8fafc;
            color: #333;
        }
        .content {
            margin-left: 250px;
            padding: 2rem;
            min-height: 100vh;
        }
        .card {
            background: white;
            border-radius: 16px;
            box-shadow: 0 10px 25px rgba(0,0,0,0.08);
            overflow: hidden;
        }
        .card-header {
            background: linear-gradient(135deg, #1E293B 0%, #334155 100%);
            color: white;
            padding: 1.5rem;
        }
        .card-header h2 {
            font-family: 'Roboto', sans-serif;
            font-weight: 700;
        }
        .form-control, .form-control:focus {
            border-radius: 8px;
            padding: 12px;
            box-shadow: none;
        }
        .btn-primary {
            background: linear-gradient(45deg, #FF5722, #FF8A65);
            border: none;
            padding: 12px 30px;
            border-radius: 8px;
            font-weight: 600;
            transition: all 0.3s;
        }
        .btn-primary:hover {
            background: linear-gradient(45deg, #E64A19, #FF7043);
            transform: translateY(-2px);
        }
        .btn-back {
            background: #6c757d;
            color: white;
            padding: 12px 20px;
            border-radius: 8px;
            text-decoration: none;
            display: inline-block;
        }
        .alert {
            border-radius: 10px;
            padding: 15px;
            margin-bottom: 20px;
        }
        @media (max-width: 768px) {
            .content { margin-left: 0; padding: 1rem; }
            .card-header { padding: 1rem; }
        }
    </style>
</head>
<body>
    <!-- Top Nav -->
    <nav class="top-nav sticky top-0 text-white p-4 flex justify-between items-center shadow-lg z-40 backdrop-blur-md bg-white/10">
        <div class="logo text-2xl font-bold">CourseLMS Admin</div>
        <div>
            <a href="contacts.php" class="btn-back">
                Back to Messages
            </a>
        </div>
    </nav>

    <div class="content">
        <div class="max-w-3xl mx-auto">
            <div class="card">
                <div class="card-header">
                    <h2 class="mb-0">নতুন ইমেইল পাঠান</h2>
                    <p class="mb-0 opacity-90">প্রফেশনাল ইমেইল পাঠান</p>
                </div>

                <div class="card-body p-4">
                    <!-- Session Message -->
                    <?php if (isset($_SESSION['status'])): ?>
                        <div class="alert alert-<?php echo $_SESSION['status'] == 'ok' ? 'success' : 'danger'; ?>">
                            <?php 
                            echo $_SESSION['result']; 
                            unset($_SESSION['result'], $_SESSION['status']);
                            ?>
                        </div>
                    <?php endif; ?>

                    <!-- Send Mail Form -->
                    <form method="POST">
                        <div class="mb-3">
                            <label class="form-label">প্রাপকের ইমেইল</label>
                            <input type="email" name="to_email" class="form-control" placeholder="example@domain.com" required>
                        </div>

                        <div class="mb-3">
                            <label class="form-label">বিষয়</label>
                            <input type="text" name="subject" class="form-control" placeholder="আপনার ইমেইলের বিষয় লিখুন" required>
                        </div>

                        <div class="mb-3">
                            <label class="form-label">বার্তা</label>
                            <textarea name="message" class="form-control" rows="8" placeholder="আপনার বার্তা লিখুন..." required></textarea>
                        </div>

                        <div class="d-flex gap-3">
                            <button type="submit" name="send_mail" class="btn btn-primary">
                                Send Email
                            </button>
                            <a href="contacts.php" class="btn btn-back">
                                Cancel
                            </a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <?php include "include/footer.php"; ?>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>