<?php
declare(strict_types=1);

require_once __DIR__ . '/app/Helpers/bootstrap.php';

$enabled = db()->prepare('SELECT setting_value FROM ' . table('settings') . " WHERE setting_key = 'contact_enabled'");
$enabled->execute();
if ($enabled->fetchColumn() !== '1') { http_response_code(404); exit('Not found.'); }

$error = null; $sent = false;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name=trim((string)($_POST['name']??'')); $email=trim((string)($_POST['email']??'')); $subject=trim((string)($_POST['subject']??'')); $message=trim((string)($_POST['message']??''));
    if ($name==='' || !filter_var($email,FILTER_VALIDATE_EMAIL) || $subject==='' || $message==='') { $error=t('contact.error_invalid'); }
    else {
        $settings = triptale_mail_settings();
        $to = (string)($settings['reply_to'] ?: $settings['from_email']);
        $fromName = str_replace(["\r", "\n"], '', (string)$settings['from_name']);
        $fromEmail = str_replace(["\r", "\n"], '', (string)$settings['from_email']);
        $safeSubject = str_replace(["\r", "\n"], '', $subject);
        // Die Systemadresse bleibt immer der sichtbare Absender. Die Adresse
        // der Person aus dem Formular wird ausschließlich als Antwortadresse
        // gesetzt, damit Antworten direkt bei ihr ankommen.
        $headers = "MIME-Version: 1.0\r\n"
            . "Content-type: text/plain; charset=UTF-8\r\n"
            . "From: {$fromName} <{$fromEmail}>\r\n"
            . "Reply-To: {$email}";
        $body = "Name: {$name}\nE-Mail: {$email}\n\n{$message}";
        $sent = triptale_send_mail($to, 'TripTale: ' . $safeSubject, $body);
        if (!$sent) { $error = triptale_mail_failure_message('contact.error_send'); }
    }
}
?><!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title><?=htmlspecialchars(t('contact.title'))?></title><style>body{font-family:system-ui;background:#f4f6f8;margin:0;padding:32px}main{max-width:650px;margin:auto;background:#fff;padding:28px;border-radius:14px}.button,button{display:inline-block;padding:10px 13px;background:#126e82;color:#fff;border:0;border-radius:7px;text-decoration:none;font:inherit;font-weight:700;cursor:pointer}.back{background:#e3ebee;color:#126e82;margin:0 0 12px}label{display:block;margin-top:14px;font-weight:700}input,textarea{box-sizing:border-box;width:100%;padding:10px;margin-top:5px}textarea{min-height:150px}button{margin-top:20px}.error,.ok{padding:12px;border-radius:7px}.error{background:#ffe6e3}.ok{background:#e6f5e9}@media(max-width:600px){body{padding:12px}main{padding:18px}.back{display:inline-block;width:auto;max-width:100%;text-align:left}}</style></head><body><main><a class="button back" href="/dashboard.php"><?=htmlspecialchars(t('contact.back'))?></a><h1><?=htmlspecialchars(t('contact.title'))?></h1><?php if($sent):?><p class="ok"><?=htmlspecialchars(t('contact.sent'))?></p><?php endif;?><?php if($error):?><p class="error"><?=htmlspecialchars($error)?></p><?php endif;?><form method="post"><label><?=htmlspecialchars(t('contact.name'))?><input name="name" required></label><label><?=htmlspecialchars(t('contact.email'))?><input name="email" type="email" required></label><label><?=htmlspecialchars(t('contact.subject'))?><input name="subject" required></label><label><?=htmlspecialchars(t('contact.message'))?><textarea name="message" required></textarea></label><button><?=htmlspecialchars(t('contact.send'))?></button></form></main></body></html>
