<?php
require_once __DIR__ . '/config/config.php';
$pageTitle = 'Enquiry';
$success = false;
$error = '';
$prefillProduct = isset($_GET['product']) ? trim($_GET['product']) : '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = trim($_POST['name'] ?? '');
    $email = trim($_POST['email'] ?? '');
    $phone = trim($_POST['phone'] ?? '');
    $subject = trim($_POST['subject'] ?? '');
    $message = trim($_POST['message'] ?? '');
    if (!$name || !$email || !$message) {
        $error = 'Please fill in Name, Email and Message.';
    } else {
        $pdo = getDB();
        $stmt = $pdo->prepare("INSERT INTO enquiries (name, email, phone, subject, message) VALUES (?, ?, ?, ?, ?)");
        $stmt->execute([$name, $email, $phone, $subject, $message]);
        if (file_exists(__DIR__ . '/includes/smtp_mail.php')) {
            require_once __DIR__ . '/includes/smtp_mail.php';
            notifyEnquiryOrContact('enquiry', [
                'name' => $name, 'email' => $email, 'phone' => $phone, 'subject' => $subject, 'message' => $message
            ]);
        }
        $success = true;
    }
}
$assetBase = (BASE_URL && BASE_URL !== '/') ? rtrim(BASE_URL, '/') . '/' : '';
$contactEmail = getSetting('contact_email', 'info@crimson.com');
$contactPhone = getSetting('contact_phone', '');
require_once __DIR__ . '/includes/header.php';
?>
<div class="enquiry-page-wrap">
    <header class="enquiry-page-header">
        <div class="container enquiry-page-header-inner">
            <span class="enquiry-page-eyebrow">Get in touch</span>
            <h1 class="enquiry-page-title">Enquiry</h1>
            <p class="enquiry-page-lead">Tell us what you need—box sizes, quantities, printing—and we’ll get back to you shortly.</p>
        </div>
    </header>

    <div class="page-content enquiry-page">
        <div class="container enquiry-layout">
            <?php if ($success): ?>
            <div class="enquiry-success">
                <div class="enquiry-success-icon">✓</div>
                <h2>Thank you</h2>
                <p>Your enquiry has been submitted. Our team will respond as soon as possible.</p>
                <a href="<?php echo htmlspecialchars($assetBase); ?>index.php" class="btn-glow btn-primary-modern">Back to home</a>
                <a href="<?php echo htmlspecialchars($assetBase); ?>products.php" class="btn-glow enquiry-success-secondary">Browse products</a>
            </div>
            <?php else: ?>
            <aside class="enquiry-aside">
                <h3>Direct contact</h3>
                <?php if ($contactEmail): ?>
                <p class="enquiry-aside-item">
                    <span class="enquiry-aside-label">Email</span>
                    <a href="mailto:<?php echo htmlspecialchars($contactEmail); ?>"><?php echo htmlspecialchars($contactEmail); ?></a>
                </p>
                <?php endif; ?>
                <?php if ($contactPhone): ?>
                <p class="enquiry-aside-item">
                    <span class="enquiry-aside-label">Phone</span>
                    <a href="tel:<?php echo preg_replace('/\s+/', '', $contactPhone); ?>"><?php echo htmlspecialchars($contactPhone); ?></a>
                </p>
                <?php endif; ?>
                <p class="enquiry-aside-note">Prefer email? Use the form and we’ll reply from our official address.</p>
            </aside>
            <div class="enquiry-form-card">
                <?php if ($error): ?>
                <div class="enquiry-alert enquiry-alert--error"><?php echo htmlspecialchars($error); ?></div>
                <?php endif; ?>
                <?php if ($prefillProduct): ?>
                <div class="enquiry-alert enquiry-alert--info">Enquiring about: <strong><?php echo htmlspecialchars($prefillProduct); ?></strong></div>
                <?php endif; ?>
                <form method="post" class="enquiry-form-modern">
                    <div class="enquiry-field">
                        <label for="enq-name">Name <span class="req">*</span></label>
                        <input id="enq-name" type="text" name="name" required value="<?php echo htmlspecialchars($_POST['name'] ?? ''); ?>" placeholder="Your name" autocomplete="name">
                    </div>
                    <div class="enquiry-field">
                        <label for="enq-email">Email <span class="req">*</span></label>
                        <input id="enq-email" type="email" name="email" required value="<?php echo htmlspecialchars($_POST['email'] ?? ''); ?>" placeholder="you@company.com" autocomplete="email">
                    </div>
                    <div class="enquiry-field">
                        <label for="enq-phone">Phone</label>
                        <input id="enq-phone" type="tel" name="phone" value="<?php echo htmlspecialchars($_POST['phone'] ?? ''); ?>" placeholder="Optional">
                    </div>
                    <div class="enquiry-field enquiry-field--full">
                        <label for="enq-subject">Subject / Product</label>
                        <input id="enq-subject" type="text" name="subject" value="<?php echo htmlspecialchars($_POST['subject'] ?? $prefillProduct); ?>" placeholder="e.g. Double-wall boxes, 500 units">
                    </div>
                    <div class="enquiry-field enquiry-field--full">
                        <label for="enq-message">Message <span class="req">*</span></label>
                        <textarea id="enq-message" name="message" rows="5" required placeholder="Describe sizes, quantities, delivery location…"><?php echo htmlspecialchars($_POST['message'] ?? ''); ?></textarea>
                    </div>
                    <button type="submit" class="btn-glow btn-primary-modern enquiry-submit">Submit enquiry</button>
                </form>
            </div>
            <?php endif; ?>
        </div>
    </div>
</div>
<?php require_once __DIR__ . '/includes/footer.php'; ?>
