<?php
/**
 * Send Test Email - Admin Panel
 * Digital Product Store
 */

require_once __DIR__ . '/includes/header.php';

$page_title = 'Send Test Email';

$message = '';
$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $test_email = sanitize_email($_POST['test_email'] ?? '');
    
    if (empty($test_email) || !is_valid_email($test_email)) {
        $error = 'Please enter a valid email address.';
    } else {
        $subject = 'Test Email from ' . get_setting('site_name', 'Digital Store');
        $body = '<h2>Test Email</h2>
        <p>This is a test email from your Digital Store admin panel.</p>
        <p>If you received this email, your email configuration is working correctly.</p>
        <p>Sent at: ' . date('Y-m-d H:i:s') . '</p>';
        
        if (send_email($test_email, $subject, $body)) {
            $message = 'Test email sent successfully to ' . htmlspecialchars($test_email);
            log_admin_activity($pdo, $admin_id, 'send_test_email', "Sent test email to: {$test_email}");
        } else {
            $error = 'Failed to send test email. Please check your email settings.';
        }
    }
}
?>

<div class="page-header">
    <div>
        <h1>Send Test Email</h1>
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
                <li class="breadcrumb-item"><a href="<?php echo ADMIN_URL; ?>/index.php">Dashboard</a></li>
                <li class="breadcrumb-item active">Send Test Email</li>
            </ol>
        </nav>
    </div>
</div>

<?php if ($message): ?>
<div class="alert alert-success alert-dismissible fade show">
    <i class="fas fa-check-circle me-2"></i><?php echo $message; ?>
    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>

<?php if ($error): ?>
<div class="alert alert-danger alert-dismissible fade show">
    <i class="fas fa-exclamation-circle me-2"></i><?php echo $error; ?>
    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>

<div class="row">
    <div class="col-lg-6">
        <div class="card">
            <div class="card-header">
                <i class="fas fa-envelope me-2"></i>Send Test Email
            </div>
            <div class="card-body">
                <p class="text-muted">Enter an email address to send a test email and verify your email configuration.</p>
                
                <form method="POST" action="">
                    <?php echo csrf_field(); ?>
                    
                    <div class="mb-3">
                        <label for="test_email" class="form-label">Email Address</label>
                        <input type="email" name="test_email" id="test_email" class="form-control" 
                               placeholder="test@example.com" required>
                        <div class="form-text">Enter the email address where you want to receive the test.</div>
                    </div>
                    
                    <button type="submit" class="btn btn-primary">
                        <i class="fas fa-paper-plane me-2"></i>Send Test Email
                    </button>
                </form>
            </div>
        </div>
        
        <div class="card mt-4">
            <div class="card-header">
                <i class="fas fa-info-circle me-2"></i>Email Settings
            </div>
            <div class="card-body">
                <p class="mb-2"><strong>From Email:</strong> <?php echo htmlspecialchars(get_setting('smtp_from_email', 'Not configured')); ?></p>
                <p class="mb-2"><strong>From Name:</strong> <?php echo htmlspecialchars(get_setting('smtp_from_name', 'Not configured')); ?></p>
                <a href="<?php echo ADMIN_URL; ?>/settings.php" class="btn btn-sm btn-outline-primary">
                    <i class="fas fa-cog me-2"></i>Edit Email Settings
                </a>
            </div>
        </div>
    </div>
</div>

<?php require_once __DIR__ . '/includes/footer.php'; ?>
