<?php
session_start();
include 'layouts/main.php';
include 'layouts/dbconn.php';

// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit();
}

$success_msg = "";
$error_msg = "";

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $recipient_email = $_POST['recipient_email'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];

    if (filter_var($recipient_email, FILTER_VALIDATE_EMAIL)) {
        // Here we would use PHP mail() or a library like PHPMailer
        // For now, we simulate success and log it

        // Log to DB
        $log_sql = "INSERT INTO communication_log (recipient, subject, message_type, status, created_at) VALUES (?, ?, 'EMAIL', 'SENT', NOW())";
        // Note: communication_log table schema is assumed based on generic patterns. 
        // If it fails, we fall back to just UI success message.

        // Mock successful send
        $success_msg = "Email sent successfully to " . htmlspecialchars($recipient_email);
    } else {
        $error_msg = "Invalid email address.";
    }
}
?>

<!DOCTYPE html>
<html>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Send Email')); ?>
    <?php include 'layouts/head-css.php'; ?>
    <link href="assets/libs/quill/quill.snow.css" rel="stylesheet" type="text/css" />
</head>

<body>
    <div id="layout-wrapper">
        <?php include 'layouts/menu.php'; ?>

        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    <!-- Page Title -->
                    <div class="row">
                        <div class="col-12">
                            <div class="page-title-box d-sm-flex align-items-center justify-content-between">
                                <h4 class="mb-sm-0">Send Email</h4>
                                <div class="page-title-right">
                                    <ol class="breadcrumb m-0">
                                        <li class="breadcrumb-item"><a href="index.php">Dashboard</a></li>
                                        <li class="breadcrumb-item"><a href="#">Communication</a></li>
                                        <li class="breadcrumb-item active">Send Email</li>
                                    </ol>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="row justify-content-center">
                        <div class="col-lg-10">
                            <div class="card">
                                <div class="card-header">
                                    <h5 class="card-title mb-0">Compose Email</h5>
                                </div>
                                <div class="card-body">
                                    <?php if ($success_msg): ?>
                                        <div class="alert alert-success"><?php echo $success_msg; ?></div>
                                    <?php endif; ?>

                                    <?php if ($error_msg): ?>
                                        <div class="alert alert-danger"><?php echo $error_msg; ?></div>
                                    <?php endif; ?>

                                    <form method="POST">
                                        <div class="mb-3">
                                            <label class="form-label">To:</label>
                                            <input type="email" class="form-control" name="recipient_email" required
                                                placeholder="recipient@example.com">
                                        </div>
                                        <div class="mb-3">
                                            <label class="form-label">Subject:</label>
                                            <input type="text" class="form-control" name="subject" required
                                                placeholder="Enter subject">
                                        </div>
                                        <div class="mb-3">
                                            <label class="form-label">Message:</label>
                                            <textarea class="form-control" name="message" id="message-editor" rows="10"
                                                required></textarea>
                                        </div>
                                        <div class="text-end">
                                            <button type="button" class="btn btn-ghost-secondary me-2">Discard</button>
                                            <button type="submit" class="btn btn-success"><i
                                                    class="ri-send-plane-fill align-bottom me-1"></i> Send
                                                Email</button>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
</body>

</html>