<?php include 'includes/header.php'; ?>
<?php include 'includes/sidebar.php'; ?>
<?php require 'includes/db_connect.php'; ?>

<?php
require'phpmailer/PHPMailerAutoload.php';
// use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';

if (isset($_POST['send_email'])) {
    $staff_email = $_POST['staff_email'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];

    $mail = new PHPMailer(true);
    try {
    $mail->Host = "relay-hosting.secureserver.net";
$mail->Port = 25;                   
$mail->SMTPDebug = 0;
$mail->SMTPSecure = "none";                 
$mail->SMTPAuth = false;
                            $mail->Username='support@eaglestarinvestment.com';
                            $mail->Password='100%EagleStar100%';
                            
                            $mail->setFrom('support@eaglestarinvestment.com', 'EagleStar Investment. ');
                            $mail->addAddress($staff_email);
                            $mail->addReplyTo('support@eaglestarinvestment.com', 'EagleStar Investment.');
                                    
                                    

        // Content
        $mail->isHTML(true);
        $mail->Subject = $subject;
        $mail->Body    = $message;

        $mail->send();
        echo "<script>alert('Email sent successfully.'); window.location='staff_list.php';</script>";
    } catch (Exception $e) {
        echo "<script>alert('Error sending email: {$mail->ErrorInfo}');</script>";
    }
}
?>

<div class="container mt-4">
    <h4 class="text-orange">Send Email to Staff</h4>

    <form method="post" class="mt-3">
        <div class="mb-3">
            <label>Select Staff</label>
           <select name="staff_email" class="form-control" required>
                <option value="">-- Select Staff --</option>
                <?php
                $staffs = $conn->query("SELECT * FROM staff ORDER BY fullname ASC");
                while ($staff = $staffs->fetch_assoc()) {
                    echo "<option value='{$staff['email']}'>{$staff['name']}  {$staff['email']}</option>";
                }
                ?>
            </select>
        </div>

        <div class="mb-3">
            <label>Subject</label>
            <input type="text" name="subject" class="form-control" required>
        </div>

        <div class="mb-3">
            <label>Message</label>
            <textarea name="message" class="form-control" rows="5" required></textarea>
        </div>

        <button type="submit" name="send_email" class="btn btn-warning text-white">Send Email</button>
    </form>
</div>

<?php include 'includes/footer.php'; ?>
