<?php
require_once "config.php";
require_once "auth.php";
require_once "email_helper.php";

requireLogin();

$success = false;
$error = null;
$debugInfo = null;

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $testEmail = $_POST['test_email'] ?? '';
    
    if (empty($testEmail)) {
        $error = "Molimo unesite email adresu za test.";
    } else {
        $subject = "Test Email - Royal Transport";
        $body = "Ovo je test email sa Royal Transport sistema.\n\n";
        $body .= "Ako ste primili ovaj email, SMTP konfiguracija je ispravna!\n\n";
        $body .= "Vreme slanja: " . date('d.m.Y H:i:s') . "\n";
        $body .= "SMTP Host: " . SMTP_HOST . "\n";
        $body .= "SMTP Port: " . SMTP_PORT . "\n";
        $body .= "SMTP Secure: " . SMTP_SECURE . "\n\n";
        $body .= "Royal Transport";
        
        ob_start();
        $result = sendEmail($testEmail, $subject, $body, false);
        $debugInfo = ob_get_clean();
        
        if ($result) {
            $success = true;
        } else {
            $error = "Greška prilikom slanja emaila. Proverite server logove za više detalja.";
            $error .= "<br><small>Proverite error_log fajl ili server logove za detalje.</small>";
        }
    }
}

$phpmailerExists = file_exists(__DIR__ . '/vendor/autoload.php') || file_exists(__DIR__ . '/PHPMailer/src/Exception.php');
?>

<?php include "header.php"; ?>

<div class="container mt-4">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title mb-0">
                        <i class="bi bi-envelope-check me-2"></i>
                        Test Email Slanja
                    </h3>
                </div>
                <div class="card-body">
                    <?php if ($success): ?>
                        <div class="alert alert-success">
                            <i class="bi bi-check-circle me-2"></i>
                            <strong>Uspešno!</strong> Test email je poslat na <strong><?php echo htmlspecialchars($_POST['test_email']); ?></strong>
                            <br><small>Proverite inbox (i spam folder) za primljeni email.</small>
                            <?php if ($debugInfo): ?>
                                <br><br><strong>Debug info:</strong><br>
                                <pre class="small bg-light p-2 mt-2"><?php echo htmlspecialchars($debugInfo); ?></pre>
                            <?php endif; ?>
                        </div>
                    <?php endif; ?>
                    
                    <?php if ($error): ?>
                        <div class="alert alert-danger">
                            <i class="bi bi-exclamation-triangle me-2"></i>
                            <?php echo $error; ?>
                            <?php if ($debugInfo): ?>
                                <br><br><strong>Debug info:</strong><br>
                                <pre class="small bg-light p-2 mt-2"><?php echo htmlspecialchars($debugInfo); ?></pre>
                            <?php endif; ?>
                        </div>
                    <?php endif; ?>
                    
                    <?php if (!$phpmailerExists): ?>
                        <div class="alert alert-warning">
                            <i class="bi bi-exclamation-triangle me-2"></i>
                            <strong>PHPMailer nije instaliran!</strong> Email će biti poslat preko standardnog PHP mail() funkcije.
                            <br><small>Instalirajte PHPMailer za SMTP funkcionalnost. Proverite EMAIL_SETUP.md za uputstva.</small>
                        </div>
                    <?php endif; ?>
                    
                    <form method="post">
                        <div class="mb-3">
                            <label for="test_email" class="form-label">Email adresa za test</label>
                            <input type="email" class="form-control" id="test_email" name="test_email" 
                                   placeholder="test@example.com" required 
                                   value="<?php echo isset($_POST['test_email']) ? htmlspecialchars($_POST['test_email']) : ''; ?>">
                            <div class="form-text">Unesite email adresu na koju želite da pošaljete test email.</div>
                        </div>
                        
                        <div class="mb-3">
                            <div class="card bg-light">
                                <div class="card-body">
                                    <h6 class="card-title">Trenutna SMTP konfiguracija:</h6>
                                    <ul class="mb-0 small">
                                        <li><strong>Host:</strong> <?php echo SMTP_HOST; ?></li>
                                        <li><strong>Port:</strong> <?php echo SMTP_PORT; ?></li>
                                        <li><strong>Secure:</strong> <?php echo SMTP_SECURE; ?></li>
                                        <li><strong>Username:</strong> <?php echo SMTP_USERNAME; ?></li>
                                        <li><strong>From Email:</strong> <?php echo SMTP_FROM_EMAIL; ?></li>
                                        <li><strong>SMTP Enabled:</strong> <?php echo SMTP_ENABLED ? 'Da' : 'Ne'; ?></li>
                                        <li><strong>PHPMailer Instaliran:</strong> <?php echo $phpmailerExists ? 'Da' : 'Ne'; ?></li>
                                    </ul>
                                </div>
                            </div>
                        </div>
                        
                        <div class="d-grid gap-2">
                            <button type="submit" class="btn btn-primary">
                                <i class="bi bi-send me-2"></i>Pošalji test email
                            </button>
                            <a href="index.php" class="btn btn-secondary">Nazad</a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include "footer.php"; ?>
